PHP Class REST_Controller

A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.
Author: Phil Sturgeon, Chris Kacerguis
Inheritance: extends MX_Controller
Afficher le fichier Open project: chriskacerguis/codeigniter-restserver Class Usage Examples

Protected Properties

Свойство Type Description
$_allow boolean If the request is allowed based on the API key provided
$_apiuser object Information about the current API user
$_args array The arguments from GET, POST, PUT, DELETE, PATCH, HEAD and OPTIONS request methods combined
$_delete_args array The arguments for the DELETE request method
$_enable_xss boolean Enable XSS flag Determines whether the XSS filter is always active when GET, OPTIONS, HEAD, POST, PUT, DELETE and PATCH data is encountered Set automatically based on config setting
$_end_rtime string The end of the response time from the server
$_get_args array The arguments for the GET request method
$_head_args array The arguments for the HEAD request method
$_insert_id string The insert_id of the log entry (if we have one)
$_options_args array The arguments for the OPTIONS request method
$_patch_args array The arguments for the PATCH request method
$_post_args array The arguments for the POST request method
$_put_args array The arguments for the PUT request method
$_query_args array The arguments for the query parameters
$_start_rtime string The start of the response time from the server
$_supported_formats array List all supported methods, the first will be the default format
$_user_ldap_dn string The LDAP Distinguished Name of the User post authentication
$allowed_http_methods array List of allowed HTTP methods
$check_cors boolean Whether or not to perform a CORS check and apply CORS headers to the request
$http_status_codes array HTTP status codes and their respective description Note: Only the widely used HTTP status codes are used
$methods array Defines the list of method properties such as limit, log and level
$request object Contains details about the request Fields: body, format, method, ssl Note: This is a dynamic object (stdClass)
$response object Contains details about the response Fields: format, lang Note: This is a dynamic object (stdClass)
$rest object Contains details about the REST API Fields: db, ignore_limits, key, level, user_id Note: This is a dynamic object (stdClass)
$rest_format string | null This defines the rest format Must be overridden it in a controller so that it is set

Méthodes publiques

Méthode Description
__construct ( string $config = 'rest' ) Constructor for the REST API
__destruct ( ) : void Deconstructor
_remap ( string $object_called, array $arguments = [] ) Requests are not made to methods directly, the request will be for an "object". This simply maps the object and method to the correct Controller method
delete ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a DELETE request
get ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a GET request
head ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a HEAD request
options ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a OPTIONS request
patch ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a PATCH request
post ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a POST request
put ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from a PUT request
query ( null $key = NULL, null $xss_clean = NULL ) : array | string | null Retrieve a value from the query parameters
response ( array | null $data = NULL, integer | null $http_code = NULL, boolean $continue = FALSE ) Takes mixed data and optionally a status code, then creates the response
set_response ( array | null $data = NULL, integer | null $http_code = NULL ) Takes mixed data and optionally a status code, then creates the response within the buffers of the Output class. The response is sent to the client lately by the framework, after the current controller's method termination.
validation_errors ( ) : array Retrieve the validation errors

Méthodes protégées

