Coding standards
Security in ResourceSpace
Developer reference
Database
Mcp browse functions
Mcp report functions

mcp_check_search_source()

Description

Validate the source of special browsing searches before asking the native search engine for matches.

Parameters

ColumnTypeDefaultDescription
$query: string
or checksums
$query

Location

include/mcp_browse_functions.php lines 84 to 131

Definition

 
function mcp_check_search_source(string $query): void
{
    
// Restrict native searches which can consult unrestricted reports, checksums or private memberships.
    
if (preg_match('/!(?:duplicates|report|unused|contributions|geo|colour|rgb)/i', $query)) {
        throw new 
DomainException('Use a scoped MCP report or search by keywords, field, collection or resource reference.');
    }
    
$field_ids = [];
    if (
preg_match_all('/@@!*([0-9]+)/', $query, $matches)) {
        foreach (
array_unique($matches[1]) as $id) {
            
$node = [];
            if (!
get_node($id, $node) || !$node['active']) {
                throw new 
DomainException('Search field or option unavailable.');
            }
            
$field_ids[] = (int) $node['resource_type_field'];
        }
    }
    if (
preg_match('/!empty(?![0-9]+(?:\b|$))/i', $query)) {
        throw new 
DomainException('Use !empty followed by a visible numeric field ID.');
    }
    if (
preg_match_all('/!(?:empty|hasdata)([0-9]+)/i', $query, $matches)) {
        
$field_ids = array_merge($field_ids, $matches[1]);
    }
    foreach (
array_unique($field_ids) as $id) {
        
$field = get_resource_type_field($id);
        if (
            !
$field || !$field['active'] || !$field['display_field'] || $field['hide_when_restricted']
            || !
metadata_field_view_access($id)
        ) {
            throw new 
DomainException('Search field or option unavailable.');
        }
    }
    if (
preg_match('/^!related(?:pushed)?(\d+)/i', trim($query), $match)) {
        
mcp_visible_resource((int) $match[1]);
        if (!
$GLOBALS['enable_related_resources']) {
            throw new 
DomainException('Relationships unavailable.');
        }
    }
    if (
str_starts_with(ltrim($query), '!collection')) {
        if (
            
checkperm('b') || !preg_match('/^!collection([1-9]\d*)(?:$|[ ,])/', trim($query), $match)
            || !
collection_readable((int) $match[1])
        ) {
            throw new 
DomainException('Collection unavailable.');
        }
        
// The ordinary search page may rebuild smart collections as a side effect.
        
$GLOBALS['allow_smart_collections'] = false;
    }
}

This article was last updated 3rd December 2026 16:35 Europe/London time based on the source file dated 3rd December 2026 16:30 Europe/London time.