rest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. /*
  4. |--------------------------------------------------------------------------
  5. | HTTP protocol
  6. |--------------------------------------------------------------------------
  7. |
  8. | Set to force the use of HTTPS for REST API calls
  9. |
  10. */
  11. $config['force_https'] = FALSE;
  12. /*
  13. |--------------------------------------------------------------------------
  14. | REST Output Format
  15. |--------------------------------------------------------------------------
  16. |
  17. | The default format of the response
  18. |
  19. | 'array': Array data structure
  20. | 'csv': Comma separated file
  21. | 'json': Uses json_encode(). Note: If a GET query string
  22. | called 'callback' is passed, then jsonp will be returned
  23. | 'html' HTML using the table library in CodeIgniter
  24. | 'php': Uses var_export()
  25. | 'serialized': Uses serialize()
  26. | 'xml': Uses simplexml_load_string()
  27. |
  28. */
  29. $config['rest_default_format'] = 'json';
  30. /*
  31. |--------------------------------------------------------------------------
  32. | REST Supported Output Formats
  33. |--------------------------------------------------------------------------
  34. |
  35. | The following setting contains a list of the supported/allowed formats.
  36. | You may remove those formats that you don't want to use.
  37. | If the default format $config['rest_default_format'] is missing within
  38. | $config['rest_supported_formats'], it will be added silently during
  39. | REST_Controller initialization.
  40. |
  41. */
  42. $config['rest_supported_formats'] = [
  43. 'json',
  44. 'array',
  45. 'csv',
  46. 'html',
  47. 'jsonp',
  48. 'php',
  49. 'serialized',
  50. 'xml',
  51. ];
  52. /*
  53. |--------------------------------------------------------------------------
  54. | REST Status Field Name
  55. |--------------------------------------------------------------------------
  56. |
  57. | The field name for the status inside the response
  58. |
  59. */
  60. $config['rest_status_field_name'] = 'status';
  61. /*
  62. |--------------------------------------------------------------------------
  63. | REST Message Field Name
  64. |--------------------------------------------------------------------------
  65. |
  66. | The field name for the message inside the response
  67. |
  68. */
  69. $config['rest_message_field_name'] = 'error';
  70. /*
  71. |--------------------------------------------------------------------------
  72. | Enable Emulate Request
  73. |--------------------------------------------------------------------------
  74. |
  75. | Should we enable emulation of the request (e.g. used in Mootools request)
  76. |
  77. */
  78. $config['enable_emulate_request'] = TRUE;
  79. /*
  80. |--------------------------------------------------------------------------
  81. | REST Realm
  82. |--------------------------------------------------------------------------
  83. |
  84. | Name of the password protected REST API displayed on login dialogs
  85. |
  86. | e.g: My Secret REST API
  87. |
  88. */
  89. $config['rest_realm'] = 'REST API';
  90. /*
  91. |--------------------------------------------------------------------------
  92. | REST Login
  93. |--------------------------------------------------------------------------
  94. |
  95. | Set to specify the REST API requires to be logged in
  96. |
  97. | FALSE No login required
  98. | 'basic' Unsecured login
  99. | 'digest' More secured login
  100. | 'session' Check for a PHP session variable. See 'auth_source' to set the
  101. | authorization key
  102. |
  103. */
  104. $config['rest_auth'] = FALSE;
  105. /*
  106. |--------------------------------------------------------------------------
  107. | REST Login Source
  108. |--------------------------------------------------------------------------
  109. |
  110. | Is login required and if so, the user store to use
  111. |
  112. | '' Use config based users or wildcard testing
  113. | 'ldap' Use LDAP authentication
  114. | 'library' Use a authentication library
  115. |
  116. | Note: If 'rest_auth' is set to 'session' then change 'auth_source' to the name of the session variable
  117. |
  118. */
  119. $config['auth_source'] = 'ldap';
  120. /*
  121. |--------------------------------------------------------------------------
  122. | Allow Authentication and API Keys
  123. |--------------------------------------------------------------------------
  124. |
  125. | Where you wish to have Basic, Digest or Session login, but also want to use API Keys (for limiting
  126. | requests etc), set to TRUE;
  127. |
  128. */
  129. $config['allow_auth_and_keys'] = TRUE;
  130. $config['strict_api_and_auth'] = TRUE; // force the use of both api and auth before a valid api request is made
  131. /*
  132. |--------------------------------------------------------------------------
  133. | REST Login Class and Function
  134. |--------------------------------------------------------------------------
  135. |
  136. | If library authentication is used define the class and function name
  137. |
  138. | The function should accept two parameters: class->function($username, $password)
  139. | In other cases override the function _perform_library_auth in your controller
  140. |
  141. | For digest authentication the library function should return already a stored
  142. | md5(username:restrealm:password) for that username
  143. |
  144. | e.g: md5('admin:REST API:1234') = '1e957ebc35631ab22d5bd6526bd14ea2'
  145. |
  146. */
  147. $config['auth_library_class'] = '';
  148. $config['auth_library_function'] = '';
  149. /*
  150. |--------------------------------------------------------------------------
  151. | Override auth types for specific class/method
  152. |--------------------------------------------------------------------------
  153. |
  154. | Set specific authentication types for methods within a class (controller)
  155. |
  156. | Set as many config entries as needed. Any methods not set will use the default 'rest_auth' config value.
  157. |
  158. | e.g:
  159. |
  160. | $config['auth_override_class_method']['deals']['view'] = 'none';
  161. | $config['auth_override_class_method']['deals']['insert'] = 'digest';
  162. | $config['auth_override_class_method']['accounts']['user'] = 'basic';
  163. | $config['auth_override_class_method']['dashboard']['*'] = 'none|digest|basic';
  164. |
  165. | 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)
  166. | Acceptable values are; 'none', 'digest' and 'basic'.
  167. |
  168. */
  169. // $config['auth_override_class_method']['deals']['view'] = 'none';
  170. // $config['auth_override_class_method']['deals']['insert'] = 'digest';
  171. // $config['auth_override_class_method']['accounts']['user'] = 'basic';
  172. // $config['auth_override_class_method']['dashboard']['*'] = 'basic';
  173. // ---Uncomment list line for the wildard unit test
  174. // $config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic';
  175. /*
  176. |--------------------------------------------------------------------------
  177. | Override auth types for specific 'class/method/HTTP method'
  178. |--------------------------------------------------------------------------
  179. |
  180. | example:
  181. |
  182. | $config['auth_override_class_method_http']['deals']['view']['get'] = 'none';
  183. | $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none';
  184. | $config['auth_override_class_method_http']['deals']['*']['options'] = 'none';
  185. */
  186. // ---Uncomment list line for the wildard unit test
  187. // $config['auth_override_class_method_http']['wildcard_test_cases']['*']['options'] = 'basic';
  188. /*
  189. |--------------------------------------------------------------------------
  190. | REST Login Usernames
  191. |--------------------------------------------------------------------------
  192. |
  193. | Array of usernames and passwords for login, if ldap is configured this is ignored
  194. |
  195. */
  196. $config['rest_valid_logins'] = ['admin' => '1234'];
  197. /*
  198. |--------------------------------------------------------------------------
  199. | Global IP White-listing
  200. |--------------------------------------------------------------------------
  201. |
  202. | Limit connections to your REST server to White-listed IP addresses
  203. |
  204. | Usage:
  205. | 1. Set to TRUE and select an auth option for extreme security (client's IP
  206. | address must be in white-list and they must also log in)
  207. | 2. Set to TRUE with auth set to FALSE to allow White-listed IPs access with no login
  208. | 3. Set to FALSE but set 'auth_override_class_method' to 'white-list' to
  209. | restrict certain methods to IPs in your white-list
  210. |
  211. */
  212. $config['rest_ip_whitelist_enabled'] = FALSE;
  213. /*
  214. |--------------------------------------------------------------------------
  215. | REST Handle Exceptions
  216. |--------------------------------------------------------------------------
  217. |
  218. | Handle exceptions caused by the controller
  219. |
  220. */
  221. $config['rest_handle_exceptions'] = TRUE;
  222. /*
  223. |--------------------------------------------------------------------------
  224. | REST IP White-list
  225. |--------------------------------------------------------------------------
  226. |
  227. | Limit connections to your REST server with a comma separated
  228. | list of IP addresses
  229. |
  230. | e.g: '123.456.789.0, 987.654.32.1'
  231. |
  232. | 127.0.0.1 and 0.0.0.0 are allowed by default
  233. |
  234. */
  235. $config['rest_ip_whitelist'] = '';
  236. /*
  237. |--------------------------------------------------------------------------
  238. | Global IP Blacklisting
  239. |--------------------------------------------------------------------------
  240. |
  241. | Prevent connections to the REST server from blacklisted IP addresses
  242. |
  243. | Usage:
  244. | 1. Set to TRUE and add any IP address to 'rest_ip_blacklist'
  245. |
  246. */
  247. $config['rest_ip_blacklist_enabled'] = FALSE;
  248. /*
  249. |--------------------------------------------------------------------------
  250. | REST IP Blacklist
  251. |--------------------------------------------------------------------------
  252. |
  253. | Prevent connections from the following IP addresses
  254. |
  255. | e.g: '123.456.789.0, 987.654.32.1'
  256. |
  257. */
  258. $config['rest_ip_blacklist'] = '';
  259. /*
  260. |--------------------------------------------------------------------------
  261. | REST Database Group
  262. |--------------------------------------------------------------------------
  263. |
  264. | Connect to a database group for keys, logging, etc. It will only connect
  265. | if you have any of these features enabled
  266. |
  267. */
  268. $config['rest_database_group'] = 'default';
  269. /*
  270. |--------------------------------------------------------------------------
  271. | REST API Keys Table Name
  272. |--------------------------------------------------------------------------
  273. |
  274. | The table name in your database that stores API keys
  275. |
  276. */
  277. $config['rest_keys_table'] = 'keys';
  278. /*
  279. |--------------------------------------------------------------------------
  280. | REST Enable Keys
  281. |--------------------------------------------------------------------------
  282. |
  283. | When set to TRUE, the REST API will look for a column name called 'key'.
  284. | If no key is provided, the request will result in an error. To override the
  285. | column name see 'rest_key_column'
  286. |
  287. | Default table schema:
  288. | CREATE TABLE `keys` (
  289. | `id` INT(11) NOT NULL AUTO_INCREMENT,
  290. | `user_id` INT(11) NOT NULL,
  291. | `key` VARCHAR(40) NOT NULL,
  292. | `level` INT(2) NOT NULL,
  293. | `ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
  294. | `is_private_key` TINYINT(1) NOT NULL DEFAULT '0',
  295. | `ip_addresses` TEXT NULL DEFAULT NULL,
  296. | `date_created` INT(11) NOT NULL,
  297. | PRIMARY KEY (`id`)
  298. | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  299. |
  300. */
  301. $config['rest_enable_keys'] = FALSE;
  302. /*
  303. |--------------------------------------------------------------------------
  304. | REST Table Key Column Name
  305. |--------------------------------------------------------------------------
  306. |
  307. | If not using the default table schema in 'rest_enable_keys', specify the
  308. | column name to match e.g. my_key
  309. |
  310. */
  311. $config['rest_key_column'] = 'key';
  312. /*
  313. |--------------------------------------------------------------------------
  314. | REST API Limits method
  315. |--------------------------------------------------------------------------
  316. |
  317. | Specify the method used to limit the API calls
  318. |
  319. | Available methods are :
  320. | $config['rest_limits_method'] = 'IP_ADDRESS'; // Put a limit per ip address
  321. | $config['rest_limits_method'] = 'API_KEY'; // Put a limit per api key
  322. | $config['rest_limits_method'] = 'METHOD_NAME'; // Put a limit on method calls
  323. | $config['rest_limits_method'] = 'ROUTED_URL'; // Put a limit on the routed URL
  324. |
  325. */
  326. $config['rest_limits_method'] = 'ROUTED_URL';
  327. /*
  328. |--------------------------------------------------------------------------
  329. | REST Key Length
  330. |--------------------------------------------------------------------------
  331. |
  332. | Length of the created keys. Check your default database schema on the
  333. | maximum length allowed
  334. |
  335. | Note: The maximum length is 40
  336. |
  337. */
  338. $config['rest_key_length'] = 40;
  339. /*
  340. |--------------------------------------------------------------------------
  341. | REST API Key Variable
  342. |--------------------------------------------------------------------------
  343. |
  344. | Custom header to specify the API key
  345. | Note: Custom headers with the X- prefix are deprecated as of
  346. | 2012/06/12. See RFC 6648 specification for more details
  347. |
  348. */
  349. $config['rest_key_name'] = 'X-API-KEY';
  350. /*
  351. |--------------------------------------------------------------------------
  352. | REST Enable Logging
  353. |--------------------------------------------------------------------------
  354. |
  355. | When set to TRUE, the REST API will log actions based on the column names 'key', 'date',
  356. | 'time' and 'ip_address'. This is a general rule that can be overridden in the
  357. | $this->method array for each controller
  358. |
  359. | Default table schema:
  360. | CREATE TABLE `logs` (
  361. | `id` INT(11) NOT NULL AUTO_INCREMENT,
  362. | `uri` VARCHAR(255) NOT NULL,
  363. | `method` VARCHAR(6) NOT NULL,
  364. | `params` TEXT DEFAULT NULL,
  365. | `api_key` VARCHAR(40) NOT NULL,
  366. | `ip_address` VARCHAR(45) NOT NULL,
  367. | `time` INT(11) NOT NULL,
  368. | `rtime` FLOAT DEFAULT NULL,
  369. | `authorized` VARCHAR(1) NOT NULL,
  370. | `response_code` smallint(3) DEFAULT '0',
  371. | PRIMARY KEY (`id`)
  372. | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  373. |
  374. */
  375. $config['rest_enable_logging'] = FALSE;
  376. /*
  377. |--------------------------------------------------------------------------
  378. | REST API Logs Table Name
  379. |--------------------------------------------------------------------------
  380. |
  381. | If not using the default table schema in 'rest_enable_logging', specify the
  382. | table name to match e.g. my_logs
  383. |
  384. */
  385. $config['rest_logs_table'] = 'logs';
  386. /*
  387. |--------------------------------------------------------------------------
  388. | REST Method Access Control
  389. |--------------------------------------------------------------------------
  390. | When set to TRUE, the REST API will check the access table to see if
  391. | the API key can access that controller. 'rest_enable_keys' must be enabled
  392. | to use this
  393. |
  394. | Default table schema:
  395. | CREATE TABLE `access` (
  396. | `id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
  397. | `key` VARCHAR(40) NOT NULL DEFAULT '',
  398. | `all_access` TINYINT(1) NOT NULL DEFAULT '0',
  399. | `controller` VARCHAR(50) NOT NULL DEFAULT '',
  400. | `date_created` DATETIME DEFAULT NULL,
  401. | `date_modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  402. | PRIMARY KEY (`id`)
  403. | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  404. |
  405. */
  406. $config['rest_enable_access'] = FALSE;
  407. /*
  408. |--------------------------------------------------------------------------
  409. | REST API Access Table Name
  410. |--------------------------------------------------------------------------
  411. |
  412. | If not using the default table schema in 'rest_enable_access', specify the
  413. | table name to match e.g. my_access
  414. |
  415. */
  416. $config['rest_access_table'] = 'access';
  417. /*
  418. |--------------------------------------------------------------------------
  419. | REST API Param Log Format
  420. |--------------------------------------------------------------------------
  421. |
  422. | When set to TRUE, the REST API log parameters will be stored in the database as JSON
  423. | Set to FALSE to log as serialized PHP
  424. |
  425. */
  426. $config['rest_logs_json_params'] = FALSE;
  427. /*
  428. |--------------------------------------------------------------------------
  429. | REST Enable Limits
  430. |--------------------------------------------------------------------------
  431. |
  432. | When set to TRUE, the REST API will count the number of uses of each method
  433. | by an API key each hour. This is a general rule that can be overridden in the
  434. | $this->method array in each controller
  435. |
  436. | Default table schema:
  437. | CREATE TABLE `limits` (
  438. | `id` INT(11) NOT NULL AUTO_INCREMENT,
  439. | `uri` VARCHAR(255) NOT NULL,
  440. | `count` INT(10) NOT NULL,
  441. | `hour_started` INT(11) NOT NULL,
  442. | `api_key` VARCHAR(40) NOT NULL,
  443. | PRIMARY KEY (`id`)
  444. | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  445. |
  446. | To specify the limits within the controller's __construct() method, add per-method
  447. | limits with:
  448. |
  449. | $this->method['METHOD_NAME']['limit'] = [NUM_REQUESTS_PER_HOUR];
  450. |
  451. | See application/controllers/api/example.php for examples
  452. */
  453. $config['rest_enable_limits'] = FALSE;
  454. /*
  455. |--------------------------------------------------------------------------
  456. | REST API Limits Table Name
  457. |--------------------------------------------------------------------------
  458. |
  459. | If not using the default table schema in 'rest_enable_limits', specify the
  460. | table name to match e.g. my_limits
  461. |
  462. */
  463. $config['rest_limits_table'] = 'limits';
  464. /*
  465. |--------------------------------------------------------------------------
  466. | REST Ignore HTTP Accept
  467. |--------------------------------------------------------------------------
  468. |
  469. | Set to TRUE to ignore the HTTP Accept and speed up each request a little.
  470. | Only do this if you are using the $this->rest_format or /format/xml in URLs
  471. |
  472. */
  473. $config['rest_ignore_http_accept'] = FALSE;
  474. /*
  475. |--------------------------------------------------------------------------
  476. | REST AJAX Only
  477. |--------------------------------------------------------------------------
  478. |
  479. | Set to TRUE to allow AJAX requests only. Set to FALSE to accept HTTP requests
  480. |
  481. | Note: If set to TRUE and the request is not AJAX, a 505 response with the
  482. | error message 'Only AJAX requests are accepted.' will be returned.
  483. |
  484. | Hint: This is good for production environments
  485. |
  486. */
  487. $config['rest_ajax_only'] = FALSE;
  488. /*
  489. |--------------------------------------------------------------------------
  490. | REST Language File
  491. |--------------------------------------------------------------------------
  492. |
  493. | Language file to load from the language directory
  494. |
  495. */
  496. $config['rest_language'] = 'english';
  497. /*
  498. |--------------------------------------------------------------------------
  499. | CORS Check
  500. |--------------------------------------------------------------------------
  501. |
  502. | Set to TRUE to enable Cross-Origin Resource Sharing (CORS). Useful if you
  503. | are hosting your API on a different domain from the application that
  504. | will access it through a browser
  505. |
  506. */
  507. $config['check_cors'] = FALSE;
  508. /*
  509. |--------------------------------------------------------------------------
  510. | CORS Allowable Headers
  511. |--------------------------------------------------------------------------
  512. |
  513. | If using CORS checks, set the allowable headers here
  514. |
  515. */
  516. $config['allowed_cors_headers'] = [
  517. 'Origin',
  518. 'X-Requested-With',
  519. 'Content-Type',
  520. 'Accept',
  521. 'Access-Control-Request-Method'
  522. ];
  523. /*
  524. |--------------------------------------------------------------------------
  525. | CORS Allowable Methods
  526. |--------------------------------------------------------------------------
  527. |
  528. | If using CORS checks, you can set the methods you want to be allowed
  529. |
  530. */
  531. $config['allowed_cors_methods'] = [
  532. 'GET',
  533. 'POST',
  534. 'OPTIONS',
  535. 'PUT',
  536. 'PATCH',
  537. 'DELETE'
  538. ];
  539. /*
  540. |--------------------------------------------------------------------------
  541. | CORS Allow Any Domain
  542. |--------------------------------------------------------------------------
  543. |
  544. | Set to TRUE to enable Cross-Origin Resource Sharing (CORS) from any
  545. | source domain
  546. |
  547. */
  548. $config['allow_any_cors_domain'] = FALSE;
  549. /*
  550. |--------------------------------------------------------------------------
  551. | CORS Allowable Domains
  552. |--------------------------------------------------------------------------
  553. |
  554. | Used if $config['check_cors'] is set to TRUE and $config['allow_any_cors_domain']
  555. | is set to FALSE. Set all the allowable domains within the array
  556. |
  557. | e.g. $config['allowed_origins'] = ['http://www.example.com', 'https://spa.example.com']
  558. |
  559. */
  560. $config['allowed_cors_origins'] = [];