📄 general.php
字号:
<?# General functions, useful across the whole solutionfunction get_resource_path($ref,$size,$generate,$extension="jpg",$scramble=-1,$page=1,$watermarked=false) { # returns the correct path to resource $ref of size $size ($size==empty string is original resource) # If one or more of the folders do not exist, and $generate=true, then they are generated if ($size=="") { # For the full size, check to see if the full path is set and if so return that. $fp=sql_value("select file_path value from resource where ref='$ref'",""); # Test to see if this nosize file is of the extension asked for, else skip the file_path and return a filestore path. # If using staticsync, file path will be set already, but we still want the filestore path for a nosize preview jpg. # Also, returning the original filename when a nosize 'jpg' is looked for is no good, since imagemagick.php deletes $target. $test_ext = explode(".",$fp);$test_ext=trim(strtolower($test_ext[count($test_ext)-1])); if (($test_ext == $extension)){ if ((strlen($fp)>0) && (strpos($fp,"/")!==false)) { global $syncdir; return $syncdir . "/" . $fp; } } } global $scramble_key; if ($scramble===-1) { # Find the system default scramble setting if not specified if (isset($scramble_key) && ($scramble_key!="")) {$scramble=true;} else {$scramble=false;} } if ($scramble) { # Create a scrambled path using the scramble key # It should be very difficult or impossible to work out the scramble key, and therefore access # other resources, based on the scrambled path of a single resource. $scramblepath=substr(md5($ref . "_" . $scramble_key),0,15); } if ($extension=="") {$extension="jpg";} $folder="filestore/"; #if (!file_exists(dirname(__FILE__) . $folder)) {mkdir(dirname(__FILE__) . $folder,0777);} for ($n=0;$n<strlen($ref);$n++) { $folder.=substr($ref,$n,1); if (($scramble) && ($n==(strlen($ref)-1))) {$folder.="_" . $scramblepath;} $folder.="/"; #echo "<li>" . $folder; if ((!(file_exists($folder))) && $generate) {mkdir($folder,0777);chmod($folder,0777);} } # Add the page to the filename for everything except page 1. if ($page==1) {$p="";} else {$p="_" . $page;} # Add the watermarked url too if ($watermarked) {$p.="_wm";} return $folder . $ref . $size . $p . "." . $extension; } function get_resource_data($ref) { # Returns basic resource data (from the resource table alone) for resource $ref. # For 'dynamic' field data, see get_resource_field_data $resource=sql_query("select * from resource where ref='$ref'"); if (count($resource)==0) { if ($ref>0) { return false; } else { # For batch upload templates (negative reference numbers), generate a new resource. sql_query("insert into resource (ref) values ('$ref')"); $resource=sql_query("select * from resource where ref='$ref'"); } } # update hit count sql_query("update resource set hit_count=hit_count+1 where ref='$ref'"); return $resource[0]; }function get_resource_field_data($ref,$multi=false) { # Returns field data and field properties (resource_type_field and resource_data tables) # for this resource, for display in an edit / view form. $return=array(); $fields=sql_query("select *,f.required frequired,f.ref fref from resource_type_field f left join resource_data d on d.resource_type_field=f.ref and d.resource='$ref' where (f.resource_type=0 or f.resource_type=999 " . (($multi)?"":" or f.resource_type in (select resource_type from resource where ref='$ref')") . ") order by f.order_by,f.ref"); for ($n=0;$n<count($fields);$n++) { if (checkperm("f*") || checkperm("f" . $fields[$n]["fref"])) {$return[]=$fields[$n];} } return $return; }function get_resource_field_data_batch($refs) { # Returns field data and field properties (resource_type_field and resource_data tables) # for all the resource references in the array $refs. # This will use a single SQL query and is therefore a much more efficient way of gathering # resource data for a list of resources (e.g. search result display for a page of resources). if (count($refs)==0) {return array();} # return an empty array if no resources specified (for empty result sets) $refsin=join(",",$refs); $results=sql_query("select d.resource,f.*,d.value from resource_type_field f left join resource_data d on d.resource_type_field=f.ref and d.resource in ($refsin) where (f.resource_type=0 or f.resource_type in (select resource_type from resource where ref=d.resource)) order by d.resource,f.order_by,f.ref"); $return=array(); $res=0; for ($n=0;$n<count($results);$n++) { if ($results[$n]["resource"]!=$res) { # moved on to the next resource if ($res!=0) {$return[$res]=$resdata;} $resdata=array(); $res=$results[$n]["resource"]; } #echo "<li>" . $res . " - " . $results[$n]["ref"] . ":" . $results[$n]; # copy name/value into resdata array $resdata[$results[$n]["ref"]]=$results[$n]; } $return[$res]=$resdata; return $return; } function get_resource_types() { # Returns a list of resource types. $r=sql_query("select * from resource_type order by ref"); # Translate names for ($n=0;$n<count($r);$n++) { $r[$n]["name"]=i18n_get_translated($r[$n]["name"]); } return $r; }function get_resource_top_keywords($resource,$count) { # Return the top $count keywords (by hitcount) used by $resource. # This is for the 'Find Similar' search. # Keywords that are too short or too long, or contain numbers are dropped - they are probably not as meaningful in # the contexts of this search (consider being offered "12" or "OKB-34" as an option?) return sql_array("select distinct k.ref,k.keyword value from keyword k,resource_keyword r,resource_type_field f where k.ref=r.keyword and r.resource='$resource' and f.ref=r.resource_type_field and f.use_for_similar=1 and length(k.keyword)>=3 and length(k.keyword)<=15 and k.keyword not like '%0%' and k.keyword not like '%1%' and k.keyword not like '%2%' and k.keyword not like '%3%' and k.keyword not like '%4%' and k.keyword not like '%5%' and k.keyword not like '%6%' and k.keyword not like '%7%' and k.keyword not like '%8%' and k.keyword not like '%9%' order by k.hit_count desc limit $count"); }function split_keywords($search,$index=false) { # Takes $search and returns an array of individual keywords. # Remove any real / unescaped lf/cr $search=str_replace("\r"," ",$search); $search=str_replace("\n"," ",$search); $search=str_replace("\\r"," ",$search); $search=str_replace("\\n"," ",$search); /* # We used to 'clean' strings but this did not work for multiple languages. # This became pointless anyway when splitting using multiple chars later on # so this block has been commented. $validchars=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789nueaeiou:,;-/"; $ns=""; for ($n=0;$n<strlen($search);$n++) { $c=substr($search,$n,1); if (strpos($validchars,$c)!==false) {$ns.=$c;} } */ $ns=trim_spaces($search); if ((substr($ns,0,1)==",") || ($index==false && strpos($ns,":")!==false)) # special 'constructed' query type, split using comma so # we support keywords with spaces. { $ns=str_replace(array("/","_",".","; ","-","(",")","'","\"","\\")," ",$ns); $ns=trim_spaces($ns); $return=explode(",",strtolower($ns)); # If we are indexing, append any values that contain spaces. # Important! Solves the searching for keywords with spaces issue. # Consider: for any keyword that has spaces, append to the array each individual word too # so for example: South Asia,USA becomes South Asia,USA,South,Asia # so a plain search for 'south asia' will match those with the keyword 'south asia' because the resource # will also be linked to the words 'south' and 'asia'. if ($index) { $return2=$return; for ($n=0;$n<count($return);$n++) { $keyword=trim($return[$n]); if (strpos($keyword," ")!==false) { # append each word $words=explode(" ",$keyword); for ($m=0;$m<count($words);$m++) {$return2[]=trim($words[$m]);} } } return trim_array($return2); } else { return trim_array($return); } } else { # split using spaces and similar chars $ns=str_replace(array(";",",","_",":","/",".","; ","-","(",")","'","\"","\\")," ",$ns); $ns=trim_spaces($ns); return trim_array(explode(" ",strtolower($ns))); } }function resolve_keyword($keyword) { # Returns the keyword reference for $keyword, or false if no such keyword exists. return sql_value("select ref value from keyword where keyword='" . trim(escape_check($keyword)) . "'",false); } function trim_spaces($text) { # replace multiple spaces with a single space while (strpos($text," ")!==false) { $text=str_replace(" "," ",$text); } return trim($text); } function update_resource_keyword_hitcount($resource,$search) { # For the specified $resource, increment the hitcount for each matching keyword in $search # This is done into a temporary column first (new_hit_count) so existing results are not affected. # copy_hitcount_to_live() is then executed at a set interval to make this data live. $keywords=split_keywords($search); $keys=array(); for ($n=0;$n<count($keywords);$n++) { $keyword=$keywords[$n]; if (strpos($keyword,":")!==false) { $k=explode(":",$keyword); $keyword=$k[1]; } $found=resolve_keyword($keyword); if ($found!==false) {$keys[]=resolve_keyword($keyword);} } if (count($keys)>0) {sql_query("update resource_keyword set new_hit_count=new_hit_count+1 where resource='$resource' and keyword in (" . join(",",$keys) . ")");} } function copy_hitcount_to_live() { # Copy the temporary hit count used for relevance matching to the live column so it's activated (see comment for # update_resource_keyword_hitcount()) sql_query("update resource_keyword set hit_count=new_hit_count"); } function get_image_sizes($ref,$internal=false,$extension="jpg",$onlyifexists=true) { # Returns a table of available image sizes for resource $ref. # The original image file assumes the name of the 'nearest size (up)' in the table # loop through all image sizes $sizes=sql_query("select * from preview_size order by width desc"); $return=array(); $outputfirst=false;$lastname="";$lastpreview=0; for ($n=0;$n<count($sizes);$n++) { $path=get_resource_path($ref,$sizes[$n]["id"],false,$extension); if (file_exists($path) || (!$onlyifexists)) { if ($outputfirst==false) { # add the original image $path2=get_resource_path($ref,'',false,$extension); if (file_exists($path2)) { $returnline=array(); $returnline["name"]=$lastname; $returnline["allow_preview"]=$lastpreview; $returnline["allow_restricted"]=$lastrestricted; $returnline["path"]=$path2; $returnline["id"]=""; if ((list($sw,$sh) = @getimagesize($path2))===false) {$sw=0;$sh=0;} if (($filesize=filesize($path2))===false) {$returnline["filesize"]="?";$returnline["filedown"]="?";} else {$returnline["filedown"]=ceil($filesize/50000) . " seconds @ broadband";$returnline["filesize"]=formatfilesize($filesize);} $returnline["width"]=$sw; $returnline["height"]=$sh; $return[]=$returnline; $outputfirst=true; } } if (($sizes[$n]["internal"]==0) || ($internal)) { $returnline=array(); $returnline["name"]=$sizes[$n]["name"]; $returnline["allow_preview"]=$sizes[$n]["allow_preview"]; $returnline["allow_restricted"]=$sizes[$n]["allow_restricted"]; $returnline["path"]=$path; $returnline["id"]=$sizes[$n]["id"]; if ((list($sw,$sh) = @getimagesize($path))===false) {$sw=0;$sh=0;} if (($filesize=@filesize($path))===false) {$returnline["filesize"]="?";$returnline["filedown"]="?";} else {$returnline["filedown"]=ceil($filesize/50000) . " seconds @ broadband";$filesize=formatfilesize($filesize);} $returnline["filesize"]=$filesize; $returnline["width"]=$sw; $returnline["height"]=$sh; $return[]=$returnline; } } $lastname=$sizes[$n]["name"]; $lastpreview=$sizes[$n]["allow_preview"]; $lastrestricted=$sizes[$n]["allow_restricted"]; } return $return; }function trim_array($array) { # removes whitespace from the beginning/end of all elements in an array for ($n=0;$n<count($array);$n++) { $array[$n]=trim($array[$n]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -