Skip to content

Commit

Permalink
Merge pull request #523 from ivantcholakov/master
Browse files Browse the repository at this point in the history
Summary updates and adding simple pages for quick test
  • Loading branch information
chriskacerguis committed Jul 24, 2015
2 parents 4d1a0cc + c82d05e commit 7ab3116
Show file tree
Hide file tree
Showing 14 changed files with 461 additions and 97 deletions.
11 changes: 11 additions & 0 deletions application/config/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>

<p>Directory access is forbidden.</p>

</body>
</html>
6 changes: 3 additions & 3 deletions application/config/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@
|
| example:
|
| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none';
| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none';
| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none';
| $config['auth_override_class_method_http']['deals']['view']['get'] = 'none';
| $config['auth_override_class_method_http']['deals']['insert']['post'] = 'none';
| $config['auth_override_class_method_http']['deals']['*']['options'] = 'none';
*/

// ---Uncomment list line for the wildard unit test
Expand Down
62 changes: 62 additions & 0 deletions application/config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| example.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://codeigniter.com/user_guide/general/routing.html
|
| -------------------------------------------------------------------------
| RESERVED ROUTES
| -------------------------------------------------------------------------
|
| There are three reserved routes:
|
| $route['default_controller'] = 'welcome';
|
| This route indicates which controller class should be loaded if the
| URI contains no data. In the above example, the "welcome" class
| would be loaded.
|
| $route['404_override'] = 'errors/page_missing';
|
| This route will tell the Router which controller/method to use if those
| provided in the URL cannot be matched to a valid route.
|
| $route['translate_uri_dashes'] = FALSE;
|
| This is not exactly a route, but allows you to automatically route
| controller and method names that contain dashes. '-' isn't a valid
| class or method name character, so it requires translation.
| When you set this option to TRUE, it will replace ALL dashes in the
| controller and method URI segments.
|
| Examples: my-controller/index -> my_controller/index
| my-controller/my-method -> my_controller/my_method
*/
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

/*
| -------------------------------------------------------------------------
| Sample REST API Routes
| -------------------------------------------------------------------------
*/
$route['api/example/users/(:num)'] = 'api/example/users/id/$1'; // Example 4
$route['api/example/users/(:num)(\.)([a-zA-Z0-9_-]+)(.*)'] = 'api/example/users/id/$1/format/$3$4'; // Example 8
12 changes: 12 additions & 0 deletions application/controllers/Rest_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Rest_server extends CI_Controller {

public function index()
{
$this->load->helper('url');

$this->load->view('rest_server');
}
}
27 changes: 27 additions & 0 deletions application/controllers/Welcome.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->helper('url');

$this->load->view('welcome_message');
}
}
71 changes: 29 additions & 42 deletions application/controllers/api/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,19 @@ function __construct()
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
}

public function users_get($id_param = NULL)
public function users_get()
{
// Users from a data store e.g. database
// $user = $this->some_model->getSomething($id);
$users = [
1 => ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
2 => ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
3 => ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
];

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

// If NULL, then check the id passed as users/:id
if ($id === NULL)
{
$id = $id_param;
}
// If the id parameter doesn't exist return all the users

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

}

// Check if the id is a valid integer
if (ctype_digit($id))
{
// Cast as an int
$id = (int) $id;
}
// Find and return a single record for a particular user.

// If not a valid id
$id = (int) $id;

// Validate the id.
if ($id <= 0)
{
// Set the response and exit
// Invalid id, set the response and exit.
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}

// Get the user from the array, by retrieving the id from the GET request
$user = isset($users[$id]) ? $users[$id] : NULL;
// Get the user from the array, using the id as key for retreival.
// Usually a model is to be used for this.

$user = NULL;

if (!empty($users))
{
foreach ($users as $key => $value)
{
if (isset($value['id']) && $value['id'] === $id)
{
$user = $value;
}
}
}

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

