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

execute_api_call()

Description

Execute the specified API function.

Parameters

ColumnTypeDefaultDescription
$query string The query string passed to the API
$pretty boolean false Should the JSON encoded result be 'pretty' i.e. formatted for reading?

Return

bool|string

Location

include/api_functions.php lines 76 to 186

Definition

 
function execute_api_call($query,$pretty=false)
    {
    
$params=[];parse_str($query,$params);
    if (!
array_key_exists("function",$params)) {return false;}
    
$function=$params["function"];
    if (!
function_exists("api_" $function)) {return false;}

    global 
$lang;

    
// Check if this is a whitelisted function for browser use (native mode bypasses $enable_remote_apis=false;)
    
if (
        
defined("API_AUTHMODE_NATIVE")
        && !
in_array($functionAPI_NATIVE_WHITELIST)
    ) {
        
ajax_unauthorized();
    }
    
// Construct an array of the real params, setting default values as necessary
    
$setparams = [];
    
$n 0;
    
$fct = new ReflectionFunction("api_" $function);
    foreach (
$fct->getParameters() as $fparam)
        {
        
$paramkey $n 1;
        
$param_name $fparam->getName();
        
debug("API: Checking for parameter " $param_name " (param" $paramkey ")");
        if (
array_key_exists("param" $paramkey,$params))
            {
            
debug ("API: " $param_name " -   value has been passed : '" $params["param" $paramkey] . "'");
            
$setparams[$n] = $params["param" $paramkey];
            }
        elseif(
array_key_exists($param_name$params))
            {
            
debug("API: {$param_name} - value has been passed (by name): '" json_encode($params[$param_name]) . "'");

            
// Check if array;
            
$type $fparam->getType();
            if(
gettype($type) == "object")
                {
                
// type is an object
                
$type $type->getName();
                }
            if(
$fparam->hasType() && gettype($type) == "string" && $type == "array" && !is_array($params[$param_name]))
                {
                
// Decode as must be json encoded if array
                
$GLOBALS["use_error_exception"] = true;
                try
                    {
                    
$decoded json_decode($params[$param_name],JSON_OBJECT_AS_ARRAY);
                    }
                catch (
Exception $e)
                    {
                    
$error str_replace(
                        array(
"%arg""%expected-type""%type"),
                        array(
$param_name"array (json encoded)",$lang['unknown']),
                        
$lang["error-type-mismatch"]);
                    return 
json_encode($error);
                    }
                unset(
$GLOBALS["use_error_exception"]);
                
// Check passed data type after decode
                
if(gettype($decoded) != "array")
                    {
                    
$error str_replace(
                        array(
"%arg""%expected-type""%type"),
                        array(
$param_name"array (json encoded)"$lang['unknown']),
                        
$lang["error-type-mismatch"]);
                    return 
json_encode($error);
                    }
                
$params[$param_name] = $decoded;
                }

            
$setparams[$n] = $params[$param_name];
            }
        elseif (
$fparam->isOptional())
            {
            
// Set default value if nothing passed e.g. from API test tool
            
debug ("API: " $param_name " -  setting default value = '" $fparam->getDefaultValue() . "'");
            
$setparams[$n] = $fparam->getDefaultValue();
            }
        else
            {
             
// Set as empty
            
debug ("API: {$param_name} -  setting empty value");
            
$setparams[$n] = "";
            }
        
$n++;
        }

    
debug("API: calling api_" $function);
    
$result call_user_func_array("api_" $function$setparams);

    if(
$pretty)
        {
            
debug("API: json_encode() using JSON_PRETTY_PRINT");
            
$json_encoded_result json_encode($result,(defined('JSON_PRETTY_PRINT')?JSON_PRETTY_PRINT:0));
        }
    else
        {
            
debug("API: json_encode()");
            
$json_encoded_result json_encode($result);
        }
    if(
json_last_error() !== JSON_ERROR_NONE)
        {
        
debug("API: JSON error: " json_last_error_msg());
        
debug("API: JSON error when \$result = " print_r($resulttrue));

        
$json_encoded_result json_encode($result,JSON_UNESCAPED_UNICODE);
        }

    return 
$json_encoded_result;

    }

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