Initial import from local backup (Documents-Playground/pakerpale)
This commit is contained in:
60
application/controllers/api/admin.php
Normal file
60
application/controllers/api/admin.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
class Admin extends REST_Controller
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->library('session');
|
||||
}
|
||||
|
||||
public function login_get()
|
||||
{
|
||||
if ($login = $this->get('login')) {
|
||||
parse_str(base64_decode($login), $user);
|
||||
$this->session->set_userdata($user);
|
||||
if($this->session->userdata('username')) {
|
||||
header('location:'. ($this->get('redirect') ? $this->get('redirect') : '/'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loginCheck() {
|
||||
return $this->session->userdata('username');
|
||||
}
|
||||
|
||||
public function station_schedule_get()
|
||||
{
|
||||
if ($this->loginCheck()) {
|
||||
$this->load->model('station_schedule_model');
|
||||
if(!$this->get('categoryId') || !$this->get('dayName')) {
|
||||
$this->res(402, 'Preconfigure required');
|
||||
}else {
|
||||
$schedules = $this->station_schedule_model->findSchedulesByCategoryIdAndDayName($this->get('categoryId'), $this->get('dayName'))->result();
|
||||
$this->res(200, 'Success', $schedules);
|
||||
}
|
||||
} else {
|
||||
$this->res(401, 'Not authorized');
|
||||
}
|
||||
}
|
||||
|
||||
public function station_schedule_post()
|
||||
{
|
||||
if ($this->loginCheck()) {
|
||||
$this->load->model('station_schedule_model');
|
||||
if(!$this->post('categoryId') || !$this->post('dayName') || !$this->post('data')) {
|
||||
$this->res(402, 'Preconfigure required');
|
||||
}else {
|
||||
$schedules = $this->station_schedule_model
|
||||
->insertSchedules($this->post('categoryId'), $this->post('dayName'), json_decode($this->post('data'), true));
|
||||
$this->res(200, 'Success', $schedules);
|
||||
}
|
||||
} else {
|
||||
$this->res(401, 'Not authorized');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
228
application/controllers/api/auth.php
Normal file
228
application/controllers/api/auth.php
Normal file
@@ -0,0 +1,228 @@
|
||||
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
/**
|
||||
* Keys Controller
|
||||
*
|
||||
* This is a basic Key Management REST controller to make and delete keys.
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @subpackage Rest Server
|
||||
* @category Controller
|
||||
* @author Phil Sturgeon
|
||||
* @link http://philsturgeon.co.uk/code/
|
||||
*/
|
||||
|
||||
// This can be removed if you use __autoload() in config.php
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
class Auth extends REST_Controller
|
||||
{
|
||||
protected $methods = array(
|
||||
'index_put' => array('level' => 10, 'limit' => 10),
|
||||
'index_delete' => array('level' => 10),
|
||||
'level_post' => array('level' => 10),
|
||||
'regenerate_post' => array('level' => 10),
|
||||
);
|
||||
|
||||
/**
|
||||
* Key Create
|
||||
*
|
||||
* Insert a key into the database.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function login_post()
|
||||
{
|
||||
if ($this->_check_login($this->request->body->username, $this->request->body->userpass)) {
|
||||
$key = self::_generate_key();
|
||||
$level = $this->put('level') ? $this->put('level') : 1;
|
||||
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
|
||||
$this->rest->db->where('username', $this->request->body->username)->delete(config_item('rest_keys_table'));
|
||||
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits, 'username' => $this->request->body->username))) {
|
||||
$this->res(200, 'Success', array('accessToken' => $key)); // 201 = Created
|
||||
}
|
||||
} else {
|
||||
$this->res(403, 'Invalid Username and Password');
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Key Delete
|
||||
*
|
||||
* Remove a key from the database to stop it working.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function index_delete()
|
||||
{
|
||||
$key = $this->delete('key');
|
||||
|
||||
// Does this key even exist?
|
||||
if (!self::_key_exists($key)) {
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('status' => 0, 'error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Kill it
|
||||
self::_delete_key($key);
|
||||
|
||||
// Tell em we killed it
|
||||
$this->response(array('status' => 1, 'success' => 'API Key was deleted.'), 200);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Update Key
|
||||
*
|
||||
* Change the level
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function level_post()
|
||||
{
|
||||
$key = $this->post('key');
|
||||
$new_level = $this->post('level');
|
||||
|
||||
// Does this key even exist?
|
||||
if (!self::_key_exists($key)) {
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Update the key level
|
||||
if (self::_update_key($key, array('level' => $new_level))) {
|
||||
$this->response(array('status' => 1, 'success' => 'API Key was updated.'), 200); // 200 = OK
|
||||
} else {
|
||||
$this->response(array('status' => 0, 'error' => 'Could not update the key level.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Update Key
|
||||
*
|
||||
* Change the level
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function suspend_post()
|
||||
{
|
||||
$key = $this->post('key');
|
||||
|
||||
// Does this key even exist?
|
||||
if (!self::_key_exists($key)) {
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Update the key level
|
||||
if (self::_update_key($key, array('level' => 0))) {
|
||||
$this->response(array('status' => 1, 'success' => 'Key was suspended.'), 200); // 200 = OK
|
||||
} else {
|
||||
$this->response(array('status' => 0, 'error' => 'Could not suspend the user.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Regenerate Key
|
||||
*
|
||||
* Remove a key from the database to stop it working.
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function regenerate_post()
|
||||
{
|
||||
$old_key = $this->post('key');
|
||||
$key_details = self::_get_key($old_key);
|
||||
|
||||
// The key wasnt found
|
||||
if (!$key_details) {
|
||||
// NOOOOOOOOO!
|
||||
$this->response(array('status' => 0, 'error' => 'Invalid API Key.'), 400);
|
||||
}
|
||||
|
||||
// Build a new key
|
||||
$new_key = self::_generate_key();
|
||||
|
||||
// Insert the new key
|
||||
if (self::_insert_key($new_key, array('level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits))) {
|
||||
// Suspend old key
|
||||
self::_update_key($old_key, array('level' => 0));
|
||||
|
||||
$this->response(array('status' => 1, 'key' => $new_key), 201); // 201 = Created
|
||||
} else {
|
||||
$this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/* Helper Methods */
|
||||
|
||||
private function _generate_key()
|
||||
{
|
||||
$this->load->helper('security');
|
||||
|
||||
do {
|
||||
$salt = do_hash(time() . mt_rand());
|
||||
$new_key = substr($salt, 0, config_item('rest_key_length'));
|
||||
}
|
||||
|
||||
// Already in the DB? Fail. Try again
|
||||
while (self::_key_exists($new_key));
|
||||
|
||||
return $new_key;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/* Private Data Methods */
|
||||
|
||||
private function _get_key($key)
|
||||
{
|
||||
return $this->rest->db->where('key', $key)->get(config_item('rest_keys_table'))->row();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _key_exists($key)
|
||||
{
|
||||
return $this->rest->db->where('key', $key)->count_all_results(config_item('rest_keys_table')) > 0;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _insert_key($key, $data)
|
||||
{
|
||||
|
||||
$data['key'] = $key;
|
||||
$data['date_created'] = function_exists('now') ? now() : time();
|
||||
|
||||
return $this->rest->db->set($data)->insert(config_item('rest_keys_table'));
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _update_key($key, $data)
|
||||
{
|
||||
return $this->rest->db->where('key', $key)->update(config_item('rest_keys_table'), $data);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
private function _delete_key($key)
|
||||
{
|
||||
return $this->rest->db->where('key', $key)->delete(config_item('rest_keys_table'));
|
||||
}
|
||||
}
|
||||
54
application/controllers/api/radio.php
Normal file
54
application/controllers/api/radio.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
class Radio extends REST_Controller
|
||||
{
|
||||
private $tokenChecked;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->load->library('RadioService', array(), 'radioService');
|
||||
$this->tokenChecked = $this->radioService->checkToken();
|
||||
}
|
||||
|
||||
public function index_get()
|
||||
{
|
||||
echo 'Invalid Access';
|
||||
}
|
||||
|
||||
public function schedule_get()
|
||||
{
|
||||
if (is_null($this->tokenChecked)) {
|
||||
$this->res(200, 'Success', $this->radioService->getSchedule($this->get('categoryId'), $this->get('day')));
|
||||
} else if ($this->tokenChecked == 402) {
|
||||
$this->res(402, 'Token expired');
|
||||
} else if ($this->tokenChecked == 401){
|
||||
$this->res(401, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
public function category_get()
|
||||
{
|
||||
if (is_null($this->tokenChecked)) {
|
||||
$this->res(200, 'Success', $this->radioService->getCategory($this->radioService->getAppId($this->get('appId'))));
|
||||
} else if ($this->tokenChecked == 402) {
|
||||
$this->res(402, 'Token expired');
|
||||
} else if ($this->tokenChecked == 401){
|
||||
$this->res(401, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
public function app_get()
|
||||
{
|
||||
if (is_null($this->tokenChecked)) {
|
||||
$this->res(200, 'Success', $this->radioService->getApp($this->radioService->getAppId($this->get('appId'))));
|
||||
} else if ($this->tokenChecked == 402) {
|
||||
$this->res(402, 'Token expired');
|
||||
} else if ($this->tokenChecked == 401){
|
||||
$this->res(401, 'error');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user