📄 resource_functions.php
字号:
function add_keyword_mappings($ref,$string,$resource_type_field) { # For each instance of a keyword in $string, add a keyword->resource mapping. # Create keywords that do not yet exist. # Increase the hit count of each keyword that matches. # Store the position and field the string was entered against for advanced searching. if (trim($string)=="") {return false;} $keywords=split_keywords($string,true); for ($n=0;$n<count($keywords);$n++) { global $noadd; if ((!(in_array($keywords[$n],$noadd))) && (strlen($keywords[$n])<=50)) { #echo "<li>adding " . $keywords[$n]; $keyword=resolve_keyword($keywords[$n]); if ($keyword===false) { # This is a new keyword. Create and discover the new keyword ref. sql_query("insert into keyword(keyword,soundex,hit_count) values ('" . escape_check($keywords[$n]) . "',soundex('" . escape_check($keywords[$n]) . "'),0)"); $keyword=sql_insert_id(); #echo "<li>New keyword."; } # create mapping, increase hit count. sql_query("insert into resource_keyword(resource,keyword,position,resource_type_field) values ('$ref','$keyword','$n','$resource_type_field')"); sql_query("update keyword set hit_count=hit_count+1 where ref='$keyword' limit 1"); # Log this daily_stat("Keyword added to resource",$keyword); } } } function update_field($resource,$field,$value) { # Updates a field. Works out the previous value, so this is not efficient if we already know what this previous value is $existing=sql_value("select value from resource_data where resource='$resource' and resource_type_field='$field'",""); remove_keyword_mappings($resource,$existing,$field); add_keyword_mappings($resource,$value,$field); sql_query("delete from resource_data where resource='$resource' and resource_type_field='$field' limit 1"); $value=escape_check($value); sql_query("insert into resource_data(resource,resource_type_field,value) values ('$resource','$field','$value')"); # Also update resource table? $column=sql_value("select resource_column value from resource_type_field where ref='$field'",""); if (strlen($column)>0) { sql_query("update resource set $column = '$value' where ref='$resource'"); } } function email_resource($resource,$resourcename,$fromusername,$userlist,$message) { # Attempt to resolve all users in the string $userlist to user references. # Add $collection to these user's 'My Collections' page # Send them an e-mail linking to this collection global $baseurl,$email_from,$applicationname,$lang; if (trim($userlist)=="") {return ($lang["mustspecifyoneusername"]);} $ulist=trim_array(explode(",",$userlist)); $emails=array(); $key_required=array(); for ($n=0;$n<count($ulist);$n++) { $uname=$ulist[$n]; $email=sql_value("select email value from user where username='" . escape_check($uname) . "'",''); if ($email=='') { # Not a recognised user, if @ sign present, assume e-mail address specified if (strpos($uname,"@")===false) {return($lang["couldnotmatchallusernames"]);} $emails[$n]=$uname; $key_required[$n]=true; } else { # Add e-mail address from user accunt $emails[$n]=$email; $key_required[$n]=false; } } # Send an e-mail to each resolved user / e-mail address $subject="$applicationname: $resourcename"; if ($message!="") {$message="\n\n" . $lang["message"] . ": " . str_replace(array("\\n","\\r","\\"),array("\n","\r",""),$message);} for ($n=0;$n<count($emails);$n++) { $key=""; # Do we need to add an external access key for this user (e-mail specified rather than username)? if ($key_required[$n]) { $k=substr(md5(time()),0,10); sql_query("insert into external_access_keys(resource,access_key) values ('$resource','$k');"); $key="&k=". $k; } # Build message and send. $body="$fromusername " . $lang["hasemailedyouaresource"] . "$message\n\n" . $lang["clicktoviewresource"] . "\n\n" . $baseurl . "/?r=" . $resource . $key; send_mail($emails[$n],$subject,$body); } # Return an empty string (all OK). return ""; }function delete_resource($ref) { if ($ref<0) {return false;} # Can't delete the template # Delete the resource, all related entries in tables and all files on disk # Delete files first $extension=sql_value("select file_extension value from resource where ref='$ref'","jpg"); if ($extension=="") {$extension="jpg";} $extension2=sql_value("select preview_extension value from resource where ref='$ref'","jpg"); if ($extension2=="") {$extension2="jpg";} $sizes=get_image_sizes($ref,true,$extension); for ($n=0;$n<count($sizes);$n++) { $path=get_resource_path($ref,$sizes[$n]["id"],false,$extension); if (file_exists($path)) {unlink($path);} } $sizes=get_image_sizes($ref,true,$extension2); for ($n=0;$n<count($sizes);$n++) { # Also delete alternative previews (where extension is different) $path=get_resource_path($ref,$sizes[$n]["id"],false,$extension2); if (file_exists($path)) {unlink($path);} } $path=get_resource_path($ref,"",false,$extension); if (file_exists($path)) {unlink($path);} # Delete all database entries sql_query("delete from resource where ref='$ref'"); sql_query("delete from resource_data where resource='$ref'"); sql_query("delete from resource_keyword where resource='$ref'"); sql_query("delete from resource_related where resource='$ref' or related='$ref'"); sql_query("delete from collection_resource where resource='$ref'"); sql_query("delete from resource_custom_access where resource='$ref'"); sql_query("delete from external_access_keys where resource='$ref'"); }function get_max_resource_ref() { # Returns the highest resource reference in use. return sql_value("select max(ref) value from resource",0); }function get_resource_ref_range($lower,$higher) { # Returns an array of resource references in the range $lower to $upper. return sql_array("select ref value from resource where ref>='$lower' and ref<='$higher' and archive=0 order by ref",0); } function copy_resource($from,$resource_type=-1) { # Create a new resource, copying all data from the resource with reference $from. # Note this copies only the data and not any attached file. It's very unlikely the # same file would be in the system twice, however users may want to clone an existing resource # to avoid reentering data if the resource is very similar. # If $resource_type if specified then the resource type for the new resource will be set to $resource_type # rather than simply copied from the $from resource. # Check that the resource exists if (sql_value("select count(*) value from resource where ref='$from'",0)==0) {return false;} # First copy the resources row sql_query("insert into resource(title,resource_type,creation_date,rating,country,archive,access,created_by) select title," . (($resource_type==-1)?"resource_type":("'" . $resource_type . "'")) . ",creation_date,rating,country,archive,access,created_by from resource where ref='$from';"); $to=sql_insert_id(); # Set the access level for user contributed resources. global $userref; if (!checkperm("e0")) {sql_query("update resource set access=-2,created_by='$userref' where ref='$to'");} # User contributed but e0 permission, so can contribute straight to live - still need to set 'created by' if (!checkperm("c")) {sql_query("update resource set created_by='$userref' where ref='$to'");} # Now copy all data sql_query("insert into resource_data(resource,resource_type_field,value) select '$to',resource_type_field,value from resource_data where resource='$from'"); # Copy keyword mappings sql_query("insert into resource_keyword(resource,keyword,hit_count,position,resource_type_field) select '$to',keyword,hit_count,position,resource_type_field from resource_keyword where resource='$from'"); # Copy relationships sql_query("insert into resource_related(resource,related) select '$to',related from resource_related where resource='$from'"); # Copy access sql_query("insert into resource_custom_access(resource,usergroup,access) select '$to',usergroup,access from resource_custom_access where resource='$from'"); # Log this daily_stat("Create resource",$to); resource_log($to,'c',0); return $to; } function resource_log($resource,$type,$field) { global $userref; sql_query("insert into resource_log(date,user,resource,type,resource_type_field) values (now(),'$userref','$resource','$type','$field')"); }function get_resource_log($resource) { return sql_query("select r.date,u.username,u.fullname,r.type,f.title from resource_log r left outer join user u on u.ref=r.user left outer join resource_type_field f on f.ref=r.resource_type_field where resource='$resource' order by r.date"); } function get_resource_type_name($type) { return i18n_get_translated(sql_value("select name value from resource_type where ref='$type'","")); } function get_resource_custom_access($resource) { # Return a list of usergroups with the custom access level for resource $resource (if set) return sql_query("select g.ref,g.name,g.permissions,c.access from usergroup g left outer join resource_custom_access c on g.ref=c.usergroup and c.resource='$resource' order by (g.permissions like '%v%') desc,g.name"); } function save_resource_custom_access($resource) { $groups=get_resource_custom_access($resource); sql_query("delete from resource_custom_access where resource='$resource'"); for ($n=0;$n<count($groups);$n++) { $usergroup=$groups[$n]["ref"]; $access=getvalescaped("custom_" . $usergroup,0); sql_query("insert into resource_custom_access(resource,usergroup,access) values ('$resource','$usergroup','$access')"); } } function get_custom_access($resource,$usergroup) { return sql_value("select access value from resource_custom_access where resource='$resource' and usergroup='$usergroup'",2); } function get_themes_by_resource($ref) { return sql_query("select c.ref,c.theme,c.name,u.fullname from collection_resource cr join collection c on cr.collection=c.ref and cr.resource='$ref' and c.public=1 left outer join user u on c.user=u.ref order by length(theme) desc"); }function update_resource_type($ref,$type) { sql_query("update resource set resource_type='$type' where ref='$ref'"); } function relate_to_array($ref,$array) { # Relates a resource to each in a simple array of ref numbers sql_query("delete from resource_related where resource='$ref'"); sql_query("insert into resource_related(resource,related) values ($ref," . join("),(" . $ref . ",",$array) . ")"); } function get_exiftool_fields() { # Returns a list of exiftool fields, which are basically fields with an 'exiftool field' set. return sql_query("select ref,exiftool_field from resource_type_field where length(exiftool_field)>0 order by exiftool_field"); }function write_metadata($path,$ref) { global $exiftool_path; if (isset($exiftool_path)) { if (file_exists(stripslashes($exiftool_path) . "/exiftool")) { if(!is_dir("filestore/tmp")){mkdir("filestore/tmp",0777);} $filename = pathinfo($path);
$filename = $filename['basename']; $tmpfile="filestore/tmp/".$filename; copy($path,$tmpfile); #Now that we have already copied the original file, we can use exiftool's overwrite_original on the tmpfile $command=$exiftool_path."/exiftool -overwrite_original "; $write_to=get_exiftool_fields(); for($i=0;$i< count($write_to);$i++) { $field=explode(",",$write_to[$i]['exiftool_field']); foreach ($field as $field){ $command.="-".$field."=\"".get_data_by_field($ref,$write_to[$i]['ref']) . "\" " ;} } $command.=" $tmpfile"; $output=shell_exec($command) or die("problem writing metadata:". $output); return $tmpfile; } } }function delete_exif_tmpfile($tmpfile){ if(file_exists($tmpfile)){unlink ($tmpfile);}}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -