VM暫存
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Mitac_service.php 9.2KB

před 8 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // 要求扣款 (ALTOB to MITAC)
  88. public function parking_fee_altob()
  89. {
  90. $seqno = $this->input->post('seqno', true); // 通訊序號
  91. $lpr = $this->input->post('lpr', true); // 車牌
  92. $in_time = $this->input->post('in_time', true); // 進場時間
  93. $out_time = $this->input->post('out_time', true); // 出場時間
  94. $gate_id = $this->input->post('gate_id', true); // 驗票機編號
  95. $ck = $this->input->post('ck', true); // 驗証碼
  96. // 通訊內容
  97. $parms = array(
  98. 'seqno' => $seqno,
  99. 'lpr' => $lpr,
  100. 'in_time' => $in_time,
  101. 'out_time' => $out_time,
  102. 'gate_id' => $gate_id);
  103. if($ck != md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['in_time']. 'o'. $parms['out_time'] . 'b'. $parms['gate_id'] . __FUNCTION__))
  104. {
  105. trigger_error(__FUNCTION__ . '..ck_error..' . print_r($parms, true));
  106. exit; // 中斷
  107. }
  108. trigger_error(__FUNCTION__ . '..' . print_r($parms, true));
  109. $this->mitac_service_model->parking_fee_altob($parms);
  110. exit;
  111. }
  112. // 回應扣款成功 (MITAC to ALTOB)
  113. public function deduct_result()
  114. {
  115. $seqno = $this->input->post('seqno', true);
  116. $lpr = $this->input->post('lpr', true);
  117. $in_time = $this->input->post('in_time', true);
  118. $out_time = $this->input->post('out_time', true);
  119. $gate_id = $this->input->post('gate_id', true);
  120. $amt = $this->input->post('amt', true);
  121. $amt_discount = $this->input->post('amt_discount', true);
  122. $amt_real = $this->input->post('amt_real', true);
  123. $ck = $this->input->post('ck', true);
  124. // 通訊內容
  125. $parms = array(
  126. 'seqno' => $seqno,
  127. 'lpr' => $lpr,
  128. 'in_time' => $in_time,
  129. 'out_time' => $out_time,
  130. 'gate_id' => $gate_id,
  131. 'amt' => $amt,
  132. 'amt_discount' => $amt_discount,
  133. 'amt_real' => $amt_real);
  134. if($ck != md5($parms['seqno']. 'a' . date('dmh') . 'l' . $parms['lpr'] . 't'. $parms['amt']. 'o'. $parms['amt_discount'] . 'b'. $parms['amt_real'] . __FUNCTION__))
  135. {
  136. trigger_error(__FUNCTION__ . '..ck_error..' . print_r($parms, true));
  137. exit; // 中斷
  138. }
  139. trigger_error(__FUNCTION__ . '..' . print_r($parms, true));
  140. $this->mitac_service_model->deduct_result($parms);
  141. exit;
  142. }
  143. // http://localhost/mitac_service.html/test_parking_fee_altob
  144. public function test_parking_fee_altob()
  145. {
  146. $function_name = 'parking_fee_altob';
  147. $seqno = '20161004_101010';
  148. $lpr = 'AA1234';
  149. $in_time = '2017-11-11 16:40:02';
  150. $out_time = '2017-11-11 16:58:36';
  151. $gate_id = 1;
  152. // 通訊內容
  153. $parms = array(
  154. 'seqno' => $seqno,
  155. 'lpr' => $lpr,
  156. 'in_time' => $in_time,
  157. 'out_time' => $out_time,
  158. 'gate_id' => $gate_id);
  159. // 驗証碼
  160. $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);
  161. // 測試呼叫
  162. $ch = curl_init();
  163. curl_setopt($ch, CURLOPT_URL, "http://localhost/mitac_service.html/{$function_name}");
  164. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  165. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  166. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  167. curl_setopt($ch, CURLOPT_POST, TRUE);
  168. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms));
  169. $rs = curl_exec($ch);
  170. curl_close($ch);
  171. echo $rs;
  172. exit;
  173. }
  174. // http://localhost/mitac_service.html/test_49993/
  175. public function test_49993()
  176. {
  177. $seqno = '201711111_027771';
  178. $lpr = 'TEST1111B';
  179. $in_time = '20171111_190048';
  180. $out_time = '20171111_190048';
  181. $gate_id = 0;
  182. $amt = 66;
  183. $amt_discount = 10;
  184. $amt_real = 56;
  185. $msg = implode(',', ['Altob', 'DeductResult', $seqno, $lpr, $in_time, $out_time, $gate_id, $amt, $amt_discount, $amt_real]);
  186. $error_str = '';
  187. $service_port = 49993;
  188. $address = "192.168.10.201";
  189. /* Create a TCP/IP socket. */
  190. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  191. if ($socket === false) {
  192. echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  193. $error_str .= "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
  194. } else {
  195. echo "OK.\n";
  196. }
  197. echo "Attempting to connect to '{$address}' on port '{$service_port}'...";
  198. $result = socket_connect($socket, $address, $service_port);
  199. if ($result === false) {
  200. echo "socket_connect() failed.\nReason: ({$result}) " . socket_strerror(socket_last_error($socket)) . "\n";
  201. $error_str .= "socket_connect() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  202. } else {
  203. echo "OK.\n";
  204. }
  205. /*
  206. $in = pack('Ca5Ca3Ca9Ca7Ca19', 0x1c, $seqno, 0x1c, $cmd, 0x1c,
  207. $token, 0x1f, $lpr, 0x1f, $in_time
  208. );
  209. */
  210. $in = $msg;
  211. $out = '';
  212. echo "socket_write:";
  213. echo "{$in}<br/><br/>";
  214. if(!socket_write($socket, $in, strlen($in)))
  215. {
  216. echo('<p>Write failed</p>');
  217. $error_str .= "socket_write() failed: reason: " . socket_strerror(socket_last_error($socket)) . "\n";
  218. }
  219. echo "socket_write..OK..";
  220. echo "<br/><br/>socket_read:";
  221. $out = socket_read($socket, 2048) or die("Could not read server responsen");
  222. //while ($out = socket_read($socket, 2048)) {
  223. // echo $out;
  224. //}
  225. echo "{$out}<br/><br/>";
  226. echo "Closing socket...";
  227. socket_close($socket);
  228. echo "OK.\n\n";
  229. trigger_error($error_str);
  230. exit;
  231. }
  232. }