Skip to content

Commit 95dd903

Browse files
Added response field names and small fixes
1 parent 976ca45 commit 95dd903

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

application/config/rest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@
2424
*/
2525
$config['rest_default_format'] = 'xml';
2626

27+
/*
28+
|--------------------------------------------------------------------------
29+
| REST Status field name
30+
|--------------------------------------------------------------------------
31+
|
32+
| The field name for the status of the response
33+
|
34+
| 'status'
35+
|
36+
*/
37+
$config['rest_status_field_name'] = 'status';
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| REST message field name
42+
|--------------------------------------------------------------------------
43+
|
44+
| The field name for the message inside the response
45+
|
46+
| 'error'
47+
|
48+
*/
49+
$config['rest_message_field_name'] = 'error';
50+
2751
/*
2852
|--------------------------------------------------------------------------
2953
| Enable emulate request

application/libraries/REST_Controller.php

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,6 @@ public function __construct()
270270
// Developers can extend this class and add a check in here
271271
$this->early_checks();
272272

273-
// Check if there is a specific auth type for the current class/method
274-
$this->auth_override = $this->_auth_override_check();
275-
276273
$this->rest = new StdClass();
277274

278275
// Load DB if its enabled
@@ -285,14 +282,18 @@ public function __construct()
285282
$this->rest->db = $this->db;
286283
}
287284

285+
// Check if there is a specific auth type for the current class/method
286+
// _auth_override_check could exit so we need $this->rest->db initialized before
287+
$this->auth_override = $this->_auth_override_check();
288+
288289
// Checking for keys? GET TO WorK!
289290
if (config_item('rest_enable_keys')) {
290291
$this->_allow = $this->_detect_api_key();
291292
}
292293

293294
// only allow ajax requests
294295
if (!$this->input->is_ajax_request() and config_item('rest_ajax_only')) {
295-
$response = array('status' => false, 'error' => 'Only AJAX requests are accepted.');
296+
$response = array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'Only AJAX requests are accepted.');
296297
$this->response($response, 406); // Set status to 406 NOT ACCEPTABLE
297298
}
298299

@@ -345,11 +346,11 @@ public function _remap($object_called, $arguments)
345346
{
346347
// Should we answer if not over SSL?
347348
if (config_item('force_https') and !$this->_detect_ssl()) {
348-
$this->response(array('status' => false, 'error' => 'Unsupported protocol'), 403);
349+
$this->response(array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'Unsupported protocol'), 403);
349350
}
350351

351352
$pattern = '/^(.*)\.('.implode('|', array_keys($this->_supported_formats)).')$/';
352-
353+
$matches = array();
353354
if (preg_match($pattern, $object_called, $matches)) {
354355
$object_called = $matches[1];
355356
}
@@ -367,8 +368,8 @@ public function _remap($object_called, $arguments)
367368
if (config_item('rest_enable_logging') and $log_method) {
368369
$this->_log_request();
369370
}
370-
371-
$this->response(array('status' => false, 'error' => 'Invalid API Key.'), 403);
371+
372+
$this->response(array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'Invalid API Key '.$this->rest->key), 403);
372373
}
373374

374375
// Check to see if this key has access to the requested controller.
@@ -377,19 +378,19 @@ public function _remap($object_called, $arguments)
377378
$this->_log_request();
378379
}
379380

380-
$this->response(array('status' => false, 'error' => 'This API key does not have access to the requested controller.'), 401);
381+
$this->response(array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'This API key does not have access to the requested controller.'), 401);
381382
}
382383

383384
// Sure it exists, but can they do anything with it?
384385
if ( ! method_exists($this, $controller_method)) {
385-
$this->response(array('status' => false, 'error' => 'Unknown method.'), 404);
386+
$this->response(array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'Unknown method.'), 404);
386387
}
387388

388389
// Doing key related stuff? Can only do it if they have a key right?
389390
if (config_item('rest_enable_keys') and !empty($this->rest->key)) {
390391
// Check the limit
391392
if (config_item('rest_enable_limits') and !$this->_check_limit($controller_method)) {
392-
$response = array('status' => false, 'error' => 'This API key has reached the hourly limit for this method.');
393+
$response = array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'This API key has reached the hourly limit for this method.');
393394
$this->response($response, 401);
394395
}
395396

@@ -405,7 +406,7 @@ public function _remap($object_called, $arguments)
405406
}
406407

407408
// They don't have good enough perms
408-
$response = array('status' => false, 'error' => 'This API key does not have enough permissions.');
409+
$response = array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'This API key does not have enough permissions.');
409410
$authorized or $this->response($response, 401);
410411
}
411412

@@ -560,6 +561,7 @@ protected function _detect_output_format()
560561
$pattern = '/\.('.implode('|', array_keys($this->_supported_formats)).')$/';
561562

562563
// Check if a file extension is used when no get arguments provided
564+
$matches = array();
563565
if (!$this->_get_args and preg_match($pattern, $this->uri->uri_string(), $matches)) {
564566
return $matches[1];
565567
}
@@ -728,7 +730,6 @@ protected function _detect_lang()
728730
$langs = explode(',', $lang);
729731

730732
$return_langs = array();
731-
$i = 1;
732733
foreach ($langs as $lang) {
733734
// Remove weight and strip space
734735
list($lang) = explode(';', $lang);
@@ -1267,7 +1268,7 @@ protected function _check_login($username = '', $password = null)
12671268
}
12681269

12691270
if ($auth_source == 'library') {
1270-
log_message('debug', 'performing Library authentication for $username');
1271+
log_message('debug', 'performing Library authentication for '.$username);
12711272

12721273
return $this->_perform_library_auth($username, $password);
12731274
}
@@ -1319,7 +1320,7 @@ protected function _prepare_basic_auth()
13191320
// most other servers
13201321
elseif ($this->input->server('HTTP_AUTHENTICATION')) {
13211322
if (strpos(strtolower($this->input->server('HTTP_AUTHENTICATION')), 'basic') === 0) {
1322-
list($username, $password) = explode(':', base64_decode(substr($this->input->server('HTTP_AUTHorIZATION'), 6)));
1323+
list($username, $password) = explode(':', base64_decode(substr($this->input->server('HTTP_AUTHORIZATION'), 6)));
13231324
}
13241325
}
13251326

@@ -1343,8 +1344,8 @@ protected function _prepare_digest_auth()
13431344
// because the PHP ISAPI module in IIS acts different from CGI
13441345
if ($this->input->server('PHP_AUTH_DIGEST')) {
13451346
$digest_string = $this->input->server('PHP_AUTH_DIGEST');
1346-
} elseif ($this->input->server('HTTP_AUTHorIZATION')) {
1347-
$digest_string = $this->input->server('HTTP_AUTHorIZATION');
1347+
} elseif ($this->input->server('HTTP_AUTHORIZATION')) {
1348+
$digest_string = $this->input->server('HTTP_AUTHORIZATION');
13481349
} else {
13491350
$digest_string = "";
13501351
}
@@ -1356,6 +1357,7 @@ protected function _prepare_digest_auth()
13561357
}
13571358

13581359
// We need to retrieve authentication informations from the $auth_data variable
1360+
$matches = array();
13591361
preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response)=[\'"]?([^\'",]+)@', $digest_string, $matches);
13601362
$digest = (empty($matches[1]) || empty($matches[2])) ? array() : array_combine($matches[1], $matches[2]);
13611363

@@ -1407,7 +1409,7 @@ protected function _check_whitelist_auth()
14071409
}
14081410

14091411
if ( ! in_array($this->input->ip_address(), $whitelist)) {
1410-
$this->response(array('status' => false, 'error' => 'Not authorized'), 401);
1412+
$this->response(array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'IP not authorized'), 401);
14111413
}
14121414
}
14131415

@@ -1424,7 +1426,7 @@ protected function _force_login($nonce = '')
14241426
header('WWW-Authenticate: Digest realm="'.$this->config->item('rest_realm').'", qop="auth", nonce="'.$nonce.'", opaque="'.md5($this->config->item('rest_realm')).'"');
14251427
}
14261428

1427-
$this->response(array('status' => false, 'error' => 'Not authorized'), 401);
1429+
$this->response(array(config_item('rest_status_field_name') => false, config_item('rest_message_field_name') => 'Not authorized'), 401);
14281430
}
14291431

14301432
/**

0 commit comments

Comments
 (0)