Méthode Description
_auth_override_check ( ) : boolean Check if there is a specific auth type set for the current class/method/HTTP-method being called
_check_access ( ) : boolean Check to see if the API key has access to the controller and methods
_check_blacklist_auth ( ) : void Checks if the client's ip is in the 'rest_ip_blacklist' config and generates a 401 response
_check_cors ( ) : void Checks allowed domains, and adds appropriate headers for HTTP access control (CORS)
_check_limit ( string $controller_method ) : boolean Check if the requests to a controller method exceed a limit
_check_login ( string $username = NULL, boolean | string $password = FALSE ) : boolean Check if the user is logged in
_check_php_session ( ) : void Check to see if the user is logged in with a PHP session key
_check_whitelist_auth ( ) : void Check if the client's ip is in the 'rest_ip_whitelist' config and generates a 401 response
_detect_api_key ( ) : boolean See if the user has provided an API key
_detect_input_format ( ) : string | null Get the input format e.g. json or xml
_detect_lang ( ) : string | null Preferred return language
_detect_method ( ) : string | null Get the HTTP request string e.g. get or post
_detect_output_format ( ) : mixed | null | string Detect which format should be used to output the data
_force_login ( string $nonce = '' ) : void Force logging in by setting the WWW-Authenticate header
_get_default_output_format ( ) : string Gets the default format from the configuration. Fallbacks to 'json' if the corresponding configuration option $config['rest_default_format'] is missing or is empty
_log_access_time ( ) : boolean Updates the log table with the total access time
_log_request ( boolean $authorized = FALSE ) : boolean Add the request to the log table
_log_response_code ( $http_code ) : boolean Updates the log table with HTTP response code
_parse_delete ( ) : void Parse the DELETE request arguments
_parse_get ( ) : void Parse the GET request arguments
_parse_head ( ) : void Parse the HEAD request arguments
_parse_options ( ) : void Parse the OPTIONS request arguments
_parse_patch ( ) : void Parse the PATCH request arguments
_parse_post ( ) : void Parse the POST request arguments
_parse_put ( ) : void Parse the PUT request arguments
_parse_query ( ) : void Parse the query parameters
_perform_ldap_auth ( string $username = '', string $password = NULL ) : boolean Perform LDAP Authentication
_perform_library_auth ( string $username = '', string $password = NULL ) : boolean Perform Library Authentication - Override this function to change the way the library is called
_prepare_basic_auth ( ) : void Prepares for basic authentication
_prepare_digest_auth ( ) : void Prepares for digest authentication
_xss_clean ( string $value, boolean $xss_clean ) : string Sanitizes data so that Cross Site Scripting Hacks can be prevented
early_checks ( ) : void Extend this function to apply additional checking early on in the process
preflight_checks ( ) Checks to see if we have everything we need to run this library.

Method Details

__construct() public méthode

Constructor for the REST API
public __construct ( string $config = 'rest' )
$config string Configuration filename minus the file extension e.g: my_rest.php is passed as 'my_rest'

__destruct() public méthode

Deconstructor
Author: Chris Kacerguis
public __destruct ( ) : void
Résultat void

_auth_override_check() protected méthode

Check if there is a specific auth type set for the current class/method/HTTP-method being called
protected _auth_override_check ( ) : boolean
Résultat boolean

_check_access() protected méthode

Check to see if the API key has access to the controller and methods
protected _check_access ( ) : boolean
Résultat boolean TRUE the API key has access; otherwise, FALSE

_check_blacklist_auth() protected méthode

Checks if the client's ip is in the 'rest_ip_blacklist' config and generates a 401 response
protected _check_blacklist_auth ( ) : void
Résultat void

_check_cors() protected méthode

Checks allowed domains, and adds appropriate headers for HTTP access control (CORS)
protected _check_cors ( ) : void
Résultat void

_check_limit() protected méthode

Check if the requests to a controller method exceed a limit
protected _check_limit ( string $controller_method ) : boolean
$controller_method string The method being called
Résultat boolean TRUE the call limit is below the threshold; otherwise, FALSE

_check_login() protected méthode

Check if the user is logged in
protected _check_login ( string $username = NULL, boolean | string $password = FALSE ) : boolean
$username string The user's name
$password boolean | string The user's password
Résultat boolean

_check_php_session() protected méthode

Check to see if the user is logged in with a PHP session key
protected _check_php_session ( ) : void
Résultat void

_check_whitelist_auth() protected méthode

Check if the client's ip is in the 'rest_ip_whitelist' config and generates a 401 response
protected _check_whitelist_auth ( ) : void
Résultat void

_detect_api_key() protected méthode

See if the user has provided an API key
protected _detect_api_key ( ) : boolean
Résultat boolean

_detect_input_format() protected méthode

Get the input format e.g. json or xml
protected _detect_input_format ( ) : string | null
Résultat string | null Supported input format; otherwise, NULL

_detect_lang() protected méthode

Preferred return language
protected _detect_lang ( ) : string | null
Résultat string | null The language code

_detect_method() protected méthode

Get the HTTP request string e.g. get or post
protected _detect_method ( ) : string | null
Résultat string | null Supported request method as a lowercase string; otherwise, NULL if not supported

_detect_output_format() protected méthode

Detect which format should be used to output the data
protected _detect_output_format ( ) : mixed | null | string
Résultat mixed | null | string Output format

_force_login() protected méthode

