Skip to content

Commit 7ab3116

Browse files
Merge pull request #523 from ivantcholakov/master
Summary updates and adding simple pages for quick test
2 parents 4d1a0cc + c82d05e commit 7ab3116

14 files changed

+461
-97
lines changed

application/config/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>403 Forbidden</title>
5+
</head>
6+
<body>
7+
8+
<p>Directory access is forbidden.</p>
9+
10+
</body>
11+
</html>

application/config/rest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@
160160
|
161161
| example:
162162
|
163-
| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none';
164-
| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none';
165-
| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none';
163+
| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none';
164+
| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none';
165+
| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none';
166166
*/
167167

168168
// ---Uncomment list line for the wildard unit test

application/config/routes.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
/*
5+
| -------------------------------------------------------------------------
6+
| URI ROUTING
7+
| -------------------------------------------------------------------------
8+
| This file lets you re-map URI requests to specific controller functions.
9+
|
10+
| Typically there is a one-to-one relationship between a URL string
11+
| and its corresponding controller class/method. The segments in a
12+
| URL normally follow this pattern:
13+
|
14+
| example.com/class/method/id/
15+
|
16+
| In some instances, however, you may want to remap this relationship
17+
| so that a different class/function is called than the one
18+
| corresponding to the URL.
19+
|
20+
| Please see the user guide for complete details:
21+
|
22+
| http://codeigniter.com/user_guide/general/routing.html
23+
|
24+
| -------------------------------------------------------------------------
25+
| RESERVED ROUTES
26+
| -------------------------------------------------------------------------
27+
|
28+
| There are three reserved routes:
29+
|
30+
| $route['default_controller'] = 'welcome';
31+
|
32+
| This route indicates which controller class should be loaded if the
33+
| URI contains no data. In the above example, the "welcome" class
34+
| would be loaded.
35+
|
36+
| $route['404_override'] = 'errors/page_missing';
37+
|
38+
| This route will tell the Router which controller/method to use if those
39+
| provided in the URL cannot be matched to a valid route.
40+
|
41+
| $route['translate_uri_dashes'] = FALSE;
42+
|
43+
| This is not exactly a route, but allows you to automatically route
44+
| controller and method names that contain dashes. '-' isn't a valid
45+
| class or method name character, so it requires translation.
46+
| When you set this option to TRUE, it will replace ALL dashes in the
47+
| controller and method URI segments.
48+
|
49+
| Examples: my-controller/index -> 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
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Rest_server extends CI_Controller {
5+
6+
public function index()
7+
{
8+
$this->load->helper('url');
9+
10+
$this->load->view('rest_server');
11+
}
12+
}

application/controllers/Welcome.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Welcome extends CI_Controller {
5+
6+
/**
7+
* Index Page for this controller.
8+
*
9+
* Maps to the following URL
10+
* http://example.com/index.php/welcome
11+
* - or -
12+
* http://example.com/index.php/welcome/index
13+
* - or -
14+
* Since this controller is set as the default controller in
15+
* config/routes.php, it's displayed at http://example.com/
16+
*
17+
* So any other public methods not prefixed with an underscore will
18+
* map to /index.php/welcome/<method_name>
19+
* @see http://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+
}

application/controllers/api/Example.php

+29-42
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,19 @@ function __construct()
3030
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
3131
}
3232

33-
public function users_get($id_param = NULL)
33+
public function users_get()
3434
{
3535
// Users from a data store e.g. database
36-
// $user = $this->some_model->getSomething($id);
3736
$users = [
38-
1 => ['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
39-
2 => ['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
40-
3 => ['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
37+
['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
38+
['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
39+
['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
4140
];
4241

43-
// Get the id parameter value
4442
$id = $this->get('id');
4543

46-
// If NULL, then check the id passed as users/:id
47-
if ($id === NULL)
48-
{
49-
$id = $id_param;
50-
}
44+
// If the id parameter doesn't exist return all the users
5145

52-
// If the id parameter and query parameter don't exist, return all users instead
5346
if ($id === NULL)
5447
{
5548
// Check if the users data store contains users (in case the database result returns NULL)
@@ -66,28 +59,36 @@ public function users_get($id_param = NULL)
6659
'error' => 'No users were found'
6760
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
6861
}
69-
7062
}
7163

72-
// Check if the id is a valid integer
73-
if (ctype_digit($id))
74-
{
75-
// Cast as an int
76-
$id = (int) $id;
77-
}
64+
// Find and return a single record for a particular user.
7865

79-
// If not a valid id
66+
$id = (int) $id;
67+
68+
// Validate the id.
8069
if ($id <= 0)
8170
{
82-
// Set the response and exit
71+
// Invalid id, set the response and exit.
8372
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
8473
}
8574

86-
// Get the user from the array, by retrieving the id from the GET request
87-
$user = isset($users[$id]) ? $users[$id] : NULL;
75+
// Get the user from the array, using the id as key for retreival.
76+
// Usually a model is to be used for this.
77+
78+
$user = NULL;
79+
80+
if (!empty($users))
81+
{
82+
foreach ($users as $key => $value)
83+
{
84+
if (isset($value['id']) && $value['id'] === $id)
85+
{
86+
$user = $value;
87+
}
88+
}
89+
}
8890

89-
// If a user exists in the data store e.g. database
90-
if ($user)
91+
if (!empty($user))
9192
{
9293
$this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
9394
}
@@ -113,25 +114,11 @@ public function users_post()
113114
$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
114115
}
115116

116-
public function users_delete($id_param = NULL)
117+
public function users_delete()
117118
{
118-
// Get the id parameter value
119-
$id = $this->get('id');
120-
121-
// If NULL, then check the id passed as users/:id
122-
if ($id === NULL)
123-
{
124-
$id = $id_param;
125-
}
126-
127-
// Check if the id is a valid integer
128-
if (ctype_digit($id))
129-
{
130-
// Cast as an int
131-
$id = (int) $id;
132-
}
119+
$id = (int) $this->get('id');
133120

134-
// If not a valid id
121+
// Validate the id.
135122
if ($id <= 0)
136123
{
137124
// Set the response and exit

application/controllers/api/Key.php

+22-22
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class Key extends REST_Controller {
3434
public function index_put()
3535
{
3636
// Build a new key
37-
$key = self::_generate_key();
37+
$key = $this->_generate_key();
3838

3939
// If no key level provided, provide a generic key
4040
$level = $this->put('level') ? $this->put('level') : 1;
4141
$ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int) $this->put('ignore_limits') : 1;
4242

4343
// Insert the new key
44-
if (self::_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
44+
if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
4545
{
4646
$this->response([
4747
'status' => TRUE,
@@ -68,7 +68,7 @@ public function index_delete()
6868
$key = $this->delete('key');
6969

7070
// Does this key exist?
71-
if (!self::_key_exists($key))
71+
if (!$this->_key_exists($key))
7272
{
7373
// It doesn't appear the key exists
7474
$this->response([
@@ -78,7 +78,7 @@ public function index_delete()
7878
}
7979

8080
// Destroy it
81-
self::_delete_key($key);
81+
$this->_delete_key($key);
8282

8383
// Respond that the key was destroyed
8484
$this->response([
@@ -99,7 +99,7 @@ public function level_post()
9999
$new_level = $this->post('level');
100100

101101
// Does this key exist?
102-
if (!self::_key_exists($key))
102+
if (!$this->_key_exists($key))
103103
{
104104
// It doesn't appear the key exists
105105
$this->response([
@@ -109,7 +109,7 @@ public function level_post()
109109
}
110110

111111
// Update the key level
112-
if (self::_update_key($key, ['level' => $new_level]))
112+
if ($this->_update_key($key, ['level' => $new_level]))
113113
{
114114
$this->response([
115115
'status' => TRUE,
@@ -126,7 +126,7 @@ public function level_post()
126126
}
127127

128128
/**
129-
* Change the level
129+
* Suspend a key
130130
*
131131
* @access public
132132
* @return void
@@ -136,7 +136,7 @@ public function suspend_post()
136136
$key = $this->post('key');
137137

138138
// Does this key exist?
139-
if (!self::_key_exists($key))
139+
if (!$this->_key_exists($key))
140140
{
141141
// It doesn't appear the key exists
142142
$this->response([
@@ -146,7 +146,7 @@ public function suspend_post()
146146
}
147147

148148
// Update the key level
149-
if (self::_update_key($key, ['level' => 0]))
149+
if ($this->_update_key($key, ['level' => 0]))
150150
{
151151
$this->response([
152152
'status' => TRUE,
@@ -163,15 +163,15 @@ public function suspend_post()
163163
}
164164

165165
/**
166-
* Remove a key from the database to stop it working
166+
* Regenerate a key
167167
*
168168
* @access public
169169
* @return void
170170
*/
171171
public function regenerate_post()
172172
{
173173
$old_key = $this->post('key');
174-
$key_details = self::_get_key($old_key);
174+
$key_details = $this->_get_key($old_key);
175175

176176
// Does this key exist?
177177
if (!$key_details)
@@ -184,13 +184,13 @@ public function regenerate_post()
184184
}
185185

