Collections functions
General functions
Render functions
Theme permission functions
Resource 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 32 to 367

Definition

 
function get_resource_path(
    
$ref,
    
$getfilepath,
    
$size '',
    
$generate true,
    
$extension 'jpg',
    
$scramble true,
    
$page 1,
    
$watermarked false,
    
$file_modified '',
    
$alternative = -1,
    
$includemodified true
)
    {
    
# 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(!preg_match('/^[a-zA-Z0-9]+$/',(string) $extension))
        {
        
$extension 'jpg';
        }
    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;
        }

    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;

    
// 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 && !in_array($size,array("col","thm","pre")))
        {
        global 
$baseurl$k$get_resource_path_extra_download_query_string_params;

        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'         => $ref,
                
'size'        => $size,
                
'ext'         => $extension,
                
'page'        => $page,
                
'alternative' => $alternative,
                
'k'           => $k,
                
'noattach'    => 'true',
            ),
            
$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="";
    
#if (!file_exists(dirname(__FILE__) . $folder)) {mkdir(dirname(__FILE__) . $folder,0777);}

    # 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))
                {
                
// determin 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';
        }

    
# Append modified date/time to the URL so the cached copy is not used if the file is changed.
    
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))
                {
                
$file .= "?v=" urlencode( This site uses cookies for interactivity and analytics      Accept cookies