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 9642 to 9708

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($resources,1000) as $checkresources) {
        
$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;
            }
        }

        
db_begin_transaction("checkresources");
        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);
        
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"0,1);
        }
        
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"1,0);
         }
         
db_end_transaction("logrecoveredresources");
    }
    return 
$return_failed;
}

This article was last updated 11th December 2024 20:35 Europe/London time based on the source file dated 11th December 2024 15:55 Europe/London time.