parent
b5bb74faee
commit
e5e01cdb02
@ -0,0 +1,626 @@
|
||||
<?php
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| HTTP protocol
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to force the use of HTTPS for REST API calls
|
||||
|
|
||||
*/
|
||||
$config['force_https'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Output Format
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The default format of the response
|
||||
|
|
||||
| 'array': Array data structure
|
||||
| 'csv': Comma separated file
|
||||
| 'json': Uses json_encode(). Note: If a GET query string
|
||||
| called 'callback' is passed, then jsonp will be returned
|
||||
| 'html' HTML using the table library in CodeIgniter
|
||||
| 'php': Uses var_export()
|
||||
| 'serialized': Uses serialize()
|
||||
| 'xml': Uses simplexml_load_string()
|
||||
|
|
||||
*/
|
||||
$config['rest_default_format'] = 'json';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Supported Output Formats
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The following setting contains a list of the supported/allowed formats.
|
||||
| You may remove those formats that you don't want to use.
|
||||
| If the default format $config['rest_default_format'] is missing within
|
||||
| $config['rest_supported_formats'], it will be added silently during
|
||||
| REST_Controller initialization.
|
||||
|
|
||||
*/
|
||||
$config['rest_supported_formats'] = [
|
||||
'json',
|
||||
'array',
|
||||
'csv',
|
||||
'html',
|
||||
'jsonp',
|
||||
'php',
|
||||
'serialized',
|
||||
'xml',
|
||||
];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Status Field Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The field name for the status inside the response
|
||||
|
|
||||
*/
|
||||
$config['rest_status_field_name'] = 'status';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Message Field Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The field name for the message inside the response
|
||||
|
|
||||
*/
|
||||
$config['rest_message_field_name'] = 'error';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Enable Emulate Request
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Should we enable emulation of the request (e.g. used in Mootools request)
|
||||
|
|
||||
*/
|
||||
$config['enable_emulate_request'] = true;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Realm
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Name of the password protected REST API displayed on login dialogs
|
||||
|
|
||||
| e.g: My Secret REST API
|
||||
|
|
||||
*/
|
||||
$config['rest_realm'] = 'REST API';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Login
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to specify the REST API requires to be logged in
|
||||
|
|
||||
| FALSE No login required
|
||||
| 'basic' Unsecured login
|
||||
| 'digest' More secured login
|
||||
| 'session' Check for a PHP session variable. See 'auth_source' to set the
|
||||
| authorization key
|
||||
|
|
||||
*/
|
||||
$config['rest_auth'] = 'basic';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Login Source
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Is login required and if so, the user store to use
|
||||
|
|
||||
| '' Use config based users or wildcard testing
|
||||
| 'ldap' Use LDAP authentication
|
||||
| 'library' Use a authentication library
|
||||
|
|
||||
| Note: If 'rest_auth' is set to 'session' then change 'auth_source' to the name of the session variable
|
||||
|
|
||||
*/
|
||||
$config['auth_source'] = '';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Allow Authentication and API Keys
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Where you wish to have Basic, Digest or Session login, but also want to use API Keys (for limiting
|
||||
| requests etc), set to TRUE;
|
||||
|
|
||||
*/
|
||||
$config['allow_auth_and_keys'] = true;
|
||||
$config['strict_api_and_auth'] = true; // force the use of both api and auth before a valid api request is made
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Login Class and Function
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If library authentication is used define the class and function name
|
||||
|
|
||||
| The function should accept two parameters: class->function($username, $password)
|
||||
| In other cases override the function _perform_library_auth in your controller
|
||||
|
|
||||
| For digest authentication the library function should return already a stored
|
||||
| md5(username:restrealm:password) for that username
|
||||
|
|
||||
| e.g: md5('admin:REST API:1234') = '1e957ebc35631ab22d5bd6526bd14ea2'
|
||||
|
|
||||
*/
|
||||
$config['auth_library_class'] = '';
|
||||
$config['auth_library_function'] = '';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Override auth types for specific class/method
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set specific authentication types for methods within a class (controller)
|
||||
|
|
||||
| Set as many config entries as needed. Any methods not set will use the default 'rest_auth' config value.
|
||||
|
|
||||
| e.g:
|
||||
|
|
||||
| $config['auth_override_class_method']['deals']['view'] = 'none';
|
||||
| $config['auth_override_class_method']['deals']['insert'] = 'digest';
|
||||
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
|
||||
| $config['auth_override_class_method']['dashboard']['*'] = 'none|digest|basic';
|
||||
|
|
||||
| Here 'deals', 'accounts' and 'dashboard' are controller names, 'view', 'insert' and 'user' are methods within. An asterisk may also be used to specify an authentication method for an entire classes methods. Ex: $config['auth_override_class_method']['dashboard']['*'] = 'basic'; (NOTE: leave off the '_get' or '_post' from the end of the method name)
|
||||
| Acceptable values are; 'none', 'digest' and 'basic'.
|
||||
|
|
||||
*/
|
||||
// $config['auth_override_class_method']['deals']['view'] = 'none';
|
||||
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
|
||||
// $config['auth_override_class_method']['accounts']['user'] = 'basic';
|
||||
// $config['auth_override_class_method']['dashboard']['*'] = 'basic';
|
||||
|
||||
// ---Uncomment list line for the wildard unit test
|
||||
// $config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Override auth types for specific 'class/method/HTTP method'
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| example:
|
||||
|
|
||||
| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none';
|
||||
| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none';
|
||||
| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none';
|
||||
*/
|
||||
|
||||
// ---Uncomment list line for the wildard unit test
|
||||
// $config['auth_override_class_method_http']['wildcard_test_cases']['*']['options'] = 'basic';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Login Usernames
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Array of usernames and passwords for login, if ldap is configured this is ignored
|
||||
|
|
||||
*/
|
||||
$config['rest_valid_logins'] = ['admin' => '1234'];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Global IP White-listing
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Limit connections to your REST server to White-listed IP addresses
|
||||
|
|
||||
| Usage:
|
||||
| 1. Set to TRUE and select an auth option for extreme security (client's IP
|
||||
| address must be in white-list and they must also log in)
|
||||
| 2. Set to TRUE with auth set to FALSE to allow White-listed IPs access with no login
|
||||
| 3. Set to FALSE but set 'auth_override_class_method' to 'white-list' to
|
||||
| restrict certain methods to IPs in your white-list
|
||||
|
|
||||
*/
|
||||
$config['rest_ip_whitelist_enabled'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Handle Exceptions
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Handle exceptions caused by the controller
|
||||
|
|
||||
*/
|
||||
$config['rest_handle_exceptions'] = true;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST IP White-list
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Limit connections to your REST server with a comma separated
|
||||
| list of IP addresses
|
||||
|
|
||||
| e.g: '123.456.789.0, 987.654.32.1'
|
||||
|
|
||||
| 127.0.0.1 and 0.0.0.0 are allowed by default
|
||||
|
|
||||
*/
|
||||
$config['rest_ip_whitelist'] = '';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Global IP Blacklisting
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Prevent connections to the REST server from blacklisted IP addresses
|
||||
|
|
||||
| Usage:
|
||||
| 1. Set to TRUE and add any IP address to 'rest_ip_blacklist'
|
||||
|
|
||||
*/
|
||||
$config['rest_ip_blacklist_enabled'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST IP Blacklist
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Prevent connections from the following IP addresses
|
||||
|
|
||||
| e.g: '123.456.789.0, 987.654.32.1'
|
||||
|
|
||||
*/
|
||||
$config['rest_ip_blacklist'] = '';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Database Group
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Connect to a database group for keys, logging, etc. It will only connect
|
||||
| if you have any of these features enabled
|
||||
|
|
||||
*/
|
||||
$config['rest_database_group'] = 'default';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Keys Table Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The table name in your database that stores API keys
|
||||
|
|
||||
*/
|
||||
$config['rest_keys_table'] = 'keys';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Enable Keys
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When set to TRUE, the REST API will look for a column name called 'key'.
|
||||
| If no key is provided, the request will result in an error. To override the
|
||||
| column name see 'rest_key_column'
|
||||
|
|
||||
| Default table schema:
|
||||
| CREATE TABLE `keys` (
|
||||
| `id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
| `user_id` INT(11) NOT NULL,
|
||||
| `key` VARCHAR(40) NOT NULL,
|
||||
| `level` INT(2) NOT NULL,
|
||||
| `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
|
||||
| `is_private_key` TINYINT(1) NOT NULL DEFAULT '0',
|
||||
| `ip_addresses` TEXT NULL DEFAULT NULL,
|
||||
| `date_created` INT(11) NOT NULL,
|
||||
| PRIMARY KEY (`id`)
|
||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
|
||||
*/
|
||||
$config['rest_enable_keys'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Table Key Column Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If not using the default table schema in 'rest_enable_keys', specify the
|
||||
| column name to match e.g. my_key
|
||||
|
|
||||
*/
|
||||
$config['rest_key_column'] = 'key';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Limits method
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Specify the method used to limit the API calls
|
||||
|
|
||||
| Available methods are :
|
||||
| $config['rest_limits_method'] = 'IP_ADDRESS'; // Put a limit per ip address
|
||||
| $config['rest_limits_method'] = 'API_KEY'; // Put a limit per api key
|
||||
| $config['rest_limits_method'] = 'METHOD_NAME'; // Put a limit on method calls
|
||||
| $config['rest_limits_method'] = 'ROUTED_URL'; // Put a limit on the routed URL
|
||||
|
|
||||
*/
|
||||
$config['rest_limits_method'] = 'ROUTED_URL';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Key Length
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Length of the created keys. Check your default database schema on the
|
||||
| maximum length allowed
|
||||
|
|
||||
| Note: The maximum length is 40
|
||||
|
|
||||
*/
|
||||
$config['rest_key_length'] = 40;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Key Variable
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Custom header to specify the API key
|
||||
|
||||
| Note: Custom headers with the X- prefix are deprecated as of
|
||||
| 2012/06/12. See RFC 6648 specification for more details
|
||||
|
|
||||
*/
|
||||
$config['rest_key_name'] = 'X-API-KEY';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Enable Logging
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When set to TRUE, the REST API will log actions based on the column names 'key', 'date',
|
||||
| 'time' and 'ip_address'. This is a general rule that can be overridden in the
|
||||
| $this->method array for each controller
|
||||
|
|
||||
| Default table schema:
|
||||
| CREATE TABLE `logs` (
|
||||
| `id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
| `uri` VARCHAR(255) NOT NULL,
|
||||
| `method` VARCHAR(6) NOT NULL,
|
||||
| `params` TEXT DEFAULT NULL,
|
||||
| `api_key` VARCHAR(40) NOT NULL,
|
||||
| `ip_address` VARCHAR(45) NOT NULL,
|
||||
| `time` INT(11) NOT NULL,
|
||||
| `rtime` FLOAT DEFAULT NULL,
|
||||
| `authorized` VARCHAR(1) NOT NULL,
|
||||
| `response_code` smallint(3) DEFAULT '0',
|
||||
| PRIMARY KEY (`id`)
|
||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
|
||||
*/
|
||||
$config['rest_enable_logging'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Logs Table Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If not using the default table schema in 'rest_enable_logging', specify the
|
||||
| table name to match e.g. my_logs
|
||||
|
|
||||
*/
|
||||
$config['rest_logs_table'] = 'logs';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Method Access Control
|
||||
|--------------------------------------------------------------------------
|
||||
| When set to TRUE, the REST API will check the access table to see if
|
||||
| the API key can access that controller. 'rest_enable_keys' must be enabled
|
||||
| to use this
|
||||
|
|
||||
| Default table schema:
|
||||
| CREATE TABLE `access` (
|
||||
| `id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||
| `key` VARCHAR(40) NOT NULL DEFAULT '',
|
||||
| `all_access` TINYINT(1) NOT NULL DEFAULT '0',
|
||||
| `controller` VARCHAR(50) NOT NULL DEFAULT '',
|
||||
| `date_created` DATETIME DEFAULT NULL,
|
||||
| `date_modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
| PRIMARY KEY (`id`)
|
||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
|
||||
*/
|
||||
$config['rest_enable_access'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Access Table Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If not using the default table schema in 'rest_enable_access', specify the
|
||||
| table name to match e.g. my_access
|
||||
|
|
||||
*/
|
||||
$config['rest_access_table'] = 'access';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Param Log Format
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When set to TRUE, the REST API log parameters will be stored in the database as JSON
|
||||
| Set to FALSE to log as serialized PHP
|
||||
|
|
||||
*/
|
||||
$config['rest_logs_json_params'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Enable Limits
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| When set to TRUE, the REST API will count the number of uses of each method
|
||||
| by an API key each hour. This is a general rule that can be overridden in the
|
||||
| $this->method array in each controller
|
||||
|
|
||||
| Default table schema:
|
||||
| CREATE TABLE `limits` (
|
||||
| `id` INT(11) NOT NULL AUTO_INCREMENT,
|
||||
| `uri` VARCHAR(255) NOT NULL,
|
||||
| `count` INT(10) NOT NULL,
|
||||
| `hour_started` INT(11) NOT NULL,
|
||||
| `api_key` VARCHAR(40) NOT NULL,
|
||||
| PRIMARY KEY (`id`)
|
||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
|
||||
| To specify the limits within the controller's __construct() method, add per-method
|
||||
| limits with:
|
||||
|
|
||||
| $this->methods['METHOD_NAME']['limit'] = [NUM_REQUESTS_PER_HOUR];
|
||||
|
|
||||
| See application/controllers/api/example.php for examples
|
||||
*/
|
||||
$config['rest_enable_limits'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST API Limits Table Name
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If not using the default table schema in 'rest_enable_limits', specify the
|
||||
| table name to match e.g. my_limits
|
||||
|
|
||||
*/
|
||||
$config['rest_limits_table'] = 'limits';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Ignore HTTP Accept
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to TRUE to ignore the HTTP Accept and speed up each request a little.
|
||||
| Only do this if you are using the $this->rest_format or /format/xml in URLs
|
||||
|
|
||||
*/
|
||||
$config['rest_ignore_http_accept'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST AJAX Only
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to TRUE to allow AJAX requests only. Set to FALSE to accept HTTP requests
|
||||
|
|
||||
| Note: If set to TRUE and the request is not AJAX, a 505 response with the
|
||||
| error message 'Only AJAX requests are accepted.' will be returned.
|
||||
|
|
||||
| Hint: This is good for production environments
|
||||
|
|
||||
*/
|
||||
$config['rest_ajax_only'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| REST Language File
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Language file to load from the language directory
|
||||
|
|
||||
*/
|
||||
$config['rest_language'] = 'english';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CORS Check
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to TRUE to enable Cross-Origin Resource Sharing (CORS). Useful if you
|
||||
| are hosting your API on a different domain from the application that
|
||||
| will access it through a browser
|
||||
|
|
||||
*/
|
||||
$config['check_cors'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CORS Allowable Headers
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If using CORS checks, set the allowable headers here
|
||||
|
|
||||
*/
|
||||
$config['allowed_cors_headers'] = [
|
||||
'Origin',
|
||||
'X-Requested-With',
|
||||
'Content-Type',
|
||||
'Accept',
|
||||
'Access-Control-Request-Method',
|
||||
];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CORS Allowable Methods
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If using CORS checks, you can set the methods you want to be allowed
|
||||
|
|
||||
*/
|
||||
$config['allowed_cors_methods'] = [
|
||||
'GET',
|
||||
'POST',
|
||||
'OPTIONS',
|
||||
'PUT',
|
||||
'PATCH',
|
||||
'DELETE',
|
||||
];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CORS Allow Any Domain
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Set to TRUE to enable Cross-Origin Resource Sharing (CORS) from any
|
||||
| source domain
|
||||
|
|
||||
*/
|
||||
$config['allow_any_cors_domain'] = false;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CORS Allowable Domains
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Used if $config['check_cors'] is set to TRUE and $config['allow_any_cors_domain']
|
||||
| is set to FALSE. Set all the allowable domains within the array
|
||||
|
|
||||
| e.g. $config['allowed_origins'] = ['http://www.example.com', 'https://spa.example.com']
|
||||
|
|
||||
*/
|
||||
$config['allowed_cors_origins'] = [];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CORS Forced Headers
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| If using CORS checks, always include the headers and values specified here
|
||||
| in the OPTIONS client preflight.
|
||||
| Example:
|
||||
| $config['forced_cors_headers'] = [
|
||||
| 'Access-Control-Allow-Credentials' => 'true'
|
||||
| ];
|
||||
|
|
||||
| Added because of how Sencha Ext JS framework requires the header
|
||||
| Access-Control-Allow-Credentials to be set to true to allow the use of
|
||||
| credentials in the REST Proxy.
|
||||
| See documentation here:
|
||||
| http://docs.sencha.com/extjs/6.5.2/classic/Ext.data.proxy.Rest.html#cfg-withCredentials
|
||||
|
|
||||
*/
|
||||
$config['forced_cors_headers'] = [];
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
require APPPATH . 'libraries/RestController.php';
|
||||
|
||||
use App\RestServer\RestController;
|
||||
|
||||
class StoreFrontAPIController extends RestController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Set timezone to Philippines
|
||||
date_default_timezone_set('Asia/Manila');
|
||||
}
|
||||
|
||||
public function index_get() {
|
||||
// Fetch data logic here
|
||||
$data = ['message' => 'GET request received'];
|
||||
$this->response($data, 200);
|
||||
}
|
||||
|
||||
public function itemModel_get($modelno) {
|
||||
|
||||
$this->load->model("Items");
|
||||
$result = $this->Items->getItemInfoByModelNo('GE-4558');
|
||||
|
||||
// disabled during testing
|
||||
//$result = $this->Items->getItemInfoByModelNo($modelno);
|
||||
|
||||
$itemcount = $result->num_rows();
|
||||
|
||||
if($itemcount > 0)
|
||||
{
|
||||
$row = $result->row();
|
||||
|
||||
$item = [
|
||||
'itemcode' => $row->itemcode,
|
||||
'modelno' => $row->modelno,
|
||||
'item_desc' => $row->item_desc,
|
||||
'srp' => $row->srp,
|
||||
'catCode' => $row->catCode,
|
||||
'goldID' => $row->goldID
|
||||
];
|
||||
|
||||
$data = ['status' => 'FOUND',
|
||||
'item_count' => $itemcount,
|
||||
'data' => $item];
|
||||
}
|
||||
else
|
||||
$data = ['status' => 'NOTFOUND',
|
||||
'item_count' => 0,
|
||||
'data' => null];
|
||||
|
||||
$this->response($data, 200);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* English language
|
||||
*/
|
||||
|
||||
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
|
||||
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
|
||||
$lang['text_rest_ip_denied'] = 'IP denied';
|
||||
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
|
||||
$lang['text_rest_unauthorized'] = 'Unauthorized';
|
||||
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
|
||||
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
|
||||
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
|
||||
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
|
||||
$lang['text_rest_ip_address_time_limit'] = 'This IP Address has reached the time limit for this method';
|
||||
$lang['text_rest_unknown_method'] = 'Unknown method';
|
||||
$lang['text_rest_unsupported'] = 'Unsupported protocol';
|
@ -0,0 +1,499 @@
|
||||
<?php
|
||||
|
||||
namespace App\RestServer;
|
||||
|
||||
use Exception;
|
||||
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Format class
|
||||
* Help convert between various formats such as XML, JSON, CSV, etc.
|
||||
*
|
||||
* @author Phil Sturgeon, Chris Kacerguis, @softwarespot
|
||||
* @license http://www.dbad-license.org/
|
||||
*/
|
||||
class Format
|
||||
{
|
||||
/**
|
||||
* Array output format.
|
||||
*/
|
||||
const ARRAY_FORMAT = 'array';
|
||||
|
||||
/**
|
||||
* Comma Separated Value (CSV) output format.
|
||||
*/
|
||||
const CSV_FORMAT = 'csv';
|
||||
|
||||
/**
|
||||
* Json output format.
|
||||
*/
|
||||
const JSON_FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* HTML output format.
|
||||
*/
|
||||
const HTML_FORMAT = 'html';
|
||||
|
||||
/**
|
||||
* PHP output format.
|
||||
*/
|
||||
const PHP_FORMAT = 'php';
|
||||
|
||||
/**
|
||||
* Serialized output format.
|
||||
*/
|
||||
const SERIALIZED_FORMAT = 'serialized';
|
||||
|
||||
/**
|
||||
* XML output format.
|
||||
*/
|
||||
const XML_FORMAT = 'xml';
|
||||
|
||||
/**
|
||||
* Default format of this class.
|
||||
*/
|
||||
const DEFAULT_FORMAT = self::JSON_FORMAT; // Couldn't be DEFAULT, as this is a keyword
|
||||
|
||||
/**
|
||||
* CodeIgniter instance.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
private $_CI;
|
||||
|
||||
/**
|
||||
* Data to parse.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $_data = [];
|
||||
|
||||
/**
|
||||
* Type to convert from.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_from_type = null;
|
||||
|
||||
/**
|
||||
* DO NOT CALL THIS DIRECTLY, USE factory().
|
||||
*
|
||||
* @param null $data
|
||||
* @param null $from_type
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($data = null, $from_type = null)
|
||||
{
|
||||
// Get the CodeIgniter reference
|
||||
$this->_CI = &get_instance();
|
||||
|
||||
// Load the inflector helper
|
||||
$this->_CI->load->helper('inflector');
|
||||
|
||||
// If the provided data is already formatted we should probably convert it to an array
|
||||
if ($from_type !== null) {
|
||||
if (method_exists($this, '_from_'.$from_type)) {
|
||||
$data = call_user_func([$this, '_from_'.$from_type], $data);
|
||||
} else {
|
||||
throw new Exception('Format class does not support conversion from "'.$from_type.'".');
|
||||
}
|
||||
}
|
||||
|
||||
// Set the member variable to the data passed
|
||||
$this->_data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an instance of the format class
|
||||
* e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();.
|
||||
*
|
||||
* @param mixed $data Data to convert/parse
|
||||
* @param string $from_type Type to convert from e.g. json, csv, html
|
||||
*
|
||||
* @return object Instance of the format class
|
||||
*/
|
||||
public static function factory($data, $from_type = null)
|
||||
{
|
||||
// $class = __CLASS__;
|
||||
// return new $class();
|
||||
|
||||
return new static($data, $from_type);
|
||||
}
|
||||
|
||||
// FORMATTING OUTPUT ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Format data as an array.
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
*
|
||||
* @return array Data parsed as an array; otherwise, an empty array
|
||||
*/
|
||||
public function to_array($data = null)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// Cast as an array if not already
|
||||
if (is_array($data) === false) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
$array = [];
|
||||
foreach ((array) $data as $key => $value) {
|
||||
if (is_object($value) === true || is_array($value) === true) {
|
||||
$array[$key] = $this->to_array($value);
|
||||
} else {
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data as XML.
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @param null $structure
|
||||
* @param string $basenode
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function to_xml($data = null, $structure = null, $basenode = 'xml')
|
||||
{
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
if ($structure === null) {
|
||||
$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
|
||||
}
|
||||
|
||||
// Force it to be something useful
|
||||
if (is_array($data) === false && is_object($data) === false) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
|
||||
//change false/true to 0/1
|
||||
if (is_bool($value)) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
|
||||
// no numeric keys in our xml please!
|
||||
if (is_numeric($key)) {
|
||||
// make string key...
|
||||
$key = (singular($basenode) != $basenode) ? singular($basenode) : 'item';
|
||||
}
|
||||
|
||||
// replace anything not alpha numeric
|
||||
$key = preg_replace('/[^a-z_\-0-9]/i', '', $key);
|
||||
|
||||
if ($key === '_attributes' && (is_array($value) || is_object($value))) {
|
||||
$attributes = $value;
|
||||
if (is_object($attributes)) {
|
||||
$attributes = get_object_vars($attributes);
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute_name => $attribute_value) {
|
||||
$structure->addAttribute($attribute_name, $attribute_value);
|
||||
}
|
||||
}
|
||||
// if there is another array found recursively call this function
|
||||
elseif (is_array($value) || is_object($value)) {
|
||||
$node = $structure->addChild($key);
|
||||
|
||||
// recursive call.
|
||||
$this->to_xml($value, $node, $key);
|
||||
} else {
|
||||
// add single node.
|
||||
$value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
|
||||
|
||||
$structure->addChild($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $structure->asXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data as HTML.
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function to_html($data = null)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// Cast as an array if not already
|
||||
if (is_array($data) === false) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
// Check if it's a multi-dimensional array
|
||||
if (isset($data[0]) && count($data) !== count($data, COUNT_RECURSIVE)) {
|
||||
// Multi-dimensional array
|
||||
$headings = array_keys($data[0]);
|
||||
} else {
|
||||
// Single array
|
||||
$headings = array_keys($data);
|
||||
$data = [$data];
|
||||
}
|
||||
|
||||
// Load the table library
|
||||
$this->_CI->load->library('table');
|
||||
|
||||
$this->_CI->table->set_heading($headings);
|
||||
|
||||
foreach ($data as $row) {
|
||||
// Suppressing the "array to string conversion" notice
|
||||
// Keep the "evil" @ here
|
||||
$row = @array_map('strval', $row);
|
||||
|
||||
$this->_CI->table->add_row($row);
|
||||
}
|
||||
|
||||
return $this->_CI->table->generate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @link http://www.metashock.de/2014/02/create-csv-file-in-memory-php/
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
* @param string $delimiter The optional delimiter parameter sets the field
|
||||
* delimiter (one character only). NULL will use the default value (,)
|
||||
* @param string $enclosure The optional enclosure parameter sets the field
|
||||
* enclosure (one character only). NULL will use the default value (")
|
||||
*
|
||||
* @return string A csv string
|
||||
*/
|
||||
public function to_csv($data = null, $delimiter = ',', $enclosure = '"')
|
||||
{
|
||||
// Use a threshold of 1 MB (1024 * 1024)
|
||||
$handle = fopen('php://temp/maxmemory:1048576', 'w');
|
||||
if ($handle === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// If NULL, then set as the default delimiter
|
||||
if ($delimiter === null) {
|
||||
$delimiter = ',';
|
||||
}
|
||||
|
||||
// If NULL, then set as the default enclosure
|
||||
if ($enclosure === null) {
|
||||
$enclosure = '"';
|
||||
}
|
||||
|
||||
// Cast as an array if not already
|
||||
if (is_array($data) === false) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
// Check if it's a multi-dimensional array
|
||||
if (isset($data[0]) && count($data) !== count($data, COUNT_RECURSIVE)) {
|
||||
// Multi-dimensional array
|
||||
$headings = array_keys($data[0]);
|
||||
} else {
|
||||
// Single array
|
||||
$headings = array_keys($data);
|
||||
$data = [$data];
|
||||
}
|
||||
|
||||
// Apply the headings
|
||||
fputcsv($handle, $headings, $delimiter, $enclosure);
|
||||
|
||||
foreach ($data as $record) {
|
||||
// If the record is not an array, then break. This is because the 2nd param of
|
||||
// fputcsv() should be an array
|
||||
if (is_array($record) === false) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Suppressing the "array to string conversion" notice.
|
||||
// Keep the "evil" @ here.
|
||||
$record = @array_map('strval', $record);
|
||||
|
||||
// Returns the length of the string written or FALSE
|
||||
fputcsv($handle, $record, $delimiter, $enclosure);
|
||||
}
|
||||
|
||||
// Reset the file pointer
|
||||
rewind($handle);
|
||||
|
||||
// Retrieve the csv contents
|
||||
$csv = stream_get_contents($handle);
|
||||
|
||||
// Close the handle
|
||||
fclose($handle);
|
||||
|
||||
// Convert UTF-8 encoding to UTF-16LE which is supported by MS Excel
|
||||
$csv = mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');
|
||||
|
||||
return $csv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode data as json.
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
*
|
||||
* @return string Json representation of a value
|
||||
*/
|
||||
public function to_json($data = null)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
// Get the callback parameter (if set)
|
||||
$callback = $this->_CI->input->get('callback');
|
||||
|
||||
if (empty($callback) === true) {
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
// We only honour a jsonp callback which are valid javascript identifiers
|
||||
elseif (preg_match('/^[a-z_\$][a-z0-9\$_]*(\.[a-z_\$][a-z0-9\$_]*)*$/i', $callback)) {
|
||||
// Return the data as encoded json with a callback
|
||||
return $callback.'('.json_encode($data, JSON_UNESCAPED_UNICODE).');';
|
||||
}
|
||||
|
||||
// An invalid jsonp callback function provided.
|
||||
// Though I don't believe this should be hardcoded here
|
||||
$data['warning'] = 'INVALID JSONP CALLBACK: '.$callback;
|
||||
|
||||
return json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode data as a serialized array.
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
*
|
||||
* @return string Serialized data
|
||||
*/
|
||||
public function to_serialized($data = null)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
return serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format data using a PHP structure.
|
||||
*
|
||||
* @param mixed|null $data Optional data to pass, so as to override the data passed
|
||||
* to the constructor
|
||||
*
|
||||
* @return mixed String representation of a variable
|
||||
*/
|
||||
public function to_php($data = null)
|
||||
{
|
||||
// If no data is passed as a parameter, then use the data passed
|
||||
// via the constructor
|
||||
if ($data === null && func_num_args() === 0) {
|
||||
$data = $this->_data;
|
||||
}
|
||||
|
||||
return var_export($data, true);
|
||||
}
|
||||
|
||||
// INTERNAL FUNCTIONS
|
||||
|
||||
/**
|
||||
* @param string $data XML string
|
||||
*
|
||||
* @return array XML element object; otherwise, empty array
|
||||
*/
|
||||
protected function _from_xml($data)
|
||||
{
|
||||
return $data ? (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data CSV string
|
||||
* @param string $delimiter The optional delimiter parameter sets the field
|
||||
* delimiter (one character only). NULL will use the default value (,)
|
||||
* @param string $enclosure The optional enclosure parameter sets the field
|
||||
* enclosure (one character only). NULL will use the default value (")
|
||||
*
|
||||
* @return array A multi-dimensional array with the outer array being the number of rows
|
||||
* and the inner arrays the individual fields
|
||||
*/
|
||||
protected function _from_csv($data, $delimiter = ',', $enclosure = '"')
|
||||
{
|
||||
// If NULL, then set as the default delimiter
|
||||
if ($delimiter === null) {
|
||||
$delimiter = ',';
|
||||
}
|
||||
|
||||
// If NULL, then set as the default enclosure
|
||||
if ($enclosure === null) {
|
||||
$enclosure = '"';
|
||||
}
|
||||
|
||||
return str_getcsv($data, $delimiter, $enclosure);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data Encoded json string
|
||||
*
|
||||
* @return mixed Decoded json string with leading and trailing whitespace removed
|
||||
*/
|
||||
protected function _from_json($data)
|
||||
{
|
||||
return json_decode(trim($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data Data to unserialize
|
||||
*
|
||||
* @return mixed Unserialized data
|
||||
*/
|
||||
protected function _from_serialize($data)
|
||||
{
|
||||
return unserialize(trim($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $data Data to trim leading and trailing whitespace
|
||||
*
|
||||
* @return string Data with leading and trailing whitespace removed
|
||||
*/
|
||||
protected function _from_php($data)
|
||||
{
|
||||
return trim($data);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue