Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

get_resource_path()

Parameters

ColumnTypeDefaultDescription
$ref
$getfilepath
$size ''
$generate true
$extension 'jpg'
$scramble true
$page 1
$watermarked false
$file_modified ''
$alternative -1
$includemodified true

Location

include/resource_functions.php lines 34 to 414

Definition

 
function get_resource_path(
    
$ref,
    
$getfilepath,
    
$size '',
    
$generate true,
    
$extension 'jpg',
    
$scramble true,
    
$page 1,
    
$watermarked false,
    
$file_modified '',
    
$alternative = -1,
    
$includemodified true
)
    {
    global 
$storagedir$originals_separate_storage$fstemplate_alt_threshold$fstemplate_alt_storagedir,
    
$fstemplate_alt_storageurl$fstemplate_alt_scramblekey$scramble_key$hide_real_filepath,
    
$migrating_scrambled$scramble_key_old$filestore_evenspread$filestore_migrate,
    
$baseurl$k$get_resource_path_extra_download_query_string_params$resource_path_pull_cache;

    if(isset(
$resource_path_pull_cache[$ref]) && strtolower((string)$extension) == 'jpg')
        {
        
$ref $resource_path_pull_cache[$ref]["ref"];
        }

    
$size safe_file_name((string) $size);
    
$extension safe_file_name((string) $extension);
    if (
is_banned_extension($extension)) {
        
$extension 'jpg';
    }

    if (!
is_int_loose($file_modified)) {
        
$file_modified '';
    }

    
# 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($extension=='icc')
        {
        
# use the preview path
        
$size='pre';
        }

    
$override hook(
        
'get_resource_path_override',
        
'',
        array(
$ref$getfilepath$size$generate$extension$scramble$page$watermarked$file_modified$alternative$includemodified)
    );

    if(
is_string($override))
        {
        return 
$override;
        }

    
$refresh_key='';
    
// Determine v value early so that it can also be used when $hide_real_filepath is enabled
    
if (!$getfilepath && $includemodified)
        {
        if (
$file_modified=="")
            {
            
# Work out the value from the file on disk
            
$disk_path=get_resource_path($ref,true,$size,false,$extension,$scramble,$page,$watermarked,'',$alternative,false);
            if (
file_exists($disk_path))
                {
                
$refresh_key urlencode(filemtime($disk_path));
                }
            }
        else
            {
            
# Use the provided value
            
$refresh_key .= urlencode($file_modified);
            }
        }

    
// Return URL pointing to download.php. download.php will call again get_resource_path() to ask for the physical path
    
if(!$getfilepath && $hide_real_filepath)
        {
        if(
            !isset(
$get_resource_path_extra_download_query_string_params)
            || 
is_null($get_resource_path_extra_download_query_string_params)
            || !
is_array($get_resource_path_extra_download_query_string_params)
        )
            {
            
$get_resource_path_extra_download_query_string_params = array();
            }

        return 
generateURL(
            
"{$baseurl}/pages/download.php",
            array(
                
'ref'         => (int) $ref,
                
'size'        => $size,
                
'ext'         => $extension,
                
'page'        => (int) $page,
                
'alternative' => (int) $alternative,
                
'watermarked' => $watermarked,
                
'k'           => $k,
                
'noattach'    => 'true',
                
'v'           => $refresh_key,
            ),
            
$get_resource_path_extra_download_query_string_params);
        }

    if (
$size=="")
        {
        
# For the full size, check to see if the full path is set and if so return that.
        
global $get_resource_path_fpcache;
        
truncate_cache_arrays();

        if (!isset(
$get_resource_path_fpcache[$ref])) {$get_resource_path_fpcache[$ref]=ps_value("select file_path value from resource where ref=?",array("i",$ref),"");}
        
$fp=$get_resource_path_fpcache[$ref]??"";

        
# Test to see if this nosize file is of the extension asked for, else skip the file_path and return a $storagedir path.
        # If using staticsync, file path will be set already, but we still want the $storagedir path for a nosize preview jpg.
        # Also, returning the original filename when a nosize 'jpg' is looked for is no good, since preview_preprocessing.php deletes $target.
        
$fp $fp ?? "";
        
$test_ext explode(".",$fp);$test_ext=trim(strtolower($test_ext[count($test_ext)-1]));

        if ((
$test_ext == $extension || $alternative 0) && strlen($fp)>&& (strpos($fp,"/")!==false || strlen($fp)>1))
            {
            if (
$getfilepath)
                {
                global 
$syncdir;
                
$syncdirmodified=hook("modifysyncdir","all",array($ref$fp$alternative)); if ($syncdirmodified!=""){return $syncdirmodified;}
                if (
$alternative <= 0) {
                    return 
$syncdir "/" $fp;
                }
                elseif(!
$generate)
                    {
                    
// Alternative file and using staticsync. Would not be generating path if checking for an existing file.
                    // Check if file is present in syncdir, else continue to get the $storagedir location
                    
$altfile get_alternative_file($ref,$alternative);
                    if(
$altfile["file_extension"]==$extension && file_exists($altfile["file_name"]))
                        {return 
$altfile["file_name"];}
                    }
                }
            else
                {
                global 
$baseurl_short$k;
                return 
$baseurl_short "pages/download.php?ref={$ref}&size={$size}&ext={$extension}&noattach=true&k={$k}&page={$page}&alternative={$alternative}";
                }
            }
        }

    
// 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.
    
if($scramble && isset($scramble_key) && '' != $scramble_key)
        {
        
$skey $scramble_key;

        
// FSTemplate support - for trial system templates
        
if($fstemplate_alt_threshold && $ref $fstemplate_alt_threshold && -== $alternative)
            {
            
$skey $fstemplate_alt_scramblekey;
            }
        
$scramblepath substr(md5("{$ref}_{$skey}"), 015);
        }

    if (
$extension=="") {$extension="jpg";}

    
$folder="";

    
# Original separation support
    
if($originals_separate_storage)
        {
        global 
$originals_separate_storage_ffmpegalts_as_previews;
        if(
$alternative>&& $originals_separate_storage_ffmpegalts_as_previews)
            {
            
$alt_data=ps_query('select ref,resource,name,description,file_name,file_extension,file_size,creation_date,unoconv,alt_type,page_count from resource_alt_files where ref=?',array("i",$alternative));
            if(!empty(
$alt_data))
                {
                
// Determine if this file was created from $ffmpeg_alternatives
                
$ffmpeg_alt=alt_is_ffmpeg_alternative($alt_data[0]);
                if(
$ffmpeg_alt)
                    {
                    
$path_suffix="/resized/";
                    }
                else
                    {
                    
$path_suffix="/original/";
                    }
                }
            else
                {
                
$path_suffix="/original/";
                }
            }
        elseif(
$size=="")
            {
            
# Original file (core file or alternative)
            
$path_suffix="/original/";
            }
        else
            {
            
# Preview or thumb
            
$path_suffix="/resized/";
            }
        }
    else
        {
        
// If getting the physical path, use the appropriate directory separator. For URL, it can only use forward
        // slashes (/). For more information, see RFC 3986 (https://tools.ietf.org/html/rfc3986)
        
$path_suffix = ($getfilepath DIRECTORY_SEPARATOR "/");
        }

    for (
$n=0;$n<strlen($ref);$n++)
        {
        
// If using $filestore_evenspread then the path is generated using the least significant figure first instead of the greatest significant figure
        
$refpos $filestore_evenspread ? -($n+1) : $n;
        
$folder .= substr($ref,$refpos,1);

        if (
$scramble && isset($scramblepath) && ($n==(strlen($ref)-1)))
            {
            
$folder.="_" $scramblepath;
            }

        
$folder.="/";
        if (!(
file_exists($storagedir $path_suffix $folder)) && $generate)
            {
            
$GLOBALS["use_error_exception"] = true;
            try
                {
                
mkdir($storagedir $path_suffix $folder0777true);
                }
            catch (
Exception $e)
                {
                
$returned_error $e->getMessage();
                
debug("get_resource_path - mkdir(): " $returned_error);
                if (
substr($returned_errorstrpos($returned_error'mkdir(): ') + 9) != 'File exists')
                    {
                    
trigger_error($returned_errorE_USER_WARNING);
                    }
                }
            unset(
$GLOBALS["use_error_exception"]);
            
chmod($storagedir $path_suffix $folder0777);
            }
        }

    
# Add the page to the filename for everything except page 1.
    
if ($page==1) {$p="";} else {$p="_" $page;}

    
# Add the alternative file ID to the filename if provided
    
if ($alternative>0) {$a="_alt_" $alternative;} else {$a="";}

    
# Add the watermarked url too
    
if ($watermarked) {$p.="_wm";}

    
$sdir=$storagedir;

    
# FSTemplate support - for trial system templates
    
if ($fstemplate_alt_threshold>&& $ref<$fstemplate_alt_threshold && $alternative==-1)
        {
        
$sdir=$fstemplate_alt_storagedir;
        }
    
# switch the size back so the icc profile name matches the original name and find the original extension
    
$icc=false;
    if (
$extension=='icc')
        {
        
$size='';
        
$icc=true;
        
$extension=ps_value("select file_extension value from resource where ref=?", array("i",$ref), 'jpg');
        }

    
$filefolder=$sdir $path_suffix $folder;

    
# Fetching the file path? Add the full path to the file
    
if ($getfilepath)
        {
        
$folder=$filefolder;
        }
    else
        {
        global 
$storageurl;$surl=$storageurl;

        
# FSTemplate support - for trial system templates
        
if ($fstemplate_alt_threshold>&& $ref<$fstemplate_alt_threshold && $alternative==-1)
            {
            
$surl=$fstemplate_alt_storageurl;
            }

        
$folder=$surl $path_suffix $folder;
        }
    if (
$scramble && isset($skey))
        {
        
$file_old=$filefolder $ref $size $p $a "." $extension;
        
$file_new=$filefolder $ref $size $p $a "_" substr(md5($ref $size $p $a $skey),0,15) . "." $extension;
        
$file=$folder $ref $size $p $a "_" substr(md5($ref $size $p $a $skey),0,15) . "." $extension;
        if (
file_exists($file_old))
            {
            
rename($file_old$file_new);
            }
        }
    else
        {
        
$file=$folder $ref $size $p $a "." $extension;
        }

    if(
$icc)
        {
        
$file.='.icc';
        }

    if(
trim($refresh_key) != '')
        {
        
$file .= "?v={$refresh_key}";
        }

    if ((
$scramble && isset($migrating_scrambled) && $migrating_scrambled) || ($filestore_migrate && $filestore_evenspread))
        {
        
// Check if there is a file at the path using no/previous scramble key or with $filestore_evenspread=false;
        // Most will normally have been moved using pages/tools/xfer_scrambled.php or pages/tools/filestore_migrate.php

        // Flag to set whether we are migrating to even out filestore distibution or because of scramble key change
        
$redistribute_mode $filestore_migrate;

        
// Get the new paths without migrating to prevent infinite recursion
        
$migrating_scrambled false;
        
$filestore_migrate false;
        
$newpath $getfilepath $file get_resource_path($ref,true,$size,true,$extension,true,$page,false,'',$alternative);

        
// Use old settings to get old path before migration and migrate if found
        
if($redistribute_mode)
            {
            
$filestore_evenspread false;
            }
        else
            {
            
$scramble_key_saved $scramble_key;
            
$scramble_key = isset($scramble_key_old) ? $scramble_key_old "";
            }

        if (
$icc)
            {
            
$extension 'icc';
            }

        
$oldfilepath=get_resource_path($ref,true,$size,false,$extension,true,$page,false,'',$alternative);
        if (
file_exists($oldfilepath))
            {
            if(!
file_exists(dirname($newpath)))
                {
                
mkdir(dirname($newpath),0777,true);
                }
            
rename ($oldfilepath,$newpath);
            }

        
// Reset key/evenspread value
        
if($redistribute_mode)
            {
            
$filestore_evenspread true;
            
$filestore_migrate true;
            }
        else
            {
            
$scramble_key $scramble_key_saved;
            
$migrating_scrambled true;
            }
        }

    if(!
file_exists($file)
        && !
$getfilepath
        
&& $alternative==-1
        
&& !$generate
        
&& !defined("GETRESOURCEPATHNORECURSE" $ref)
        )
        {
        
$resdata get_resource_data($ref);
        
$pullresource related_resource_pull($resdata);
        if(
$pullresource !== false)
            {
            
define("GETRESOURCEPATHNORECURSE" $ref,true);
            if(
$size == "hpr" && is_jpeg_extension($pullresource["file_extension"]))
                {
                
// If a JPG then no 'hpr' will be available
                
$size "";
                }
            
$file get_resource_path($pullresource["ref"],$getfilepath,$size,false,$extension,$scramble,$page,$watermarked,$file_modified,-1,$includemodified);
            }
        }

    return 
$file;
    }

This article was last updated 4th December 2024 08:35 Europe/London time based on the source file dated 28th November 2024 12:10 Europe/London time.