VM暫存
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

151 lignes
4.7KB

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. file: Acti_service.php 與 acti 介接相關都放這支
  4. */
  5. // ----- 定義常數(路徑, cache秒數) -----
  6. define('APP_VERSION', '100'); // 版本號
  7. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  8. define('APP_NAME', 'acti_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/acti_service.'); // log file name
  17. class Acti_service extends CI_Controller
  18. {
  19. var $vars = array();
  20. function __construct()
  21. {
  22. parent::__construct();
  23. // ----- 程式開發階段log設定 -----
  24. if (@ENVIRONMENT == 'development')
  25. {
  26. ini_set('display_errors', '1');
  27. //error_reporting(E_ALL ^ E_NOTICE);
  28. error_reporting(E_ALL);
  29. }
  30. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  31. // 阻檔未知的 IP
  32. $from_ip = $this->my_ip();
  33. if(!in_array($from_ip, array(
  34. '127.0.0.1',
  35. '192.168.10.130',
  36. '192.168.10.131',
  37. '192.168.10.132',
  38. '192.168.10.133',
  39. '192.168.10.134',
  40. '192.168.10.135',
  41. '192.168.10.136',
  42. '192.168.10.137',
  43. '192.168.10.138',
  44. '192.168.10.139'
  45. )))
  46. {
  47. trigger_error('refused://from:'.$from_ip.'..refused..'.print_r($_REQUEST, true));
  48. exit;
  49. }
  50. $method_name = $this->router->fetch_method();
  51. if (in_array($method_name, array('sos')))
  52. {
  53. ob_end_clean();
  54. ignore_user_abort();
  55. ob_start();
  56. echo 'ok';
  57. header('Connection: close');
  58. header('Content-Length: ' . ob_get_length());
  59. ob_end_flush();
  60. flush();
  61. }
  62. }
  63. // 取得 IP
  64. function my_ip()
  65. {
  66. if (getenv('HTTP_X_FORWARDED_FOR'))
  67. {
  68. $ip = getenv('HTTP_X_FORWARDED_FOR');
  69. }
  70. elseif (getenv('HTTP_X_REAL_IP'))
  71. {
  72. $ip = getenv('HTTP_X_REAL_IP');
  73. }
  74. else {
  75. $ip = $_SERVER['REMOTE_ADDR'];
  76. }
  77. return $ip;
  78. }
  79. // 發生錯誤時集中在此處理
  80. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  81. {
  82. $log_msg = explode('://', $errstr);
  83. if (count($log_msg) > 1)
  84. {
  85. $log_file = $log_msg[0];
  86. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  87. }
  88. else
  89. {
  90. $log_file = APP_NAME;
  91. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  92. }
  93. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  94. }
  95. // 顯示logs
  96. public function show_logs()
  97. {
  98. $lines = $this->uri->segment(3); // 顯示行數
  99. if (empty($lines)) $lines = 100; // 無行數參數, 預設為40行
  100. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  101. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  102. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  103. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  104. echo '</pre></body></html>';
  105. }
  106. // [區網] 由設備端呼叫
  107. public function sos()
  108. {
  109. $msg = isset($_REQUEST['Message']) ? $_REQUEST['Message'] : '';
  110. trigger_error(__FUNCTION__ . "..Message..{$msg}..". print_r($_REQUEST, true));
  111. $msg_arr = explode('-', $msg);
  112. $station_no = isset($msg_arr['0']) ? $msg_arr['0'] : 0;
  113. $machine_no = isset($msg_arr['1']) ? $msg_arr['1'] : 0;
  114. //$station_no = $this->uri->segment(3);
  115. //$machine_no = $this->uri->segment(4);
  116. if(!isset($station_no) || !isset($machine_no))
  117. {
  118. trigger_error(__FUNCTION__ . '..unknown msg..');
  119. exit;
  120. }
  121. require_once(ALTOB_SYNC_FILE) ;
  122. // 傳送 SOS
  123. $sync_agent = new AltobSyncAgent();
  124. $sync_agent->init($station_no, date('Y-m-d H:i:s'));
  125. $sync_result = $sync_agent->sync_st_sos($machine_no);
  126. trigger_error( "..sync_st_sos.." . $sync_result);
  127. }
  128. }