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ů.

Acti_service.php 4.1KB

před 8 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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('127.0.0.1')))
  34. {
  35. trigger_error('refused://from:'.$from_ip.'..refused..'.print_r($_REQUEST, true));
  36. exit;
  37. }
  38. $method_name = $this->router->fetch_method();
  39. if (in_array($method_name, array('sos')))
  40. {
  41. ob_end_clean();
  42. ignore_user_abort();
  43. ob_start();
  44. echo 'ok';
  45. header('Connection: close');
  46. header('Content-Length: ' . ob_get_length());
  47. ob_end_flush();
  48. flush();
  49. }
  50. }
  51. // 取得 IP
  52. function my_ip()
  53. {
  54. if (getenv('HTTP_X_FORWARDED_FOR'))
  55. {
  56. $ip = getenv('HTTP_X_FORWARDED_FOR');
  57. }
  58. elseif (getenv('HTTP_X_REAL_IP'))
  59. {
  60. $ip = getenv('HTTP_X_REAL_IP');
  61. }
  62. else {
  63. $ip = $_SERVER['REMOTE_ADDR'];
  64. }
  65. return $ip;
  66. }
  67. // 發生錯誤時集中在此處理
  68. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  69. {
  70. $log_msg = explode('://', $errstr);
  71. if (count($log_msg) > 1)
  72. {
  73. $log_file = $log_msg[0];
  74. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  75. }
  76. else
  77. {
  78. $log_file = APP_NAME;
  79. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  80. }
  81. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  82. }
  83. // 顯示logs
  84. public function show_logs()
  85. {
  86. $lines = $this->uri->segment(3); // 顯示行數
  87. if (empty($lines)) $lines = 100; // 無行數參數, 預設為40行
  88. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  89. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  90. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  91. echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  92. echo '</pre></body></html>';
  93. }
  94. // [區網] 由設備端呼叫
  95. public function sos()
  96. {
  97. $station_no = $this->uri->segment(3); // 場站編號
  98. $machine_no = $this->uri->segment(4); // 設備編號
  99. trigger_error(__FUNCTION__ . "..{$station_no},{$machine_no}..");
  100. require_once(ALTOB_SYNC_FILE) ;
  101. // 傳送 SOS
  102. $sync_agent = new AltobSyncAgent();
  103. $sync_agent->init($station_no, date('Y-m-d H:i:s'));
  104. $sync_result = $sync_agent->sync_st_sos($machine_no);
  105. trigger_error( "..sync_st_sos.." . $sync_result);
  106. }
  107. }