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.

198 line
7.0KB

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. ALTOB base controller
  4. */
  5. class CC_Controller extends CI_Controller
  6. {
  7. public $vars;
  8. function __construct($app_name)
  9. {
  10. parent::__construct();
  11. define('APP_NAME', $app_name); // 應用系統名稱
  12. define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name
  13. // ----- 程式開發階段log設定 -----
  14. if (@ENVIRONMENT == 'development')
  15. {
  16. ini_set('display_errors', '1');
  17. //error_reporting(E_ALL ^ E_NOTICE);
  18. error_reporting(E_ALL);
  19. }
  20. set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log
  21. $method_name = $this->router->fetch_method();
  22. $request_assoc = $this->uri->uri_to_assoc(3);
  23. trigger_error(__FUNCTION__ . '://..' . $method_name. '..uri_to_assoc..' . print_r($request_assoc, true));
  24. // ----- 常數 -----
  25. define('APP_VERSION', '100'); // 版本號
  26. define('MAX_AGE', 604800); // cache秒數, 此定義1個月
  27. define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') . ($_SERVER['SERVER_PORT'] != 60123 ? ':' . $_SERVER['SERVER_PORT'] : '') .'/'); // URL for 同外網IP, 不同PORT
  28. define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib
  29. define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib
  30. define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑
  31. define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑
  32. define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views
  33. $this->vars = array();
  34. $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21)
  35. $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); // 數字化時間(20151012143621)
  36. $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012)
  37. // 共用記憶體
  38. $this->vars['mcache'] = new Memcache;
  39. if(!$this->vars['mcache']->pconnect(MEMCACHE_HOST, MEMCACHE_PORT)){ trigger_error('..Could not connect mcache..'); }
  40. }
  41. // 發生錯誤時集中在此處理
  42. public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  43. {
  44. $log_msg = explode('://', $errstr);
  45. if (count($log_msg) > 1 && substr($log_msg[0], -4) != 'http')
  46. {
  47. $log_file = $log_msg[0];
  48. $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n";
  49. }
  50. else
  51. {
  52. $log_file = APP_NAME;
  53. $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  54. }
  55. error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  56. }
  57. // 顯示靜態網頁(html檔)
  58. public function show_page($page_name, &$data = null)
  59. {
  60. $page_file = PAGE_PATH.$page_name.'.php';
  61. $last_modified_time = filemtime($page_file);
  62. // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁
  63. // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月
  64. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT');
  65. header('Etag: '. APP_VERSION);
  66. header('Cache-Control: public');
  67. // 20170921
  68. header("cache-Control: no-store, no-cache, must-revalidate");
  69. header("cache-Control: post-check=0, pre-check=0", false);
  70. header("Pragma: no-cache");
  71. header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
  72. if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  73. {
  74. header('HTTP/1.1 304 Not Modified');
  75. }
  76. else
  77. {
  78. $this->load->view(APP_NAME.'/'.$page_name, $data);
  79. }
  80. }
  81. // 顯示logs
  82. public function show_logs()
  83. {
  84. $lines = $this->uri->segment(3); // 顯示行數
  85. if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行
  86. // echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><pre style="white-space: pre-wrap;">';
  87. echo '<html lang="zh-TW"><body><pre style="white-space: pre-wrap;">';
  88. passthru('/usr/bin/tail -n ' . $lines . ' ' . LOG_PATH . APP_NAME . '.' . date('Ymd').'.log.txt'); // 利用linux指令顯示倒數幾行的logs內容
  89. echo "\n----- " . LOG_PATH . APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';
  90. echo '</pre></body></html>';
  91. }
  92. // 送出html code
  93. public function get_html()
  94. {
  95. $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), array());
  96. }
  97. // 驗証 IP
  98. public function is_ip_valid()
  99. {
  100. $client_ip = $this->my_ip();
  101. if(!in_array($client_ip, array('127.0.0.1', '61.219.172.11', '61.219.172.82')))
  102. {
  103. trigger_error('..block..from:'.$client_ip.'..unknown network..');
  104. return false;
  105. }
  106. return true;
  107. }
  108. // 取得 IP
  109. public function my_ip()
  110. {
  111. if (getenv('HTTP_X_FORWARDED_FOR'))
  112. {
  113. $ip = getenv('HTTP_X_FORWARDED_FOR');
  114. }
  115. elseif (getenv('HTTP_X_REAL_IP'))
  116. {
  117. $ip = getenv('HTTP_X_REAL_IP');
  118. }
  119. else {
  120. $ip = $_SERVER['REMOTE_ADDR'];
  121. }
  122. return $ip;
  123. }
  124. // 啟動 MQTT
  125. public function init_mqtt()
  126. {
  127. require_once(MQ_CLASS_FILE);
  128. $station_setting = $this->data_model()->station_setting_query();
  129. $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST;
  130. $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT;
  131. $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid());
  132. $this->vars['mqtt']->connect();
  133. }
  134. // 資料同步模組
  135. public function data_model()
  136. {
  137. return $this->app_model('sync_data');
  138. }
  139. // 啟動模組
  140. public function app_model($app_name='')
  141. {
  142. $model_name = !empty($app_name) ? $app_name . '_model' : APP_NAME . '_model';
  143. $this->load->model($model_name);
  144. $this->$model_name->init($this->vars);
  145. return $this->$model_name;
  146. }
  147. // 取得場站編號
  148. public function get_station_no()
  149. {
  150. $station_setting = $this->data_model()->station_setting_query();
  151. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  152. return $station_no_arr[0];
  153. }
  154. // 取得免費時間
  155. public function get_free_time()
  156. {
  157. $station_setting = $this->data_model()->station_setting_query();
  158. if(!isset($station_setting['settings']))
  159. return 0;
  160. $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']);
  161. $station_no = $station_no_arr[0];
  162. if(!isset($station_setting['settings'][$station_no]['free_time']))
  163. return 0;
  164. return $station_setting['settings'][$station_no]['free_time'];
  165. }
  166. }