├── .codeclimate.yml
├── AUTHORS.md
├── CHANGELOG.md
├── LICENSE
├── README.md
├── application
├── config
│ ├── config.php.sample
│ ├── hooks.php
│ ├── index.html
│ ├── ldap.php
│ ├── profiler.php
│ ├── rest.php
│ └── routes.php
├── controllers
│ ├── Rest_server.php
│ ├── Welcome.php
│ ├── api
│ │ ├── Example.php
│ │ ├── Key.php
│ │ └── index.html
│ └── index.html
├── helpers
│ ├── db_helper.php
│ └── index.html
├── hooks
│ └── hooks.profiler.php
├── language
│ ├── bulgarian
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── dutch
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── english
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── french
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── german
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── greek
│ │ └── rest_controller_lang.php
│ ├── index.html
│ ├── indonesia
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── italian
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── korean
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── portuguese-brazilian
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── romanian
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── serbian_cyr
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── serbian_lat
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── simplified-chinese
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── spanish
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ ├── traditional-chinese
│ │ ├── index.html
│ │ └── rest_controller_lang.php
│ └── turkish
│ │ ├── index.html
│ │ └── rest_controller_lang.php
├── libraries
│ ├── Format.php
│ ├── REST_Controller.php
│ └── index.html
├── migrations
│ ├── 20170706025420_create_table_users.php
│ ├── 20170706030520_create_table_api_keys.php
│ ├── 20170706031435_create_table_api_logs.php
│ ├── 20170706032133_create_table_api_access.php
│ ├── 20170706032825_create_table_api_limits.php
│ └── index.html
└── views
│ ├── index.html
│ ├── rest_server.php
│ └── welcome_message.php
├── composer.json
└── documentation
├── 404.html
├── class-Example.html
├── class-Format.html
├── class-Key.html
├── class-REST_Controller.html
├── class-Rest_server.html
├── class-Welcome.html
├── elementlist.js
├── index.html
├── package-CodeIgniter.Libraries.html
├── package-CodeIgniter.Rest.html
├── package-CodeIgniter.html
├── package-None.html
├── resources
├── collapsed.png
├── combined.js
├── footer.png
├── inherit.png
├── resize.png
├── sort.png
├── style.css
├── tree-cleaner.png
├── tree-hasnext.png
├── tree-last.png
└── tree-vertical.png
├── source-class-Example.html
├── source-class-Format.html
├── source-class-Key.html
├── source-class-REST_Controller.html
├── source-class-Rest_server.html
└── source-class-Welcome.html
/.codeclimate.yml:
--------------------------------------------------------------------------------
1 | exclude_paths:
2 | - "documentation/"
3 | - "application/language/"
--------------------------------------------------------------------------------
/AUTHORS.md:
--------------------------------------------------------------------------------
1 | # The Core Team
2 |
3 | * [Chris Kacerguis](//github.com/chriskacerguis)
4 | * [Phil Sturgeon](//github.com/philsturgeon)
5 |
6 | ### Special Thanks To
7 |
8 | * [Fabian Hanisch](//github.com/Hanisch-IT)
9 |
10 | *For a list of people who have contributed to the codebase, see [GitHub's list of contributors](https://github.com/chriskacerguis/codeigniter-restserver/graphs/contributors). Anyone who has contributed please do a PR and add to this file.*
11 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | Changelog:
2 | ===========
3 |
4 | ### UNRELEASED
5 | * Added support for CodeIgniter controller's index methods (index_GET, index_POST...)
6 | * Added exceptions handling when the method could not be found
7 |
8 | ### 2.7.2
9 |
10 | * Added $this->query() in which query parameters can now be obtained regardless of whether a GET request is sent or not
11 | * Added doc comments added to functions
12 | * Added HTTP status constants e.g. REST_Controller::HTTP_OK
13 | * Added new CSV formatting function
14 | * Fixed numerous bug fixes
15 | * Updated API calls limit can be based on API key, routed url or method name
16 | * Updated documentation
17 | * Updated examples (thanks @ivantcholakov and @lagaisse)
18 | * Updated many functions by re-writing (thanks @softwarespot)
19 | * Updated performance increase
20 |
21 | ### 2.7.0
22 |
23 | * Added Blacklist IP option
24 | * Added controller based access controls
25 | * Added support for OPTIONS, PATCH, and HEAD (from boh1996)
26 | * Added logging of the time it takes for a request (rtime column in DB)
27 | * Changed DB schemas to use InnoDB, not MyISAM
28 | * Updated Readme to reflect new developer (Chris Kacerguis)
29 |
30 | ### 2.6.2
31 |
32 | * Update CodeIgniter files to 2.1.3
33 | * Fixed issue #165
34 |
35 | ### 2.6.1
36 |
37 | * Update CodeIgniter files to 2.1.2
38 | * Log Table support for IPv6 & NULL parameters
39 | * Abstract out the processes of firing a controller method within _remap() to an separate method
40 | * Moved GET, POST, PUT, and DELETE parsing to separate methods, allowing them to be overridden as needed
41 | * Small bug-fix for a PHP 5.3 strlen error
42 | * Fixed some PHP 5.4 warnings
43 | * Fix for bug in Format.php's to_html() which failed to detect if $data was really a multidimensional array.
44 | * Fix for empty node on XML output format, for false = 0, true = 1.
45 |
46 | ### 2.6.0
47 |
48 | * Added loads of PHPDoc comments.
49 | * Response where method doesn't exist is now "HTTP 405 Method Not Allowed", not "HTTP 404 Not Found".
50 | * Compatible with PHP 5.4.
51 | * Added support for gzip compression.
52 | * Fix the apache\_request\_header function with CGI.
53 | * Fixed up correctly .foo extensions to work when get arguments provided.
54 | * Allows method emulation via X-HTTP-Method-Override
55 | * Support for Backbone.emulateHTTP improved.
56 | * Combine both URI segment and GET params instead of using one or the other
57 | * Separate each piece of the WWW-Authenticate header for digest requests with a comma.
58 | * Added IP white-list option.
59 |
60 | ### 2.5
61 |
62 | * Instead of just seeing item, item, item, the singular version of the base-node will be used if possible. [Example](http://d.pr/RS46).
63 | * Re-factored to use the Format library, which will soon be merged with CodeIgniter.
64 | * Fixed Limit bug (limit of 5 would allow 6 requests).
65 | * Added logging for invalid API key requests.
66 | * Changed serialize to serialized.
67 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright (c) 2012 - 2015 Phil Sturgeon, Chris Kacerguis
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CodeIgniter Rest Server
2 |
3 |
4 | A fully RESTful server implementation for CodeIgniter using one library, one
5 | config file and one controller.
6 |
7 | ## Requirements
8 |
9 | 1. PHP 5.4 or greater
10 | 2. CodeIgniter 3.0+
11 |
--------------------------------------------------------------------------------
/application/config/config.php.sample:
--------------------------------------------------------------------------------
1 | ]+$/i
170 | |
171 | | DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!!
172 | |
173 | | Note: This option is ignored for CLI requests.
174 | |
175 | */
176 | $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
177 |
178 | /*
179 | |--------------------------------------------------------------------------
180 | | Enable Query Strings
181 | |--------------------------------------------------------------------------
182 | |
183 | | By default CodeIgniter uses search-engine friendly segment based URLs:
184 | | example.com/who/what/where/
185 | |
186 | | You can optionally enable standard query string based URLs:
187 | | example.com?who=me&what=something&where=here
188 | |
189 | | Options are: TRUE or FALSE (boolean)
190 | |
191 | | The other items let you set the query string 'words' that will
192 | | invoke your controllers and its functions:
193 | | example.com/index.php?c=controller&m=function
194 | |
195 | | Please note that some of the helpers won't work as expected when
196 | | this feature is enabled, since CodeIgniter is designed primarily to
197 | | use segment based URLs.
198 | |
199 | */
200 | $config['enable_query_strings'] = FALSE;
201 | $config['controller_trigger'] = 'c';
202 | $config['function_trigger'] = 'm';
203 | $config['directory_trigger'] = 'd';
204 |
205 | /*
206 | |--------------------------------------------------------------------------
207 | | Error Logging Threshold
208 | |--------------------------------------------------------------------------
209 | |
210 | | You can enable error logging by setting a threshold over zero. The
211 | | threshold determines what gets logged. Threshold options are:
212 | |
213 | | 0 = Disables logging, Error logging TURNED OFF
214 | | 1 = Error Messages (including PHP errors)
215 | | 2 = Debug Messages
216 | | 3 = Informational Messages
217 | | 4 = All Messages
218 | |
219 | | You can also pass an array with threshold levels to show individual error types
220 | |
221 | | array(2) = Debug Messages, without Error Messages
222 | |
223 | | For a live site you'll usually only enable Errors (1) to be logged otherwise
224 | | your log files will fill up very fast.
225 | |
226 | */
227 | $config['log_threshold'] = 0;
228 |
229 | /*
230 | |--------------------------------------------------------------------------
231 | | Error Logging Directory Path
232 | |--------------------------------------------------------------------------
233 | |
234 | | Leave this BLANK unless you would like to set something other than the default
235 | | application/logs/ directory. Use a full server path.
236 | |
237 | */
238 | $config['log_path'] = '';
239 |
240 | /*
241 | |--------------------------------------------------------------------------
242 | | Log File Extension
243 | |--------------------------------------------------------------------------
244 | |
245 | | The default filename extension for log files. The default 'php' allows for
246 | | protecting the log files via basic scripting, when they are to be stored
247 | | under a publicly accessible directory.
248 | |
249 | | Note: Leaving it blank will default to 'php'.
250 | |
251 | */
252 | $config['log_file_extension'] = '';
253 |
254 | /*
255 | |--------------------------------------------------------------------------
256 | | Log File Permissions
257 | |--------------------------------------------------------------------------
258 | |
259 | | The file system permissions to be applied on newly created log files.
260 | |
261 | | IMPORTANT: This MUST be an integer (no quotes) and you MUST use octal
262 | | integer notation (i.e. 0700, 0644, etc.)
263 | */
264 | $config['log_file_permissions'] = 0644;
265 |
266 | /*
267 | |--------------------------------------------------------------------------
268 | | Date Format for Logs
269 | |--------------------------------------------------------------------------
270 | |
271 | | Each item that is logged has an associated date. You can use PHP date
272 | | codes to set your own date formatting
273 | |
274 | */
275 | $config['log_date_format'] = 'Y-m-d H:i:s';
276 |
277 | /*
278 | |--------------------------------------------------------------------------
279 | | Error Views Directory Path
280 | |--------------------------------------------------------------------------
281 | |
282 | | Leave this BLANK unless you would like to set something other than the default
283 | | application/views/errors/ directory. Use a full server path.
284 | |
285 | */
286 | $config['error_views_path'] = '';
287 |
288 | /*
289 | |--------------------------------------------------------------------------
290 | | Cache Directory Path
291 | |--------------------------------------------------------------------------
292 | |
293 | | Leave this BLANK unless you would like to set something other than the default
294 | | application/cache/ directory. Use a full server path.
295 | |
296 | */
297 | $config['cache_path'] = '';
298 |
299 | /*
300 | |--------------------------------------------------------------------------
301 | | Cache Include Query String
302 | |--------------------------------------------------------------------------
303 | |
304 | | Whether to take the URL query string into consideration when generating
305 | | output cache files. Valid options are:
306 | |
307 | | FALSE = Disabled
308 | | TRUE = Enabled, take all query parameters into account.
309 | | Please be aware that this may result in numerous cache
310 | | files generated for the same page over and over again.
311 | | array('q') = Enabled, but only take into account the specified list
312 | | of query parameters.
313 | |
314 | */
315 | $config['cache_query_string'] = FALSE;
316 |
317 | /*
318 | |--------------------------------------------------------------------------
319 | | Encryption Key
320 | |--------------------------------------------------------------------------
321 | |
322 | | If you use the Encryption class, you must set an encryption key.
323 | | See the user guide for more info.
324 | |
325 | | https://codeigniter.com/user_guide/libraries/encryption.html
326 | |
327 | */
328 | $config['encryption_key'] = '';
329 |
330 | /*
331 | |--------------------------------------------------------------------------
332 | | Session Variables
333 | |--------------------------------------------------------------------------
334 | |
335 | | 'sess_driver'
336 | |
337 | | The storage driver to use: files, database, redis, memcached
338 | |
339 | | 'sess_cookie_name'
340 | |
341 | | The session cookie name, must contain only [0-9a-z_-] characters
342 | |
343 | | 'sess_expiration'
344 | |
345 | | The number of SECONDS you want the session to last.
346 | | Setting to 0 (zero) means expire when the browser is closed.
347 | |
348 | | 'sess_save_path'
349 | |
350 | | The location to save sessions to, driver dependent.
351 | |
352 | | For the 'files' driver, it's a path to a writable directory.
353 | | WARNING: Only absolute paths are supported!
354 | |
355 | | For the 'database' driver, it's a table name.
356 | | Please read up the manual for the format with other session drivers.
357 | |
358 | | IMPORTANT: You are REQUIRED to set a valid save path!
359 | |
360 | | 'sess_match_ip'
361 | |
362 | | Whether to match the user's IP address when reading the session data.
363 | |
364 | | WARNING: If you're using the database driver, don't forget to update
365 | | your session table's PRIMARY KEY when changing this setting.
366 | |
367 | | 'sess_time_to_update'
368 | |
369 | | How many seconds between CI regenerating the session ID.
370 | |
371 | | 'sess_regenerate_destroy'
372 | |
373 | | Whether to destroy session data associated with the old session ID
374 | | when auto-regenerating the session ID. When set to FALSE, the data
375 | | will be later deleted by the garbage collector.
376 | |
377 | | Other session cookie settings are shared with the rest of the application,
378 | | except for 'cookie_prefix' and 'cookie_httponly', which are ignored here.
379 | |
380 | */
381 | $config['sess_driver'] = 'files';
382 | $config['sess_cookie_name'] = 'ci_session';
383 | $config['sess_expiration'] = 7200;
384 | $config['sess_save_path'] = NULL;
385 | $config['sess_match_ip'] = FALSE;
386 | $config['sess_time_to_update'] = 300;
387 | $config['sess_regenerate_destroy'] = FALSE;
388 |
389 | /*
390 | |--------------------------------------------------------------------------
391 | | Cookie Related Variables
392 | |--------------------------------------------------------------------------
393 | |
394 | | 'cookie_prefix' = Set a cookie name prefix if you need to avoid collisions
395 | | 'cookie_domain' = Set to .your-domain.com for site-wide cookies
396 | | 'cookie_path' = Typically will be a forward slash
397 | | 'cookie_secure' = Cookie will only be set if a secure HTTPS connection exists.
398 | | 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript)
399 | |
400 | | Note: These settings (with the exception of 'cookie_prefix' and
401 | | 'cookie_httponly') will also affect sessions.
402 | |
403 | */
404 | $config['cookie_prefix'] = '';
405 | $config['cookie_domain'] = '';
406 | $config['cookie_path'] = '/';
407 | $config['cookie_secure'] = FALSE;
408 | $config['cookie_httponly'] = FALSE;
409 |
410 | /*
411 | |--------------------------------------------------------------------------
412 | | Cross Site Request Forgery
413 | |--------------------------------------------------------------------------
414 | | Enables a CSRF cookie token to be set. When set to TRUE, token will be
415 | | checked on a submitted form. If you are accepting user data, it is strongly
416 | | recommended CSRF protection be enabled.
417 | |
418 | | 'csrf_token_name' = The token name
419 | | 'csrf_cookie_name' = The cookie name
420 | | 'csrf_expire' = The number in seconds the token should expire.
421 | | 'csrf_regenerate' = Regenerate token on every submission
422 | | 'csrf_exclude_uris' = Array of URIs which ignore CSRF checks
423 | */
424 | $config['csrf_protection'] = FALSE;
425 | $config['csrf_token_name'] = 'csrf_test_name';
426 | $config['csrf_cookie_name'] = 'csrf_cookie_name';
427 | $config['csrf_expire'] = 7200;
428 | $config['csrf_regenerate'] = TRUE;
429 | $config['csrf_exclude_uris'] = array();
430 |
431 | /*
432 | |--------------------------------------------------------------------------
433 | | Output Compression
434 | |--------------------------------------------------------------------------
435 | |
436 | | Enables Gzip output compression for faster page loads. When enabled,
437 | | the output class will test whether your server supports Gzip.
438 | | Even if it does, however, not all browsers support compression
439 | | so enable only if you are reasonably sure your visitors can handle it.
440 | |
441 | | Only used if zlib.output_compression is turned off in your php.ini.
442 | | Please do not use it together with httpd-level output compression.
443 | |
444 | | VERY IMPORTANT: If you are getting a blank page when compression is enabled it
445 | | means you are prematurely outputting something to your browser. It could
446 | | even be a line of whitespace at the end of one of your scripts. For
447 | | compression to work, nothing can be sent before the output buffer is called
448 | | by the output class. Do not 'echo' any values with compression enabled.
449 | |
450 | */
451 | $config['compress_output'] = FALSE;
452 |
453 | /*
454 | |--------------------------------------------------------------------------
455 | | Master Time Reference
456 | |--------------------------------------------------------------------------
457 | |
458 | | Options are 'local' or any PHP supported timezone. This preference tells
459 | | the system whether to use your server's local time as the master 'now'
460 | | reference, or convert it to the configured one timezone. See the 'date
461 | | helper' page of the user guide for information regarding date handling.
462 | |
463 | */
464 | $config['time_reference'] = 'local';
465 |
466 | /*
467 | |--------------------------------------------------------------------------
468 | | Reverse Proxy IPs
469 | |--------------------------------------------------------------------------
470 | |
471 | | If your server is behind a reverse proxy, you must whitelist the proxy
472 | | IP addresses from which CodeIgniter should trust headers such as
473 | | HTTP_X_FORWARDED_FOR and HTTP_CLIENT_IP in order to properly identify
474 | | the visitor's IP address.
475 | |
476 | | You can use both an array or a comma-separated list of proxy addresses,
477 | | as well as specifying whole subnets. Here are a few examples:
478 | |
479 | | Comma-separated: '10.0.1.200,192.168.5.0/24'
480 | | Array: array('10.0.1.200', '192.168.5.0/24')
481 | */
482 | $config['proxy_ips'] = '';
483 |
--------------------------------------------------------------------------------
/application/config/hooks.php:
--------------------------------------------------------------------------------
1 | 'ProfilerEnabler',
17 | 'function' => 'enableProfiler',
18 | 'filename' => 'hooks.profiler.php',
19 | 'filepath' => 'hooks',
20 | 'params' => array()
21 | );
--------------------------------------------------------------------------------
/application/config/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 403 Forbidden
5 |
6 |
7 |
8 |
Directory access is forbidden.
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/application/config/ldap.php:
--------------------------------------------------------------------------------
1 | my_controller/index
50 | | my-controller/my-method -> my_controller/my_method
51 | */
52 | $route['default_controller'] = 'welcome';
53 | $route['404_override'] = '';
54 | $route['translate_uri_dashes'] = TRUE;
55 |
56 | /*
57 | | -------------------------------------------------------------------------
58 | | Sample REST API Routes
59 | | -------------------------------------------------------------------------
60 | */
61 | $route['api/example/users/(:num)'] = 'api/example/users/id/$1'; // Example 4
62 | $route['api/example/users/(:num)(\.)([a-zA-Z0-9_-]+)(.*)'] = 'api/example/users/id/$1/format/$3$4'; // Example 8
63 |
--------------------------------------------------------------------------------
/application/controllers/Rest_server.php:
--------------------------------------------------------------------------------
1 | load->helper('url');
10 |
11 | $this->load->view('rest_server');
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/application/controllers/Welcome.php:
--------------------------------------------------------------------------------
1 |
19 | * @see https://codeigniter.com/user_guide/general/urls.html
20 | */
21 | public function index()
22 | {
23 | $this->load->helper('url');
24 |
25 | $this->load->view('welcome_message');
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/application/controllers/api/Example.php:
--------------------------------------------------------------------------------
1 | methods['users_get']['limit'] = 500; // 500 requests per hour per user/key
32 | $this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key
33 | $this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
34 | }
35 |
36 | public function users_get()
37 | {
38 | // Users from a data store e.g. database
39 | $users = [
40 | ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
41 | ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
42 | ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
43 | ];
44 |
45 | $id = $this->get('id');
46 |
47 | // If the id parameter doesn't exist return all the users
48 |
49 | if ($id === NULL)
50 | {
51 | // Check if the users data store contains users (in case the database result returns NULL)
52 | if ($users)
53 | {
54 | // Set the response and exit
55 | $this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
56 | }
57 | else
58 | {
59 | // Set the response and exit
60 | $this->response([
61 | 'status' => FALSE,
62 | 'message' => 'No users were found'
63 | ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
64 | }
65 | }
66 |
67 | // Find and return a single record for a particular user.
68 |
69 | $id = (int) $id;
70 |
71 | // Validate the id.
72 | if ($id <= 0)
73 | {
74 | // Invalid id, set the response and exit.
75 | $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
76 | }
77 |
78 | // Get the user from the array, using the id as key for retrieval.
79 | // Usually a model is to be used for this.
80 |
81 | $user = NULL;
82 |
83 | if (!empty($users))
84 | {
85 | foreach ($users as $key => $value)
86 | {
87 | if (isset($value['id']) && $value['id'] === $id)
88 | {
89 | $user = $value;
90 | }
91 | }
92 | }
93 |
94 | if (!empty($user))
95 | {
96 | $this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
97 | }
98 | else
99 | {
100 | $this->set_response([
101 | 'status' => FALSE,
102 | 'message' => 'User could not be found'
103 | ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
104 | }
105 | }
106 |
107 | public function users_post()
108 | {
109 | // $this->some_model->update_user( ... );
110 | $message = [
111 | 'id' => 100, // Automatically generated by the model
112 | 'name' => $this->post('name'),
113 | 'email' => $this->post('email'),
114 | 'message' => 'Added a resource'
115 | ];
116 |
117 | $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
118 | }
119 |
120 | public function users_delete()
121 | {
122 | $id = (int) $this->get('id');
123 |
124 | // Validate the id.
125 | if ($id <= 0)
126 | {
127 | // Set the response and exit
128 | $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
129 | }
130 |
131 | // $this->some_model->delete_something($id);
132 | $message = [
133 | 'id' => $id,
134 | 'message' => 'Deleted the resource'
135 | ];
136 |
137 | $this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
138 | }
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/application/controllers/api/Key.php:
--------------------------------------------------------------------------------
1 | ['level' => 10, 'limit' => 10],
25 | 'index_delete' => ['level' => 10],
26 | 'level_post' => ['level' => 10],
27 | 'regenerate_post' => ['level' => 10],
28 | ];
29 |
30 | /**
31 | * Insert a key into the database
32 | *
33 | * @access public
34 | * @return void
35 | */
36 | public function index_put()
37 | {
38 | // Build a new key
39 | $key = $this->_generate_key();
40 |
41 | // If no key level provided, provide a generic key
42 | $level = $this->put('level') ? $this->put('level') : 1;
43 | $ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int) $this->put('ignore_limits') : 1;
44 |
45 | // Insert the new key
46 | if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
47 | {
48 | $this->response([
49 | 'status' => TRUE,
50 | 'key' => $key
51 | ], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
52 | }
53 | else
54 | {
55 | $this->response([
56 | 'status' => FALSE,
57 | 'message' => 'Could not save the key'
58 | ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
59 | }
60 | }
61 |
62 | /**
63 | * Remove a key from the database to stop it working
64 | *
65 | * @access public
66 | * @return void
67 | */
68 | public function index_delete()
69 | {
70 | $key = $this->delete('key');
71 |
72 | // Does this key exist?
73 | if (!$this->_key_exists($key))
74 | {
75 | // It doesn't appear the key exists
76 | $this->response([
77 | 'status' => FALSE,
78 | 'message' => 'Invalid API key'
79 | ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
80 | }
81 |
82 | // Destroy it
83 | $this->_delete_key($key);
84 |
85 | // Respond that the key was destroyed
86 | $this->response([
87 | 'status' => TRUE,
88 | 'message' => 'API key was deleted'
89 | ], REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
90 | }
91 |
92 | /**
93 | * Change the level
94 | *
95 | * @access public
96 | * @return void
97 | */
98 | public function level_post()
99 | {
100 | $key = $this->post('key');
101 | $new_level = $this->post('level');
102 |
103 | // Does this key exist?
104 | if (!$this->_key_exists($key))
105 | {
106 | // It doesn't appear the key exists
107 | $this->response([
108 | 'status' => FALSE,
109 | 'message' => 'Invalid API key'
110 | ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
111 | }
112 |
113 | // Update the key level
114 | if ($this->_update_key($key, ['level' => $new_level]))
115 | {
116 | $this->response([
117 | 'status' => TRUE,
118 | 'message' => 'API key was updated'
119 | ], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
120 | }
121 | else
122 | {
123 | $this->response([
124 | 'status' => FALSE,
125 | 'message' => 'Could not update the key level'
126 | ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
127 | }
128 | }
129 |
130 | /**
131 | * Suspend a key
132 | *
133 | * @access public
134 | * @return void
135 | */
136 | public function suspend_post()
137 | {
138 | $key = $this->post('key');
139 |
140 | // Does this key exist?
141 | if (!$this->_key_exists($key))
142 | {
143 | // It doesn't appear the key exists
144 | $this->response([
145 | 'status' => FALSE,
146 | 'message' => 'Invalid API key'
147 | ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
148 | }
149 |
150 | // Update the key level
151 | if ($this->_update_key($key, ['level' => 0]))
152 | {
153 | $this->response([
154 | 'status' => TRUE,
155 | 'message' => 'Key was suspended'
156 | ], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
157 | }
158 | else
159 | {
160 | $this->response([
161 | 'status' => FALSE,
162 | 'message' => 'Could not suspend the user'
163 | ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
164 | }
165 | }
166 |
167 | /**
168 | * Regenerate a key
169 | *
170 | * @access public
171 | * @return void
172 | */
173 | public function regenerate_post()
174 | {
175 | $old_key = $this->post('key');
176 | $key_details = $this->_get_key($old_key);
177 |
178 | // Does this key exist?
179 | if (!$key_details)
180 | {
181 | // It doesn't appear the key exists
182 | $this->response([
183 | 'status' => FALSE,
184 | 'message' => 'Invalid API key'
185 | ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
186 | }
187 |
188 | // Build a new key
189 | $new_key = $this->_generate_key();
190 |
191 | // Insert the new key
192 | if ($this->_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits]))
193 | {
194 | // Suspend old key
195 | $this->_update_key($old_key, ['level' => 0]);
196 |
197 | $this->response([
198 | 'status' => TRUE,
199 | 'key' => $new_key
200 | ], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
201 | }
202 | else
203 | {
204 | $this->response([
205 | 'status' => FALSE,
206 | 'message' => 'Could not save the key'
207 | ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
208 | }
209 | }
210 |
211 | /* Helper Methods */
212 |
213 | private function _generate_key()
214 | {
215 | do
216 | {
217 | // Generate a random salt
218 | $salt = base_convert(bin2hex($this->security->get_random_bytes(64)), 16, 36);
219 |
220 | // If an error occurred, then fall back to the previous method
221 | if ($salt === FALSE)
222 | {
223 | $salt = hash('sha256', time() . mt_rand());
224 | }
225 |
226 | $new_key = substr($salt, 0, config_item('rest_key_length'));
227 | }
228 | while ($this->_key_exists($new_key));
229 |
230 | return $new_key;
231 | }
232 |
233 | /* Private Data Methods */
234 |
235 | private function _get_key($key)
236 | {
237 | return $this->rest->db
238 | ->where(config_item('rest_key_column'), $key)
239 | ->get(config_item('rest_keys_table'))
240 | ->row();
241 | }
242 |
243 | private function _key_exists($key)
244 | {
245 | return $this->rest->db
246 | ->where(config_item('rest_key_column'), $key)
247 | ->count_all_results(config_item('rest_keys_table')) > 0;
248 | }
249 |
250 | private function _insert_key($key, $data)
251 | {
252 | $data[config_item('rest_key_column')] = $key;
253 | $data['date_created'] = function_exists('now') ? now() : time();
254 |
255 | return $this->rest->db
256 | ->set($data)
257 | ->insert(config_item('rest_keys_table'));
258 | }
259 |
260 | private function _update_key($key, $data)
261 | {
262 | return $this->rest->db
263 | ->where(config_item('rest_key_column'), $key)
264 | ->update(config_item('rest_keys_table'), $data);
265 | }
266 |
267 | private function _delete_key($key)
268 | {
269 | return $this->rest->db
270 | ->where(config_item('rest_key_column'), $key)
271 | ->delete(config_item('rest_keys_table'));
272 | }
273 |
274 | }
275 |
--------------------------------------------------------------------------------
/application/controllers/api/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 403 Forbidden
5 |
6 |
7 |
8 |
You have probably clicked on a link that is outdated and points to a page that does not exist any more or you have made an typing error in the address.
99 |
To continue please try to find requested page in the menu, or use search field on the top.
Maps to the following URL http://example.com/index.php/welcome
153 | - or - http://example.com/index.php/welcome/index
154 | - or -
155 | Since this controller is set as the default controller in
156 | config/routes.php, it's displayed at http://example.com/
157 |
158 |
So any other public methods not prefixed with an underscore will
159 | map to /index.php/welcome/