Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

delete_resource()

Parameters

ColumnTypeDefaultDescription
$ref

Location

include/resource_functions.php lines 2521 to 2635

Definition

 
function delete_resource($ref)
    {
    global 
$userref;
    
# Delete the resource, all related entries in tables and all files on disk
    
$ref      escape_check($ref);
    
$resource get_resource_data($ref);
        
    if (!
$resource
        
||
            (
                (
                
checkperm("D")
                ||
                (isset(
$allow_resource_deletion) && !$allow_resource_deletion)
                ||
                !
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,'');
        
        
# Remove the resource from any collections
        
sql_query("delete from collection_resource where resource='$ref'");
            
        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);
                }
            }
        }
    
    
# 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);
    @
rcRmdir ($dirpath); // try to delete directory, but if we do not have permission fail silently for now
    
    # Log the deletion of this resource for any collection it was in. 
    
$in_collections=sql_query("select * from collection_resource where resource = '$ref'");
    if (
count($in_collections)>0){
        for(
$n=0;$n<count($in_collections);$n++)
            {
            
collection_log($in_collections[$n]['collection'],'d',$in_collections[$n]['resource']);
            }
        }

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

    
# Delete all database entries
    
clear_resource_data($ref);
    
sql_query("delete from resource where ref='$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'");
    
sql_query("delete from resource_alt_files where resource='$ref'");
    
sql_query(
        
"    DELETE an
               FROM annotation_node AS an
         INNER JOIN annotation AS a ON a.ref = an.annotation
              WHERE a.resource = '
{$ref}'"
    
);
    
sql_query("DELETE FROM annotation WHERE resource = '{$ref}'");
    
hook("afterdeleteresource");
    
    return 
true;
    }

This article was last updated 23rd November 2020 16:35 Europe/London time based on the source file dated 13th November 2020 16:50 Europe/London time.