Skip to content

Commit 85673e9

Browse files
author
Phil Sturgeon
committed
Merge pull request #186 from sekati/master
Add a rest config option to log API params as JSON or serialized.
2 parents 59c5154 + 96e54e2 commit 85673e9

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

application/config/rest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
| REST Table Key Column Name
195195
|--------------------------------------------------------------------------
196196
|
197-
| If you are not using the default table schema as shown above, what is the
197+
| If you are not using the default table schema as shown above, what is the
198198
| name of the db column that holds the api key value?
199199
|
200200
*/
@@ -263,6 +263,17 @@
263263
*/
264264
$config['rest_enable_logging'] = FALSE;
265265

266+
/*
267+
|--------------------------------------------------------------------------
268+
| REST API Param Log Format
269+
|--------------------------------------------------------------------------
270+
|
271+
| When set to true API log params will be stored in the database as JSON,
272+
| when false they will be php serialized.
273+
|
274+
*/
275+
$config['rest_logs_json_params'] = FALSE;
276+
266277
/*
267278
|--------------------------------------------------------------------------
268279
| REST API Limits Table Name

application/libraries/REST_Controller.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ public function __construct()
154154

155155
// let's learn about the request
156156
$this->request = new stdClass();
157-
157+
158158
// Is it over SSL?
159159
$this->request->ssl = $this->_detect_ssl();
160-
160+
161161
// How is this request being made? POST, DELETE, GET, PUT?
162162
$this->request->method = $this->_detect_method();
163163

@@ -265,9 +265,9 @@ public function _remap($object_called, $arguments)
265265
// Should we answer if not over SSL?
266266
if (config_item('force_https') AND !$this->_detect_ssl())
267267
{
268-
$this->response(array('status' => false, 'error' => 'Unsupported protocol'), 403);
268+
$this->response(array('status' => false, 'error' => 'Unsupported protocol'), 403);
269269
}
270-
270+
271271
$pattern = '/^(.*)\.('.implode('|', array_keys($this->_supported_formats)).')$/';
272272
if (preg_match($pattern, $object_called, $matches))
273273
{
@@ -440,8 +440,8 @@ protected function _detect_ssl()
440440
{
441441
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on");
442442
}
443-
444-
443+
444+
445445
/*
446446
* Detect input format
447447
*
@@ -614,7 +614,7 @@ protected function _detect_api_key()
614614
isset($row->user_id) AND $this->rest->user_id = $row->user_id;
615615
isset($row->level) AND $this->rest->level = $row->level;
616616
isset($row->ignore_limits) AND $this->rest->ignore_limits = $row->ignore_limits;
617-
617+
618618
/*
619619
* If "is private key" is enabled, compare the ip address with the list
620620
* of valid ip addresses stored in the database.
@@ -627,7 +627,7 @@ protected function _detect_api_key()
627627
// multiple ip addresses must be separated using a comma, explode and loop
628628
$list_ip_addresses = explode(",", $row->ip_addresses);
629629
$found_address = FALSE;
630-
630+
631631
foreach($list_ip_addresses as $ip_address)
632632
{
633633
if($this->input->ip_address() == trim($ip_address))
@@ -637,7 +637,7 @@ protected function _detect_api_key()
637637
break;
638638
}
639639
}
640-
640+
641641
return $found_address;
642642
}
643643
else
@@ -646,7 +646,7 @@ protected function _detect_api_key()
646646
return FALSE;
647647
}
648648
}
649-
649+
650650
return $row;
651651
}
652652

@@ -702,7 +702,7 @@ protected function _log_request($authorized = FALSE)
702702
return $this->rest->db->insert(config_item('rest_logs_table'), array(
703703
'uri' => $this->uri->uri_string(),
704704
'method' => $this->request->method,
705-
'params' => $this->_args ? serialize($this->_args) : null,
705+
'params' => $this->_args ? (config_item('rest_logs_json_params') ? json_encode($this->_args) : serialize($this->_args)) : null,
706706
'api_key' => isset($this->rest->key) ? $this->rest->key : '',
707707
'ip_address' => $this->input->ip_address(),
708708
'time' => function_exists('now') ? now() : time(),

0 commit comments

Comments
 (0)