VM暫存
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

354 linhas
12KB

  1. <?php
  2. /*
  3. file: vip.php 交通局VIP管理系統
  4. */
  5. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  6. require_once(MQ_CLASS_FILE);
  7. session_start(); //we need to call PHP's session object to access it through CI
  8. class Vip extends CI_Controller
  9. {
  10. var $vars = array(); // 共用變數
  11. function __construct()
  12. {
  13. parent::__construct();
  14. // ----- 程式開發階段log設定 -----
  15. if (@ENVIRONMENT == 'development')
  16. {
  17. ini_set('display_errors', '1');
  18. //error_reporting(E_ALL ^ E_NOTICE);
  19. error_reporting(E_ALL);
  20. }
  21. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  22. /**
  23. // 共用記憶體
  24. $this->vars['mcache'] = new Memcache;
  25. $this->vars['mcache']->connect(MEMCACHE_HOST, MEMCACHE_POST) or die ('Could not connect memcache');
  26. // mqtt subscribe
  27. $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_POST, 'cario');
  28. if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); }
  29. **/
  30. // ----- 定義常數(路徑, cache秒數) -----
  31. define('APP_VERSION', '100'); // 版本號
  32. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  33. define('APP_NAME', 'vip'); // 應用系統名稱
  34. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  35. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  36. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  37. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  38. define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib
  39. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  40. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path
  41. $this->load->model('vip_model');
  42. $this->vip_model->init($this->vars);
  43. // load library
  44. $this->load->library(array('form_validation','session'));
  45. // load helpers
  46. $this->load->helper(array('form'));
  47. // ajax code
  48. define('RESULT_SUCCESS', 'ok');
  49. define('RESULT_FORM_VALIDATION_FAIL', '-1');
  50. define('RESULE_FAIL', 'gg');
  51. }
  52. // 發生錯誤時集中在此處理
  53. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  54. {
  55. // ex: car_err://message....
  56. //$log_msg = explode('://', $errstr);
  57. /*
  58. if (count($log_msg) > 1)
  59. {
  60. $log_file = LOG_PATH.$log_msg[0];
  61. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  62. }
  63. else
  64. {
  65. $log_file = LOG_PATH.APP_NAME;
  66. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  67. }
  68. */
  69. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  70. //error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  71. error_log($str, 3, LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  72. }
  73. // 顯示靜態網頁(html檔)
  74. protected function show_page($page_name, &$data = null)
  75. {
  76. $page_file = PAGE_PATH.$page_name.'.php';
  77. $last_modified_time = filemtime($page_file);
  78. // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁
  79. // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月
  80. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT');
  81. header('Etag: '. APP_VERSION);
  82. header('Cache-Control: public');
  83. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  84. {
  85. header('HTTP/1.1 304 Not Modified');
  86. }
  87. else
  88. {
  89. $this->load->view(APP_NAME.'/'.$page_name, $data);
  90. }
  91. }
  92. public function index()
  93. {
  94. if($this->session->userdata('logged_in'))
  95. {
  96. $session_data = $this->session->userdata('logged_in');
  97. $data['username'] = $session_data['username'];
  98. $data['type'] = $session_data['type'];
  99. if($data['type'] == 'ma')
  100. {
  101. $this->show_page('admin_page', $data); // 進階管理者介面
  102. }
  103. else
  104. {
  105. $this->show_page('main_page', $data); // 一般管理者介面
  106. }
  107. }
  108. else
  109. {
  110. //If no session, redirect to login page
  111. //redirect('login', 'refresh');
  112. $this->show_page('login_page');
  113. }
  114. }
  115. // 登入
  116. public function user_login()
  117. {
  118. // form_validation
  119. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required|xss_clean');
  120. $this->form_validation->set_rules('pswd', 'pswd', 'trim|required|xss_clean');
  121. if($this->form_validation->run() == FALSE)
  122. {
  123. return RESULT_FORM_VALIDATION_FAIL;
  124. }
  125. // go model
  126. $data = array
  127. (
  128. 'login_name' => $this->input->post('login_name', true),
  129. 'pswd' => $this->input->post('pswd', true)
  130. );
  131. $result = $this->vip_model->user_login($data);
  132. if($result)
  133. {
  134. $sess_array = array();
  135. foreach($result as $row)
  136. {
  137. $sess_array = array
  138. (
  139. 'username' => $row->login_name ,
  140. 'type' => $row->type
  141. );
  142. $this->session->set_userdata('logged_in', $sess_array);
  143. }
  144. echo RESULT_SUCCESS;
  145. }
  146. else
  147. {
  148. return RESULE_FAIL;
  149. }
  150. }
  151. // 登出
  152. public function user_logout()
  153. {
  154. $this->session->unset_userdata('logged_in');
  155. session_destroy();
  156. return RESULT_SUCCESS;
  157. }
  158. // 新增與修改
  159. public function member_add()
  160. {
  161. // form_validation (required)
  162. $this->form_validation->set_rules('member_no', 'member_no', 'trim|required|xss_clean');
  163. $this->form_validation->set_rules('station_no', 'station_no', 'trim|required|xss_clean');
  164. $this->form_validation->set_rules('lpr', 'lpr', 'trim|required|xss_clean|alpha_numeric');
  165. $this->form_validation->set_rules('start_date', 'start_date', 'trim|required|xss_clean');
  166. $this->form_validation->set_rules('end_date', 'end_date', 'trim|required|xss_clean');
  167. $this->form_validation->set_rules('member_name', 'member_name', 'trim|required|xss_clean');
  168. $this->form_validation->set_rules('mobile_no', 'mobile_no', 'trim|required|xss_clean');
  169. // form_validation (basic)
  170. $this->form_validation->set_rules('remarks', 'remarks', 'trim|xss_clean');
  171. if($this->form_validation->run() == FALSE)
  172. {
  173. return RESULT_FORM_VALIDATION_FAIL;
  174. }
  175. // go model
  176. $data = array
  177. (
  178. 'member_no' => $this->input->post('member_no', true),
  179. 'station_no' => $this->input->post('station_no', true),
  180. 'lpr' => strtoupper($this->input->post('lpr', true)),
  181. 'start_date' => $this->input->post('start_date', true),
  182. 'end_date' => $this->input->post('end_date', true),
  183. 'member_name' => $this->input->post('member_name', true),
  184. 'member_nick_name' => $this->input->post('member_name', true),
  185. 'mobile_no' => $this->input->post('mobile_no', true),
  186. 'remarks' => $this->input->post('remarks', true)
  187. );
  188. $this->vip_model->vip_add($data);
  189. echo RESULT_SUCCESS;
  190. }
  191. // 查詢
  192. public function member_query()
  193. {
  194. $data = $this->vip_model->vip_query();
  195. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  196. }
  197. // 刪除
  198. public function member_delete()
  199. {
  200. // form_validation
  201. $this->form_validation->set_rules('member_no', 'member_no', 'trim|required|xss_clean');
  202. if($this->form_validation->run() == FALSE)
  203. {
  204. return RESULE_FAIL;
  205. }
  206. // go model
  207. $member_no = $this->input->post('member_no', true);
  208. $this->vip_model->member_delete($member_no);
  209. echo RESULT_SUCCESS;
  210. }
  211. // 管理者新增與修改
  212. public function user_add()
  213. {
  214. // 判斷target_name分流insert or update
  215. $this->form_validation->set_rules('target_name', 'target_name', 'trim|xss_clean');
  216. // form_validation (basic)
  217. $this->form_validation->set_rules('type', 'type', 'trim|required|xss_clean');
  218. $this->form_validation->set_rules('user_name', 'user_name', 'trim|xss_clean');
  219. $this->form_validation->set_rules('email', 'email', 'trim|xss_clean');
  220. $this->form_validation->set_rules('mobile_no', 'mobile_no', 'trim|xss_clean');
  221. $this->form_validation->set_rules('tel', 'tel', 'trim|xss_clean');
  222. $this->form_validation->set_rules('car_plate', 'car_plate', 'trim|xss_clean');
  223. if($this->form_validation->run() == FALSE)
  224. {
  225. return RESULT_FORM_VALIDATION_FAIL;
  226. }
  227. $target_name = $this->input->post('target_name', true);
  228. if($target_name == '')
  229. {
  230. // insert 流程
  231. // form_validation (required)
  232. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required|xss_clean');
  233. $this->form_validation->set_rules('pswd', 'pswd', 'trim|required|xss_clean');
  234. if($this->form_validation->run() == FALSE)
  235. {
  236. return RESULT_FORM_VALIDATION_FAIL;
  237. }
  238. // go model
  239. $data = array
  240. (
  241. 'type' => $this->input->post('type', true),
  242. 'login_name' => $this->input->post('login_name', true),
  243. 'pswd' => MD5($this->input->post('pswd', true)),
  244. 'user_name' => $this->input->post('user_name', true),
  245. 'email' => $this->input->post('email', true),
  246. 'mobile_no' => $this->input->post('mobile_no', true),
  247. 'tel' => $this->input->post('tel', true),
  248. 'car_plate' => strtoupper($this->input->post('car_plate', true))
  249. );
  250. $this->vip_model->user_insert($data);
  251. echo RESULT_SUCCESS;
  252. }
  253. else
  254. {
  255. // update 流程
  256. // go model
  257. $data = array
  258. (
  259. 'type' => $this->input->post('type', true),
  260. 'user_name' => $this->input->post('user_name', true),
  261. 'email' => $this->input->post('email', true),
  262. 'mobile_no' => $this->input->post('mobile_no', true),
  263. 'tel' => $this->input->post('tel', true),
  264. 'car_plate' => strtoupper($this->input->post('car_plate', true))
  265. );
  266. $this->vip_model->user_update($data, $target_name);
  267. echo RESULT_SUCCESS;
  268. }
  269. }
  270. // 管理者查詢
  271. public function user_query()
  272. {
  273. $data = $this->vip_model->user_query();
  274. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  275. }
  276. // 管理者刪除
  277. public function user_delete()
  278. {
  279. // form_validation
  280. $this->form_validation->set_rules('login_name', 'login_name', 'trim|required|xss_clean');
  281. if($this->form_validation->run() == FALSE)
  282. {
  283. return RESULE_FAIL;
  284. }
  285. // go model
  286. $login_name = $this->input->post('login_name', true);
  287. $this->vip_model->user_delete($login_name);
  288. echo RESULT_SUCCESS;
  289. }
  290. }