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

check_resources()

Description

Check integrity of primary resource files

then file checksums will be checked

Parameters

ColumnTypeDefaultDescription
$resources array [] Array of resource data e.g. from search results
$presenceonly bool Check for file presence only? If false (and if $file_checksums is enabled)

Return

array Array of resource IDs that have failed to verify

Location

include/resource_functions.php lines 8928 to 9070

Definition

 
function check_resources(array $resources = [], bool $presenceonly false): array
{
    if (
count($resources) === 0) {
        
$resources get_resources_to_validate();
    }
    if (
count($resources) === 0) {
        echo 
" - No resources require integrity checks" PHP_EOL;
        return [];
    }

    
$existingfailed array_column(array_filter($resources, function ($resource) {
        return 
$resource["integrity_fail"] == 1;
    }), 
"ref");

    
$return_failed = [];
    foreach (
array_chunk($resources1000) as $checkresources) {
        
db_begin_transaction("checkresources");

        
// Resources with no file_size have no file so can't be checked.
        
$resources_with_no_file = array();
        
$resource_with_file = array();
        foreach(
$checkresources as $check_ref) {
            if (!
is_null($check_ref["file_size"]) && $check_ref["file_size"] > 0) {
                
$resource_with_file[] = $check_ref;
            } elseif ((int) 
$check_ref["no_file"] === && (int) $check_ref["integrity_fail"] !== 1) {
                
$resources_with_no_file[] = $check_ref;
            }
        }
        
$checkresources $resource_with_file;
        
$resources_with_no_file array_column($resources_with_no_file'ref');

        
$had_uploads_file_found = array();
        
$had_uploads_no_file_found = array();
        if (
count($resources_with_no_file) > 0) {
            
// Extra check of resources with no file to make sure they never had one at any point.
            
$had_uploads ps_array("
                            SELECT DISTINCT resource value 
                            FROM resource_log
                            WHERE resource IN (" 
ps_param_insert(count($resources_with_no_file)) . ")
                            AND type = ?"
,
                            
array_merge(ps_param_fill($resources_with_no_file'i'), ['s'LOG_CODE_UPLOADED])
                        );

            if (
count($had_uploads) > 0) {
                
// At this point a resource may have no file_size set but still have had an uploaded file
                
$check_no_upload_resources validate_resource_files($had_uploads, array("is_readable" => true));
                foreach (
$check_no_upload_resources as $ref => $result) {
                    if (
$result) {
                        
$had_uploads_file_found[] = $ref;
                    } else {
                        
$had_uploads_no_file_found[] = $ref;
                    }
                }

                if (
count($had_uploads_file_found) > 0) {
                    
// There is actually a file. file_size was 0 or null and shouldn't have been.
                    // Update file_size but don't mark this resource as invalid so it'll be checked on the next pass.
                    
foreach ($had_uploads_file_found as $update_ref_file_size) {
                        
$update_ref_file_size get_resource_data($update_ref_file_size);
                        
$resource_path get_resource_path($update_ref_file_size['ref'], true""false$update_ref_file_size['file_extension']);
                        
$filesize filesize_unlimited($resource_path);
                        
$filesize_sql "UPDATE resource SET file_size = ? WHERE ref = ?;";
                        
ps_query($filesize_sql, array('i'$filesize'i'$update_ref_file_size['ref']));
                    }
                }

                if (
count($had_uploads_no_file_found) > 0) {
                    
// Checking for the file confirmed it wasn't there so mark as integrity failed.
                    
$failed_sql "UPDATE resource SET integrity_fail = 1 WHERE ref IN (" ps_param_insert(count($had_uploads_no_file_found)) . ")";
                    
$failed_params ps_param_fill($had_uploads_no_file_found"i");
                    
ps_query($failed_sql$failed_params);
                }
            }

            
$no_uploads array_diff($resources_with_no_file$had_uploads);

            if (
count($no_uploads) > 0) {
                
// No evidence of a file ever being uploaded. Mark these as having no_file
                
ps_query("
                    UPDATE resource 
                    SET no_file = 1
                    WHERE ref IN (" 
ps_param_insert(count($no_uploads)) . ")",
                    
ps_param_fill($no_uploads"i")
                );
            }
        }

        if (
count($checkresources) === 0) {
            
db_end_transaction("checkresources");
            return array();
        }

        
$checks = [];
        
$checks["is_readable"] = true// Always check for file presence as checksums may not have been generated yet
        
if (!$presenceonly && $GLOBALS["file_checksums"]) {
            
$checks["get_checksum"]  = "%RESOURCE%file_checksum";
        }
        
$results validate_resource_files($checkresources$checks);

        
$failed = [];
        
$succeeded = [];
        foreach (
$results as $ref => $result) {
            if (
$result === false) {
                
$failed[] = $ref;
            } else {
                
$succeeded[] = $ref;
            }
        }

        if (
count($failed) > 0) {
            if (
count($failed) > 0) {
                
$failed_sql "UPDATE resource SET integrity_fail = 1 WHERE ref IN (" ps_param_insert(count($failed)) . ")";
                
$failed_params ps_param_fill($failed"i");
                
ps_query($failed_sql$failed_params);
            }
        }
        if (
count($succeeded) > 0) {
            
$success_sql "UPDATE resource SET integrity_fail = 0,no_file=0, last_verified=NOW() WHERE ref IN (" ps_param_insert(count($succeeded)) . ")";
            
$success_params ps_param_fill($succeeded"i");
            
ps_query($success_sql$success_params);
        }
        
// Add any failures to the array to return
        
$return_failed array_merge($return_failed$failed$had_uploads_no_file_found);
        
db_end_transaction("checkresources");

        
// Log any newly failed resources
        
db_begin_transaction("logfailedresources");
        
$arr_newfails array_diff($failed$existingfailed);
        foreach (
$arr_newfails as $newfail) {
            
resource_log($newfailLOG_CODE_SYSTEM0"Failed file integrity check"01);
        }
        
db_end_transaction("logfailedresources");

         
// Log any recovered resources
         
db_begin_transaction("logrecoveredresources");
         
$arr_recovered array_intersect($succeeded$existingfailed);
        foreach (
$arr_recovered as $recovered) {
            
resource_log($recoveredLOG_CODE_SYSTEM0"Passed file integrity check"10);
        }
         
db_end_transaction("logrecoveredresources");
    }
    return 
$return_failed;
}

This article was last updated 17th November 2025 20:35 Europe/London time based on the source file dated 13th November 2025 15:20 Europe/London time.