Skip to content

Commit 0c01bb6

Browse files
author
Phil Sturgeon
committed
Merge branch 'master' of github.com:philsturgeon/codeigniter-restserver
2 parents 9f42f82 + 3eed558 commit 0c01bb6

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

application/config/rest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@
6161
*/
6262
$config['rest_database_group'] = 'default';
6363

64+
/*
65+
|--------------------------------------------------------------------------
66+
| REST API Keys Table Name
67+
|--------------------------------------------------------------------------
68+
|
69+
| The table name in your database that stores API Keys.
70+
|
71+
| 'keys'
72+
|
73+
*/
74+
$config['rest_keys_table'] = 'keys';
75+
6476
/*
6577
|--------------------------------------------------------------------------
6678
| REST Enable Keys
@@ -98,17 +110,28 @@
98110

99111
/*
100112
|--------------------------------------------------------------------------
101-
| REST Key Length
113+
| REST API Key Variable
102114
|--------------------------------------------------------------------------
103115
|
104-
| How long should created keys be? Double check this in your db schema.
116+
| Which variable will provide us the API Key
105117
|
106-
| Default: 32
107-
| Max: 40
118+
| Default: X-API-KEY
108119
|
109120
*/
110121
$config['rest_key_name'] = 'X-API-KEY';
111122

123+
/*
124+
|--------------------------------------------------------------------------
125+
| REST API Logs Table Name
126+
|--------------------------------------------------------------------------
127+
|
128+
| The table name in your database that stores logs.
129+
|
130+
| 'logs'
131+
|
132+
*/
133+
$config['rest_logs_table'] = 'logs';
134+
112135
/*
113136
|--------------------------------------------------------------------------
114137
| REST Enable Logging
@@ -135,6 +158,18 @@
135158
*/
136159
$config['rest_enable_logging'] = FALSE;
137160

161+
/*
162+
|--------------------------------------------------------------------------
163+
| REST API Limits Table Name
164+
|--------------------------------------------------------------------------
165+
|
166+
| The table name in your database that stores limits.
167+
|
168+
| 'logs'
169+
|
170+
*/
171+
$config['rest_limits_table'] = 'limits';
172+
138173
/*
139174
|--------------------------------------------------------------------------
140175
| REST Enable Limits

application/libraries/REST_Controller.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class REST_Controller extends Controller
2222
'xml' => 'application/xml',
2323
'rawxml' => 'application/xml',
2424
'json' => 'application/json',
25+
'jsonp' => 'application/json',
2526
'serialize' => 'application/vnd.php.serialized',
2627
'php' => 'text/plain',
2728
'html' => 'text/html',
@@ -95,17 +96,17 @@ function __construct()
9596
// Which format should the data be returned in?
9697
$this->request->lang = $this->_detect_lang();
9798

98-
// Load DB if its enabled
99-
if (config_item('rest_database_group') AND (config_item('rest_enable_keys') OR config_item('rest_enable_logging')))
100-
{
101-
$this->rest->db = $this->load->database(config_item('rest_database_group'), TRUE);
102-
}
103-
104-
// Checking for keys? GET TO WORK!
105-
if (config_item('rest_enable_keys'))
106-
{
107-
$this->_allow = $this->_detect_api_key();
108-
}
99+
// Load DB if its enabled
100+
if (config_item('rest_database_group') AND (config_item('rest_enable_keys') OR config_item('rest_enable_logging')))
101+
{
102+
$this->rest->db = $this->load->database(config_item('rest_database_group'), TRUE);
103+
}
104+
105+
// Checking for keys? GET TO WORK!
106+
if (config_item('rest_enable_keys'))
107+
{
108+
$this->_allow = $this->_detect_api_key();
109+
}
109110
}
110111

111112
/*
@@ -319,7 +320,7 @@ private function _detect_api_key()
319320
// Find the key from server or arguments
320321
if ($key = isset($this->_args['API-Key']) ? $this->_args['API-Key'] : $this->input->server($key_name))
321322
{
322-
if ( ! $row = $this->rest->db->where('key', $key)->get('keys')->row())
323+
if ( ! $row = $this->rest->db->where('key', $key)->get(config_item('rest_keys_table'))->row())
323324
{
324325
return FALSE;
325326
}
@@ -377,7 +378,7 @@ private function _detect_lang()
377378
*/
378379
private function _log_request($authorized = FALSE)
379380
{
380-
return $this->rest->db->insert('logs', array(
381+
return $this->rest->db->insert(config_item('rest_logs_table'), array(
381382
'uri' => $this->uri->uri_string(),
382383
'method' => $this->request->method,
383384
'params' => serialize($this->_args),
@@ -410,7 +411,7 @@ private function _check_limit($controller_method)
410411
$result = $this->rest->db
411412
->where('uri', $this->uri->uri_string())
412413
->where('api_key', $this->rest->key)
413-
->get('limits')
414+
->get(config_item('rest_limits_table'))
414415
->row();
415416

416417
// No calls yet, or been an hour since they called
@@ -438,7 +439,7 @@ private function _check_limit($controller_method)
438439
->where('uri', $this->uri->uri_string())
439440
->where('api_key', $this->rest->key)
440441
->set('count', 'count + 1', FALSE)
441-
->update('limits');
442+
->update(config_item('limits'));
442443
}
443444

444445
return TRUE;
@@ -816,6 +817,12 @@ private function _format_json($data = array())
816817
{
817818
return json_encode($data);
818819
}
820+
821+
// Encode as JSONP
822+
private function _format_jsonp($data = array())
823+
{
824+
return $this->get('callback').'('.json_encode($data).')';
825+
}
819826

820827
// Encode as Serialized array
821828
private function _format_serialize($data = array())

0 commit comments

Comments
 (0)