Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php if ( ! defined('BASEPATH')) header('Location: ../index.php/error/403');
- class Admin extends Controller {
- public $title = 'Hawk Intellitrac Admin';
- /**
- * Admin Model Object
- * @var Admin_model
- */
- public $adm;
- /**
- * User Model Object
- * @var User_model
- */
- public $user;
- /**
- * Client Model Object
- * @var Client_model
- */
- public $client;
- /**
- * Device Model Object
- * @var Device_model
- */
- public $device;
- function __construct()
- {
- session_start();
- parent::__construct();
- $this->load_model('admin_model', 'adm');
- if( ! isset($_SESSION['is_logged_in']) ) $_SESSION['is_logged_in'] = FALSE;
- }
- public function index()
- {
- $this->_goto_login_page();
- /*if($_SESSION['level'] !== 1)
- {
- pageRedirect('admin/user');
- }*/
- $data = $this->_generate_data('dashboard');
- $data['include'] = 'admin/dashboard';
- $assets = $GLOBALS['config']['base_url'] . 'assets/';
- $admin = $assets . "admin";
- $data['ajs'][] = "$admin/js/app/node-ctrl.js";
- $this->load->view('admin/base', $data);
- }
- public function user()
- {
- $this->_goto_login_page();
- $data = $this->_generate_data('user');
- $data['include'] = 'admin/user';
- $this->load->view('admin/base', $data);
- }
- public function message()
- {
- $this->_goto_login_page();
- $data = $this->_generate_data('message');
- $data['include'] = 'admin/message';
- $this->load->view('admin/base', $data);
- }
- public function log()
- {
- $this->_goto_login_page();
- $data = $this->_generate_data('log');
- $data['include'] = 'admin/log';
- $this->load->view('admin/base', $data);
- }
- public function device()
- {
- $this->_goto_login_page();
- $data = $this->_generate_data('device');
- $assets = $GLOBALS['config']['base_url'] . 'assets/';
- $admin = $assets . "admin";
- $data['include'] = 'admin/device';
- // $data['ajs']['chosen'] = "$admin/js/chosen.js";
- $this->load->view('admin/base', $data);
- }
- public function admin_user()
- {
- $this->_goto_login_page();
- if($_SESSION['level'] !== 1)
- {
- pageRedirect('admin/');
- }
- $data = $this->_generate_data('admin_user');
- $data['include'] = 'admin/admin_user';
- $this->load->view('admin/base', $data);
- }
- public function profile()
- {
- $this->_goto_login_page();
- $data = $this->_generate_data('profile');
- $data['include'] = 'admin/profile';
- $this->load->view('admin/base', $data);
- }
- public function recache_pos()
- {
- $this->_goto_login_page();
- $data = $this->_generate_data('recache_pos');
- $assets = $GLOBALS['config']['base_url'] . 'assets/';
- $admin = $assets . "admin";
- $data['include'] = 'admin/recache_pos';
- $data['ajs'][] = "$admin/js/app/node-ctrl.js";
- $this->load->view('admin/base', $data);
- }
- public function monitor(){
- $this->_goto_login_page();
- $data = $this->_generate_data('monitor');
- $assets = $GLOBALS['config']['base_url'] . 'assets/';
- $admin = $assets . "admin";
- $data['include'] = 'admin/monitor';
- $this->load->view('admin/base', $data);
- $data['ajs'][] = "http://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.min.js";
- $data['ajs'][] = "$admin/js/app/monitor-ctrl.js";
- }
- private function _generate_data($page)
- {
- $assets = $GLOBALS['config']['base_url'] . 'assets/';
- $angularjs = $assets . 'vendor/angular/angular.js';
- $socketio = $assets . 'vendor/socket.io/socket.io.js';
- $favicon = $assets . 'img/favicon.ico';
- $admin = $assets . "admin";
- $jsData = array(
- 'userdata' => $this->adm->admin_detail($_SESSION['userdata']['id']),
- 'sioPort' => $GLOBALS['config']['socketio_port'],
- 'sioHttp' => $GLOBALS['config']['socketio_scheme']
- );
- $data = array(
- 'assets_path' => $admin,
- 'page_path' => $GLOBALS['config']['base_url'] . 'index.php/admin/',
- 'favicon' => $favicon,
- 'topjs' => array(
- "$admin/js/jquery-1.7.2.min.js",
- $angularjs,
- $socketio
- ),
- 'myJS' => json_encode($jsData),
- 'username' => $_SESSION['username'],
- 'level' => $_SESSION['level'],
- 'page_title' => $this->title,
- 'page' => $page,
- 'ajs' => array(
- "{$assets}js/vendor/chosen.jquery.min.js",
- "$admin/js/chosen.js",
- "$admin/js/moment.min.js",
- "$admin/js/app/app.js",
- "$admin/js/app/$page-ctrl.js",
- "$admin/js/bootstrap.js",
- "$admin/js/jquery.datetimepicker.full.min.js",
- "$assets../bower_components/ng-ckeditor/libs/ckeditor/ckeditor.js",
- "$assets../bower_components/ng-ckeditor/ng-ckeditor.min.js"
- // $assets . "js/watcher/services.js",
- // $assets . "js/watcher/filters.js",
- // $assets . "js/watcher/directives.js"
- ),
- 'acss' => array(
- // $assets . "css/vendor/bootstrap.css",
- // $assets . "css/vendor/bootstrap-responsive.css"
- "{$assets}css/vendor/chosen.css",
- "$admin/css/bootstrap.min.css",
- "$admin/css/bootstrap-responsive.min.css",
- "$admin/css/font-awesome.css",
- "$admin/css/style.css",
- "$admin/css/jquery.datetimepicker.css",
- "$admin/css/pages/dashboard.css",
- "$assets../bower_components/ng-ckeditor/ng-ckeditor.css"
- )
- );
- return $data;
- }
- public function update_user_message()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $this->load_model('user_model', 'user');
- $data = array(
- 'status' => 0,
- 'statusText' => 'Did not updated'
- );
- if($this->has_request('username'))
- {
- $username = $this->get_input('username');
- $messageId = $this->get_input('message');
- $update = $this->user->update_additional($username, 'login_message', $messageId);
- if($update)
- {
- $data = array(
- 'status' => 1,
- 'statusText' => 'Updated'
- );
- }
- }
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function update_block()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $this->load_model('user_model', 'user');
- $data = array(
- 'status' => 0,
- 'statusText' => 'Did not updated'
- );
- if($this->has_request('username'))
- {
- $username = $this->get_input('username');
- $blocked = $this->get_input('blocked');
- $update = $this->user->update_additional($username, 'blocked', $blocked);
- $active = $blocked == '1' ? 0 : 1;
- $this->user->set_active($username, $active);
- if($update)
- {
- $data = array(
- 'status' => 1,
- 'statusText' => 'Updated'
- );
- }
- }
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function get_users()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $this->load_model('user_model', 'user');
- $m_array = array(
- 'username' => 'admin',
- 'rights' => 64,
- 'clientid' => 'global'
- );
- $data = $this->user->get_users($m_array);
- foreach ($data as $k => $v) {
- $client = $this->user->complete_details($v['username']);
- $data[$k]['tradingname'] = $v['clientid'];
- $data[$k]['message'] = NULL;
- $data[$k]['blocked'] = NULL;
- if($client['status'] === 1)
- {
- $data[$k]['tradingname'] = $client['data']['client_details']['tradingname'];
- }
- $additional = $this->user->get_additional_data($v['username']);
- if(isset($additional['login_message']))
- {
- $data[$k]['message'] = $additional['login_message'];
- }
- if(isset($additional['blocked']))
- {
- $data[$k]['blocked'] = $additional['blocked'];
- }
- }
- $this->_get_last_activity($data);
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function get_admin_users()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $data = $this->adm->get_users();
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function get_company()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $this->load_model('client_model', 'client');
- $data = $this->client->get_all();
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function get_device()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $this->load_model('device_model', 'device');
- $clientid = $this->get_input("clientid");
- $data = $this->device->gw_clientid($clientid);
- $devices = array();
- if(count($data) > 0)
- {
- $devices = $data[0]['devices'];
- foreach ($devices as $k => $v)
- {
- $devices[$k]['param'] = $this->device->technical_details($v['deviceid']);
- }
- }
- header('Content-Type: application/json');
- echo json_encode($devices);
- }
- public function load_all_devices()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $this->load_model('device_model', 'device');
- $devices_data = $this->device->get_all();
- $devices = array();
- if (is_array($devices_data))
- {
- $devices = array_reduce($devices_data, function ($arr, $v) {
- foreach ($v['devices'] as $_i => $_v) {
- $arr[] = array(
- 'deviceid' => $_v['deviceid'],
- 'name' => $_v['name'],
- );
- }
- return $arr;
- }, array());
- usort($devices, function ($a, $b) {
- $n1 = $a['name'];
- $n2 = $b['name'];
- return $n1 === $n2 ? 0 : $n1 > $n2 ? 1 : -1;
- });
- }
- header('Content-Type: application/json');
- echo json_encode($devices);
- }
- public function get_message_template()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $data = $this->adm->get_login_message();
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function create_message()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $resp = array(
- 'status' => 0,
- 'statusText' => 'Message not created'
- );
- if($this->has_request('admin_id'))
- {
- $data = array(
- 'title' => $this->get_input("title"),
- 'content' => $this->get_input("content"),
- 'admin_id' => $this->get_input("admin_id")
- );
- $create = $this->adm->create_message($data);
- if($create)
- {
- $resp = array(
- 'status' => 1,
- 'statusText' => 'Created'
- );
- }
- }
- header('Content-Type: application/json');
- echo json_encode($resp);
- }
- public function update_message()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $resp = array(
- 'status' => 0,
- 'statusText' => 'Message not updated'
- );
- if($this->has_request('id'))
- {
- $data = array( 'id' => $this->get_input("id") );
- if($this->has_request( "title" )) $data['title'] = $this->get_input( "title" );
- if($this->has_request( "content" )) $data['content'] = $this->get_input( "content" );
- if($this->has_request( "default" )) $data['default'] = $this->get_input( "default" );
- $update = $this->adm->update_message($data);
- if($update)
- {
- $resp = array(
- 'status' => 1,
- 'statusText' => 'Updated'
- );
- }
- }
- header('Content-Type: application/json');
- echo json_encode($resp);
- }
- public function remove_message()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $resp = array(
- 'status' => 0,
- 'statusText' => 'Message not removed'
- );
- if($this->has_request('id'))
- {
- $id = $this->get_input("id");
- $remove = $this->adm->remove_message($id);
- if($remove)
- {
- $resp = array(
- 'status' => 1,
- 'statusText' => 'Removed'
- );
- }
- }
- header('Content-Type: application/json');
- echo json_encode($resp);
- }
- public function update_admin()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $resp = array(
- 'status' => 0,
- 'statusText' => 'User data not updated'
- );
- $update = FALSE;
- if($this->has_request('username'))
- {
- $id = $this->get_input('id');
- $data = array(
- 'username' => $this->get_input('username'),
- 'fullname' => $this->get_input('fullname'),
- 'email' => $this->get_input('email')
- );
- $update = FALSE;
- if($this->has_request('new_password'))
- {
- $data['password'] = md5($this->get_input('new_password'));
- $old_password = $this->get_input('old_password');
- $is_exist = $this->adm->is_admin_exist($data['username'], $old_password);
- if($is_exist)
- {
- $update = $this->adm->update_admin($id, $data);
- }
- else
- {
- $resp = array(
- 'status' => 2,
- 'statusText' => 'Your old password is wrong.'
- );
- }
- }
- else
- {
- $update = $this->adm->update_admin($id, $data);
- }
- }
- if($update)
- {
- $resp = array(
- 'status' => 1,
- 'statusText' => 'Updated'
- );
- }
- header('Content-Type: application/json');
- echo json_encode($resp);
- }
- public function log_message($page = 1, $limit = 20, $filter = '')
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $pointer = ($page - 1) * $limit;
- $data = $this->adm->log_message($pointer, $limit, $filter);
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function log_php($page = 1, $limit = 20, $filter = '')
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $pointer = ($page - 1) * $limit;
- $data = $this->adm->log_php($pointer, $limit, $filter);
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function log_php_update($id , $status)
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $update = $this->adm->log_php_update($id , $status);
- header('Content-Type: application/json');
- if($update)
- {
- echo json_encode(array(
- 'status' => 1,
- 'statusText' => 'SUCCESS'
- ));
- return;
- }
- echo json_encode(array(
- 'status' => 0,
- 'statusText' => 'FAILED'
- ));
- }
- public function log_php_remove($id)
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $remove = $this->adm->log_php_remove($id);
- header('Content-Type: application/json');
- if($remove)
- {
- echo json_encode(array(
- 'status' => 1,
- 'statusText' => 'SUCCESS'
- ));
- return;
- }
- echo json_encode(array(
- 'status' => 0,
- 'statusText' => 'FAILED'
- ));
- }
- public function log_js($page = 1, $limit = 20, $filter = '')
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $pointer = ($page - 1) * $limit;
- $data = $this->adm->log_js($pointer, $limit, $filter);
- header('Content-Type: application/json');
- echo json_encode($data);
- }
- public function log_js_update($id , $status)
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $update = $this->adm->log_js_update($id , $status);
- header('Content-Type: application/json');
- if($update)
- {
- echo json_encode(array(
- 'status' => 1,
- 'statusText' => 'SUCCESS'
- ));
- return;
- }
- echo json_encode(array(
- 'status' => 0,
- 'statusText' => 'FAILED'
- ));
- }
- public function log_js_remove($id)
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $remove = $this->adm->log_js_remove($id);
- header('Content-Type: application/json');
- if($remove)
- {
- echo json_encode(array(
- 'status' => 1,
- 'statusText' => 'SUCCESS'
- ));
- return;
- }
- echo json_encode(array(
- 'status' => 0,
- 'statusText' => 'FAILED'
- ));
- }
- public function get_data_monitor()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- }
- public function login()
- {
- $this->_goto_home_page();
- $assets = $GLOBALS['config']['base_url'] . 'assets/';
- $favicon = $assets . 'img/favicon.ico';
- $admin = $assets . "admin";
- $data = array(
- 'assets_path' => $admin,
- 'favicon' => $favicon,
- 'topjs' => array(),
- 'page_title' => $this->title,
- 'ajs' => array(
- "$admin/js/jquery-1.7.2.min.js",
- // "$admin/js/app/controllers.js",
- ),
- 'acss' => array(
- // $assets . "css/vendor/bootstrap.css",
- // $assets . "css/vendor/bootstrap-responsive.css"
- "$admin/css/bootstrap.min.css",
- "$admin/css/bootstrap-responsive.min.css",
- "$admin/css/font-awesome.css",
- "$admin/css/style.css",
- "$admin/css/pages/signin.css"
- )
- );
- $this->load->view('admin/login', $data);
- }
- public function logout()
- {
- $_SESSION = array();
- session_destroy();
- $this->_goto_login_page();
- }
- public function login_check()
- {
- if($_SESSION['is_logged_in'] === TRUE) die('You are already logged in');
- $username = $this->get_input('username');
- $password = $this->get_input('password');
- $check = $this->adm->login_check($username, $password);
- if($check['status'] === 1)
- {
- $_SESSION['is_logged_in'] = TRUE;
- $_SESSION['username'] = $check['data']['username'];
- $_SESSION['level'] = $check['data']['level'];
- $_SESSION['userdata'] = $check['data'];
- }
- header('Content-Type: application/json');
- echo json_encode($check);
- die();
- }
- public function start_recache()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $location = __DIR__ . '/../tmp/caching_position/';
- $start_date = $this->get_input('start_date');
- $end_date = $this->get_input('end_date');
- $devices = $this->get_input('devices');
- foreach ($devices as $k => $v)
- {
- $content = array(
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'deviceid' => $v,
- );
- $touch_file = $this->_touch_recache_file($location, $content);
- }
- header('Content-Type: application/json');
- echo json_encode(array(
- 'status' => $touch_file ? 1 : 0,
- 'statusText' => $touch_file ? 'SUCCESS' : 'FAILED',
- ));
- }
- public function start_recache_dj()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $location = __DIR__ . '/../tmp/caching_drv_journey/';
- $start_date = $this->get_input('start_date');
- $end_date = $this->get_input('end_date');
- $clientid = $this->get_input('clientid');
- $content = array(
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'clientid' => $clientid,
- );
- $touch_file = $this->_touch_recache_file($location, $content);
- header('Content-Type: application/json');
- echo json_encode(array(
- 'status' => $touch_file ? 1 : 0,
- 'statusText' => $touch_file ? 'SUCCESS' : 'FAILED',
- ));
- }
- public function get_incomplete_caching()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $location = __DIR__ . '/../tmp/caching_position/';
- $incomple_data = $this->_read_incomplete_caching($location);
- header('Content-Type: application/json');
- echo json_encode($incomple_data);
- }
- public function get_incomplete_caching_dj()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $location = __DIR__ . '/../tmp/caching_drv_journey/';
- $incomple_data = $this->_read_incomplete_caching($location);
- header('Content-Type: application/json');
- echo json_encode($incomple_data);
- }
- public function log_js_ignore()
- {
- if($_SESSION['is_logged_in'] !== TRUE) die('You are not authorized!');
- $file = $this->get_input('file');
- $message = $this->get_input('message');
- $success = $this->adm->log_js_ignore($file, $message);
- header('Content-Type: application/json');
- if($success)
- {
- echo json_encode(array(
- 'status' => 1,
- 'statusText' => 'SUCCESS'
- ));
- return;
- }
- echo json_encode(array(
- 'status' => 0,
- 'statusText' => 'FAILED'
- ));
- }
- private function _get_last_activity(&$data)
- {
- $users = array();
- $filename = $GLOBALS['config']['real_path'] . 'log/access.log';
- $lineCount = 0;
- $handle = fopen($filename, "r");
- if(!file_exists($filename)) return $data;
- // $timer = microtime(true);
- while( ! feof($handle) )
- {
- $line = fgets($handle);
- $test = preg_match("/^(\d{4}-\d\d-\d\d\s\d\d:\d\d:\d\d)\s\|\s([\w\.]+)/", $line, $match);
- if($test === 1) $users[$match[2]] = $match[1];
- $lineCount++;
- }
- foreach ($data as $k => $v)
- {
- $user = explode('@', $v['username']);
- $username = $user[0];
- $data[$k]['last_active'] = " - ";
- if(isset($users[$username]))
- {
- $data[$k]['last_active'] = strtotime($users[$username]) * 1000;
- }
- }
- return $data;
- }
- private function _goto_login_page()
- {
- if( ! isset($_SESSION['is_logged_in']) ) $_SESSION['is_logged_in'] = FALSE;
- if($_SESSION['is_logged_in'] !== TRUE)
- {
- pageRedirect('admin/login');
- }
- }
- private function _goto_home_page()
- {
- if( ! isset($_SESSION['is_logged_in']) ) $_SESSION['is_logged_in'] = FALSE;
- if($_SESSION['is_logged_in'] === TRUE)
- {
- pageRedirect('admin');
- }
- }
- private function _touch_recache_file($location, $content)
- {
- $put = 0;
- if(!file_exists($location))
- {
- mkdir($location);
- chmod($location, 0775);
- }
- $json_data = json_encode($content);
- $file_name = md5($json_data);
- $full_path = $location . $file_name;
- if(!file_exists($full_path))
- {
- $put = file_put_contents($full_path, $json_data);
- }
- return $put > 0;
- }
- private function _read_incomplete_caching($location)
- {
- $output = array();
- if(is_dir($location))
- {
- $files = scandir($location);
- foreach ($files as $i => $v)
- {
- if($v === '.' || $v === '..') continue;
- $full_path = $location . $v;
- if(!file_exists($full_path)) continue;
- $elapsed_time = time() - filemtime($full_path);
- $req = json_decode(file_get_contents($full_path), TRUE);
- $req['elapsed_time'] = $elapsed_time;
- $output[] = $req;
- }
- }
- return $output;
- }
- }
- /* End of file admin.php */
- /* Location: ./controllers/admin.php */
Add Comment
Please, Sign In to add comment