login.class.php 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of login
  7. *
  8. * @author Administrator
  9. */
  10. class login {
  11. private static $_login;
  12. public static function getInstance() {
  13. if (!self::$_login) {
  14. $c = __CLASS__;
  15. self::$_login = new $c;
  16. }
  17. return self::$_login;
  18. }
  19. private function getLoginSql($uname) {
  20. return "select user_login,(select active from public.employee ee where ee.employee_id=u.employee_id) as employee_id_active, can_visit_vgm,can_add_booking, can_add_tk_status,truck_driver,po_booking,o_final_delivery_u,ipad_view_po,can_view_doc,can_upload_doc,can_add_catalog,can_add_po,packing_list_company,is_only_vgm,contact_id_user,is_demo, ra_password as password,employee_id, contact_id, user_type, last_pwd_change, EXTRACT(DAY from (now() - last_pwd_change)) as last_pwd_change_date, email, user_webtype_id, active, is_online, station, allow_login_remote, can_see_amslog,can_view_eccn, can_see_isflog, can_see_isflog_withaddress,
  21. customer_search_type, customer_destination, can_add_ams, can_add_isf, air_station, air_sales, ocean_station, ocean_sales,ocean_following_sales,ocean_following_sales_or,air_following_sales,air_following_sales_or, trucking_station, ocean_dest_op, can_see_password, can_add_opsales_code, ocean_station_or, ocean_agent_or, ocean_sales_or, ocean_dest_op_or, air_station_or, air_sales_or, trucking_station_or,
  22. can_add_user, can_add_employee, can_add_contact, company_name, ams_email, isf_email, customer_discharge, online_active, is_super, ocean_agent,active, can_send_email, view_file_format as docdownload, container_status, consolidated_cbsa_code, can_add_aci,
  23. air_customers, air_customer_search_type,trucking_customers,trucking_customer_search_type, upload_document, view_file_format, event_type, belong_schemas, main_schemas, error_login_count, EXTRACT(EPOCH FROM (now()-COALESCE(error_login_time, now()))) as second, po_status, view_air_file_format,
  24. special_customer_event, can_edi_vgm, isf_aci_ams_station,login_version,is_kerry_shipment,can_visit_delivery,currency_group from public.ra_online_user u where lower(user_login) = '" . strtolower($uname) . "'";
  25. }
  26. public function do_login() {
  27. $login_error_times = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Login_Error_Times'");
  28. $lock_user_seconds = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Lock_User_Seconds'");
  29. if (!empty($uname) || !empty($password)) {
  30. } else {
  31. $uname = common::check_input($_POST['uname']);
  32. $is_verify = common::check_input($_POST['verifcation_code']);
  33. //首先校用户登录
  34. $AES_encrypted = $this->AES_encrypted($is_verify);
  35. $secret_key = common::excuteOneSql("select secret_key from customer_service_secret_key
  36. where secret_key = '$is_verify'
  37. and create_time >= current_date - INTERVAL '3 months' limit 1");
  38. //记录这次的密钥记录
  39. common::excuteUpdateSql("INSERT INTO public.customer_service_secret_key(secret_key, create_time)VALUES ('$is_verify', now());");
  40. if(!empty($AES_encrypted) && empty($secret_key)){
  41. }else{
  42. $data = array(
  43. 'msg' => 'verifcation_error',
  44. 'data' => ''
  45. );
  46. common::echo_json_encode(400, $data);
  47. exit();
  48. }
  49. $sql = $this->getLoginSql($uname);
  50. $rs = common::excuteObjectSql($sql);
  51. if (!empty($rs)) {
  52. if (empty($rs['belong_schemas'])) {
  53. $rs['belong_schemas'] = "public";
  54. }
  55. if (empty($rs['main_schemas'])) {
  56. $rs['main_schemas'] = "public";
  57. }
  58. //验证employee是否active
  59. if (!empty($rs["employee_id"]) && $rs["employee_id_active"] != "t") {
  60. if (strtolower(Soure) =='topocean'){
  61. if (strtolower($rs['user_type']) == "employee" && utils::endWith($rs['email'], "cn")) {
  62. $data = "<a href='mailto:lilyyang@topocean.com.cn'>lilyyang@topocean.com.cn</a>";
  63. }else{
  64. $data = "<a href='mailto:winnie@topocean.com'>winnie@topocean.com</a>";
  65. }
  66. }
  67. if (strtolower(Soure) =='apex'){
  68. $data = "<a href='mailto:maria.wang@apexshipping.com.cn'>maria.wang@apexshipping.com.cn</a>";
  69. }
  70. $data = array(
  71. 'code' => 'no_active',
  72. 'login_version' => $rs["login_version"],
  73. 'data' => $data,
  74. 'msg' => "Please check with Doc Center $data for searching function"
  75. );
  76. common::echo_json_encode(500, $data);
  77. $this->failedLogin($uname, 'Employee not active');
  78. exit();
  79. }
  80. //处理登录状态
  81. $userInfo = common::check_input($_COOKIE['userInfo']);
  82. $noCheckPwd = false;
  83. if (!empty($userInfo)) {
  84. $userInfoSplit = explode("_", $userInfo);
  85. if ($uname == $userInfoSplit[0]) {
  86. if ($userInfoSplit[1] == md5($rs['password'])) {
  87. $noCheckPwd = true;
  88. }
  89. }
  90. }
  91. if ($rs['error_login_count'] > $login_error_times && $rs['second'] < $lock_user_seconds) {
  92. $data = array(
  93. 'msg' => 'error_times',
  94. 'login_version' => $rs["login_version"],
  95. 'data' => ceil(($lock_user_seconds - $rs['second']) / 60)
  96. );
  97. common::echo_json_encode(400, $data);
  98. $this->failedLogin($uname, 'Failed login too times');
  99. exit();
  100. }
  101. if ($rs['is_online'] != 't') {
  102. if (strtolower($rs['user_type']) != "employee") {
  103. $data =array(
  104. 'code' => 'no_online',
  105. 'login_version' => $rs["login_version"],
  106. 'data' => '',
  107. 'msg' => 'No activation or insufficient permissions'
  108. );
  109. common::echo_json_encode(500, $data);
  110. $this->failedLogin($uname, 'Online is not active');
  111. exit();
  112. }
  113. }
  114. //if ($rs['decrypt_password'] != $_POST['psw']) {
  115. if ($noCheckPwd) {
  116. }else{
  117. if ($rs['password'] != $_POST['psw']) {
  118. common::excuteUpdateSql("update public.ra_online_user set error_login_count=error_login_count+1, error_login_time=now() where lower(user_login) = '" . strtolower($uname) . "'");
  119. $data = array(
  120. 'msg' => 'password_error',
  121. 'login_version' => $rs["login_version"],
  122. 'data' => ''
  123. );
  124. common::echo_json_encode(400, $data);
  125. $this->failedLogin($uname, 'Password is wrong');
  126. exit();
  127. }
  128. }
  129. if ($rs['online_active'] != 't') {
  130. $data = array(
  131. 'code' => 'no_active',
  132. 'login_version' => $rs["login_version"],
  133. 'data' => '',
  134. 'msg' => 'Please check with Doc Center for searching function'
  135. );
  136. common::echo_json_encode(500, $data);
  137. $this->failedLogin($uname, 'Online is not active');
  138. exit();
  139. }
  140. //check password length
  141. $tar = utils::checkPassword($rs['password']);
  142. if (!empty($tar)) {
  143. $data = array(
  144. 'code' => $tar,
  145. 'login_version' => $rs["login_version"],
  146. 'data' => '',
  147. 'msg' => $tar
  148. );
  149. common::echo_json_encode(500, $data);
  150. exit();
  151. }
  152. if (empty($rs['last_pwd_change'])) {
  153. $data = array(
  154. 'code' => 'first_login',
  155. 'login_version' => $rs["login_version"],
  156. 'data' => '',
  157. 'msg' => 'First login, please change your password'
  158. );
  159. common::echo_json_encode(500, $data);
  160. exit();
  161. }
  162. //get more infor by employee_id or contact_id
  163. $sql = '';
  164. $diffdate = $rs['last_pwd_change_date'];
  165. $user_type = $rs['user_type'];
  166. //if user is customer, check company
  167. if (strtolower($user_type) == 'customer') {
  168. $company = $rs['company_name'];
  169. } else {
  170. if (!empty($rs['station']))
  171. $company = $rs['station'];
  172. }
  173. if (strtolower($uname) == 'ra.admin') {
  174. $company = 'Admin';
  175. }
  176. // get system config
  177. $sql = "SELECT lower(ra_name) as ra_name, ra_value from ra_online_config where lower(ra_name) in ('employee_session_timeout', 'customer_session_timeout', 'password_change_alert', 'employee_password_change_cycle', 'customer_password_change_cycle')";
  178. $rs1s = common::excuteListSql($sql);
  179. foreach ($rs1s as $rs1) {
  180. if ($rs1['ra_name'] == 'employee_session_timeout')
  181. $EMPLOYEE_SESSION_TIMEOUT = $rs1['ra_value'];
  182. if ($rs1['ra_name'] == 'customer_session_timeout')
  183. $CUSTOMER_SESSION_TIMEOUT = $rs1['ra_value'];
  184. if ($rs1['ra_name'] == 'password_change_alert')
  185. $PASSWORD_CHANGE_ALERT = $rs1['ra_value'];
  186. if ($rs1['ra_name'] == 'employee_password_change_cycle')
  187. $EMPLOYEE_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  188. if ($rs1['ra_name'] == 'customer_password_change_cycle')
  189. $CUSTOMER_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  190. }
  191. $sql="select item_value from config where item='passwordChangePeriod'";
  192. $pcp = common::excuteObjectSql($sql);
  193. $passwordChangePeriod = json_decode($pcp["item_value"],true);
  194. if (strtolower($rs['user_type']) == 'employee') {
  195. $PASSWORD_CHANGE_CYCLE = $EMPLOYEE_PASSWORD_CHANGE_CYCLE;
  196. $SESSION_TIMEOUT = $EMPLOYEE_SESSION_TIMEOUT;
  197. //如果有新配置,则采用新配置
  198. if (!empty($pcp)) {
  199. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Employee"]["days"];
  200. $PASSWORD_CHANGE_ALERT = $passwordChangePeriod["Employee"]["advanceDays"];
  201. }
  202. } else {
  203. $PASSWORD_CHANGE_CYCLE = $CUSTOMER_PASSWORD_CHANGE_CYCLE;
  204. $SESSION_TIMEOUT = $CUSTOMER_SESSION_TIMEOUT;
  205. //如果有新配置,则采用新配置
  206. if (!empty($pcp)) {
  207. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Customer"]["days"];
  208. $PASSWORD_CHANGE_ALERT = $passwordChangePeriod["Customer"]["advanceDays"];
  209. }
  210. }
  211. $loginName = $rs['user_login'];
  212. $email = $rs['email'];
  213. //Timeout
  214. if ($diffdate > $PASSWORD_CHANGE_CYCLE) {
  215. if(empty($email)){
  216. $data = array(
  217. 'status' => '0',
  218. 'code' => 'login user email is empty',
  219. 'login_version' => $rs["login_version"],
  220. 'msg' => 'login user email is empty'
  221. );
  222. common::echo_json_encode(500, $data);
  223. exit();
  224. }else{
  225. $this -> passwordExpires($loginName,$email,$uname);
  226. }
  227. }
  228. if ($diffdate == $PASSWORD_CHANGE_CYCLE) {// Due today
  229. $login_tmp = array(
  230. 'msg' => 'today',
  231. 'uname' => $uname,
  232. 'login_version' => $rs["login_version"],
  233. 'data' => ''
  234. );
  235. } elseif ($diffdate >= ($PASSWORD_CHANGE_CYCLE - $PASSWORD_CHANGE_ALERT)) {// Password expires soon, JS Tips
  236. $login_tmp = array(
  237. 'msg' => 'last',
  238. 'uname' => $uname,
  239. 'login_version' => $rs["login_version"],
  240. 'data' => $PASSWORD_CHANGE_CYCLE - $diffdate,
  241. 'is_only_vgm' => $rs["is_only_vgm"]
  242. );
  243. }
  244. //insert into log table
  245. $ip = common::ip();
  246. $sql = "insert into public.ra_online_user_login_log (user_name,manufacturer,from_app,ip,date_time, session_id) values ('" . common::check_input($uname) . "', '" . common::check_input($company) . "','Online','$ip',now(), '" . session_id() . "')";
  247. if (common::excuteUpdateSql($sql)) {
  248. common::excuteUpdateSql("update public.ra_online_user set Last_Login_Time = now(), error_login_count=0, error_login_time=null where lower(user_login) = '" . strtolower($uname) . "'");
  249. if (isset($login_tmp)) {
  250. common::echo_json_encode(200, $login_tmp);
  251. } else {
  252. $data = array(
  253. 'msg' => 'success',
  254. 'uname' => $uname,
  255. 'login_version' => $rs["login_version"],
  256. 'data' => '',
  257. 'is_only_vgm' => $rs["is_only_vgm"]
  258. );
  259. common::echo_json_encode(200, $data);
  260. }
  261. $online_user = $rs;
  262. $online_user['user_login'] = $uname;
  263. $online_user['company'] = $company;
  264. $online_user['password'] = "";
  265. if (!_isAdmin()) {
  266. if ($rs["is_only_vgm"] == "t") {//VGM用户写死
  267. $sql = "select array_to_string(ARRAY(select url_action from public.ra_online_permission where url_action in ('ocean_order','password','vgm') and menu_id in ('ship','profile') order by order_by asc), ',')";
  268. $rrrs = common::excuteOneSql($sql);
  269. } else {
  270. $sql = "select array_to_string(ARRAY(select p.url_action from public.ra_online_user_permission up left join public.ra_online_user u on up.user_name = u.user_login left join public.ra_online_permission p on up.p_id = p.id where lower(u.user_login) = '" . common::check_input(strtolower($uname)) . "'), ',')";
  271. $rrrs = common::excuteOneSql($sql);
  272. if (strtolower($rs['user_type']) == "employee" && empty($rrrs)) {
  273. $sql = "select array_to_string(ARRAY(select url_action from public.ra_online_permission where is_customer = true order by order_by asc), ',')";
  274. $rrrs = common::excuteOneSql($sql);
  275. }
  276. }
  277. $online_user['permission'] = $rrrs;
  278. }
  279. if (!empty($online_user['docdownload']))
  280. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from ra_online_file_format where lower(serial_no) " . common::getInNotInSql($online_user['docdownload']) . " and active = true group by display_name order by min(id)";
  281. else {
  282. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from ra_online_file_format where active = true";
  283. if (strtolower($online_user['user_type']) == "customer")
  284. $sql .= " and client_display = true";
  285. $sql .= " group by display_name order by min(id)";
  286. }
  287. $online_user['view_doc_type'] = common::excuteListSql($sql);
  288. if (!empty($online_user['view_air_file_format']))
  289. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from air_file_format where lower(serial_no) " . common::getInNotInSql($online_user['view_air_file_format']) . " and active = true group by display_name order by min(id)";
  290. else {
  291. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from air_file_format where active = true";
  292. if (strtolower($online_user['user_type']) == "customer")
  293. $sql .= " and client_display = true";
  294. $sql .= " group by display_name order by min(id)";
  295. }
  296. $online_user['view_air_doc_type'] = common::excuteListSql($sql);
  297. //补充aci and ams CustomerLogin station - public
  298. $ocean_station_temp = $online_user['ocean_station'];
  299. $online_user['session_ocean_station'] = $this->getOriginOrAgent($ocean_station_temp);
  300. $ocean_agent_temp = $online_user['ocean_agent'];
  301. $online_user['session_ocean_agent'] = $this->getOriginOrAgent($ocean_agent_temp);
  302. $_SESSION['ONLINE_USER'] = $online_user;
  303. $_SESSION['LAST_OPERATE_TIME'] = time();
  304. $_SESSION['SESSION_TIMEOUT'] = $SESSION_TIMEOUT;
  305. //判断是否记录密码 add
  306. if ($_POST['rememberpwd'] === 'true') {
  307. if (!$noCheckPwd) {
  308. $user_info = $uname . "_" . md5($rs['password']);
  309. setcookie('userInfo', $user_info, time() + 30 * 24 * 3600, "/");
  310. }
  311. } else {
  312. setcookie('userInfo', '', time() - 1, "/");
  313. }
  314. if ($rs['is_super'] == "t") {
  315. $schemas_list = common::excuteListSql("select * from schemas_list");
  316. } else {
  317. $schemas_list = common::excuteListSql("select * from schemas_list where schemas_name=any(regexp_split_to_array('" . $rs['belong_schemas'] . "'::text, ';'::text))");
  318. }
  319. ///if (count($schemas_list) > 1) {
  320. foreach ($schemas_list as $sk => $sv) {
  321. if ($sv['schemas_name'] == "public") {
  322. continue;
  323. }
  324. $ttdd = common::excuteObjectSql("select contact_id_user, employee_id, contact_id, user_type, email, user_webtype_id, active, is_online, station, allow_login_remote, can_see_amslog,can_view_eccn, can_see_isflog, can_see_isflog_withaddress,
  325. customer_search_type, customer_destination, can_add_ams, can_add_isf, air_station, air_sales, ocean_station, ocean_sales,ocean_following_sales,ocean_following_sales_or,air_following_sales,air_following_sales_or, trucking_station, ocean_dest_op, can_see_password, can_add_opsales_code, ocean_station_or, ocean_agent_or, ocean_sales_or, ocean_dest_op_or, air_station_or, air_sales_or, trucking_station_or,
  326. can_add_user, can_add_employee, can_add_contact, company_name, ams_email, isf_email, customer_discharge, online_active, is_super, ocean_agent,active, can_send_email, view_file_format as docdownload, container_status, consolidated_cbsa_code, can_add_aci,
  327. air_customers, air_customer_search_type,trucking_customers,trucking_customer_search_type, upload_document, view_file_format, event_type, po_status, view_air_file_format, special_customer_event, can_edi_vgm, isf_aci_ams_station, is_kerry_shipment from " . $sv['schemas_name'] . ".ra_online_user where lower(user_login) = '" . strtolower($uname) . "'");
  328. if (empty($ttdd)) {
  329. unset($schemas_list[$sk]);
  330. continue;
  331. }
  332. if (!empty($ttdd['docdownload'])) {
  333. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".ra_online_file_format where lower(serial_no) " . common::getInNotInSql($ttdd['docdownload']) . " and active = true group by display_name order by min(id)";
  334. } else {
  335. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".ra_online_file_format where active = true";
  336. if (strtolower($ttdd['user_type']) == "customer")
  337. $sql .= " and client_display = true";
  338. $sql .= " group by display_name order by min(id)";
  339. }
  340. $ttdd['view_doc_type'] = common::excuteListSql($sql);
  341. if (!empty($ttdd['view_air_file_format'])) {
  342. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".air_file_format where lower(serial_no) " . common::getInNotInSql($ttdd['view_air_file_format']) . " and active = true group by display_name order by min(id)";
  343. } else {
  344. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".air_file_format where active = true";
  345. if (strtolower($ttdd['user_type']) == "customer")
  346. $sql .= " and client_display = true";
  347. $sql .= " group by display_name order by min(id)";
  348. }
  349. $ttdd['view_air_doc_type'] = common::excuteListSql($sql);
  350. //补充aci and ams CustomerLogin station - other like sfs
  351. $ocean_station_temp = $ttdd['ocean_station'];
  352. $ttdd['session_ocean_station'] = $this->getOriginOrAgent($ocean_station_temp);
  353. $ocean_agent_temp = $ttdd['ocean_agent'];
  354. $ttdd['session_ocean_agent'] = $this->getOriginOrAgent($ocean_agent_temp);
  355. $_SESSION[$sv['schemas_name'] . '_ONLINE_USER'] = $ttdd;
  356. }
  357. //}
  358. $_SESSION['schemas_list'] = $schemas_list;
  359. //不再返回登录页面,直接跳转
  360. if (isset($_GET['up'])) {
  361. if($_GET['v'] == 'new'){
  362. header("Location: main_new_version.php?action=main");
  363. }else{
  364. header("Location: main.php?action=main");
  365. }
  366. }
  367. exit();
  368. } else {
  369. $data = array(
  370. 'code' => 'database_error',
  371. 'login_version' => $rs["login_version"],
  372. 'msg' => 'database_error'
  373. );
  374. common::echo_json_encode(500, $data);
  375. exit();
  376. }
  377. } else {
  378. $data = array(
  379. 'code' => 'no_exist',
  380. 'login_version' => $rs["login_version"],
  381. 'msg' => 'The username or password you entered is incorrect'
  382. );
  383. common::echo_json_encode(500, $data);
  384. exit();
  385. }
  386. }
  387. }
  388. public function check_uname(){
  389. $uname = common::check_input($_POST['uname']);
  390. $sql = $this->getLoginSql($uname);
  391. $rs = common::excuteObjectSql($sql);
  392. if (!empty($rs)) {
  393. //只是验证用户是否存在,是否激活
  394. //验证employee是否active
  395. if (!empty($rs["employee_id"]) && $rs["employee_id_active"] != "t") {
  396. if (strtolower(Soure) =='topocean'){
  397. if (strtolower($rs['user_type']) == "employee" && utils::endWith($rs['email'], "cn")) {
  398. $data = "<a href='mailto:lilyyang@topocean.com.cn'>lilyyang@topocean.com.cn</a>";
  399. }else{
  400. $data = "<a href='mailto:winnie@topocean.com'>winnie@topocean.com</a>";
  401. }
  402. }
  403. if (strtolower(Soure) =='apex'){
  404. $data = "<a href='mailto:maria.wang@apexshipping.com.cn'>maria.wang@apexshipping.com.cn</a>";
  405. }
  406. $data = array(
  407. 'msg' => 'no_active',
  408. );
  409. common::echo_json_encode(200, $data);
  410. exit();
  411. }
  412. if ($rs['is_online'] != 't') {
  413. if (strtolower($rs['user_type']) != "employee") {
  414. $data =array(
  415. 'msg' => 'no_online',
  416. );
  417. common::echo_json_encode(200, $data);
  418. exit();
  419. }
  420. }
  421. if ($rs['online_active'] != 't') {
  422. $data = array(
  423. 'msg' => 'no_active',
  424. );
  425. common::echo_json_encode(200, $data);
  426. exit();
  427. }
  428. //验证成功
  429. $data = array(
  430. 'msg' => 'success',
  431. );
  432. common::echo_json_encode(200, $data);
  433. exit();
  434. } else {
  435. $data = array(
  436. 'msg' => 'no_exist',
  437. );
  438. common::echo_json_encode(200, $data);
  439. exit();
  440. }
  441. }
  442. public function verifcation_code(){
  443. // 生成一个4位随机数作为验证码
  444. //$random_num = mt_rand(1000, 9999);
  445. $random_num = $this->generateCaptcha(4);
  446. $_SESSION['captcha'] = $random_num;
  447. // 创建一个宽度为80像素、高度为30像素的图片
  448. $width = 130;
  449. $height = 40;
  450. $image = imagecreate($width, $height);
  451. // 设置颜色
  452. $white = imagecolorallocate($image, 255, 255, 255); // 白色作为背景
  453. $black = imagecolorallocate($image, 0, 0, 0); // 黑色作为文字
  454. // 填充背景
  455. imagefilledrectangle($image, 0, 0, $width, $height, $white);
  456. // 在图片上绘制四个字符
  457. $font_size = 18;
  458. $x = 34;
  459. $y = 12;
  460. for ($i = 0; $i < 4; $i++) {
  461. $char = substr($random_num, $i, 1);
  462. imagestring($image, $font_size, $x, $y, $char, $black);
  463. $x += $font_size+1;
  464. }
  465. // 返回Base64编码
  466. ob_start();
  467. imagepng($image);
  468. $image_data = ob_get_clean();
  469. // 释放内存
  470. imagedestroy($image);
  471. $data = array("imagePngBase64" =>base64_encode($image_data));
  472. common::echo_json_encode(200, $data);
  473. exit();
  474. }
  475. //邮件密码原文
  476. public function forgot_password() {
  477. $login = common::check_input($_POST['login']);
  478. $email = common::check_input($_POST['email']);
  479. $is_verify = common::check_input($_POST['verifcation_code']);
  480. //首先校用户验证
  481. $AES_encrypted = $this->AES_encrypted($is_verify);
  482. $secret_key = common::excuteOneSql("select secret_key from customer_service_secret_key
  483. where secret_key = '$is_verify'
  484. and create_time >= current_date - INTERVAL '3 months' limit 1");
  485. //记录这次的密钥记录
  486. common::excuteUpdateSql("INSERT INTO public.customer_service_secret_key(secret_key, create_time)VALUES ('$is_verify', now());");
  487. if(!empty($AES_encrypted) && empty($secret_key)){
  488. }else{
  489. $data = array(
  490. 'msg' => 'verifcation_error',
  491. 'data' => ''
  492. );
  493. common::echo_json_encode(400, $data);
  494. exit();
  495. }
  496. $msg = "";
  497. if (!empty($email) || !empty($login)) {
  498. $sql_p = "select User_Login, ra_password as password from public.ra_online_user where lower(user_login) = '" . strtolower($login) . "' and lower(email) = '" . strtolower($email) . "'";
  499. $rs = common::excuteObjectSql($sql_p);
  500. if (!empty($rs)) {
  501. $r = utils::sendEmailByPassword($login, $rs['password'], $email);
  502. if ($r == 'success') {
  503. $msg = "success";
  504. } else {
  505. $msg = $r;
  506. }
  507. } else {
  508. $msg = "Can not find this user with give login id and email, please confirm!";
  509. }
  510. } else {
  511. $msg = "Login Name or Email Required !";
  512. }
  513. if($msg == 'success'){
  514. $data = array(
  515. 'msg' => $msg,
  516. );
  517. common::echo_json_encode(200, $data);
  518. }else{
  519. $data = array(
  520. 'msg' => $msg,
  521. );
  522. common::echo_json_encode(500, $data);
  523. }
  524. exit();
  525. }
  526. public function generateCaptcha($length = 6) {
  527. $captcha = '';
  528. $chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
  529. for ($i = 0; $i < $length; $i++) {
  530. // 随机选择字母或数字
  531. $char = $chars[mt_rand(0, strlen($chars) - 1)];
  532. $captcha .= strval($char);
  533. }
  534. return $captcha;
  535. }
  536. public function do_login_auto() {
  537. $uname = common::check_input($_GET['u']);
  538. $password = common::check_input($_GET['p']);
  539. $login_error_times = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Login_Error_Times'");
  540. $lock_user_seconds = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Lock_User_Seconds'");
  541. if (!empty($uname) || !empty($password)) {
  542. } else {
  543. $uname = common::check_input($_POST['uname']);
  544. $sql = $this->getLoginSql($uname);
  545. $rs = common::excuteObjectSql($sql);
  546. if (empty($rs['belong_schemas'])) {
  547. $rs['belong_schemas'] = "public";
  548. }
  549. if (empty($rs['main_schemas'])) {
  550. $rs['main_schemas'] = "public";
  551. }
  552. if (!empty($rs)) {
  553. //验证employee是否active
  554. if (!empty($rs["employee_id"]) && $rs["employee_id_active"] != "t") {
  555. if (strtolower(Soure) =='topocean'){
  556. if (strtolower($rs['user_type']) == "employee" && utils::endWith($rs['email'], "cn")) {
  557. $data = "<a href='mailto:lilyyang@topocean.com.cn'>lilyyang@topocean.com.cn</a>";
  558. }else{
  559. $data = "<a href='mailto:winnie@topocean.com'>winnie@topocean.com</a>";
  560. }
  561. }
  562. if (strtolower(Soure) =='apex'){
  563. $data = "<a href='mailto:maria.wang@apexshipping.com.cn'>maria.wang@apexshipping.com.cn</a>";
  564. }
  565. $data = array(
  566. 'msg' => 'no_active',
  567. 'login_version' => $rs["login_version"],
  568. 'data' => $data
  569. );
  570. common::echo_json_encode(500, $data);
  571. $this->failedLogin($uname, 'Employee not active');
  572. exit();
  573. }
  574. //add 处理登录状态
  575. $userInfo = common::check_input($_COOKIE['userInfo']);
  576. $noCheckPwd = false;
  577. if (!empty($userInfo)) {
  578. $userInfoSplit = explode("_", $userInfo);
  579. if ($uname == $userInfoSplit[0]) {
  580. if ($userInfoSplit[1] == md5($rs['password'])) {
  581. $noCheckPwd = true;
  582. }
  583. }
  584. }
  585. //if (!$noCheckPwd) {
  586. if ($rs['error_login_count'] > $login_error_times && $rs['second'] < $lock_user_seconds) {
  587. $data = array(
  588. 'msg' => 'error_times',
  589. 'login_version' => $rs["login_version"],
  590. 'data' => ceil(($lock_user_seconds - $rs['second']) / 60)
  591. );
  592. common::echo_json_encode(500, $data);
  593. $this->failedLogin($uname, 'Failed login too times');
  594. exit();
  595. }
  596. if ($rs['is_online'] != 't') {
  597. if (strtolower($rs['user_type']) != "employee") {
  598. $data =array(
  599. 'msg' => 'no_online',
  600. 'login_version' => $rs["login_version"],
  601. 'data' => ''
  602. );
  603. common::echo_json_encode(500, $data);
  604. $this->failedLogin($uname, 'Online is not active');
  605. exit();
  606. }
  607. }
  608. //if ($rs['decrypt_password'] != $_POST['psw']) {
  609. if ($noCheckPwd) {
  610. }else{
  611. if ($rs['password'] != $_POST['psw']) {
  612. common::excuteUpdateSql("update public.ra_online_user set error_login_count=error_login_count+1, error_login_time=now() where lower(user_login) = '" . strtolower($uname) . "'");
  613. $data = array(
  614. 'msg' => 'password_error',
  615. 'login_version' => $rs["login_version"],
  616. 'data' => ''
  617. );
  618. common::echo_json_encode(500, $data);
  619. $this->failedLogin($uname, 'Password is wrong');
  620. exit();
  621. }
  622. }
  623. if ($rs['online_active'] != 't') {
  624. $data = array(
  625. 'msg' => 'no_active',
  626. 'login_version' => $rs["login_version"],
  627. 'data' => ''
  628. );
  629. common::echo_json_encode(500, $data);
  630. $this->failedLogin($uname, 'Online is not active');
  631. exit();
  632. }
  633. //check password length
  634. $tar = utils::checkPassword($rs['password']);
  635. if (!empty($tar)) {
  636. $data = array(
  637. 'msg' => $tar,
  638. 'login_version' => $rs["login_version"],
  639. 'data' => ''
  640. );
  641. common::echo_json_encode(500, $data);
  642. exit();
  643. }
  644. if (empty($rs['last_pwd_change'])) {
  645. $data = array(
  646. 'msg' => 'first_login',
  647. 'login_version' => $rs["login_version"],
  648. 'data' => ''
  649. );
  650. common::echo_json_encode(500, $data);
  651. exit();
  652. }
  653. //}
  654. //get more infor by employee_id or contact_id
  655. $sql = '';
  656. $diffdate = $rs['last_pwd_change_date'];
  657. $user_type = $rs['user_type'];
  658. //if user is customer, check company
  659. if (strtolower($user_type) == 'customer') {
  660. $company = $rs['company_name'];
  661. } else {
  662. if (!empty($rs['station']))
  663. $company = $rs['station'];
  664. }
  665. if (strtolower($uname) == 'ra.admin') {
  666. $company = 'Admin';
  667. }
  668. // get system config
  669. $sql = "SELECT lower(ra_name) as ra_name, ra_value from ra_online_config where lower(ra_name) in ('employee_session_timeout', 'customer_session_timeout', 'password_change_alert', 'employee_password_change_cycle', 'customer_password_change_cycle')";
  670. $rs1s = common::excuteListSql($sql);
  671. foreach ($rs1s as $rs1) {
  672. if ($rs1['ra_name'] == 'employee_session_timeout')
  673. $EMPLOYEE_SESSION_TIMEOUT = $rs1['ra_value'];
  674. if ($rs1['ra_name'] == 'customer_session_timeout')
  675. $CUSTOMER_SESSION_TIMEOUT = $rs1['ra_value'];
  676. if ($rs1['ra_name'] == 'password_change_alert')
  677. $PASSWORD_CHANGE_ALERT = $rs1['ra_value'];
  678. if ($rs1['ra_name'] == 'employee_password_change_cycle')
  679. $EMPLOYEE_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  680. if ($rs1['ra_name'] == 'customer_password_change_cycle')
  681. $CUSTOMER_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  682. }
  683. $sql="select item_value from config where item='passwordChangePeriod'";
  684. $pcp = common::excuteObjectSql($sql);
  685. $passwordChangePeriod = json_decode($pcp["item_value"],true);
  686. if (strtolower($rs['user_type']) == 'employee') {
  687. $PASSWORD_CHANGE_CYCLE = $EMPLOYEE_PASSWORD_CHANGE_CYCLE;
  688. $SESSION_TIMEOUT = $EMPLOYEE_SESSION_TIMEOUT;
  689. //如果有新配置,则采用新配置
  690. if (!empty($pcp)) {
  691. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Employee"]["days"];
  692. $PASSWORD_CHANGE_ALERT = $passwordChangePeriod["Employee"]["advanceDays"];
  693. }
  694. } else {
  695. $PASSWORD_CHANGE_CYCLE = $CUSTOMER_PASSWORD_CHANGE_CYCLE;
  696. $SESSION_TIMEOUT = $CUSTOMER_SESSION_TIMEOUT;
  697. //如果有新配置,则采用新配置
  698. if (!empty($pcp)) {
  699. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Customer"]["days"];
  700. $PASSWORD_CHANGE_ALERT = $passwordChangePeriod["Customer"]["advanceDays"];
  701. }
  702. }
  703. /* if ($diffdate > $PASSWORD_CHANGE_CYCLE) {// Timeout
  704. echo json_encode(array(
  705. 'msg' => 'password_require_change',
  706. 'login_version' => $rs["login_version"],
  707. 'data' => ''
  708. ));
  709. $this->failedLogin($uname, 'Required password change');
  710. exit();
  711. }*/
  712. $loginName = $rs['user_login'];
  713. $email = $rs['email'];
  714. if ($diffdate > $PASSWORD_CHANGE_CYCLE) {// Timeout
  715. if(empty($email)){
  716. $data = array(
  717. 'status' => '0',
  718. 'msg' => 'login user email is empty',
  719. 'login_version' => $rs["login_version"],
  720. 'data' => ''
  721. );
  722. common::echo_json_encode(500, $data);
  723. exit();
  724. }else{
  725. //$this -> passwordExpires($loginName,$email);
  726. }
  727. }
  728. if ($diffdate == $PASSWORD_CHANGE_CYCLE) {// Due today
  729. $login_tmp = array(
  730. 'msg' => 'today',
  731. 'login_version' => $rs["login_version"],
  732. 'data' => ''
  733. );
  734. } elseif ($diffdate >= ($PASSWORD_CHANGE_CYCLE - $PASSWORD_CHANGE_ALERT)) {// Password expires soon, JS Tips
  735. $login_tmp = array(
  736. 'msg' => 'last',
  737. 'login_version' => $rs["login_version"],
  738. 'data' => $PASSWORD_CHANGE_CYCLE - $diffdate,
  739. 'is_only_vgm' => $rs["is_only_vgm"]
  740. );
  741. }
  742. //insert into log table
  743. $ip = common::ip();
  744. $sql = "insert into public.ra_online_user_login_log (user_name,manufacturer,from_app,ip,date_time, session_id) values ('" . common::check_input($uname) . "', '" . common::check_input($company) . "','Online','$ip',now(), '" . session_id() . "')";
  745. if (common::excuteUpdateSql($sql)) {
  746. common::excuteUpdateSql("update public.ra_online_user set Last_Login_Time = now(), error_login_count=0, error_login_time=null where lower(user_login) = '" . strtolower($uname) . "'");
  747. //自动登录。为了方便调用,先注销掉
  748. if (isset($login_tmp)) {
  749. //common::echo_json_encode(500, $login_tmp);
  750. } else {
  751. $data = array(
  752. 'msg' => 'success',
  753. 'login_version' => $rs["login_version"],
  754. 'data' => '',
  755. 'is_only_vgm' => $rs["is_only_vgm"]
  756. );
  757. //common::echo_json_encode("200", $data);
  758. }
  759. $online_user = $rs;
  760. $online_user['user_login'] = $uname;
  761. $online_user['company'] = $company;
  762. $online_user['password'] = "";
  763. if (!_isAdmin()) {
  764. if ($rs["is_only_vgm"] == "t") {//VGM用户写死
  765. $sql = "select array_to_string(ARRAY(select url_action from public.ra_online_permission where url_action in ('ocean_order','password','vgm') and menu_id in ('ship','profile') order by order_by asc), ',')";
  766. $rrrs = common::excuteOneSql($sql);
  767. } else {
  768. $sql = "select array_to_string(ARRAY(select p.url_action from public.ra_online_user_permission up left join public.ra_online_user u on up.user_name = u.user_login left join public.ra_online_permission p on up.p_id = p.id where lower(u.user_login) = '" . common::check_input(strtolower($uname)) . "'), ',')";
  769. $rrrs = common::excuteOneSql($sql);
  770. if (strtolower($rs['user_type']) == "employee" && empty($rrrs)) {
  771. $sql = "select array_to_string(ARRAY(select url_action from public.ra_online_permission where is_customer = true order by order_by asc), ',')";
  772. $rrrs = common::excuteOneSql($sql);
  773. }
  774. }
  775. $online_user['permission'] = $rrrs;
  776. }
  777. if (!empty($online_user['docdownload']))
  778. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from ra_online_file_format where lower(serial_no) " . common::getInNotInSql($online_user['docdownload']) . " and active = true group by display_name order by min(id)";
  779. else {
  780. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from ra_online_file_format where active = true";
  781. if (strtolower($online_user['user_type']) == "customer")
  782. $sql .= " and client_display = true";
  783. $sql .= " group by display_name order by min(id)";
  784. }
  785. $online_user['view_doc_type'] = common::excuteListSql($sql);
  786. if (!empty($online_user['view_air_file_format']))
  787. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from air_file_format where lower(serial_no) " . common::getInNotInSql($online_user['view_air_file_format']) . " and active = true group by display_name order by min(id)";
  788. else {
  789. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from air_file_format where active = true";
  790. if (strtolower($online_user['user_type']) == "customer")
  791. $sql .= " and client_display = true";
  792. $sql .= " group by display_name order by min(id)";
  793. }
  794. $online_user['view_air_doc_type'] = common::excuteListSql($sql);
  795. //补充aci and ams CustomerLogin station - public
  796. $ocean_station_temp = $online_user['ocean_station'];
  797. $online_user['session_ocean_station'] = $this->getOriginOrAgent($ocean_station_temp);
  798. $ocean_agent_temp = $online_user['ocean_agent'];
  799. $online_user['session_ocean_agent'] = $this->getOriginOrAgent($ocean_agent_temp);
  800. $_SESSION['ONLINE_USER'] = $online_user;
  801. $_SESSION['LAST_OPERATE_TIME'] = time();
  802. $_SESSION['SESSION_TIMEOUT'] = $SESSION_TIMEOUT;
  803. //判断是否记录密码 add
  804. if ($_POST['rememberpwd'] === 'true') {
  805. if (!$noCheckPwd) {
  806. $user_info = $uname . "_" . md5($rs['password']);
  807. setcookie('userInfo', $user_info, time() + 30 * 24 * 3600, "/");
  808. }
  809. } else {
  810. setcookie('userInfo', '', time() - 1, "/");
  811. }
  812. if ($rs['is_super'] == "t") {
  813. $schemas_list = common::excuteListSql("select * from schemas_list");
  814. } else {
  815. $schemas_list = common::excuteListSql("select * from schemas_list where schemas_name=any(regexp_split_to_array('" . $rs['belong_schemas'] . "'::text, ';'::text))");
  816. }
  817. ///if (count($schemas_list) > 1) {
  818. foreach ($schemas_list as $sk => $sv) {
  819. if ($sv['schemas_name'] == "public") {
  820. continue;
  821. }
  822. $ttdd = common::excuteObjectSql("select contact_id_user, employee_id, contact_id, user_type, email, user_webtype_id, active, is_online, station, allow_login_remote, can_see_amslog,can_view_eccn, can_see_isflog, can_see_isflog_withaddress,
  823. customer_search_type, customer_destination, can_add_ams, can_add_isf, air_station, air_sales, ocean_station, ocean_sales,ocean_following_sales,ocean_following_sales_or,air_following_sales,air_following_sales_or, trucking_station, ocean_dest_op, can_see_password, can_add_opsales_code, ocean_station_or, ocean_agent_or, ocean_sales_or, ocean_dest_op_or, air_station_or, air_sales_or, trucking_station_or,
  824. can_add_user, can_add_employee, can_add_contact, company_name, ams_email, isf_email, customer_discharge, online_active, is_super, ocean_agent,active, can_send_email, view_file_format as docdownload, container_status, consolidated_cbsa_code, can_add_aci,
  825. air_customers, air_customer_search_type,trucking_customers,trucking_customer_search_type, upload_document, view_file_format, event_type, po_status, view_air_file_format, special_customer_event, can_edi_vgm, isf_aci_ams_station, is_kerry_shipment from " . $sv['schemas_name'] . ".ra_online_user where lower(user_login) = '" . strtolower($uname) . "'");
  826. if (empty($ttdd)) {
  827. unset($schemas_list[$sk]);
  828. continue;
  829. }
  830. if (!empty($ttdd['docdownload'])) {
  831. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".ra_online_file_format where lower(serial_no) " . common::getInNotInSql($ttdd['docdownload']) . " and active = true group by display_name order by min(id)";
  832. } else {
  833. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".ra_online_file_format where active = true";
  834. if (strtolower($ttdd['user_type']) == "customer")
  835. $sql .= " and client_display = true";
  836. $sql .= " group by display_name order by min(id)";
  837. }
  838. $ttdd['view_doc_type'] = common::excuteListSql($sql);
  839. if (!empty($ttdd['view_air_file_format'])) {
  840. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".air_file_format where lower(serial_no) " . common::getInNotInSql($ttdd['view_air_file_format']) . " and active = true group by display_name order by min(id)";
  841. } else {
  842. $sql = "select string_agg(serial_no, ';') as serial_no, string_agg(m_h, ';') as m_h, display_name from " . $sv['schemas_name'] . ".air_file_format where active = true";
  843. if (strtolower($ttdd['user_type']) == "customer")
  844. $sql .= " and client_display = true";
  845. $sql .= " group by display_name order by min(id)";
  846. }
  847. $ttdd['view_air_doc_type'] = common::excuteListSql($sql);
  848. //补充aci and ams CustomerLogin station - other like sfs
  849. $ocean_station_temp = $ttdd['ocean_station'];
  850. $ttdd['session_ocean_station'] = $this->getOriginOrAgent($ocean_station_temp);
  851. $ocean_agent_temp = $ttdd['ocean_agent'];
  852. $ttdd['session_ocean_agent'] = $this->getOriginOrAgent($ocean_agent_temp);
  853. $_SESSION[$sv['schemas_name'] . '_ONLINE_USER'] = $ttdd;
  854. }
  855. //}
  856. $_SESSION['schemas_list'] = $schemas_list;
  857. //不再返回登录页面,直接跳转
  858. if (isset($_GET['up'])) {
  859. if($_GET['v'] == 'new'){
  860. header("Location: main_new_version.php?action=main");
  861. }else{
  862. header("Location: main.php?action=main");
  863. }
  864. }
  865. //自动登录。为了方便调用,先注销掉
  866. //exit();
  867. } else {
  868. $data = array(
  869. 'msg' => 'database_error',
  870. 'login_version' => $rs["login_version"],
  871. 'data' => ''
  872. );
  873. common::echo_json_encode(500, $data);
  874. exit();
  875. }
  876. } else {
  877. $data = array(
  878. 'msg' => 'no_exist',
  879. 'login_version' => $rs["login_version"],
  880. 'data' => ''
  881. );
  882. common::echo_json_encode(500, $data);
  883. exit();
  884. }
  885. }
  886. }
  887. private function failedLogin($uname, $company){
  888. $ip = common::ip();
  889. common::excuteUpdateSql("insert into public.ra_online_user_login_log (user_name,manufacturer,from_app,ip,date_time, session_id) values ('" . common::check_input($uname) . "', '" . common::check_input($company) . "','Online','$ip',now(), '" . session_id() . "')");
  890. }
  891. //重置密码
  892. public function passwordExpires($loginName,$email,$uname){
  893. $result = array(
  894. 'msg' => "passwordExpires",
  895. 'uname' => $uname,
  896. );
  897. common::echo_json_encode(400, $result);
  898. exit();
  899. }
  900. //submit change form -- change expires password
  901. public function update_pwd_expires(){
  902. $loginName = common::check_input($_POST['uname']);
  903. $old_password = common::check_input($_POST['old_password']);
  904. $password = common::check_input($_POST['password']);
  905. //首先校验验证码 暂时注销掉
  906. // $verifcation_code = "";
  907. // $verifcation_code = common::check_input($_POST['verifcation_code']);
  908. // if (strtolower($_SESSION['captcha']) != strtolower($verifcation_code)) {
  909. // $data = array(
  910. // 'msg' => 'verifcation_error',
  911. // 'data' => ''
  912. // );
  913. // common::echo_json_encode(400, $data);
  914. // exit();
  915. // }
  916. $sql = "select ra_password as password from ra_online_user where lower(user_login) = '" . strtolower($loginName) . "'";
  917. $rs = common::excuteObjectSql($sql);
  918. $str = '';
  919. if (!empty($rs)) {
  920. if ($rs['password'] != $old_password) {
  921. $str = "Old password is incorrect!";
  922. }
  923. } else {
  924. $str = "Old password is incorrect!";
  925. }
  926. if(!empty($str)){
  927. $data = array(
  928. 'msg' => $str,
  929. 'data' => ''
  930. );
  931. common::echo_json_encode(500, $data);
  932. exit();
  933. }
  934. //验证通过,进行修改密码
  935. $msg = $this->updateExpirePassword($loginName, $password);
  936. if($msg == "success"){
  937. $data = array(
  938. 'msg' => "success",
  939. 'data' => ''
  940. );
  941. common::echo_json_encode(200, $data);
  942. exit();
  943. } else {
  944. $data = array(
  945. 'msg' => $msg,
  946. 'data' => ''
  947. );
  948. common::echo_json_encode(500, $data);
  949. exit();
  950. }
  951. }
  952. //更新密码
  953. public function updateExpirePassword($login,$new_password) {
  954. $str = common::checkPasswordRule($login, $new_password);
  955. //更新密码,擦除expire pwd痕迹
  956. if (empty($str)) {
  957. $sql = "UPDATE public.ra_online_user SET ra_password = '" . common::check_input($new_password) . "',password_new=redant_encode('".$new_password."'),
  958. last_pwd_change = now(),password_expires_keycode = null,password_expires_time = null
  959. WHERE lower(user_login) = '" . common::check_input(strtolower($login)) . "';";
  960. $rls = common::excuteUpdateSql($sql);
  961. if (!$rls) {
  962. $str = "Database Error, Try Later.";
  963. } else {
  964. $sql = "INSERT INTO public.ra_online_user_password_history (user_login, password, create_user, create_date) VALUES ('" . $login . "', " . common::check_input($new_password) . ", '" . $login . "', now());";
  965. common::excuteUpdateSql($sql);
  966. $str = "success";
  967. }
  968. }
  969. return $str;
  970. }
  971. public function getOriginOrAgent($ocean_station_temp){
  972. $session_ocean_station = "";
  973. if (strtolower($ocean_station_temp) == 'all'){
  974. $session_ocean_station = $ocean_station_temp;
  975. }
  976. if (!(strtolower($ocean_station_temp) == 'all' || empty($ocean_station_temp))){
  977. if (utils::checkExist($ocean_station_temp, ";")) {
  978. $ost = str_replace(";","','",strtolower($ocean_station_temp));
  979. $sql="select kerry_station_id,contact_id from ocean.contacts where coalesce(kerry_station_id,'') <>''and lower(contact_id) in ('".$ost."')";
  980. $tar = common::excuteListSql($sql);
  981. foreach ($tar as $tk => $tv) {
  982. $tar[$tv['contact_id']] = $tv['kerry_station_id'];
  983. }
  984. $_tt = explode(";", $ocean_station_temp);
  985. foreach ($_tt as $vv) {
  986. if (!empty($vv)){
  987. $session_ocean_station .= trim($vv).';';
  988. if (!empty($tar[$vv])) {
  989. $session_ocean_station .= $tar[trim($vv)].';';
  990. }
  991. }
  992. }
  993. } else {
  994. $session_ocean_station .= trim($ocean_station_temp).';';
  995. $temp_contacts = common::excuteObjectSql("select kerry_station_id from ocean.contacts where lower(contact_id)='".strtolower(common::check_input($ocean_station_temp))."'");
  996. if (!empty($temp_contacts['kerry_station_id'])){
  997. $session_ocean_station .= $temp_contacts['kerry_station_id'].';';
  998. }
  999. }
  1000. }
  1001. return $session_ocean_station;
  1002. }
  1003. public function logout() {
  1004. unset($_SESSION['ONLINE_USER']);
  1005. unset($_SESSION['LAST_OPERATE_TIME']);
  1006. unset($_SESSION['SESSION_TIMEOUT']);
  1007. common::sessionDestroy();
  1008. $data = array("msg" =>"logout Successful");
  1009. common::echo_json_encode(200, $data);
  1010. exit();
  1011. }
  1012. public function tracking_checked(){
  1013. $reference_number = common::check_input($_POST['reference_number']);
  1014. $is_verify = common::check_input($_POST['verifcation_code']);
  1015. if($this->signUpAndTrackingChecked($is_verify)){
  1016. $data = array("msg" =>"visit limit");
  1017. common::echo_json_encode(400, $data);
  1018. exit();
  1019. }else{
  1020. $data = $this->getTrackingInfo($reference_number);
  1021. common::echo_json_encode(200, $data);
  1022. //记录查询log情况
  1023. $detail = "";
  1024. if($data['msg'] == "success"){
  1025. $detail = "Public tracking number:".$$reference_number."; search successful";
  1026. } else {
  1027. $detail = "Public tracking number:".$$reference_number."; search fail(".$data['msg'].")";
  1028. }
  1029. utils::single_operation_log_save("Tracking","Public tracking",$detail);
  1030. exit();
  1031. }
  1032. }
  1033. private function signUpAndTrackingChecked($is_verify){
  1034. $Tracking_Search_Count = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Tracking_Search_Count'");
  1035. $ip = common::ip();
  1036. global $db;
  1037. $db->StartTrans();
  1038. $db->Execute("update tracking_login_record set visit_count = 1, visit_time=now() "
  1039. . " where ip = '$ip' and type ilike '".common::check_input($_POST['type'])."' and visit_time + '5 min' < NOW()::timestamp ") or ( (!$db->ErrorMsg()) or error_log($db->ErrorMsg(), 0));
  1040. $ipInfo = $db->GetRow("select ip, visit_count from tracking_login_record where ip = '$ip' and type ilike '".common::check_input($_POST['type'])."' and visit_time + '5 min' > NOW()::timestamp");
  1041. if(empty($ipInfo)){
  1042. $db->Execute("INSERT INTO public.tracking_login_record(ip, visit_count,visit_time,type)VALUES ('$ip', '1', now(),'".common::check_input($_POST['type'])."');") or ( (!$db->ErrorMsg()) or error_log($db->ErrorMsg(), 0));
  1043. }else{
  1044. if ($ipInfo['visit_count'] > $Tracking_Search_Count){
  1045. $AES_encrypted = $this->AES_encrypted($is_verify);
  1046. $secret_key = common::excuteOneSql("select secret_key from customer_service_secret_key
  1047. where secret_key = '$is_verify'
  1048. and create_time >= current_date - INTERVAL '3 months' limit 1");
  1049. //记录这次的密钥记录
  1050. common::excuteUpdateSql("INSERT INTO public.customer_service_secret_key(secret_key, create_time)VALUES ('$is_verify', now());");
  1051. if(!empty($AES_encrypted) && empty($secret_key)){
  1052. //归零验证次数
  1053. $db->Execute("update tracking_login_record set visit_count = 1 "
  1054. . " where ip = '$ip' and type ilike '".common::check_input($_POST['type'])."' and visit_time + '5 min' > NOW()::timestamp ") or ( (!$db->ErrorMsg()) or error_log($db->ErrorMsg(), 0));
  1055. }else{
  1056. return TRUE;
  1057. }
  1058. }else{
  1059. $db->Execute("update tracking_login_record set visit_count = visit_count::integer + 1 "
  1060. . " where ip = '$ip' and type ilike '".common::check_input($_POST['type'])."' and visit_time + '5 min' > NOW()::timestamp ") or ( (!$db->ErrorMsg()) or error_log($db->ErrorMsg(), 0));
  1061. }
  1062. }
  1063. if ($db->CompleteTrans() === FALSE) {
  1064. //出错拦截
  1065. return TRUE;
  1066. } else {
  1067. return FALSE;
  1068. }
  1069. }
  1070. private function getTrackingInfo($reference_number){
  1071. $reference_number = strtolower($reference_number);
  1072. $sql = "with o as(
  1073. SELECT o.*
  1074. from public.ocean o
  1075. where o.status::text <> 'Cancelled'::text AND o.bol_type::text <> 'BOOKING'::text AND o.bol_type::text <> 'QUOTE'::text AND o.iscts = true
  1076. AND o.iscts IS NOT NULL and ARRAY['$reference_number'] && array_append(array[lower(tracking_no)::text,lower(booking_no)::text,lower(h_bol)::text,lower(m_bol)::text, lower(po_no),lower(quote_no)::text,lower(invoice_no)],'')
  1077. union
  1078. SELECT o.*
  1079. from public.ocean o
  1080. where o.status::text <> 'Cancelled'::text AND o.bol_type::text <> 'BOOKING'::text AND o.bol_type::text <> 'QUOTE'::text AND o.iscts = true
  1081. AND o.iscts IS NOT NULL and exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number')
  1082. )
  1083. SELECT m_eta as _m_eta, h_bol as _h_bol, m_bol as _m_bol,job_no as _job_bol,
  1084. o.* ,sh.*, cn.* ,aa.*,dd.*,fd.*,oo.*,
  1085. (select uncode from ports where code = o.port_of_transshipment) as port_of_transshipment_un
  1086. from o
  1087. LEFT JOIN LATERAL ( SELECT tracking_no as _tracking_no,shippr_uncode,shipper_city,
  1088. consignee_uncode,consignee_city,incoterms,
  1089. fport_of_loading_un,
  1090. mport_of_discharge_un,
  1091. place_of_receipt_un,
  1092. place_of_delivery_un,
  1093. (select timezonecode from public.city_ports where ctrycitycode = oo.fport_of_loading_un limit 1) as pol_timezone,
  1094. (select uncity from public.ports where uncode = oo.fport_of_loading_un limit 1) as pol_uncity,
  1095. (select timezonecode from public.city_ports where ctrycitycode = oo.mport_of_discharge_un limit 1) as mpod_timezone,
  1096. (select uncity from public.ports where uncode = oo.mport_of_discharge_un limit 1) as mpod_uncity,
  1097. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_receipt_un limit 1) as por_timezone,
  1098. (select uncity from public.ports where uncode = oo.place_of_receipt_un limit 1) as por_uncity,
  1099. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_delivery_un limit 1) as pod_timezone,
  1100. (select uncity from public.ports where uncode = oo.place_of_delivery_un limit 1) as pod_uncity,
  1101. po_no as _po_no,
  1102. CASE
  1103. WHEN ((m_iffbcf is not null or m_iffbcf is null) and m_iffcpu is null and m_iffrec is null and m_iffdep is null and m_iffarr is null and m_iffafd is null) THEN 'Created'::text
  1104. WHEN ((m_iffcpu is not null or m_iffrec is not null) and m_iffdep is null and m_iffarr is null and m_iffafd is null) THEN 'Cargo Received'::text
  1105. WHEN (m_iffdep is not null and m_iffarr is null and m_iffafd is null) THEN 'Departure'::text
  1106. WHEN (m_iffarr is not null and m_iffafd is null) THEN 'Arrived'::text
  1107. WHEN (m_iffafd is not null) THEN 'Completed'::text
  1108. ELSE 'Created'::text
  1109. END AS new_status
  1110. FROM public.online_ocean oo WHERE oo.serial_no::text = o.serial_no::text) oo ON true
  1111. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1112. address_1 as cn_address_1,
  1113. address_2 as cn_address_2,
  1114. address_3 as cn_address_3,
  1115. address_4 as cn_address_4,
  1116. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1117. phone_1 as cn_phone
  1118. FROM ocean.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1119. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1120. address_1 as sh_address_1,
  1121. address_2 as sh_address_2,
  1122. address_3 as sh_address_3,
  1123. address_4 as sh_address_4,
  1124. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1125. phone_1 as sh_phone
  1126. FROM ocean.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1127. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1128. address_1 as aa_address_1,
  1129. address_2 as aa_address_2,
  1130. address_3 as aa_address_3,
  1131. address_4 as aa_address_4,
  1132. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1133. phone_1 as aa_phone,
  1134. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as aa_timezone
  1135. FROM ocean.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1136. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1137. address_1 as dd_address_1,
  1138. address_2 as dd_address_2,
  1139. address_3 as dd_address_3,
  1140. address_4 as dd_address_4,
  1141. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1142. phone_1 as dd_phone,
  1143. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as dd_timezone
  1144. FROM ocean.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1145. LEFT JOIN LATERAL ( SELECT
  1146. city as fd_city,
  1147. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as fd_timezone
  1148. FROM ocean.contacts c WHERE o.final_desination::text = c.contact_id::text) fd ON true";
  1149. $ocean_arr = common::excuteListSql($sql);
  1150. if(empty($ocean_arr)){
  1151. $sql = $this->trackingSfsSql($reference_number);
  1152. $ocean_arr = common::excuteListSql($sql);
  1153. if(empty($ocean_arr)){
  1154. $data = array("msg" =>"No matches");
  1155. return $data;
  1156. }
  1157. }
  1158. if(!empty($ocean_arr) && count($ocean_arr) > 1){
  1159. $data = array("msg" =>"Multiple results");
  1160. return $data;
  1161. }
  1162. $ocean = $ocean_arr[0];
  1163. //获取对应uncode 对应的时间
  1164. $uncodes = $ocean['fport_of_loading_un'].";".$ocean['mport_of_discharge_un'];
  1165. $codeinfo = common::getCityPortsInfo($uncodes);
  1166. //处理transportInfo信息数据
  1167. $transportInfo = array("Tracking No." =>$ocean['_tracking_no'],"status"=>$ocean['new_status'],"mode" => "Ocean Freight",
  1168. "origin" =>$ocean['shippr_uncode'],"destination" =>$ocean['consignee_uncode'],
  1169. "etd" =>$ocean['f_etd'],"atd" =>$ocean['atd'],
  1170. "etd_timezone" =>$codeinfo[$ocean['fport_of_loading_un']],
  1171. "atd_timezone" =>$codeinfo[$ocean['fport_of_loading_un']],
  1172. "eta" =>$ocean['m_eta'],"ata" =>$ocean['ata'],
  1173. "eta_timezone" =>$codeinfo[$ocean['mport_of_discharge_un']],
  1174. "ata_timezone" =>$codeinfo[$ocean['mport_of_discharge_un']]);
  1175. $data['transportInfo'] = $transportInfo;
  1176. //处理basicInfo信息数据
  1177. $vessel = utils::outDisplayForMerge($ocean['f_vessel'],$ocean['m_vessel']);
  1178. $voyage = utils::outDisplayForMerge($ocean['f_voyage'],$ocean['m_voyage']);
  1179. $basicInfo = array("MAWB/MBL No." =>$ocean['m_bol'],"HAWB/HBOL" => $ocean['h_bol'],"Carrier_Booking_No" =>$ocean['booking_no'],
  1180. "PO_NO" =>$ocean['po_no'],"Vessel/Airline" =>$vessel,"Voyage/Filght" =>$voyage,
  1181. "Incoterm" =>$ocean['incoterms'],"Service_Type" =>$ocean['service']);
  1182. //处理 拼接地址 ocean表单exp 字段无法精准分割电话和地址信息,只能从contacts表里查询
  1183. $shipper_address = common::retStationInfo($ocean['sh_address_1'], $ocean['sh_address_2'], $ocean['sh_address_3'], $ocean['sh_address_4'],
  1184. $ocean['sh_city'], $ocean['sh_state'], $ocean['sh_zipcode'], $ocean['sh_country']);
  1185. $consignee_address = common::retStationInfo($ocean['cn_address_1'], $ocean['cn_address_2'], $ocean['cn_address_3'], $ocean['cn_address_4'],
  1186. $ocean['cn_city'], $ocean['cn_state'], $ocean['cn_zipcode'], $ocean['cn_country']);
  1187. $origin_address = common::retStationInfo($ocean['aa_address_1'], $ocean['aa_address_2'], $ocean['aa_address_3'], $ocean['aa_address_4'],
  1188. $ocean['aa_city'], $ocean['aa_state'], $ocean['aa_zipcode'], $ocean['aa_country']);
  1189. $destination_address = common::retStationInfo($ocean['dd_address_1'], $ocean['dd_address_2'], $ocean['dd_address_3'], $ocean['dd_address_4'],
  1190. $ocean['dd_city'], $ocean['dd_state'], $ocean['dd_zipcode'], $ocean['dd_country']);
  1191. $shipperPartners = array("company" =>$ocean['sh_company'],"address"=>$shipper_address,"phone"=>$ocean['sh_phone']);
  1192. $consigneePartners = array("company" =>$ocean['cn_company'],"address"=>$consignee_address,"phone"=>$ocean['cn_phone']);
  1193. $originPartners = array("company" =>$ocean['aa_company'],"address"=>$origin_address,"phone"=>$ocean['aa_phone']);
  1194. $destinationPartners = array("company" =>$ocean['dd_company'],"address"=>$destination_address,"phone"=>$ocean['dd_phone']);
  1195. $businessPartners = array("shipper"=>$shipperPartners,"consignee" => $consigneePartners,"origin" => $originPartners,"destination" => $destinationPartners);
  1196. //处理marksAndDescription
  1197. $marksAndDescription = array("marks"=>$ocean['marks'],"description"=>$ocean['description']);
  1198. $sql = "SELECT " . column::getInstance()->getSearchSql('Ocean_Container') . " ,net_lbs from oc_container where lower(serial_no) = '" . strtolower($ocean['serial_no']) . "'";
  1199. $rss = common::excuteListSql($sql);
  1200. $quantity_unit = array();
  1201. $g_weight_tolal = 0;
  1202. $ch_weight_tolal = 0;
  1203. $ch_weight_tolal_grs_lbs = 0;
  1204. $cbm_tolal = 0;
  1205. foreach ($rss as $key => $rs) {
  1206. $unit = $rs['unit'];
  1207. if (array_key_exists($unit, $quantity_unit)) {
  1208. $quantity_unit[$unit] = $quantity_unit[$unit] + $rs['qty'];
  1209. } else {
  1210. $quantity_unit[$unit] = $rs['qty'];
  1211. }
  1212. $g_weight_tolal += $rs['grs_kgs'];
  1213. $ch_weight_tolal += $rs['net_lbs'];
  1214. $ch_weight_tolal_grs_lbs += $rs['grs_lbs'];
  1215. $cbm_tolal += $rs['cbm'];
  1216. }
  1217. $quantity_tolal = "";
  1218. foreach($quantity_unit as $uk => $uv){
  1219. $quantity_tolal.=$uv." ".$uk." ";
  1220. }
  1221. $ch_weight_tolal = empty($ch_weight_tolal) ? $ch_weight_tolal_grs_lbs : $ch_weight_tolal;
  1222. //Packing 不确定信息
  1223. $packing = array("Quantity/Unit"=>$quantity_tolal,"G. Weight" => $g_weight_tolal." KGS","Ch. Weight" => $ch_weight_tolal." LBS","Volume" => $cbm_tolal." CBM");
  1224. /* Container Status */
  1225. //数据库里配置好
  1226. //$sql = "SELECT " . column::getInstance()->getSearchSql('Ocean_Container_Status') . " from oc_container_v where lower(serial_no) = '" . strtolower($serial_no) . "'";
  1227. $serial_no = $ocean["serial_no"];
  1228. $ctnr_sql = "SELECT oc.ctnr, oc.serial_no,oc.size FROM oc_container oc LEFT JOIN ocean o ON oc.serial_no = o.serial_no
  1229. WHERE o.serial_no='$serial_no'";
  1230. $ctnr_data = common::excuteListSql($ctnr_sql);
  1231. foreach ($ctnr_data as $cd){
  1232. //存在柜号为空的数据情况
  1233. if(empty($cd['ctnr'])){
  1234. continue;
  1235. }
  1236. $ctnr_status_sql = "select s.source_id, s.event_base as event,
  1237. to_char(to_timestamp(s.event_date, 'YYYYMMDD'), 'YYYY-MM-DD') as eventdate,
  1238. to_char(to_timestamp(s.event_time, 'HH24MI'), 'HH24:MI') as eventtime,
  1239. e.description,s.event_type as eventtype,
  1240. s.event_code as eventcode, s.event_city as eventcity,
  1241. (select timezonecode from public.city_ports where ctrycitycode = s.event_code) as timezone,
  1242. (select uncity from public.ports where uncode = s.event_code) as uncity,
  1243. case when event ='I' then 'IFFREC'::text
  1244. when event ='AE' then 'IFFONB'::text
  1245. when event ='VD' then 'IFFDEP'::text
  1246. when event ='EB' or event ='VA' then 'IFFARR'::text
  1247. when event ='UV' then 'IFFUND'::text
  1248. when event ='VA' then 'IFFAFD'::text
  1249. when event ='AV' then 'IFFCTA'::text
  1250. when event ='CT' then 'IFFICC'::text
  1251. when event ='OA' or event ='D' then 'IFFPPD'::text
  1252. when event ='EE' then 'IFFECP'::text
  1253. else '' ::text
  1254. end as milestone_code
  1255. from public.ra_online_container_status_v s
  1256. left join ra_online_edi_event e on s.event_base = e.ra_name
  1257. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1258. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1259. order by to_timestamp(s.event_date, 'YYYYMMDD') asc,
  1260. to_timestamp(s.event_time, 'HH24MI') asc,e.ra_order asc";
  1261. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1262. //记录所有的信息
  1263. $EDI315TimeAndLocation = array();
  1264. foreach($ctnr_status as $event){
  1265. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1266. //如果存在EB 的EB 的优先级最高
  1267. continue;
  1268. }
  1269. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1270. //如果存在OA 的OA 的优先级最高
  1271. continue;
  1272. }
  1273. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone'],"location"=>$event['uncity']);
  1274. }
  1275. }
  1276. //Milestones 数据信息待定
  1277. $Milestones = common::getMilestonesInfo($ocean,$EDI315TimeAndLocation);
  1278. global $_COPYRIGHT;
  1279. $data = array('transportInfo' => $transportInfo,
  1280. 'basicInfo' => $basicInfo,
  1281. 'businessPartners' => $businessPartners,
  1282. 'packing' => $packing,
  1283. 'marksAndDescription' => $marksAndDescription,
  1284. 'Milestones' => $Milestones,
  1285. 'copyright' =>$_COPYRIGHT);
  1286. return array("msg" =>"success","data" =>$data);
  1287. }
  1288. private function trackingSfsSql($reference_number){
  1289. $sql = "with o as(
  1290. SELECT o.*
  1291. from sfs.ocean o
  1292. where o.status::text <> 'Cancelled'::text AND o.bol_type::text <> 'BOOKING'::text AND o.bol_type::text <> 'QUOTE'::text AND o.iscts = true
  1293. AND o.iscts IS NOT NULL and ARRAY['$reference_number'] && array_append(array[lower(tracking_no)::text,lower(booking_no)::text,lower(h_bol)::text,lower(m_bol)::text, lower(po_no),lower(quote_no)::text,lower(invoice_no)],'')
  1294. union
  1295. SELECT o.*
  1296. from sfs.ocean o
  1297. where o.status::text <> 'Cancelled'::text AND o.bol_type::text <> 'BOOKING'::text AND o.bol_type::text <> 'QUOTE'::text AND o.iscts = true
  1298. AND o.iscts IS NOT NULL and exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number')
  1299. )
  1300. SELECT m_eta as _m_eta, h_bol as _h_bol, m_bol as _m_bol,job_no as _job_bol,
  1301. o.* ,sh.*, cn.* ,aa.*,dd.*,fd.*,oo.*,
  1302. (select uncode from ports where code = o.port_of_transshipment) as port_of_transshipment_un
  1303. from sfs.ocean o
  1304. LEFT JOIN LATERAL ( SELECT tracking_no as _tracking_no,shippr_uncode,shipper_city,
  1305. consignee_uncode,consignee_city,incoterms,
  1306. fport_of_loading_un,
  1307. mport_of_discharge_un,
  1308. place_of_receipt_un,
  1309. place_of_delivery_un,
  1310. (select timezonecode from public.city_ports where ctrycitycode = oo.fport_of_loading_un limit 1) as pol_timezone,
  1311. (select uncity from public.ports where uncode = oo.fport_of_loading_un limit 1) as pol_uncity,
  1312. (select timezonecode from public.city_ports where ctrycitycode = oo.mport_of_discharge_un limit 1) as mpod_timezone,
  1313. (select uncity from public.ports where uncode = oo.mport_of_discharge_un limit 1) as mpod_uncity,
  1314. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_receipt_un limit 1) as por_timezone,
  1315. (select uncity from public.ports where uncode = oo.place_of_receipt_un limit 1) as por_uncity,
  1316. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_delivery_un limit 1) as pod_timezone,
  1317. (select uncity from public.ports where uncode = oo.place_of_delivery_un limit 1) as pod_uncity,
  1318. po_no as _po_no,
  1319. CASE
  1320. WHEN ((m_iffbcf is not null or m_iffbcf is null) and m_iffcpu is null and m_iffrec is null and m_iffdep is null and m_iffarr is null and m_iffafd is null) THEN 'Created'::text
  1321. WHEN ((m_iffcpu is not null or m_iffrec is not null) and m_iffdep is null and m_iffarr is null and m_iffafd is null) THEN 'Cargo Received'::text
  1322. WHEN (m_iffdep is not null and m_iffarr is null and m_iffafd is null) THEN 'Departure'::text
  1323. WHEN (m_iffarr is not null and m_iffafd is null) THEN 'Arrived'::text
  1324. WHEN (m_iffafd is not null) THEN 'Completed'::text
  1325. ELSE 'Created'::text
  1326. END AS new_status
  1327. FROM public.online_ocean oo WHERE oo.serial_no::text = o.serial_no::text) oo ON true
  1328. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1329. address_1 as cn_address_1,
  1330. address_2 as cn_address_2,
  1331. address_3 as cn_address_3,
  1332. address_4 as cn_address_4,
  1333. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1334. phone_1 as cn_phone
  1335. FROM sfs.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1336. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1337. address_1 as sh_address_1,
  1338. address_2 as sh_address_2,
  1339. address_3 as sh_address_3,
  1340. address_4 as sh_address_4,
  1341. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1342. phone_1 as sh_phone
  1343. FROM sfs.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1344. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1345. address_1 as aa_address_1,
  1346. address_2 as aa_address_2,
  1347. address_3 as aa_address_3,
  1348. address_4 as aa_address_4,
  1349. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1350. phone_1 as aa_phone,
  1351. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as aa_timezone
  1352. FROM sfs.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1353. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1354. address_1 as dd_address_1,
  1355. address_2 as dd_address_2,
  1356. address_3 as dd_address_3,
  1357. address_4 as dd_address_4,
  1358. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1359. phone_1 as dd_phone,
  1360. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as dd_timezone
  1361. FROM sfs.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1362. LEFT JOIN LATERAL ( SELECT
  1363. city as fd_city,
  1364. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as fd_timezone
  1365. FROM sfs.contacts c WHERE o.final_desination::text = c.contact_id::text) fd ON true";
  1366. return $sql;
  1367. }
  1368. private function AES_encrypted($encrypted_string){
  1369. $key = 'fT5!R1k$7Mv@4Q9X'; // 16 bytes key
  1370. $iv = '1234567890123456'; // 16 bytes IV
  1371. $decrypted = openssl_decrypt(base64_decode($encrypted_string), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  1372. return $decrypted;
  1373. }
  1374. }
  1375. ?>