setUploadDir($targetDir); // Create target dir if (!is_dir($targetDir)) { $GLOBALS["use_error_exception"] = true; try { mkdir($targetDir,0777,true); } catch (Exception $e) { // Ignore } unset($GLOBALS["use_error_exception"]); } $response = $server->serve(); // Extra check added to ensure URL uses $baseurl. Required due to reported issues with some reverse proxy configurations $tuslocation = $response->headers->get('location'); if (!empty($tuslocation) && (strpos($tuslocation, $baseurl) === false)) { $suffix = strpos($tuslocation,"/pages/upload_batch.php"); if($suffix !== false) { $tusbase = substr($tuslocation,0,$suffix); $rslocation = str_replace($tusbase,$baseurl,$tuslocation); debug("upload_batch. Correcting invalid upload URL from '" . $tuslocation . "' to '" . $rslocation . "'"); $response->headers->set('location', $rslocation); } } $response->send(); exit(0); // As this is the end of the TUS upload handler no further processing to be performed. } include_once "../include/image_processing.php"; $modal = getval("modal",""); $context = getval("context",""); $resource_type = getval('resource_type', ''); # Int representing resource type or string, see get_search_default_restypes() $collectionname = getval('entercolname', ''); $search = getval('search', ''); $offset = getval('offset', '', true); $order_by = getval('order_by', ''); $no_exif = getval('no_exif', ''); $autorotate = getval('autorotate','') == 'true'; // This is the archive state for searching, NOT the archive state to be set from the form POST $archive = getval('archive', '', true); $setarchivestate = getval('status', '', true); // Validate this workflow state is permitted or set the default if nothing passed $setarchivestate = get_default_archive_state($setarchivestate); $alternative = getval('alternative', ''); # Batch upload alternative files $replace = getval('replace', ''); # Replace Resource Batch $batch_replace_min = getval("batch_replace_min",0,true); # Replace Resource Batch - minimum ID of resource to replace $batch_replace_max = getval("batch_replace_max",0,true); # Replace Resource Batch - maximum ID $batch_replace_col = getval("batch_replace_col",0,true); # Replace Resource Batch - collection to replace $replace_resource = getval('replace_resource', ''); # Option to replace existing resource file $replace_resource_original_alt_filename = getval('replace_resource_original_alt_filename', ''); $single = getval("single","") != "" || getval("forcesingle","") != ""; $upload_here = (getval('upload_here', '') != '' ? true : false); $from_advanced_search = getval('advsearch', '') == 'true'; // Set to process upload once file upload complete $processupload = getval("processupload","") != ""; // When uploading, if there are any files in the queue that have similar names plus a suffix to distinguish between original // and alternatives (see $upload_alternatives_suffix) then, attach the matching alternatives to the resource they belong to $attach_alternatives_found_to_resources = (trim($upload_alternatives_suffix) != '') && (trim($alternative) == ''); $redirecturl = urldecode(getval("redirecturl","")); if ((!url_starts_with($baseurl, $redirecturl) && !hook("modifyredirecturl")) || !is_safe_url($redirecturl)) { $redirecturl = ''; } if ($replace_resource !== "" && resource_is_template((int) $replace_resource)) { error_alert($lang['error-permissiondenied']); } if ($replace_resource && (!get_edit_access($replace_resource) || resource_file_readonly($replace_resource))) { $replace_resource = false; } // If upload_then_edit we may not have a resource type, so we need to find the first resource type // which does not have an XU? (restrict upload) permission // This will be the resource type used for the upload, but may be changed later when extension is known // Resource types that can't be added to collections must be avoided for edit then upload mode to display the edit page for metadata entry. $all_resource_types = get_resource_types(); if($resource_type == "") { foreach($all_resource_types as $restype) { if (!checkperm("XU" . $restype["ref"]) && !in_array($restype["ref"],$collection_block_restypes)) { $resource_type = $restype["ref"]; break; } } // It is possible for there to be no 'unrestricted for upload' resource types // which means that the resource type used for the upload will be blank } # Load the configuration for the selected resource type. Allows for alternative notification addresses, etc. resource_type_config_override($resource_type); $hidden_collection = false; # Create a new collection? if($collection_add == "new" && ($processupload || !$upload_then_edit)) { # The user has chosen Create New Collection from the dropdown. if ($collectionname=="") { $collectionname = "Upload " . offset_user_local_timezone(date('YmdHis'), 'YmdHis'); # Do not translate this string, the collection name is translated when displayed! $hidden_collection = true; } $collection_add=create_collection($userref,$collectionname); if (getval("public",'0') == 1) { collection_set_public($collection_add); } if ($hidden_collection) { show_hide_collection($collection_add, false, $userref); } else { set_user_collection($userref,$collection_add); } } if($external_upload) { $rs_session = get_rs_session_id(true); $ci=get_session_collections($rs_session,$userref,true); if (count($ci)==0) { $usercollection = create_collection($userref,"New uploads",1,1,0,false,array("type" => COLLECTION_TYPE_SHARE_UPLOAD)); } else { $usercollection = $ci[0]; } $upload_review_col = $usercollection; rs_setcookie('lockedfields', '', 1); $redirecturl = generateURL( "{$baseurl}/pages/edit.php", array('upload_review_mode' => true, 'collection' => $usercollection, 'k' => $k ) ); } elseif ($upload_then_edit && $replace == "" && $replace_resource == "") { # Switch to the user's special upload collection. $upload_review_col = 0-$userref; $ci=get_collection($upload_review_col); if ($ci===false) { create_collection($userref,"New uploads",1,1,0-$userref); } if(!$processupload) { // Clear out review collection before new uploads are added to prevent inadvertent edits of old uploads remove_all_resources_from_collection(0-$userref); } # Set the redirect after upload to the start of the edit process rs_setcookie('lockedfields', '', 1); if ($upload_here && is_numeric($collection_add)) { // After edit redirect to collection uploaded to. $redirecturl = generateURL($baseurl . '/pages/search.php', array('search' => '!collection' . $collection_add)); } $redirecturl = generateURL( "{$baseurl}/pages/edit.php", array( 'upload_review_mode' => true, 'collection_add' => $collection_add, 'redirecturl' => $redirecturl, )); # Clear the user template clear_resource_data(0-$userref); } # If uploading alternative file, redirect to the resource rather than search results. if($alternative != "") { $searchparams = get_search_params(); $redirecturl = generateURL("{$baseurl}/pages/view.php", array_merge(['ref' => $alternative],$searchparams)); } $modify_redirecturl=hook('modify_redirecturl'); if($modify_redirecturl!==false) { $redirecturl=$modify_redirecturl; } if($collection_add=='undefined') { # Fallback to current user collection if nothing was passed in $collection_add = $usercollection; $uploadparams['collection_add']=$usercollection; } # Determine whether to autorotate for regular upload if($camera_autorotation) { if(isset($autorotation_preference)) { $autorotate = $autorotation_preference; } elseif($upload_then_edit) { $autorotate = $camera_autorotation_checked; } else { $autorotate = getval('autorotate', '') != ''; } } else { $autorotate = false; } $uploadparams= array( 'replace' => $replace, 'batch_replace_min' => $batch_replace_min, 'batch_replace_max' => $batch_replace_max, 'batch_replace_col' => $batch_replace_col, 'alternative' => $alternative, 'collection_add' => $collection_add, 'resource_type' => $resource_type, 'no_exif' => $no_exif, 'autorotate' => $autorotate, 'replace_resource' => $replace_resource, 'archive' => $archive, 'relateto' => getval('relateto', ''), 'filename_field' => getval('filename_field', ''), 'keep_original' => $replace_resource_preserve_option && $replace_resource_preserve_default, 'replace_resource_original_alt_filename' => $replace_resource_original_alt_filename, 'single' => ($single ? "true" : ""), 'status' => $setarchivestate, 'k' => $k, 'redirecturl' => $redirecturl, ); $searchparams = get_search_params(); global $merge_filename_with_title, $merge_filename_with_title_default, $filename_field; if($merge_filename_with_title) { $merge_filename_with_title_option = urlencode(getval('merge_filename_with_title_option', $merge_filename_with_title_default)); $merge_filename_with_title_include_extensions = urlencode(getval('merge_filename_with_title_include_extensions', '')); $merge_filename_with_title_spacer = urlencode(getval('merge_filename_with_title_spacer', '')); if(strtolower($merge_filename_with_title_option) != '') { $uploadparams['merge_filename_with_title_option'] = $merge_filename_with_title_option; } if($merge_filename_with_title_include_extensions != '') { $uploadparams['merge_filename_with_title_include_extensions']=$merge_filename_with_title_include_extensions; } if($merge_filename_with_title_spacer != '') { $uploadparams['merge_filename_with_title_spacer']= $merge_filename_with_title_spacer; } } if($embedded_data_user_select || isset($embedded_data_user_select_fields)) { // When uploading is complete, form data is moved to $_POST foreach($processupload ? $_POST : $_GET as $getname => $getval) { if (strpos($getname,"exif_option_")!==false) { $uploadparams[urlencode($getname)] = $getval; } } if(getval("exif_override","")!="") { $uploadparams['exif_override']="true"; } } // If user wants to replace original file and make it an alternative one, make the default filename for the alternative if($replace_resource_preserve_option && '' != $replace_resource) { $original_resource_data = get_resource_data($replace_resource); $default_replace_resource_original_alt_filename = str_replace( '%EXTENSION', strtoupper($original_resource_data['file_extension']??""), $lang['replace_resource_original_description'] ); $default_replace_resource_original_alt_filename .= nicedate(date('Y-m-d H:i'), true); $uploadparams['replace_resource_original_alt_filename'] = $default_replace_resource_original_alt_filename; } $uploadurl_extra_params = array(); if($upload_here) { $uploadparams['upload_here'] = $upload_here; $uploadparams['search'] = $search; $uploadparams['resource_type'] = $resource_type; $uploadparams['status'] = $setarchivestate; if ($from_advanced_search) { $uploadparams['advsearch'] = 'true'; } } $hook_params = hook('addtopluploadurl'); if(!empty($hook_params)) { $uploadparams = array_merge($uploadparams, $hook_params); } $uploadurl = generateURL("{$baseurl}/pages/upload_batch.php", $uploadparams, $uploadurl_extra_params); $default_sort_direction="DESC"; if (substr($order_by,0,5)=="field"){$default_sort_direction="ASC";} $sort=getval("sort",$default_sort_direction); $allowed_extensions=""; if(($upload_then_edit || $replace ) && !$alternative) { $all_allowed_extensions_holder = array(); foreach ($all_resource_types as $type) { if(get_allowed_extensions_by_type($type["ref"]) == "") { $all_allowed_extensions_holder = array(); break; } else { $extensions = explode(",", get_allowed_extensions_by_type($type["ref"])); foreach ($extensions as $extension) { if ($extension != "") { array_push($all_allowed_extensions_holder, trim(strtolower($extension))); } } } } $all_allowed_extensions_holder = array_unique($all_allowed_extensions_holder); $allowed_extensions = implode(",", $all_allowed_extensions_holder); } elseif ($resource_type!="" && !$alternative) { $allowed_extensions=get_allowed_extensions_by_type($resource_type); } // Process completed upload if ($processupload) { // HTTP headers for no cache etc header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); if(!is_dir($targetDir)) { mkdir($targetDir,0777,true); } $upfilename = getval("file_name",""); $key = getval('key', 0, true); $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds set_time_limit($php_time_limit); debug("upload_batch - received file from user '" . $username . "', filename: '" . $upfilename . "'"); # Work out the extension $origextension = parse_filename_extension($upfilename); $extension = mb_strtolower($origextension); $filenameonly = basename($upfilename, ".{$origextension}"); // Clean the filename $origuploadedfilename= $upfilename; $encodedname = str_replace("/","RS_FORWARD_SLASH", base64_encode($filenameonly)) . ((!empty($origextension)) ? ".{$origextension}" : ''); $encodedname = ($key > 0 ? "{$key}-" : "") . substr($encodedname, -30); // Keep temp filename short to avoid full path exceeding path length limits. $upfilepath = $targetDir . DIRECTORY_SEPARATOR . $encodedname; debug("upload_batch - processing. Looking for file at " . $upfilepath); if(!file_exists($upfilepath) || is_dir($upfilepath)) { debug("upload_batch - unable to locate file received from user " . $username . ", file path " . $upfilepath . ", filename " . $upfilename); $result["status"] = false; $result["message"] = str_replace("[filetype]",$upfilename,$lang["error_upload_invalid_file"]); $result["error"] = 110; die(json_encode($result)); } hook('modify_upload_file','',[$upfilename,$upfilepath]); # Banned extension? global $banned_extensions; if (is_banned_extension($extension)) { debug("upload_batch - invalid file extension received from user " . $username . ", filename " . $upfilename); $result["status"] = false; $result["message"] = str_replace("[filetype]",$upfilename,$lang["error_upload_invalid_file"]); $result["error"] = 105; unlink($upfilepath); die(json_encode($result)); } hook('additional_plupload_checks'); if($allowed_extensions != "") { // Check file extension and MIME type $filemime = get_mime_type($upfilepath); $allowed_extensions=str_replace(" ","",$allowed_extensions); $allowedmime = explode(",",trim($allowed_extensions)); if(strpos($allowed_extensions,"/") === false) // List of file extensions. not MIME types { $allowedmime = array_map("allowed_type_mime",$allowedmime); } if (array_intersect($allowedmime, $filemime) === []) { $filemime_as_csv = implode(', ', $filemime); debug("upload_batch - invalid file received from user " . $username . ", filename " . $upfilename . ", mime type(s): " . $filemime_as_csv); $result["status"] = false; $result["message"] = str_replace("[filetype]", $upfilename . " (" . $filemime_as_csv . ")",$lang["error_upload_invalid_file"]); $result["error"] = 105; unlink($upfilepath); die(json_encode($result)); } } $replace_filename_field=getval("filename_field",0,true); $target_resource = []; if($replace_filename_field != 0) { $target_resource = ps_array( 'SELECT resource value FROM resource_node AS rn JOIN node AS n ON rn.node = n.ref WHERE n.resource_type_field = ? AND name = ? AND resource > ?', [ 'i', $replace_filename_field, 's', $origuploadedfilename, 'i', $fstemplate_alt_threshold ]); } # Check for duplicate files if required $duplicates=check_duplicate_checksum($upfilepath,$replace_resource); if(count(array_diff($duplicates, $target_resource))>0) { debug("upload_batch ERROR- duplicate file matches resources" . implode(",",$duplicates)); $result["status"] = false; $result["message"] = str_replace("[resources]",implode(",",$duplicates),$lang["error_upload_duplicate_file"]); $result["error"] = 108; unlink($upfilepath); die(json_encode($result)); } elseif(!hook("initialuploadprocessing")) { if ($alternative!="") { # Determine whether to autorotate for alternative upload $autorotate = false; if($camera_autorotation) { $autorotate = true; if(isset($autorotation_preference)) { $autorotate = $autorotation_preference; } } # Upload an alternative file $resource_data = get_resource_data($alternative); if($resource_data["lock_user"] > 0 && $resource_data["lock_user"] != $userref) { $result["status"] = false; $result["message"] = get_resource_lock_message($resource_data["lock_user"]); $result["error"] = 111; $result["id"] = escape($ref); $result["collection"] = escape($collection_add); } else { # Add a new alternative file $aref=add_alternative_file($alternative,$upfilename); # Find the path for this resource. $path=get_resource_path($alternative, true, "", true, $extension, -1, 1, false, "", $aref); # Move the sent file to the alternative file location $process_file_upload = process_file_upload(new SplFileInfo($upfilepath), new SplFileInfo($path), []); if (!$process_file_upload['success']) { $result["status"] = false; $result["message"] = $process_file_upload['error']->i18n($lang); $result["error"] = 104; die(json_encode($result)); } else { chmod($path,0777); $file_size = @filesize_unlimited($path); # Autorotate the alternative if required if($autorotate) { AutoRotateImage($path); } # Save alternative file data. ps_query("update resource_alt_files set file_name=?,file_extension=?,file_size=?,creation_date=now() where resource=? and ref=?",array("s",$upfilename,"s",$extension,"i",$file_size,"i",$alternative,"i",$aref)); if ($alternative_file_previews) { create_previews($alternative,false,$extension,false,false,$aref); } hook('after_alt_upload','',array($alternative,array("ref"=>$aref,"file_size"=>$file_size,"extension"=>$extension,"name"=>$upfilename,"altdescription"=>"","path"=>$path,"basefilename"=>str_ireplace("." . $extension, '', $upfilename)))); // Check to see if we need to notify users of this change if($notify_on_resource_change_days!=0) { // we don't need to wait for this.. ob_flush();flush(); notify_resource_change($alternative); } # Update disk usage update_disk_usage($alternative); hook('upload_alternative_extra', '', array($path)); $result["status"] = true; $result["message"] = $lang["alternative_file_created"]; $result["id"] = $alternative; $result["alternative"] = $aref; } } } elseif ($replace=="" && $replace_resource=="") { # Standard upload of a new resource # create ref via copy_resource() or other method // For upload_then_edit mode ONLY, set the resource type based on the extension. User // can later change this at the edit stage // IMPORTANT: Change resource type only if user has access to it if($upload_then_edit && (!($upload_here && $from_advanced_search) || !is_int_loose($resource_type))) { $resource_type_from_extension = get_resource_type_from_extension( parse_filename_extension($upfilepath), $resource_type_extension_mapping, $resource_type_extension_mapping_default ); // Only update the resource when resource_type permissions allow if(!checkperm("XU{$resource_type_from_extension}") && in_array($resource_type_from_extension,array_column($all_resource_types,"ref"))) { $resource_type = $resource_type_from_extension; // The resource type has been changed so clear the cached value $GLOBALS['get_resource_data_cache'] = array(); } } $modified_ref=hook("modifyuploadref"); if ($modified_ref!="") { $ref=$modified_ref; } elseif(!$upload_then_edit) { $ref=copy_resource(0-$userref,-1,$lang["createdfromwebuploader"]); # Copy from user template # Store original filename early in the process so that macros can benefit if (isset($filename_field)) { update_field($ref,$filename_field,$origuploadedfilename); } } // copy_resource() returns false if user doesn't have a resource template // Usually, this happens when a user had from the first time upload_then_edit mode on if($upload_then_edit || false === $ref) { $ref = create_resource($resource_type, $setarchivestate,-1,$lang["createdfromwebuploader"]); } # Check that $ref is not false - possible return value with create_resource() if(!$ref) { $result["status"] = false; $result["message"] = "Failed to create resource with given resource type: $resource_type"; $result["error"] = 125; $result["id"] = escape($ref); $result["collection"] = escape($collection_add); } else { // Check valid requested state by calling function that checks permissions update_archive_status($ref, $setarchivestate); if($upload_then_edit && $upload_here) { $search = urldecode($search); if(!empty(get_upload_here_selected_nodes($search, array()))) { add_resource_nodes($ref, get_upload_here_selected_nodes($search, array()), true); } } # Add to collection? if (is_numeric($collection_add)) { add_resource_to_collection($ref,$collection_add,false,"",$resource_type); } if ($upload_then_edit && $replace == "" && $replace_resource == "" && $collection_add != $upload_review_col) { # Also add to the user's special upload collection. add_resource_to_collection($ref,$upload_review_col,false,"",$resource_type); } $relateto = getval("relateto","",true); if($relateto!="" && !upload_share_active()) { // This has been added from a related resource upload link update_related_resource($relateto,$ref); } if($upload_then_edit && $reset_date_upload_template) { // If extracting embedded metadata than expect the date to be overriden as it would be if // upload_then_edit = false update_field($ref, $reset_date_field, date('Y-m-d H:i:s')); } # Log this daily_stat("Resource upload",$ref); $success=upload_file($ref,($no_exif=="yes" && getval("exif_override","")==""),false,$autorotate,$upfilepath); if($success && $auto_generated_resource_title_format != '' && !$upload_then_edit) { $new_auto_generated_title = ''; if(strpos($auto_generated_resource_title_format, '%title') !== false) { $resource_detail = ps_query (" SELECT d.ref, d.file_extension, n.name FROM (SELECT r.ref, r.file_extension FROM resource r WHERE r.ref = ?) as d LEFT JOIN resource_node rn ON rn.resource=d.ref LEFT JOIN node n ON n.ref=rn.node AND n.resource_type_field = ? LIMIT 1", ["i",$ref, "i",$view_title_field]); $new_auto_generated_title = str_replace( array('%title', '%resource', '%extension'), array( $resource_detail[0]['name'], $resource_detail[0]['ref'], $resource_detail[0]['file_extension'] ), $auto_generated_resource_title_format); } else { $resource_detail = ps_query (" SELECT r.ref, r.file_extension FROM resource r WHERE r.ref = ?", ["i",$ref] ); $new_auto_generated_title = str_replace( array('%resource', '%extension'), array( $resource_detail[0]['ref'], $resource_detail[0]['file_extension'] ), $auto_generated_resource_title_format); } if($new_auto_generated_title != '') { update_field($ref, $view_title_field, $new_auto_generated_title); } } hook('upload_original_extra', '', array($ref)); $after_upload_result = hook('afterpluploadfile', '', array($ref, $extension)); if (is_array($after_upload_result)) { $result["status"] = false; $result["error"] = $after_upload_result["code"]; $result["message"] = $after_upload_result["message"]; } else { $result["status"] = true; $result["message"] = $lang["created"]; $result["id"] = escape($ref); $result["collection"] = escape($collection_add); } } } elseif ($replace=="" && $replace_resource!="") { // Replacing an existing resource file // Extract data unless user has selected not to extract exif data and there are no per field options set $no_exif = ('yes' == $no_exif) && '' == getval('exif_override', ''); $keep_original = getval('keep_original', '') != ''; $success = replace_resource_file($replace_resource,$upfilepath,$no_exif,$autorotate,$keep_original); if (!$success) { $result["status"] = false; $result["message"] = $lang["error_upload_replace_file_fail"]; $result["error"] = 109; $result["id"] = $replace_resource; } else { $result["status"] = true; $result["message"] = $lang["replacefile"]; $result["error"] = 0; $result["id"] = escape($replace_resource); } } else { $no_exif = ('yes' == $no_exif) && '' == getval('exif_override', ''); $keep_original = getval('keep_original', '') != ''; if (!isset($batch_replace_col) || $batch_replace_col == 0) { $conditions = array(); $batch_replace_min = max((int)($batch_replace_min),$fstemplate_alt_threshold); $firstref = max($fstemplate_alt_threshold, $batch_replace_min); $sql = "SELECT ref value FROM resource WHERE ref >= ? "; $sql_params = array("i",$batch_replace_min); if ($batch_replace_max > 0) { $sql .= " AND ref <= ?"; $sql_params = array_merge($sql_params,["i",$batch_replace_max]); } $sql .= " ORDER BY ref ASC"; $replace_resources = ps_array($sql,$sql_params); debug("batch_replace upload: replacing files for resource IDs. Min ID: " . $batch_replace_min . (($batch_replace_max > 0) ? " Max ID: " . $batch_replace_max : "")); } else { $replace_resources = get_collection_resources($batch_replace_col); if ($replace_resources === false) { $result["status"] = false; $result["message"] = escape($lang["error_upload_replace_file_fail_invalid_collection"]); $result["error"] = 109; $result["id"] = escape($batch_replace_col); unlink($upfilepath); exit(json_encode($result)); } debug("batch_replace upload: replacing resources within collection " . $batch_replace_col . " only"); } if($replace_filename_field != 0) { $target_resourceDebug = $target_resource; $target_resourceDebug_message1= "Target resource details - target_resource: " . (count($target_resource)>0 ? json_encode($target_resource) : "NONE") . " . resource_type_field: $replace_filename_field . value: $origuploadedfilename . template_alt_threshold: $fstemplate_alt_threshold . collection: $batch_replace_col"; debug($target_resourceDebug_message1); $target_resource=array_values(array_intersect($target_resource,$replace_resources)); if(count($target_resource)==1 && !resource_file_readonly($target_resource[0])) { // A single resource has been found with the same filename $success = replace_resource_file($target_resource[0],$upfilepath,$no_exif,$autorotate,$keep_original); if (!$success) { $result["status"] = false; $result["message"] = $lang["error_upload_replace_file_fail"]; $result["error"] = 109; $result["id"] = $target_resource[0]; } else { $result["status"] = true; $result["message"] = $lang["replacefile"]; $result["error"] = 0; $result["id"] = escape($target_resource[0]); } } elseif(count($target_resource)==0) { // No resource found with the same filename $target_resourceDebug_message2 = "Target resource not found - target_resource: " . (count($target_resource)>0 ? json_encode($target_resource) : "NONE FOUND - should have been: " . (count($target_resourceDebug)>0 ? json_encode($target_resourceDebug): "NONE")) . " . Replace in resources: " . json_encode($replace_resources); debug($target_resourceDebug_message2); $result["status"] = false; $result["message"] = str_replace("[filename]",$origuploadedfilename,$lang["error_upload_replace_no_matching_file"]); $result["error"] = 106; } else { // Multiple resources found with the same filename // but we are going to replace them because $replace_batch_existing is set to true $resourcelist=implode(",",$target_resource); if ($replace_batch_existing) { foreach ($target_resource as $replaced) { $success = replace_resource_file($replaced,$upfilepath,$no_exif,$autorotate,$keep_original); if (!$success) { $result["status"] = false; $result["message"] = $lang["error_upload_replace_file_fail"]; $result["error"] = 109; $result["id"] = $replaced; } $success = upload_file($replaced, ('yes' == $no_exif && '' == getval('exif_override', '')), false, $autorotate, $upfilepath); } $result["status"] = true; $result["message"] = $lang["replacefile"]; $result["error"] = 0; $result["id"] = escape($resourcelist); } else { // Multiple resources found with the same filename $result["status"] = false; $result["message"] = str_replace("[filename]",$origuploadedfilename,$lang["error_upload_replace_multiple_matching_files"]); $result["error"] = 107; $result["id"] = $resourcelist; } } } else { # Overwrite an existing resource using the number from the filename. # Extract the number from the filename $origuploadedfilename=strtolower(str_replace(" ","_",$origuploadedfilename)); $s=explode(".",$origuploadedfilename); # does the filename follow the format xxxxx.xxx? if(2 == count($s)) { $ref = trim($s[0]); // is the first part of the filename numeric? if(is_numeric($ref) && in_array($ref,$replace_resources) && !resource_file_readonly($ref)) { debug("batch_replace upload: replacing resource with id " . $ref); daily_stat("Resource upload",$ref); # The replace may need to keep the original (if one exists) by saving it as an alternative file $keep_original = getval('keep_original', ''); $success = replace_resource_file($ref,$upfilepath,$no_exif,$autorotate,$keep_original); if (!$success) { $result["status"] = false; $result["message"] = $lang["error_upload_replace_file_fail"]; $result["error"] = 109; $result["id"] = $ref; } else { $result["status"] = true; $result["message"] = $lang["replacefile"]; $result["error"] = 0; $result["id"] = escape($ref); } } else { // No resource found with the same filename debug("batch_replace upload: No valid resource id for filename " . $origuploadedfilename); $result["status"] = false; $result["message"] = str_replace("[filename]",$origuploadedfilename,$lang["error_upload_replace_no_matching_file"]); $result["error"] = 106; } } } } } // Remove file now it has been handled if(file_exists($upfilepath)) { unlink($upfilepath); } // Return JSON-RPC response exit(json_encode($result)); } // Check if upload should be disabled because the filestore location is indexed and browseable $cfb = check_filestore_browseability(); if(!$cfb['index_disabled']) { error_alert($lang['error_generic_misconfiguration'], true, 200); exit(); } $headerinsert.=" "; include "../include/header.php"; ?>
" . escape($lang["overquota"]) . "
"; include "../include/footer.php"; exit(); } if ($alternative!="") { $alturl = generateURL($baseurl_short . 'pages/alternative_files.php',$searchparams,array("ref"=>$alternative)); ?> $replace_resource)); $viewurl = generateURL($baseurl_short . 'pages/view.php',$searchparams,array("ref"=>$replace_resource)); ?>