Skip to content

Commit

Permalink
Merge pull request chriskacerguis#186 from sekati/master
Browse files Browse the repository at this point in the history
Add a rest config option to log API params as JSON or serialized.
  • Loading branch information
Phil Sturgeon committed Feb 11, 2013
2 parents 59c5154 + 96e54e2 commit 85673e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
13 changes: 12 additions & 1 deletion application/config/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
| REST Table Key Column Name
|--------------------------------------------------------------------------
|
| If you are not using the default table schema as shown above, what is the
| If you are not using the default table schema as shown above, what is the
| name of the db column that holds the api key value?
|
*/
Expand Down Expand Up @@ -263,6 +263,17 @@
*/
$config['rest_enable_logging'] = FALSE;

/*
|--------------------------------------------------------------------------
| REST API Param Log Format
|--------------------------------------------------------------------------
|
| When set to true API log params will be stored in the database as JSON,
| when false they will be php serialized.
|
*/
$config['rest_logs_json_params'] = FALSE;

/*
|--------------------------------------------------------------------------
| REST API Limits Table Name
Expand Down
22 changes: 11 additions & 11 deletions application/libraries/REST_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ public function __construct()

// let's learn about the request
$this->request = new stdClass();

// Is it over SSL?
$this->request->ssl = $this->_detect_ssl();

// How is this request being made? POST, DELETE, GET, PUT?
$this->request->method = $this->_detect_method();

Expand Down Expand Up @@ -265,9 +265,9 @@ public function _remap($object_called, $arguments)
// Should we answer if not over SSL?
if (config_item('force_https') AND !$this->_detect_ssl())
{
$this->response(array('status' => false, 'error' => 'Unsupported protocol'), 403);
$this->response(array('status' => false, 'error' => 'Unsupported protocol'), 403);
}

$pattern = '/^(.*)\.('.implode('|', array_keys($this->_supported_formats)).')$/';
if (preg_match($pattern, $object_called, $matches))
{
Expand Down Expand Up @@ -440,8 +440,8 @@ protected function _detect_ssl()
{
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on");
}


/*
* Detect input format
*
Expand Down Expand Up @@ -614,7 +614,7 @@ protected function _detect_api_key()
isset($row->user_id) AND $this->rest->user_id = $row->user_id;
isset($row->level) AND $this->rest->level = $row->level;
isset($row->ignore_limits) AND $this->rest->ignore_limits = $row->ignore_limits;

/*
* If "is private key" is enabled, compare the ip address with the list
* of valid ip addresses stored in the database.
Expand All @@ -627,7 +627,7 @@ protected function _detect_api_key()
// multiple ip addresses must be separated using a comma, explode and loop
$list_ip_addresses = explode(",", $row->ip_addresses);
$found_address = FALSE;

foreach($list_ip_addresses as $ip_address)
{
if($this->input->ip_address() == trim($ip_address))
Expand All @@ -637,7 +637,7 @@ protected function _detect_api_key()
break;
}
}

return $found_address;
}
else
Expand All @@ -646,7 +646,7 @@ protected function _detect_api_key()
return FALSE;
}
}

return $row;
}

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

0 comments on commit 85673e9

Please sign in to comment.