Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

search_special()

Parameters

ColumnTypeDefaultDescription
$search
$sql_join
$fetchrows
$sql_prefix
$sql_suffix
$order_by
$orig_order
$select
$sql_filter
$archive
$return_disk_usage
$return_refs_only false
$returnsql false

Location

include/search_functions.php lines 1127 to 1669

Definition

 
function search_special($search,$sql_join,$fetchrows,$sql_prefix,$sql_suffix,$order_by,$orig_order,$select,$sql_filter,$archive,$return_disk_usage,$return_refs_only=false$returnsql=false)
    {
    
# Process special searches. These return early with results.
    
global $FIXED_LIST_FIELD_TYPES$lang;
    
    
# View Last
    
if (substr($search,0,5)=="!last"
        {
        
# Replace r2.ref with r.ref for the alternative query used here.

        
$order_by=str_replace("r.ref","r2.ref",$order_by);
        if (
$orig_order=="relevance")
            {
            
# Special case for ordering by relevance for this query.
            
$direction=((strpos($order_by,"DESC")===false)?"ASC":"DESC");
            
$order_by="r2.ref " $direction;
            }
       
        
        
# Extract the number of records to produce
        
$last=explode(",",$search);
        
$last=str_replace("!last","",$last[0]);

        
        if (!
is_int($last)) {$last=1000;$search="!last1000";} # 'Last' must be an integer. SQL injection filter.
        
        # Fix the ORDER BY for this query (special case due to inner query)
        
$order_by=str_replace("r.rating","rating",$order_by);
        
$sql $sql_prefix "SELECT DISTINCT *,r2.total_hit_count score FROM (SELECT $select FROM resource r $sql_join WHERE $sql_filter ORDER BY ref DESC LIMIT $last ) r2 ORDER BY $order_by$sql_suffix;
        return 
$returnsql $sql sql_query($sql,false,$fetchrows);
        }
    
     
# Collections containing resources
     # NOTE - this returns collections not resources! Not intended for use in user searches.
     # This is used when the $collection_search_includes_resource_metadata option is enabled and searches collections based on the contents of the collections.
    
if (substr($search,0,19)=="!contentscollection")
        {
        
$flags=substr($search,19,((strpos($search," ")!==false)?strpos($search," "):strlen($search)) -19); # Extract User/Public/Theme flags from the beginning of the search parameter.
        
        
if ($flags=="") {$flags="TP";} # Sensible default

        # Add collections based on the provided collection type flags.
        
$collection_filter="(";
        if (
strpos($flags,"T")!==false# Include themes
            
{
            if (
$collection_filter!="(") {$collection_filter.=" OR ";}
            
$collection_filter .= sprintf(" c.`type` = %s"COLLECTION_TYPE_FEATURED);
            }
    
     if (
strpos($flags,"P")!==false# Include public collections
            
{
            if (
$collection_filter!="(") {$collection_filter.=" OR ";}
            
$collection_filter .= sprintf(" c.`type` = %s"COLLECTION_TYPE_PUBLIC);
            }
        
        if (
strpos($flags,"U")!==false# Include the user's own collections
            
{
            if (
$collection_filter!="(") {$collection_filter.=" OR ";}
            global 
$userref;
            
$collection_filter .= sprintf(" (c.`type` = %s AND c.user = '%s')"COLLECTION_TYPE_STANDARDescape_check($userref));
            }
        
$collection_filter.=")";
        
        
# Formulate SQL
        
$sql="SELECT DISTINCT c.*, sum(r.hit_count) score, sum(r.hit_count) total_hit_count FROM collection c join resource r $sql_join join collection_resource cr on cr.resource=r.ref AND cr.collection=c.ref WHERE $sql_filter AND $collection_filter GROUP BY c.ref ORDER BY $order_by ";
        return 
$returnsql $sql sql_query($sql);
        }
    
    
# View Resources With No Downloads
    
if (substr($search,0,12)=="!nodownloads"
        {
        if (
$orig_order=="relevance") {$order_by="ref DESC";}
        
$sql=$sql_prefix "SELECT r.hit_count score, $select FROM resource r $sql_join WHERE $sql_filter AND r.ref NOT IN (SELECT DISTINCT object_ref FROM daily_stat WHERE activity_type='Resource download') GROUP BY r.ref ORDER BY $order_by$sql_suffix;
        return 
$returnsql?$sql:sql_query($sql,false,$fetchrows);
        }
    
    
# Duplicate Resources (based on file_checksum)
    
if (substr($search,0,11)=="!duplicates"
        {
        
# Extract the resource ID
        
$ref=explode(" ",$search);
        
$ref=str_replace("!duplicates","",$ref[0]);
        
$ref=explode(",",$ref);// just get the number
        
$ref=escape_check($ref[0]);

        if (
$ref!=""
            {
            
# Find duplicates of a given resource
            
if (ctype_digit($ref)) 
                {
                
$sql="SELECT DISTINCT r.hit_count score, $select FROM resource r $sql_join 
                    WHERE 
$sql_filter AND file_checksum <> '' AND file_checksum IS NOT NULL 
                                      AND file_checksum = (SELECT file_checksum FROM resource WHERE ref=
$ref AND (file_checksum <> '' AND file_checksum IS NOT NULL) ) 
                    ORDER BY file_checksum, ref"
;    
                if(
$returnsql) {return $sql;}
                
$results=sql_query($sql,false,$fetchrows);
                
$count=count($results);
                if (
$count>1
                    {
                    return 
$results;
                    }
                else 
                    {
                    return array();
                    }
                }
            else
                {
                
# Given resource is not a valid identifier
                
return array();
                }
            }
        else
            {
            
# Find all duplicate resources
            
$sql=$sql_prefix "SELECT DISTINCT r.hit_count score, $select FROM resource r $sql_join WHERE $sql_filter AND file_checksum IN (SELECT file_checksum FROM (SELECT file_checksum FROM resource WHERE file_checksum <> '' AND file_checksum IS NOT null GROUP BY file_checksum having count(file_checksum)>1)r2) ORDER BY file_checksum, ref" $sql_suffix;
            return 
$returnsql?$sql:sql_query($sql,false,$fetchrows);
            }
        }
    
    
# View Collection
    
if (substr($search011) == '!collection')
        {
        global 
$userref;

        
$colcustperm $sql_join;
        
$colcustfilter $sql_filter// to avoid allowing this sql_filter to be modified by the $access_override search in the smart collection update below!!!
             
        # Special case if a key has been provided.
        
if(getval('k''') != '')
            {
            
$sql_filter 'r.ref > 0';
            }

        
# Extract the collection number
        
$collection explode(' '$search);
        
$collection str_replace('!collection'''$collection[0]);
        
$collection explode(','$collection); // just get the number
        
$collection = (int)$collection[0];

        
# Check access
        
if(in_array($collectionarray_column(get_user_collections($userref,"","name","ASC",-1,false), "ref")) || featured_collection_check_access_control($collection))
            {
            if(!
collection_readable($collection))
                {
                return array();
                }
            }
        else
            {
            exit(
$lang["error-permissiondenied"]);
            }
        

        
# Smart collections update
        
global $allow_smart_collections$smart_collections_async;
        if(
$allow_smart_collections)
            {
            global 
$smartsearch_ref_cache;
            if(isset(
$smartsearch_ref_cache[$collection]))
                {
                
$smartsearch_ref $smartsearch_ref_cache[$collection]; // this value is pretty much constant
                
}
            else
                {
                
$smartsearch_ref sql_value('SELECT savedsearch value FROM collection WHERE ref="' $collection '"''');
                
$smartsearch_ref_cache[$collection] = $smartsearch_ref;
                }

            global 
$php_path;
            if(
$smartsearch_ref != '' && !$return_disk_usage)
                {
                if(
$smart_collections_async && isset($php_path) && file_exists($php_path '/php'))
                    {
                    
exec($php_path '/php ' dirname(__FILE__) . '/../pages/ajax/update_smart_collection.php ' escapeshellarg($collection) . ' ' '> /dev/null 2>&1 &');
                    }
                else 
                    {
                    include (
dirname(__FILE__) . '/../pages/ajax/update_smart_collection.php');
                    }
                }   
            }   
        
$searchsql $sql_prefix "SELECT DISTINCT c.date_added,c.comment,c.purchase_size,c.purchase_complete,r.hit_count score,length(c.comment) commentset, $select FROM resource r  join collection_resource c on r.ref=c.resource $colcustperm  WHERE c.collection='" $collection "' AND ($colcustfilter) GROUP BY r.ref ORDER BY $order_by$sql_suffix;
        
$collectionsearchsql=hook('modifycollectionsearchsql','',array($searchsql));

        if(
$collectionsearchsql)
            {
            
$searchsql=$collectionsearchsql;
            }    

        if(
$returnsql){return $searchsql;}
        
        if(
$return_refs_only)
            {
            
// note that we actually include archive and created_by columns too as often used to work out permission to edit collection
            
$result sql_query($searchsql,false,$fetchrows,true,2,true,array('ref','resource_type','archive','created_by','access'));
            }
        else
            {
            
$result sql_query($searchsql,false,$fetchrows);
            }

        
hook('beforereturnresults''', array($result$archive));

        return 
$result;
        }

    
# View Related - Pushed Metadata (for the view page)
    
if (substr($search,0,14)=="!relatedpushed")
        {
        
# Extract the resource number
        
$resource=explode(" ",$search);$resource=str_replace("!relatedpushed","",$resource[0]);
        
$order_by=str_replace("r.","",$order_by); # UNION below doesn't like table aliases in the ORDER BY.
        
        
$sql=$sql_prefix "SELECT DISTINCT r.hit_count score,rt.name resource_type_name, $select FROM resource r join resource_type rt on r.resource_type=rt.ref AND rt.push_metadata=1 join resource_related t on (t.related=r.ref AND t.resource='" $resource "') $sql_join  WHERE 1=1 AND $sql_filter GROUP BY r.ref 
        UNION
        SELECT DISTINCT r.hit_count score, rt.name resource_type_name, 
$select FROM resource r join resource_type rt on r.resource_type=rt.ref AND rt.push_metadata=1 join resource_related t on (t.resource=r.ref AND t.related='" $resource "') $sql_join  WHERE 1=1 AND $sql_filter GROUP BY r.ref 
        ORDER BY 
$order_by$sql_suffix;
        
        return 
$returnsql?$sql:sql_query($sql,false,$fetchrows);
        }
        
    
# View Related
    
if (substr($search,0,8)=="!related")
        {
        
# Extract the resource number
        
$resource=explode(" ",$search);$resource=str_replace("!related","",$resource[0]);
        
$order_by=str_replace("r.","",$order_by); # UNION below doesn't like table aliases in the ORDER BY.
        
        
global $pagename$related_search_show_self;
        
$sql_self '';
        if (
$related_search_show_self && $pagename == 'search')
            {
            
$sql_self " SELECT DISTINCT r.hit_count score, $select FROM resource r $sql_join WHERE r.ref=$resource AND $sql_filter GROUP BY r.ref UNION ";
            }
        
$sql=$sql_prefix $sql_self "SELECT DISTINCT r.hit_count score, $select FROM resource r join resource_related t on (t.related=r.ref AND t.r