Collections functions
General functions
Render functions
Theme permission functions
Resource functions

delete_resource()

Parameters

ColumnTypeDefaultDescription
$ref

Location

include/resource_functions.php lines 3001 to 3120

Definition

 
function delete_resource($ref)
    {
    global 
$userref;
    
# Delete the resource, all related entries in tables and all files on disk
    
$resource get_resource_data($ref);

    if (!
$resource
        
||
            (
                (
                
checkperm("D")
                ||
                !
get_edit_access($ref,$resource["archive"], false,$resource)
                ||
                (isset(
$userref) && $resource["lock_user"] > && $resource["lock_user"] != $userref)
                )
            &&
                !
hook('check_single_delete')
            &&
                
PHP_SAPI != 'cli'
            
)
        )
        {return 
false;}

    
$current_state=$resource['archive'];

    global 
$resource_deletion_state$staticsync_allow_syncdir_deletion$storagedir;
    if (isset(
$resource_deletion_state) && $current_state!=$resource_deletion_state# Really delete if already in the 'deleted' state.
        
{
        
# $resource_deletion_state is set. Do not delete this resource, instead move it to the specified state.
        
update_archive_status($ref$resource_deletion_state$current_state);

        
# log this so that administrator can tell who requested deletion
        
resource_log($ref,LOG_CODE_DELETED,'');

        
# Log the deletion of this resource for any collection it was in.
        
$in_collections ps_query("select collection, resource from collection_resource where resource = ?", array("i"$ref));

        
# Remove the resource from any collections
        
ps_query("delete from collection_resource where resource = ?", array("i"$ref));

        if (
count($in_collections) > 0)
            {
            for(
$n=0$n count($in_collections); $n++)
                {
                
collection_log($in_collections[$n]['collection'], LOG_CODE_COLLECTION_DELETED_RESOURCE$in_collections[$n]['resource']);
                }
            }

        return 
true;
        }

    
# FStemplate support - do not allow samples from the template to be deleted
    
if (resource_file_readonly($ref)) {return false;}


    
# Is transcoding
    
if ($resource['is_transcoding']==1) {return false;} # Can't delete when transcoding

    # Delete files first
    
$extensions = array();
    
$extensions[]=$resource['file_extension']?$resource['file_extension']:"jpg";
    
$extensions[]=$resource['preview_extension']?$resource['preview_extension']:"jpg";
    
$extensions[]=$GLOBALS['ffmpeg_preview_extension'];
    
$extensions[]='icc'// also remove any extracted icc profiles
    
$extensions=array_unique($extensions);

    foreach (
$extensions as $extension)
        {
        
$sizes=get_image_sizes($ref,true,$extension);
        foreach (
$sizes as $size)
            {
            if (
file_exists($size['path']) && ($staticsync_allow_syncdir_deletion || false !== strpos ($size['path'],$storagedir))) // Only delete if file is in filestore
                
{
                
$pathtofile realpath($size['path']); // avoid passing relative path to unlink function to prevent error on removal of file.
                
unlink($pathtofile);
                }
            }
        }

    
hook('delete_resource_extra''', array($resource));

    
# Delete any alternative files
    
$alternatives=get_alternative_files($ref);
    for (
$n=0;$n<count($alternatives);$n++)
        {
        
delete_alternative_file($ref,$alternatives[$n]['ref']);
        }


    
//attempt to remove directory
    
$resource_path get_resource_path($reftrue"pre"true);

    
$dirpath dirname($resource_path);
    
hook('delete_resource_path_extra''', array($dirpath));
    @
rcRmdir ($dirpath); // try to delete directory, but if we do not have permission fail silently for now

    
hook("beforedeleteresourcefromdb","",array($ref));

    
# Delete all database entries
    
clear_resource_data($ref);
    
resource_log($ref,LOG_CODE_DELETED_PERMANENTLY,'');
    
ps_query("delete from resource where ref=?",array("i",$ref));
    
ps_query("delete from collection_resource where resource=?",array("i",$ref));
    
ps_query("delete from resource_custom_access where resource=?",array("i",$ref));
    
ps_query("delete from external_access_keys where resource=?",array("i",$ref));
    
ps_query("delete from resource_alt_files where resource=?",array("i",$ref));
    
ps_query(
        
"    DELETE an
               FROM annotation_node AS an
         INNER JOIN annotation AS a ON a.ref = an.annotation
              WHERE a.resource = ?"
,array("i",$ref)
    );
    
ps_query("DELETE FROM annotation WHERE resource = ?",array("i",$ref));
    
hook("afterdeleteresource");
    
    
clear_query_cache("stats");

    return 
true;
    }

This article was last updated 4th February 2023 13:05 Europe/London time based on the source file dated 3rd February 2023 13:55 Europe/London time.