186186
// Build a new key
187-
$new_key = self::_generate_key();
187+
$new_key = $this->_generate_key();
188188

189189
// Insert the new key
190-
if (self::_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits]))
190+
if ($this->_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits]))
191191
{
192192
// Suspend old key
193-
self::_update_key($old_key, ['level' => 0]);
193+
$this->_update_key($old_key, ['level' => 0]);
194194

195195
$this->response([
196196
'status' => TRUE,
@@ -218,12 +218,12 @@ private function _generate_key()
218218
// If an error occurred, then fall back to the previous method
219219
if ($salt === FALSE)
220220
{
221-
$salt = hash('sha256', time() . mt_rand());
221+
$salt = hash('sha256', time() . mt_rand());
222222
}
223+
223224
$new_key = substr($salt, 0, config_item('rest_key_length'));
224225
}
225-
while (self::_key_exists($new_key));
226-
// Already in the DB? Fail. Try again
226+
while ($this->_key_exists($new_key));
227227

228228
return $new_key;
229229
}
@@ -233,16 +233,16 @@ private function _generate_key()
233233
private function _get_key($key)
234234
{
235235
return $this->db
236-
->where(config_item('rest_key_column'), $key)
237-
->get(config_item('rest_keys_table'))
238-
->row();
236+
->where(config_item('rest_key_column'), $key)
237+
->get(config_item('rest_keys_table'))
238+
->row();
239239
}
240240

241241
private function _key_exists($key)
242242
{
243243
return $this->db
244-
->where(config_item('rest_key_column'), $key)
245-
->count_all_results(config_item('rest_keys_table')) > 0;
244+
->where(config_item('rest_key_column'), $key)
245+
->count_all_results(config_item('rest_keys_table')) > 0;
246246
}
247247

248248
private function _insert_key($key, $data)

0 commit comments

Comments
 (0)