Force logging in by setting the WWW-Authenticate header
protected _force_login ( string $nonce = '' ) : void
$nonce string A server-specified data string which should be uniquely generated each time
Résultat void

_get_default_output_format() protected méthode

Gets the default format from the configuration. Fallbacks to 'json' if the corresponding configuration option $config['rest_default_format'] is missing or is empty
protected _get_default_output_format ( ) : string
Résultat string The default supported input format

_log_access_time() protected méthode

Updates the log table with the total access time
Author: Chris Kacerguis
protected _log_access_time ( ) : boolean
Résultat boolean TRUE log table updated; otherwise, FALSE

_log_request() protected méthode

Add the request to the log table
protected _log_request ( boolean $authorized = FALSE ) : boolean
$authorized boolean TRUE the user is authorized; otherwise, FALSE
Résultat boolean TRUE the data was inserted; otherwise, FALSE

_log_response_code() protected méthode

Updates the log table with HTTP response code
Author: Justin Chen
protected _log_response_code ( $http_code ) : boolean
$http_code int HTTP status code
Résultat boolean TRUE log table updated; otherwise, FALSE

_parse_delete() protected méthode

Parse the DELETE request arguments
protected _parse_delete ( ) : void
Résultat void

_parse_get() protected méthode

Parse the GET request arguments
protected _parse_get ( ) : void
Résultat void

_parse_head() protected méthode

Parse the HEAD request arguments
protected _parse_head ( ) : void
Résultat void

_parse_options() protected méthode

Parse the OPTIONS request arguments
protected _parse_options ( ) : void
Résultat void

_parse_patch() protected méthode

Parse the PATCH request arguments
protected _parse_patch ( ) : void
Résultat void

_parse_post() protected méthode

Parse the POST request arguments
protected _parse_post ( ) : void
Résultat void

_parse_put() protected méthode

Parse the PUT request arguments
protected _parse_put ( ) : void
Résultat void

_parse_query() protected méthode

Parse the query parameters
protected _parse_query ( ) : void
Résultat void

_perform_ldap_auth() protected méthode

Perform LDAP Authentication
protected _perform_ldap_auth ( string $username = '', string $password = NULL ) : boolean
$username string The username to validate
$password string The password to validate
Résultat boolean

_perform_library_auth() protected méthode

Perform Library Authentication - Override this function to change the way the library is called
protected _perform_library_auth ( string $username = '', string $password = NULL ) : boolean
$username string The username to validate
$password string The password to validate
Résultat boolean

_prepare_basic_auth() protected méthode

Prepares for basic authentication
protected _prepare_basic_auth ( ) : void
Résultat void

_prepare_digest_auth() protected méthode

Prepares for digest authentication
protected _prepare_digest_auth ( ) : void
Résultat void

_remap() public méthode

Requests are not made to methods directly, the request will be for an "object". This simply maps the object and method to the correct Controller method
public _remap ( string $object_called, array $arguments = [] )
$object_called string
$arguments array The arguments passed to the controller method

_xss_clean() protected méthode

Sanitizes data so that Cross Site Scripting Hacks can be prevented
protected _xss_clean ( string $value, boolean $xss_clean ) : string
$value string Input data
$xss_clean boolean Whether to apply XSS filtering
Résultat string

delete() public méthode

Retrieve a value from a DELETE request
public delete ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the DELETE request If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the DELETE request; otherwise, NULL

early_checks() protected méthode

Extend this function to apply additional checking early on in the process
protected early_checks ( ) : void
Résultat void

get() public méthode

Retrieve a value from a GET request
public get ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the GET request If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the GET request; otherwise, NULL

head() public méthode

Retrieve a value from a HEAD request
public head ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the HEAD request If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the HEAD request; otherwise, NULL

options() public méthode

Retrieve a value from a OPTIONS request
public options ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the OPTIONS request. If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the OPTIONS request; otherwise, NULL

patch() public méthode

Retrieve a value from a PATCH request
public patch ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the PATCH request If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the PATCH request; otherwise, NULL

post() public méthode

Retrieve a value from a POST request
public post ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the POST request If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the POST request; otherwise, NULL

preflight_checks() protected méthode

Checks to see if we have everything we need to run this library.
protected preflight_checks ( )

