VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
9.4KB

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. file: Mitac_service.php 與 mitac 介接相關都放這支
  4. */
  5. // ----- 定義常數(路徑, cache秒數) -----
  6. define('APP_VERSION', '100'); // 版本號
  7. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  8. define('APP_NAME', 'mitac_service'); // 應用系統名稱
  9. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  10. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL
  11. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  12. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  13. define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib
  14. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  15. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name
  16. define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/mitac_service.'); // log file name
  17. class Mitac_service extends CI_Controller
  18. {
  19. var $vars = array();
  20. function __construct()
  21. {
  22. parent::__construct();
  23. $method_name = $this->router->fetch_method();
  24. if (in_array($method_name, array('parking_fee_altob', 'deduct_result')))
  25. {
  26. ob_end_clean();
  27. ignore_user_abort();
  28. ob_start();
  29. echo 'ok';
  30. header('Connection: close');
  31. header('Content-Length: ' . ob_get_length());
  32. ob_end_flush();
  33. flush();
  34. }
  35. ignore_user_abort(); // 接受client斷線, 繼續run
  36. $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21)
  37. $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621)
  38. $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012)
  39. $this->vars['station_no'] = STATION_NO; // 本站編號
  40. // session_id(ip2long($_SERVER['REMOTE_ADDR'])); // 設定同一device為同一個session
  41. session_start();
  42. // ----- 程式開發階段log設定 -----
  43. if (@ENVIRONMENT == 'development')
  44. {
  45. ini_set('display_errors', '1');
  46. //error_reporting(E_ALL ^ E_NOTICE);
  47. error_reporting(E_ALL);
  48. }
  49. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  50. // 阻檔未知的 IP
  51. if(!in_array($_SERVER['HTTP_X_REAL_IP'], array('127.0.0.1')))
  52. {
  53. trigger_error('refused://from:'.$_SERVER['HTTP_X_REAL_IP'].'..refused..');
  54. exit;
  55. }
  56. // MITAC 模組
  57. $this->load->model('mitac_service_model');
  58. $this->mitac_service_model->init($this->vars);
  59. }
  60. // 發生錯誤時集中在此處理
  61. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  62. {
  63. $log_msg = explode('://', $errstr);
  64. if (count($log_msg) > 1)
  65. {
  66. $log_file = $log_msg[0];
  67. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  68. }
  69. else
  70. {
  71. $log_file = APP_NAME;
  72. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  73. }
  74. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  75. }
  76. // 顯示logs
  77. public function show_logs()
  78. {
  79. $lines = $this->uri->segment(3); // 顯示行數
  80. if (empty($lines)) $lines = 100; // 無行數參數, 預設為40行
  81. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  82. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  83. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  84. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  85. echo '</pre></body></html>';
  86. }
  87. // [排程] 詢問是否存活
  88. public function echo_mitac_alive()
  89. {
  90. echo $this->mitac_service_model->echo_mitac_alive();
  91. exit;
  92. }
  93. // 要求扣款 (ALTOB to MITAC)
  94. public function parking_fee_altob()
  95. {
  96. $seqno = $this->input->post('seqno', true); // 通訊序號
  97. $lpr = $this->input->post('lpr', true); // 車牌
  98. $in_time = $this->input->post('in_time', true); // 進場時間
  99. $out_time = $this->input->post('out_time', true); // 出場時間
  100. $gate_id = $this->input->post('gate_id', true); // 驗票機編號
  101. $ck = $this->input->post('ck', true); // 驗証碼
  102. // 通訊內容
  103. $parms = array(
  104. 'seqno' => $seqno,
  105. 'lpr' => $lpr,
  106. 'in_time' => $in_time,
  107. 'out_time' => $out_time,
  108. 'gate_id' => $gate_id);
  109. if($ck != md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['in_time']. 'o'. $parms['out_time'] . 'b'. $parms['gate_id'] . __FUNCTION__))
  110. {
  111. trigger_error(__FUNCTION__ . '..ck_error..' . print_r($parms, true));
  112. exit; // 中斷
  113. }
  114. trigger_error(__FUNCTION__ . '..' . print_r($parms, true));
  115. $this->mitac_service_model->parking_fee_altob($parms);
  116. exit;
  117. }
  118. // 回應扣款成功 (MITAC to ALTOB)
  119. public function deduct_result()
  120. {
  121. $seqno = $this->input->post('seqno', true);
  122. $lpr = $this->input->post('lpr', true);
  123. $in_time = $this->input->post('in_time', true);
  124. $out_time = $this->input->post('out_time', true);
  125. $gate_id = $this->input->post('gate_id', true);
  126. $amt = $this->input->post('amt', true);
  127. $amt_discount = $this->input->post('amt_discount', true);
  128. $amt_real = $this->input->post('amt_real', true);
  129. $ck = $this->input->post('ck', true);
  130. // 通訊內容
  131. $parms = array(
  132. 'seqno' => $seqno,
  133. 'lpr' => $lpr,
  134. 'in_time' => $in_time,
  135. 'out_time' => $out_time,
  136. 'gate_id' => $gate_id,
  137. 'amt' => $amt,
  138. 'amt_discount' => $amt_discount,
  139. 'amt_real' => $amt_real);
  140. if($ck != md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['amt']. 'o'. $parms['amt_discount'] . 'b'. $parms['amt_real'] . __FUNCTION__))
  141. {
  142. trigger_error(__FUNCTION__ . '..ck_error..' . print_r($parms, true));
  143. exit; // 中斷
  144. }
  145. trigger_error(__FUNCTION__ . '..' . print_r($parms, true));
  146. $this->mitac_service_model->deduct_result($parms);
  147. exit;
  148. }
  149. // http://localhost/mitac_service.html/test_parking_fee_altob
  150. public function test_parking_fee_altob()
  151. {
  152. $function_name = 'parking_fee_altob';
  153. $seqno = '20161004_101010';
  154. $lpr = 'AA1234';
  155. $in_time = '2017-11-11 16:40:02';
  156. $out_time = '2017-11-11 16:58:36';
  157. $gate_id = 1;
  158. // 通訊內容
  159. $parms = array(
  160. 'seqno' => $seqno,
  161. 'lpr' => $lpr,
  162. 'in_time' => $in_time,
  163. 'out_time' => $out_time,
  164. 'gate_id' => $gate_id);
  165. // 驗証碼
  166. $parms['ck'] = md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['in_time']. 'o'. $parms['out_time'] . 'b'. $parms['gate_id'] . $function_name);
  167. // 測試呼叫
  168. $ch = curl_init();
  169. curl_setopt($ch, CURLOPT_URL, "http://localhost/mitac_service.html/{$function_name}");
  170. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  171. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  172. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  173. curl_setopt($ch, CURLOPT_POST, TRUE);
  174. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms));
  175. $rs = curl_exec($ch);
  176. curl_close($ch);
  177. echo $rs;
  178. exit;
  179. }
  180. // http://localhost/mitac_service.html/test_49993/
  181. public function test_49993()
  182. {
  183. $seqno = '201711111_027771';
  184. $lpr = 'TEST1111B';
  185. $in_time = '20171111_190048';
  186. $out_time = '20171111_190048';
  187. $gate_id = 0;
  188. $amt = 66;
  189. $amt_discount = 10;
  190. $amt_real = 56;
  191. $msg = implode(',', ['Altob', 'DeductResult', $seqno, $lpr, $in_time, $out_time, $gate_id, $amt, $amt_discount, $amt_real]);
  192. $error_str = '';
  193. $service_port = 49993;
  194. $address = "192.168.10.201";
  195. /* Create a TCP/IP socket. */
  196. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  197. if ($socket === false) {
  198. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  199. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  200. } else {
  201. echo "OK.\n";
  202. }
  203. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  204. $result = socket_connect($socket, $address, $service_port);
  205. if ($result === false) {
  206. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  207. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  208. } else {
  209. echo "OK.\n";
  210. }
  211. /*
  212. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  213. $token, 0x1f, $lpr, 0x1f, $in_time
  214. );
  215. */
  216. $in = $msg;
  217. $out = '';
  218. echo "socket_write:";
  219. echo "{$in}<br/><br/>";
  220. if(!socket_write($socket, $in, strlen($in)))
  221. {
  222. echo('<p>Write failed</p>');
  223. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  224. }
  225. echo "socket_write..OK..";
  226. echo "<br/><br/>socket_read:";
  227. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  228. //while ($out = socket_read($socket, 2048)) {
  229. // echo $out;
  230. //}
  231. echo "{$out}<br/><br/>";
  232. echo "Closing socket...";
  233. socket_close($socket);
  234. echo "OK.\n\n";
  235. trigger_error($error_str);
  236. exit;
  237. }
  238. }