public function users_delete($id_param = NULL)
public function users_delete()
{
// Get the id parameter value
$id = $this->get('id');

// If NULL, then check the id passed as users/:id
if ($id === NULL)
{
$id = $id_param;
}

// Check if the id is a valid integer
if (ctype_digit($id))
{
// Cast as an int
$id = (int) $id;
}
$id = (int) $this->get('id');

// If not a valid id
// Validate the id.
if ($id <= 0)
{
// Set the response and exit
Expand Down
44 changes: 22 additions & 22 deletions application/controllers/api/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class Key extends REST_Controller {
public function index_put()
{
// Build a new key
$key = self::_generate_key();
$key = $this->_generate_key();

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

// Insert the new key
if (self::_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
{
$this->response([
'status' => TRUE,
Expand All @@ -68,7 +68,7 @@ public function index_delete()
$key = $this->delete('key');

// Does this key exist?
if (!self::_key_exists($key))
if (!$this->_key_exists($key))
{
// It doesn't appear the key exists
$this->response([
Expand All @@ -78,7 +78,7 @@ public function index_delete()
}

// Destroy it
self::_delete_key($key);
$this->_delete_key($key);

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

// Does this key exist?
if (!self::_key_exists($key))
if (!$this->_key_exists($key))
{
// It doesn't appear the key exists
$this->response([
Expand All @@ -109,7 +109,7 @@ public function level_post()
}

// Update the key level
if (self::_update_key($key, ['level' => $new_level]))
if ($this->_update_key($key, ['level' => $new_level]))
{
$this->response([
'status' => TRUE,
Expand All @@ -126,7 +126,7 @@ public function level_post()
}

/**
* Change the level
* Suspend a key
*
* @access public
* @return void
Expand All @@ -136,7 +136,7 @@ public function suspend_post()
$key = $this->post('key');

// Does this key exist?
if (!self::_key_exists($key))
if (!$this->_key_exists($key))
{
// It doesn't appear the key exists
$this->response([
Expand All @@ -146,7 +146,7 @@ public function suspend_post()
}

// Update the key level
if (self::_update_key($key, ['level' => 0]))
if ($this->_update_key($key, ['level' => 0]))
{
$this->response([
'status' => TRUE,
Expand All @@ -163,15 +163,15 @@ public function suspend_post()
}

/**
* Remove a key from the database to stop it working
* Regenerate a key
*
* @access public
* @return void
*/
public function regenerate_post()
{
$old_key = $this->post('key');
$key_details = self::_get_key($old_key);
$key_details = $this->_get_key($old_key);

// Does this key exist?
if (!$key_details)
Expand All @@ -184,13 +184,13 @@ public function regenerate_post()
}

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

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

$this->response([
'status' => TRUE,
Expand Down Expand Up @@ -218,12 +218,12 @@ private function _generate_key()
// If an error occurred, then fall back to the previous method
if ($salt === FALSE)
{
$salt = hash('sha256', time() . mt_rand());
$salt = hash('sha256', time() . mt_rand());
}

$new_key = substr($salt, 0, config_item('rest_key_length'));
}
while (self::_key_exists($new_key));
// Already in the DB? Fail. Try again
while ($this->_key_exists($new_key));

return $new_key;
}
Expand All @@ -233,16 +233,16 @@ private function _generate_key()
private function _get_key($key)
{
return $this->db
->where(config_item('rest_key_column'), $key)
->get(config_item('rest_keys_table'))
->row();
->where(config_item('rest_key_column'), $key)
->get(config_item('rest_keys_table'))
->row();
}

private function _key_exists($key)
{
return $this->db
->where(config_item('rest_key_column'), $key)
->count_all_results(config_item('rest_keys_table')) > 0;
->where(config_item('rest_key_column'), $key)
->count_all_results(config_item('rest_keys_table')) > 0;
}

private function _insert_key($key, $data)
Expand Down
Loading

0 comments on commit 7ab3116

Please sign in to comment.