put() public méthode

Retrieve a value from a PUT request
public put ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the PUT request If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the PUT request; otherwise, NULL

query() public méthode

Retrieve a value from the query parameters
public query ( null $key = NULL, null $xss_clean = NULL ) : array | string | null
$key null Key to retrieve from the query parameters If NULL an array of arguments is returned
$xss_clean null Whether to apply XSS filtering
Résultat array | string | null Value from the query parameters; otherwise, NULL

response() public méthode

Takes mixed data and optionally a status code, then creates the response
public response ( array | null $data = NULL, integer | null $http_code = NULL, boolean $continue = FALSE )
$data array | null Data to output to the user
$http_code integer | null HTTP status code
$continue boolean TRUE to flush the response to the client and continue running the script; otherwise, exit

set_response() public méthode

All the hooks after the controller's method termination are executable
public set_response ( array | null $data = NULL, integer | null $http_code = NULL )
$data array | null Data to output to the user
$http_code integer | null HTTP status code

validation_errors() public méthode

Retrieve the validation errors
public validation_errors ( ) : array
Résultat array

Property Details

$_allow protected_oe property

If the request is allowed based on the API key provided
protected bool $_allow
Résultat boolean

$_apiuser protected_oe property

Information about the current API user
protected object $_apiuser
Résultat object

$_args protected_oe property

The arguments from GET, POST, PUT, DELETE, PATCH, HEAD and OPTIONS request methods combined
protected array $_args
Résultat array

$_delete_args protected_oe property

The arguments for the DELETE request method
protected array $_delete_args
Résultat array

$_enable_xss protected_oe property

Enable XSS flag Determines whether the XSS filter is always active when GET, OPTIONS, HEAD, POST, PUT, DELETE and PATCH data is encountered Set automatically based on config setting
protected bool $_enable_xss
Résultat boolean

$_end_rtime protected_oe property

The end of the response time from the server
protected string $_end_rtime
Résultat string

$_get_args protected_oe property

The arguments for the GET request method
protected array $_get_args
Résultat array

$_head_args protected_oe property

The arguments for the HEAD request method
protected array $_head_args
Résultat array

$_insert_id protected_oe property

The insert_id of the log entry (if we have one)
protected string $_insert_id
Résultat string

$_options_args protected_oe property

The arguments for the OPTIONS request method
protected array $_options_args
Résultat array

$_patch_args protected_oe property

The arguments for the PATCH request method
protected array $_patch_args
Résultat array

$_post_args protected_oe property

The arguments for the POST request method
protected array $_post_args
Résultat array

$_put_args protected_oe property

The arguments for the PUT request method
protected array $_put_args
Résultat array

$_query_args protected_oe property

The arguments for the query parameters
protected array $_query_args
Résultat array

$_start_rtime protected_oe property

The start of the response time from the server
protected string $_start_rtime
Résultat string

$_supported_formats protected_oe property

List all supported methods, the first will be the default format
protected array $_supported_formats
Résultat array

$_user_ldap_dn protected_oe property

The LDAP Distinguished Name of the User post authentication
protected string $_user_ldap_dn
Résultat string

$allowed_http_methods protected_oe property

List of allowed HTTP methods
protected array $allowed_http_methods
Résultat array

$check_cors protected_oe property

Whether or not to perform a CORS check and apply CORS headers to the request
protected bool $check_cors
Résultat boolean

$http_status_codes protected_oe property

HTTP status codes and their respective description Note: Only the widely used HTTP status codes are used
protected array $http_status_codes
Résultat array

$methods protected_oe property

Defines the list of method properties such as limit, log and level
protected array $methods
Résultat array

$request protected_oe property

Contains details about the request Fields: body, format, method, ssl Note: This is a dynamic object (stdClass)
protected object $request
Résultat object

$response protected_oe property

Contains details about the response Fields: format, lang Note: This is a dynamic object (stdClass)
protected object $response
Résultat object

$rest protected_oe property

Contains details about the REST API Fields: db, ignore_limits, key, level, user_id Note: This is a dynamic object (stdClass)
protected object $rest
Résultat object

$rest_format protected_oe property

This defines the rest format Must be overridden it in a controller so that it is set
protected string|NULL $rest_format
Résultat string | null