diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..19d9b0c --- /dev/null +++ b/config/config.php @@ -0,0 +1,373 @@ + '../modules/', +); + + +/* +|-------------------------------------------------------------------------- +| Index File +|-------------------------------------------------------------------------- +| +| Typically this will be your index.php file, unless you've renamed it to +| something else. If you are using mod_rewrite to remove the page set this +| variable so that it is blank. +| +*/ +$config['index_page'] = 'ci_index.php'; + +/* +|-------------------------------------------------------------------------- +| URI PROTOCOL +|-------------------------------------------------------------------------- +| +| This item determines which server global should be used to retrieve the +| URI string. The default setting of 'AUTO' works for most servers. +| If your links do not seem to work, try one of the other delicious flavors: +| +| 'AUTO' Default - auto detects +| 'PATH_INFO' Uses the PATH_INFO +| 'QUERY_STRING' Uses the QUERY_STRING +| 'REQUEST_URI' Uses the REQUEST_URI +| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO +| +*/ +$config['uri_protocol'] = 'AUTO'; + +/* +|-------------------------------------------------------------------------- +| URL suffix +|-------------------------------------------------------------------------- +| +| This option allows you to add a suffix to all URLs generated by CodeIgniter. +| For more information please see the user guide: +| +| http://codeigniter.com/user_guide/general/urls.html +*/ + +$config['url_suffix'] = ''; + +/* +|-------------------------------------------------------------------------- +| Default Language +|-------------------------------------------------------------------------- +| +| This determines which set of language files should be used. Make sure +| there is an available translation if you intend to use something other +| than english. +| +*/ +$config['language'] = 'english'; + +/* +|-------------------------------------------------------------------------- +| Default Character Set +|-------------------------------------------------------------------------- +| +| This determines which character set is used by default in various methods +| that require a character set to be provided. +| +*/ +$config['charset'] = 'UTF-8'; + +/* +|-------------------------------------------------------------------------- +| Enable/Disable System Hooks +|-------------------------------------------------------------------------- +| +| If you would like to use the 'hooks' feature you must enable it by +| setting this variable to TRUE (boolean). See the user guide for details. +| +*/ +$config['enable_hooks'] = FALSE; + + +/* +|-------------------------------------------------------------------------- +| Class Extension Prefix +|-------------------------------------------------------------------------- +| +| This item allows you to set the filename/classname prefix when extending +| native libraries. For more information please see the user guide: +| +| http://codeigniter.com/user_guide/general/core_classes.html +| http://codeigniter.com/user_guide/general/creating_libraries.html +| +*/ +$config['subclass_prefix'] = 'CC_'; + + +/* +|-------------------------------------------------------------------------- +| Allowed URL Characters +|-------------------------------------------------------------------------- +| +| This lets you specify with a regular expression which characters are permitted +| within your URLs. When someone tries to submit a URL with disallowed +| characters they will get a warning message. +| +| As a security measure you are STRONGLY encouraged to restrict URLs to +| as few characters as possible. By default only these are allowed: a-z 0-9~%.:_- +| +| Leave blank to allow all characters -- but only if you are insane. +| +| DO NOT CHANGE THIS UNLESS YOU FULLY UNDERSTAND THE REPERCUSSIONS!! +| +*/ +$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; + + +/* +|-------------------------------------------------------------------------- +| Enable Query Strings +|-------------------------------------------------------------------------- +| +| By default CodeIgniter uses search-engine friendly segment based URLs: +| example.com/who/what/where/ +| +| By default CodeIgniter enables access to the $_GET array. If for some +| reason you would like to disable it, set 'allow_get_array' to FALSE. +| +| You can optionally enable standard query string based URLs: +| example.com?who=me&what=something&where=here +| +| Options are: TRUE or FALSE (boolean) +| +| The other items let you set the query string 'words' that will +| invoke your controllers and its functions: +| example.com/index.php?c=controller&m=function +| +| Please note that some of the helpers won't work as expected when +| this feature is enabled, since CodeIgniter is designed primarily to +| use segment based URLs. +| +*/ +$config['allow_get_array'] = TRUE; +$config['enable_query_strings'] = FALSE; +$config['controller_trigger'] = 'c'; +$config['function_trigger'] = 'm'; +$config['directory_trigger'] = 'd'; // experimental not currently in use + +/* +|-------------------------------------------------------------------------- +| Error Logging Threshold +|-------------------------------------------------------------------------- +| +| If you have enabled error logging, you can set an error threshold to +| determine what gets logged. Threshold options are: +| You can enable error logging by setting a threshold over zero. The +| threshold determines what gets logged. Threshold options are: +| +| 0 = Disables logging, Error logging TURNED OFF +| 1 = Error Messages (including PHP errors) +| 2 = Debug Messages +| 3 = Informational Messages +| 4 = All Messages +| +| For a live site you'll usually only enable Errors (1) to be logged otherwise +| your log files will fill up very fast. +| +*/ +$config['log_threshold'] = 0; + +/* +|-------------------------------------------------------------------------- +| Error Logging Directory Path +|-------------------------------------------------------------------------- +| +| Leave this BLANK unless you would like to set something other than the default +| application/logs/ folder. Use a full server path with trailing slash. +| +*/ +$config['log_path'] = ''; + +/* +|-------------------------------------------------------------------------- +| Date Format for Logs +|-------------------------------------------------------------------------- +| +| Each item that is logged has an associated date. You can use PHP date +| codes to set your own date formatting +| +*/ +$config['log_date_format'] = 'Y-m-d H:i:s'; + +/* +|-------------------------------------------------------------------------- +| Cache Directory Path +|-------------------------------------------------------------------------- +| +| Leave this BLANK unless you would like to set something other than the default +| system/cache/ folder. Use a full server path with trailing slash. +| +*/ +$config['cache_path'] = ''; + +/* +|-------------------------------------------------------------------------- +| Encryption Key +|-------------------------------------------------------------------------- +| +| If you use the Encryption class or the Session class you +| MUST set an encryption key. See the user guide for info. +| +*/ +$config['encryption_key'] = '123456'; + +/* +|-------------------------------------------------------------------------- +| Session Variables +|-------------------------------------------------------------------------- +| +| 'sess_cookie_name' = the name you want for the cookie +| 'sess_expiration' = the number of SECONDS you want the session to last. +| by default sessions last 7200 seconds (two hours). Set to zero for no expiration. +| 'sess_expire_on_close' = Whether to cause the session to expire automatically +| when the browser window is closed +| 'sess_encrypt_cookie' = Whether to encrypt the cookie +| 'sess_use_database' = Whether to save the session data to a database +| 'sess_table_name' = The name of the session database table +| 'sess_match_ip' = Whether to match the user's IP address when reading the session data +| 'sess_match_useragent' = Whether to match the User Agent when reading the session data +| 'sess_time_to_update' = how many seconds between CI refreshing Session Information +| +*/ +$config['sess_cookie_name'] = 'ci_session'; +$config['sess_expiration'] = 7200; +$config['sess_expire_on_close'] = FALSE; +$config['sess_encrypt_cookie'] = FALSE; +$config['sess_use_database'] = FALSE; +$config['sess_table_name'] = 'ci_sessions'; +$config['sess_match_ip'] = FALSE; +$config['sess_match_useragent'] = TRUE; +$config['sess_time_to_update'] = 300; + +/* +|-------------------------------------------------------------------------- +| Cookie Related Variables +|-------------------------------------------------------------------------- +| +| 'cookie_prefix' = Set a prefix if you need to avoid collisions +| 'cookie_domain' = Set to .your-domain.com for site-wide cookies +| 'cookie_path' = Typically will be a forward slash +| 'cookie_secure' = Cookies will only be set if a secure HTTPS connection exists. +| +*/ +$config['cookie_prefix'] = ""; +$config['cookie_domain'] = ""; +$config['cookie_path'] = "/"; +$config['cookie_secure'] = FALSE; + +/* +|-------------------------------------------------------------------------- +| Global XSS Filtering +|-------------------------------------------------------------------------- +| +| Determines whether the XSS filter is always active when GET, POST or +| COOKIE data is encountered +| +*/ +$config['global_xss_filtering'] = FALSE; + +/* +|-------------------------------------------------------------------------- +| Cross Site Request Forgery +|-------------------------------------------------------------------------- +| Enables a CSRF cookie token to be set. When set to TRUE, token will be +| checked on a submitted form. If you are accepting user data, it is strongly +| recommended CSRF protection be enabled. +| +| 'csrf_token_name' = The token name +| 'csrf_cookie_name' = The cookie name +| 'csrf_expire' = The number in seconds the token should expire. +*/ +$config['csrf_protection'] = FALSE; +$config['csrf_token_name'] = 'csrf_test_name'; +$config['csrf_cookie_name'] = 'csrf_cookie_name'; +$config['csrf_expire'] = 7200; + +/* +|-------------------------------------------------------------------------- +| Output Compression +|-------------------------------------------------------------------------- +| +| Enables Gzip output compression for faster page loads. When enabled, +| the output class will test whether your server supports Gzip. +| Even if it does, however, not all browsers support compression +| so enable only if you are reasonably sure your visitors can handle it. +| +| VERY IMPORTANT: If you are getting a blank page when compression is enabled it +| means you are prematurely outputting something to your browser. It could +| even be a line of whitespace at the end of one of your scripts. For +| compression to work, nothing can be sent before the output buffer is called +| by the output class. Do not 'echo' any values with compression enabled. +| +*/ +$config['compress_output'] = FALSE; + +/* +|-------------------------------------------------------------------------- +| Master Time Reference +|-------------------------------------------------------------------------- +| +| Options are 'local' or 'gmt'. This pref tells the system whether to use +| your server's local time as the master 'now' reference, or convert it to +| GMT. See the 'date helper' page of the user guide for information +| regarding date handling. +| +*/ +$config['time_reference'] = 'local'; + + +/* +|-------------------------------------------------------------------------- +| Rewrite PHP Short Tags +|-------------------------------------------------------------------------- +| +| If your PHP installation does not have short tag support enabled CI +| can rewrite the tags on-the-fly, enabling you to utilize that syntax +| in your view files. Options are TRUE or FALSE (boolean) +| +*/ +$config['rewrite_short_tags'] = FALSE; + + +/* +|-------------------------------------------------------------------------- +| Reverse Proxy IPs +|-------------------------------------------------------------------------- +| +| If your server is behind a reverse proxy, you must whitelist the proxy IP +| addresses from which CodeIgniter should trust the HTTP_X_FORWARDED_FOR +| header in order to properly identify the visitor's IP address. +| Comma-delimited, e.g. '10.0.1.200,10.0.1.201' +| +*/ +$config['proxy_ips'] = ''; + + +/* End of file config.php */ +/* Location: ./application/config/config.php */ diff --git a/controllers/Carpark.php b/controllers/Carpark.php index 35c85a2..d7008a6 100644 --- a/controllers/Carpark.php +++ b/controllers/Carpark.php @@ -2,73 +2,12 @@ /* file: carpark.php 停車管理 */ -if ( ! defined('BASEPATH')) exit('No direct script access allowed'); - -require_once(MQ_CLASS_FILE); - -// ----- 定義常數(路徑, cache秒數) ----- -define('APP_VERSION', '100'); // 版本號 -define('MAX_AGE', 604800); // cache秒數, 此定義1個月 -define('APP_NAME', 'carpark'); // 應用系統名稱 -define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views -//define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL -define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost') . ($_SERVER['SERVER_PORT'] != 60123 ? ':' . $_SERVER['SERVER_PORT'] : '') .'/'); // URL -define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑 -define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑 -define('WEB_LIB', SERVER_URL.'libs/'); // 網頁lib -define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib -define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path - -class Carpark extends CI_Controller +class Carpark extends CC_Controller { - var $vars = array(); // 共用變數 - function __construct() { - parent::__construct(); - // ----- 程式開發階段log設定 ----- - if (@ENVIRONMENT == 'development') - { - ini_set('display_errors', '1'); - //error_reporting(E_ALL ^ E_NOTICE); - error_reporting(E_ALL); - } - set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log - - // 共用記憶體 - $this->vars['mcache'] = new Memcache; - $this->vars['mcache']->connect(MEMCACHE_HOST, MEMCACHE_PORT) or die ('Could not connect memcache'); - - /* - // mqtt subscribe - $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid()); - //if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); } - $this->vars['mqtt']->connect(); - */ - - $this->load->model('carpark_model'); - $this->carpark_model->init($this->vars); - - // 資料介接模組 - $this->load->model('sync_data_model'); - $this->sync_data_model->init($this->vars); // for memcache - - // mqtt subscribe - $station_setting = $this->sync_data_model->station_setting_query(); - $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST; - $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT; - $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid()); - $this->vars['mqtt']->connect(); - - // init again - $this->sync_data_model->init($this->vars); // for mqtt - - // 產生 excel 報表 - $this->load->model('excel_model'); - $this->excel_model->init($this->vars); + parent::__construct('carpark'); - // [START] 2016/06/03 登入 - $this->load->model('user_model'); // load library $this->load->library(array('form_validation','session')); // load helpers @@ -77,73 +16,14 @@ class Carpark extends CI_Controller define('RESULT_SUCCESS', 'ok'); define('RESULT_FORM_VALIDATION_FAIL', '-1'); define('RESULE_FAIL', 'gg'); - // [START] 2016/06/03 登入 } - - // 發生錯誤時集中在此處理 - public function error_handler($errno, $errstr, $errfile, $errline, $errcontext) - { - // ex: car_err://message.... - $log_msg = explode('://', $errstr); - if (count($log_msg) > 1) - { - $log_file = LOG_PATH.$log_msg[0]; - $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n"; - } - else - { - $log_file = LOG_PATH.APP_NAME; - $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; - } - - //$str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; - error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 - } - - - // 顯示靜態網頁(html檔) - protected function show_page($page_name, &$data = null) - { - $page_file = PAGE_PATH.$page_name.'.php'; - $last_modified_time = filemtime($page_file); - - // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁 - // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月 - header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT'); - header('Etag: '. APP_VERSION); - header('Cache-Control: public'); - - // 20170921 - header("cache-Control: no-store, no-cache, must-revalidate"); - header("cache-Control: post-check=0, pre-check=0", false); - header("Pragma: no-cache"); - header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); - - if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) - { - header('HTTP/1.1 304 Not Modified'); - } - else - { - $this->load->view(APP_NAME.'/'.$page_name, $data); - } - } - - - // ------------------------------------------------ // // CRM (START) // // ------------------------------------------------ - // [test] ACTI crm alert - public function gen_test_alert() - { - echo 'test_alert'; - } - // [test] zzz public function gen_test_case() { @@ -203,11 +83,8 @@ class Carpark extends CI_Controller trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true)); - // 載入模組 - $this->load->model('cars_model'); - $this->cars_model->init($this->vars); - $this->cars_model->lprio($parms); - + // 執行 + $this->app_model('cars')->lprio($parms); echo 'ok'; exit; } @@ -229,11 +106,11 @@ class Carpark extends CI_Controller trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true)); - // 載入模組 - $this->load->model('cars_model'); - $this->cars_model->init($this->vars); - $this->cars_model->opendoor_lprio($parms); + // 初始 mqtt + $this->init_mqtt(); + // 執行 + $this->app_model('cars')->opendoor_lprio($parms); echo 'ok'; exit; } @@ -255,22 +132,22 @@ class Carpark extends CI_Controller trigger_error($LOG_FLAG . __FUNCTION__ . '..' . print_r($parms, true)); - // 載入模組 - $this->load->model('cars_model'); - $this->cars_model->init($this->vars); + // 初始 mqtt + $this->init_mqtt(); // 判斷會員身份 - $rows = $this->cars_model->get_member($lpr); + $cars_model = $this->app_model('cars'); + $rows = $cars_model->get_member($lpr); if ($rows['member_no'] == 0) { - $parms['ck'] = $this->cars_model->gen_opendoor_ck($parms, 'temp_opendoors'); // 臨停訊號 - $this->cars_model->do_temp_opendoor($parms); + $parms['ck'] = $cars_model->gen_opendoor_ck($parms, 'temp_opendoors'); // 臨停訊號 + $cars_model->do_temp_opendoor($parms); } else { - $parms['ck'] = $this->cars_model->gen_opendoor_ck($parms, 'member_opendoors'); // 月租訊號 - $this->cars_model->do_member_opendoor($parms); + $parms['ck'] = $cars_model->gen_opendoor_ck($parms, 'member_opendoors'); // 月租訊號 + $cars_model->do_member_opendoor($parms); } echo 'ok'; @@ -301,8 +178,10 @@ class Carpark extends CI_Controller if($topic == 'altob.888.mqtt') { + $data_model = $this->data_model(); + // 第一個場站編號 先不管場站 - $station_setting = $this->sync_data_model->station_setting_query(); + $station_setting = $data_model->station_setting_query(); $station_no_arr = explode(SYNC_DELIMITER_ST_NO, $station_setting['station_no']); $first_station_no = $station_no_arr[0]; @@ -325,7 +204,7 @@ class Carpark extends CI_Controller $msg_arr = explode(',', $msg); $group_id = isset($msg_arr[1]) && $msg_arr[1] == 2 ? 'M888' : 'C888'; $value = isset($msg_arr[2]) ? $msg_arr[2] : 0; - $result = $this->sync_data_model->force_sync_888($first_station_no, $group_id, $value); + $result = $data_model->force_sync_888($first_station_no, $group_id, $value); trigger_error($LOG_FLAG . __FUNCTION__ . "..{$first_station_no}|{$group_id}|{$value}..result..{$result}.."); } @@ -338,9 +217,11 @@ class Carpark extends CI_Controller { $reload = $this->input->post('reload', true); + $data_model = $this->data_model(); + if(isset($reload) && $reload > 0) { - $station_setting = $this->sync_data_model->station_setting_query(true); // 強制重新載入 + $station_setting = $data_model->station_setting_query(true); // 強制重新載入 trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true)); if(!$station_setting) @@ -352,30 +233,30 @@ class Carpark extends CI_Controller usleep(300000); // 0.3 sec delay // 費率資料同步 - $result = $this->sync_data_model->sync_price_plan(array('station_no_arr' => $station_setting['station_no'])); + $result = $data_model->sync_price_plan(array('station_no_arr' => $station_setting['station_no'])); trigger_error(__FUNCTION__ . '..sync_price_plan: '. $result); usleep(300000); // 0.3 sec delay // 會員資料同步 - $result = $this->sync_data_model->sync_members(array('station_no_arr' => $station_setting['station_no_list'])); // 20171116 upd + $result = $data_model->sync_members(array('station_no_arr' => $station_setting['station_no_list'])); // 20171116 upd trigger_error(__FUNCTION__ . '..sync_members: '. $result); usleep(300000); // 0.3 sec delay // 歐pa卡同步 - $result = $this->sync_data_model->sync_allpa_user(array('station_no_arr' => $station_setting['station_no'])); + $result = $data_model->sync_allpa_user(array('station_no_arr' => $station_setting['station_no'])); trigger_error(__FUNCTION__ . '..sync_allpa_user: '. $result); usleep(300000); // 0.3 sec delay // 在席資料同步 - $result = $this->sync_data_model->sync_pks_groups_reload($station_setting); + $result = $data_model->sync_pks_groups_reload($station_setting); trigger_error(__FUNCTION__ . '..sync_pks_groups_reload: '. $result); } else { - $station_setting = $this->sync_data_model->station_setting_query(false); + $station_setting = $data_model->station_setting_query(false); if(!$station_setting) { @@ -414,24 +295,26 @@ class Carpark extends CI_Controller } } + $data_model = $this->data_model(); + // 0. 取得場站設定 - $station_setting = $this->sync_data_model->station_setting_query(false); + $station_setting = $data_model->station_setting_query(false); trigger_error(__FUNCTION__ . '..station_setting: '. print_r($station_setting, true)); $station_no_arr = array('station_no_arr' => $station_setting['station_no']); // 1. 月租系統 - $result = $this->sync_data_model->sync_members($station_no_arr); + $result = $data_model->sync_members($station_no_arr); trigger_error(__FUNCTION__ . '..sync_members: '. $result); // 2. 同步車牌更換 if(!empty($switch_lpr_arr)) { - $this->sync_data_model->sync_switch_lpr($switch_lpr_arr); + $data_model->sync_switch_lpr($switch_lpr_arr); } // 3. 歐pa卡同步 (TODO: 暫時放在這) - $result = $this->sync_data_model->sync_allpa_user($station_no_arr); + $result = $data_model->sync_allpa_user($station_no_arr); trigger_error(__FUNCTION__ . '..sync_allpa_user: '. $result); } @@ -452,7 +335,7 @@ class Carpark extends CI_Controller exit; } - $data = $this->sync_data_model->get_last_unbalanced_cario($lpr, $station_no); + $data = $this->data_model()->get_last_unbalanced_cario($lpr, $station_no); echo json_encode($data, JSON_UNESCAPED_UNICODE); } @@ -476,48 +359,17 @@ class Carpark extends CI_Controller } // 臨停繳費 - $this->load->model('carpayment_model'); - echo $this->carpayment_model->p2payed(array('seqno' => $cario_no, 'lpr' => $lpr, 'amt' => $amt), true); + echo $this->app_model('carpayment')->p2payed(array('seqno' => $cario_no, 'lpr' => $lpr, 'amt' => $amt), true); exit; - //echo 'ok'; - } - - // 驗証 IP - function is_ip_valid() - { - $client_ip = $this->my_ip(); - if(!in_array($client_ip, array('61.219.172.11', '61.219.172.82'))) - { - trigger_error('..block..from:'.$client_ip.'..unknown network..'); - return false; - } - return true; - } - - // 取得 IP - function my_ip() - { - if (getenv('HTTP_X_FORWARDED_FOR')) - { - $ip = getenv('HTTP_X_FORWARDED_FOR'); - } - elseif (getenv('HTTP_X_REAL_IP')) - { - $ip = getenv('HTTP_X_REAL_IP'); - } - else { - $ip = $_SERVER['REMOTE_ADDR']; - } - - return $ip; } // 同步 (由排程呼叫) public function sync_minutely() { - $this->sync_data_model->sync_pks_groups(); // 同步在席現況 + $this->data_model()->sync_pks_groups(); // 同步在席現況 } + /* // 20170816 手動新增入場資料 public function gen_carin() { @@ -546,6 +398,7 @@ class Carpark extends CI_Controller $this->carpark_model->gen_carin($parms); echo 'ok'; } + */ // ------------------------------------------------ // @@ -602,7 +455,7 @@ class Carpark extends CI_Controller 'pswd' => $this->input->post('pswd', true) ); - $result = $this->user_model->user_login($data); + $result = $this->app_model('user')->user_login($data); if($result) { @@ -636,29 +489,14 @@ class Carpark extends CI_Controller - + /* // response http protected function http_return($return_code, $type) { if ($type == 'text') echo $return_code; else echo json_encode($return_code, JSON_UNESCAPED_UNICODE); - - } - - // 送出html code - public function get_html() - { - /* - $data = array - ( - 'company_no' => $this->input->post('company_no', true), // 場站統編 - 'hq_company_no' => '80682490' - ); - $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), $data); - */ - $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), array()); - } + } // 讀取cookie內容 protected function get_cookie($cookie_name) @@ -667,7 +505,6 @@ class Carpark extends CI_Controller return(json_decode($_COOKIE[$cookie_name], true)); } - // 儲存cookie內容 protected function save_cookie($cookie_name, $cookie_info) { @@ -692,7 +529,6 @@ class Carpark extends CI_Controller $data = $this->carpark_model->available_set(); $this->http_return($data, 'json'); } - // 剩餘車位數更新 public function available_update() @@ -704,7 +540,6 @@ class Carpark extends CI_Controller $this->http_return($this->carpark_model->available_update($station_no, $data), 'json'); } - // 剩餘車位數查核 public function available_check() @@ -713,21 +548,6 @@ class Carpark extends CI_Controller $this->http_return($this->carpark_model->available_check($time_point), 'json'); } - - // 顯示logs - public function show_logs() - { - $lines = $this->uri->segment(3); // 顯示行數 - if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行 - if($lines > 1000) $lines = 1000; // 最多 1000行 - - // echo '
';
-        echo '
';      
-       
-		passthru('/usr/bin/tail -n ' . $lines . '  ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt');	// 利用linux指令顯示倒數幾行的logs內容 
-        echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';   
-        echo '
'; - } // 顯示logs (cars grep 888) public function show_888_logs() @@ -745,8 +565,9 @@ class Carpark extends CI_Controller passthru('/usr/bin/tail -n ' . $lines . ' ' . $target_str); // 利用linux指令顯示倒數幾行的logs內容 echo "\n----- " . $target_str . ' -----'; echo '
'; - } + } + // 新增月租資料 public function member_add() { @@ -783,15 +604,8 @@ class Carpark extends CI_Controller $this->carpark_model->member_add($data); echo 'ok'; } - - - // 查詢月租資料 - public function member_query() - { - $data = $this->carpark_model->member_query(); - echo json_encode($data, JSON_UNESCAPED_UNICODE); - } - + + // 刪除月租資料 public function member_delete() @@ -800,16 +614,7 @@ class Carpark extends CI_Controller $this->carpark_model->member_delete($member_no); echo 'ok'; } - - - // 進出場現況表 - public function cario_list() - { - $data = $this->carpark_model->cario_list(); - echo json_encode($data, JSON_UNESCAPED_UNICODE); - } - - + // 進出場事件即時顯示 public function cario_event() { @@ -819,16 +624,7 @@ class Carpark extends CI_Controller } } - - - // 顯示圖檔(http://url/carpark.html/pics/lpr_ABY8873_O_0_0_C_20150919210022) - public function pics() - { - // ??? - readfile(CAR_PIC.$this->uri->segment(3).'/'.str_replace('/', '', $this->uri->segment(4)).'.jpg'); - } - - + // 汽車開門 public function opendoors() { @@ -868,27 +664,7 @@ class Carpark extends CI_Controller echo json_encode($data); } - - // 車號入場查詢 - public function carin_lpr_query() - { - $lpr = $this->uri->segment(3); - $data = $this->carpark_model->carin_lpr_query($lpr); - echo json_encode($data); - } - - - // 以時間查詢入場資訊 - public function carin_time_query() - { - $time_query = $this->input->post('time_query', true); - $minutes_range = $this->input->post('minutes_range', true); - - $data = $this->carpark_model->carin_time_query($time_query, $minutes_range); - echo json_encode($data); - } - - // 調撥車道設定 + // 調撥車道設定 public function reversible_lane_set() { $lane_no = $this->input->post('lane_no', true); @@ -953,24 +729,64 @@ class Carpark extends CI_Controller echo json_encode($data, JSON_UNESCAPED_UNICODE); } + */ + + // 查詢月租資料 + public function member_query() + { + $data = $this->app_model()->member_query(); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + } + + // 進出場現況表 + public function cario_list() + { + $data = $this->app_model()->cario_list(); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + } + + // 顯示圖檔(http://url/carpark.html/pics/lpr_ABY8873_O_0_0_C_20150919210022) + public function pics() + { + readfile(CAR_PIC.$this->uri->segment(3).'/'.str_replace('/', '', $this->uri->segment(4)).'.jpg'); + } + + // 車號入場查詢 + public function carin_lpr_query() + { + $lpr = $this->uri->segment(3); + $data = $this->app_model()->carin_lpr_query($lpr); + echo json_encode($data); + } + + // 以時間查詢入場資訊 + public function carin_time_query() + { + $time_query = $this->input->post('time_query', true); + $minutes_range = $this->input->post('minutes_range', true); + + $data = $this->app_model()->carin_time_query($time_query, $minutes_range); + echo json_encode($data); + } + // 查詢行動支付記錄 public function tx_bill_query() { - $data = $this->carpark_model->tx_bill_query(); + $data = $this->app_model()->tx_bill_query(); echo json_encode($data, JSON_UNESCAPED_UNICODE); } // 查詢月租繳款機記錄 public function tx_bill_ats_query() { - $data = $this->carpark_model->tx_bill_ats_query(); + $data = $this->app_model()->tx_bill_ats_query(); echo json_encode($data, JSON_UNESCAPED_UNICODE); } // 查詢樓層在席群組 public function pks_group_query() { - $data = $this->carpark_model->pks_group_query(); + $data = $this->app_model()->pks_group_query(); echo json_encode($data, JSON_UNESCAPED_UNICODE); } @@ -980,7 +796,11 @@ class Carpark extends CI_Controller $group_id = $this->uri->segment(3); // id $value = $this->uri->segment(4, 0); // value $station_no = $this->uri->segment(5); // station_no - $data = $this->sync_data_model->pks_availables_update($group_id, $value, true, $station_no); + + // 初始 mqtt + $this->init_mqtt(); + + $data = $this->data_model()->pks_availables_update($group_id, $value, true, $station_no); echo json_encode($data, JSON_UNESCAPED_UNICODE); } @@ -1002,11 +822,6 @@ class Carpark extends CI_Controller } */ - public function test() - { - //echo `whoami`; - //echo phpinfo(); - } // 博辰測試用 // http://localhost/carpark.html/test_8068_002/APK7310 @@ -1071,14 +886,4 @@ class Carpark extends CI_Controller exit; } - // test - public function test_phpexcel() - { - $query_year = 2017; - $query_month = 3; - - //$this->excel_model->export_cario_data($query_year, $query_month); - $this->excel_model->export_members(); - } - } diff --git a/controllers/Cars.php b/controllers/Cars.php index 9f17096..20aeb68 100644 --- a/controllers/Cars.php +++ b/controllers/Cars.php @@ -1,62 +1,16 @@ - -response: -var ip='66.249.82.183';var clientid='565162cb67dfb';var mqtt_ip='192.168.51.11'; - +file: cars.php 車輛進出場處理 */ - -// ----- 定義常數(路徑, cache秒數) ----- - define('APP_VERSION', '100'); // 版本號 - - define('MAX_AGE', 604800); // cache秒數, 此定義1個月 - define('APP_NAME', 'cars'); // 應用系統名稱 - - define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views - - define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL - define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑 - define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑 - define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib - define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib - define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name - define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/cario.'); // log file name - -require_once(MQ_CLASS_FILE); - -class Cars extends CI_Controller +class Cars extends CC_Controller { - var $vars = array(); - function __construct() { - // $this->time_start = microtime(true); - parent::__construct(); - - // ----- 程式開發階段log設定 ----- - if (@ENVIRONMENT == 'development') - { - ini_set('display_errors', '1'); - //error_reporting(E_ALL ^ E_NOTICE); - error_reporting(E_ALL); - } - set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log - - $method_name = $this->router->fetch_method(); - $request_assoc = $this->uri->uri_to_assoc(3); - trigger_error(__FUNCTION__ . '..' . $method_name. '..request start..' . print_r($request_assoc, true)); + parent::__construct('cars'); ignore_user_abort(); // 接受client斷線, 繼續run - if(in_array($method_name, array( + if(in_array($this->router->fetch_method(), array( 'ipcam', 'ipcam_meta', 'check_lpr_etag', 'opendoor', @@ -74,76 +28,6 @@ class Cars extends CI_Controller ob_end_flush(); flush(); } - - $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21) - $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621) - $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012) - //$this->vars['station_no'] = STATION_NO; // 本站編號 - - //session_id(ip2long($_SERVER['REMOTE_ADDR'])); // 設定同一device為同一個session - //session_start(); - - // 共用記憶體 - $this->vars['mcache'] = new Memcache; - $this->vars['mcache']->pconnect(MEMCACHE_HOST, MEMCACHE_PORT); // or die ('Could not connect memcache'); - - /* - // mqtt subscribe - $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid()); - //if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); } - if(!$this->vars['mqtt']->connect()){ trigger_error('..Could not connect mqtt..go on..'); } - */ - - // 資料介接模組 - $this->load->model('sync_data_model'); - $this->sync_data_model->init($this->vars); // for memcache - - // mqtt subscribe - $station_setting = $this->sync_data_model->station_setting_query(); - $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST; - $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT; - $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid()); - $this->vars['mqtt']->connect(); - - // init again - $this->sync_data_model->init($this->vars); // for mqtt - - $this->load->model('cars_model'); - $this->cars_model->init($this->vars); - } - - - // 發生錯誤時集中在此處理 - public function error_handler($errno, $errstr, $errfile, $errline, $errcontext) - { - $log_msg = explode('://', $errstr); - if (count($log_msg) > 1) - { - $log_file = $log_msg[0]; - $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n"; - } - else - { - $log_file = APP_NAME; - $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; - } - - error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 - } - - - // 顯示logs - public function show_logs() - { - $lines = $this->uri->segment(3); // 顯示行數 - if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行 - - // echo '
';
-        echo '
';      
-       
-		passthru('/usr/bin/tail -n ' . $lines . '  ' . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt');	// 利用linux指令顯示倒數幾行的logs內容 
-        echo "\n----- " . LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';   
-        echo '
'; } /* @@ -153,9 +37,13 @@ class Cars extends CI_Controller */ public function opendoor() { + // 初始 mqtt + $this->init_mqtt(); + + // 執行 $parms = $this->uri->uri_to_assoc(3); $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌 - $this->cars_model->opendoor_lprio($parms); + $this->app_model()->opendoor_lprio($parms); } // IVS -> 車號, 影像 @@ -179,12 +67,13 @@ http://192.168.10.201/cars.html/ipcam/sno/12119/ivsno/0/io/O/type/C/lpr/4750YC/c 上傳圖檔欄位名稱為cars */ public function ipcam() - { + { + // 執行 $parms = $this->uri->uri_to_assoc(3); $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌 // 同步並送出一次出入口 888 - $this->sync_data_model->sync_888($parms); + $this->data_model()->sync_888($parms); $pic_folder = CAR_PIC.$this->vars['date_num'].'/'; // 今日資料夾名(yyyymmdd) if (!file_exists($pic_folder)) mkdir($pic_folder); // 如果資料夾不存在, 建立日期資料夾 @@ -220,8 +109,22 @@ http://192.168.10.201/cars.html/ipcam/sno/12119/ivsno/0/io/O/type/C/lpr/4750YC/c $parms['curr_time_str'] = $this->vars['date_time']; // 現在時間, 例2015-09-21 15:36:47 $parms['pic_name'] = $config['file_name']; // 圖片檔名 - $this->cars_model->lprio($parms); // 測試eTag - } + $this->app_model()->lprio($parms); // 測試eTag + } + + /* + 出入口 + + 說明: 特殊方式進出註記 (ex. 悠遊卡) + */ + public function ipcam_meta() + { + $parms = $this->uri->uri_to_assoc(3); + $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌 + + // 執行 + $this->app_model()->ipcam_meta($parms); + } // 用車牌與eTag, 檢查資料庫 public function check_lpr_etag() @@ -229,21 +132,23 @@ http://192.168.10.201/cars.html/ipcam/sno/12119/ivsno/0/io/O/type/C/lpr/4750YC/c $lpr = $this->uri->segment(3); $etag = $this->uri->segment(4); - $this->cars_model->check_lpr_etag($lpr, $etag); + // 執行 + $this->app_model()->check_lpr_etag($lpr, $etag); exit; } // 開門 (臨停) public function temp_opendoors() { - $parms['ivsno'] = $this->uri->segment(3); + $parms['ivsno'] = $this->uri->segment(3); $parms['lpr'] = $this->uri->segment(4); $parms['ck'] = $this->uri->segment(5); + + // 初始 mqtt + $this->init_mqtt(); - // 載入 - $this->load->model('cars_model'); - $this->cars_model->init($this->vars); - $result = $this->cars_model->do_temp_opendoor($parms); + // 執行 + $result = $this->app_model()->do_temp_opendoor($parms); trigger_error(__FUNCTION__ . "..{$result}.." . print_r($parms, true)); exit; } @@ -255,36 +160,13 @@ http://192.168.10.201/cars.html/ipcam/sno/12119/ivsno/0/io/O/type/C/lpr/4750YC/c $parms['lpr'] = $this->uri->segment(4); $parms['ck'] = $this->uri->segment(5); - // 載入 - $this->load->model('cars_model'); - $this->cars_model->init($this->vars); - $result = $this->cars_model->do_member_opendoor($parms); + // 初始 mqtt + $this->init_mqtt(); + + // 執行 + $result = $this->app_model()->do_member_opendoor($parms); trigger_error(__FUNCTION__ . "..{$result}.." . print_r($parms, true)); exit; } - - /* - 出入口 - - 說明: 特殊方式進出註記 (ex. 悠遊卡) - */ - public function ipcam_meta() - { - $parms = $this->uri->uri_to_assoc(3); - $parms['lpr'] = urldecode($parms['lpr']); // 中文車牌 - $this->cars_model->ipcam_meta($parms); - } - - /* - public function test_now() - { - echo date('Y-m-d H:i:s'); - } - - public function test_phpinfo() - { - phpinfo(); - } - */ } diff --git a/core/CC_Controller.php b/core/CC_Controller.php new file mode 100644 index 0000000..b709ac4 --- /dev/null +++ b/core/CC_Controller.php @@ -0,0 +1,171 @@ +router->fetch_method(); + $request_assoc = $this->uri->uri_to_assoc(3); + trigger_error(__FUNCTION__ . '://..' . $method_name. '..uri_to_assoc..' . print_r($request_assoc, true)); + + // ----- 常數 ----- + define('APP_VERSION', '100'); // 版本號 + define('MAX_AGE', 604800); // cache秒數, 此定義1個月 + define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL + define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib + define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib + define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑 + define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑 + define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views + + $this->vars = array(); + $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21) + $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); // 數字化時間(20151012143621) + $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012) + + // 共用記憶體 + $this->vars['mcache'] = new Memcache; + if(!$this->vars['mcache']->pconnect(MEMCACHE_HOST, MEMCACHE_PORT)){ trigger_error('..Could not connect mcache..'); } + } + + // 發生錯誤時集中在此處理 + public function error_handler($errno, $errstr, $errfile, $errline, $errcontext) + { + $log_msg = explode('://', $errstr); + if (count($log_msg) > 1 && substr($log_msg[0], -4) != 'http') + { + $log_file = $log_msg[0]; + $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n"; + } + else + { + $log_file = APP_NAME; + $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; + } + + error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 + } + + // 顯示靜態網頁(html檔) + public function show_page($page_name, &$data = null) + { + $page_file = PAGE_PATH.$page_name.'.php'; + $last_modified_time = filemtime($page_file); + + // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁 + // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月 + header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT'); + header('Etag: '. APP_VERSION); + header('Cache-Control: public'); + + // 20170921 + header("cache-Control: no-store, no-cache, must-revalidate"); + header("cache-Control: post-check=0, pre-check=0", false); + header("Pragma: no-cache"); + header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); + + if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) + { + header('HTTP/1.1 304 Not Modified'); + } + else + { + $this->load->view(APP_NAME.'/'.$page_name, $data); + } + } + + // 顯示logs + public function show_logs() + { + $lines = $this->uri->segment(3); // 顯示行數 + if (empty($lines)) $lines = 140; // 無行數參數, 預設為40行 + + // echo '
';
+        echo '
';      
+       
+		passthru('/usr/bin/tail -n ' . $lines . '  ' . LOG_PATH . APP_NAME . '.' . date('Ymd').'.log.txt');	// 利用linux指令顯示倒數幾行的logs內容 
+        echo "\n----- " . LOG_PATH . APP_NAME . '.' . date('Ymd').'.log.txt' . ' -----';   
+        echo '
'; + } + + // 送出html code + public function get_html() + { + $this->load->view(APP_NAME.'/'.$this->input->post('tag_name', true), array()); + } + + // 驗証 IP + public function is_ip_valid() + { + $client_ip = $this->my_ip(); + if(!in_array($client_ip, array('127.0.0.1', '61.219.172.11', '61.219.172.82'))) + { + trigger_error('..block..from:'.$client_ip.'..unknown network..'); + return false; + } + return true; + } + + // 取得 IP + public function my_ip() + { + if (getenv('HTTP_X_FORWARDED_FOR')) + { + $ip = getenv('HTTP_X_FORWARDED_FOR'); + } + elseif (getenv('HTTP_X_REAL_IP')) + { + $ip = getenv('HTTP_X_REAL_IP'); + } + else { + $ip = $_SERVER['REMOTE_ADDR']; + } + + return $ip; + } + + // 啟動 MQTT + public function init_mqtt() + { + require_once(MQ_CLASS_FILE); + $station_setting = $this->data_model()->station_setting_query(); + $mqtt_ip = isset($station_setting['mqtt_ip']) ? $station_setting['mqtt_ip'] : MQ_HOST; + $mqtt_port = isset($station_setting['mqtt_port']) ? $station_setting['mqtt_port'] : MQ_PORT; + $this->vars['mqtt'] = new phpMQTT($mqtt_ip, $mqtt_port, uniqid()); + $this->vars['mqtt']->connect(); + } + + // 資料同步模組 + public function data_model() + { + return $this->app_model('sync_data'); + } + + // 啟動模組 + public function app_model($app_name='') + { + $model_name = !empty($app_name) ? $app_name . '_model' : APP_NAME . '_model'; + $this->load->model($model_name); + $this->$model_name->init($this->vars); + return $this->$model_name; + } +} diff --git a/core/index.html b/core/index.html new file mode 100644 index 0000000..b702fbc --- /dev/null +++ b/core/index.html @@ -0,0 +1,11 @@ + + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + diff --git a/models/User_model.php b/models/User_model.php index c083cff..9b1cb92 100644 --- a/models/User_model.php +++ b/models/User_model.php @@ -5,12 +5,18 @@ file: User_model.php 管理登入系統 class User_model extends CI_Model { + var $vars = array(); function __construct() { parent::__construct(); $this->load->database(); } + + public function init($vars) + { + $this->vars = $vars; + } // 登入 public function user_login($data)