login.class.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. $sql = "SELECT m_eta as _m_eta, h_bol as _h_bol, m_bol as _m_bol,job_no as _job_bol,
  1072. o.* ,sh.*, cn.* ,aa.*,dd.*,fd.*,oo.*,
  1073. (select uncode from ports where code = o.port_of_transshipment) as port_of_transshipment_un
  1074. from public.ocean o
  1075. LEFT JOIN LATERAL ( SELECT tracking_no as _tracking_no,shippr_uncode,shipper_city,
  1076. consignee_uncode,consignee_city,incoterms,
  1077. fport_of_loading_un,
  1078. mport_of_discharge_un,
  1079. place_of_receipt_un,
  1080. place_of_delivery_un,
  1081. (select timezonecode from public.city_ports where ctrycitycode = oo.fport_of_loading_un limit 1) as pol_timezone,
  1082. (select uncity from public.ports where uncode = oo.fport_of_loading_un limit 1) as pol_uncity,
  1083. (select timezonecode from public.city_ports where ctrycitycode = oo.mport_of_discharge_un limit 1) as mpod_timezone,
  1084. (select uncity from public.ports where uncode = oo.mport_of_discharge_un limit 1) as mpod_uncity,
  1085. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_receipt_un limit 1) as por_timezone,
  1086. (select uncity from public.ports where uncode = oo.place_of_receipt_un limit 1) as por_uncity,
  1087. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_delivery_un limit 1) as pod_timezone,
  1088. (select uncity from public.ports where uncode = oo.place_of_delivery_un limit 1) as pod_uncity,
  1089. po_no as _po_no,
  1090. CASE
  1091. 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
  1092. 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
  1093. WHEN (m_iffdep is not null and m_iffarr is null and m_iffafd is null) THEN 'Departure'::text
  1094. WHEN (m_iffarr is not null and m_iffafd is null) THEN 'Arrived'::text
  1095. WHEN (m_iffafd is not null) THEN 'Completed'::text
  1096. ELSE 'Created'::text
  1097. END AS new_status
  1098. FROM public.online_ocean oo WHERE oo.serial_no::text = o.serial_no::text) oo ON true
  1099. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1100. address_1 as cn_address_1,
  1101. address_2 as cn_address_2,
  1102. address_3 as cn_address_3,
  1103. address_4 as cn_address_4,
  1104. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1105. phone_1 as cn_phone
  1106. FROM ocean.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1107. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1108. address_1 as sh_address_1,
  1109. address_2 as sh_address_2,
  1110. address_3 as sh_address_3,
  1111. address_4 as sh_address_4,
  1112. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1113. phone_1 as sh_phone
  1114. FROM ocean.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1115. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1116. address_1 as aa_address_1,
  1117. address_2 as aa_address_2,
  1118. address_3 as aa_address_3,
  1119. address_4 as aa_address_4,
  1120. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1121. phone_1 as aa_phone,
  1122. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as aa_timezone
  1123. FROM ocean.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1124. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1125. address_1 as dd_address_1,
  1126. address_2 as dd_address_2,
  1127. address_3 as dd_address_3,
  1128. address_4 as dd_address_4,
  1129. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1130. phone_1 as dd_phone,
  1131. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as dd_timezone
  1132. FROM ocean.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1133. LEFT JOIN LATERAL ( SELECT
  1134. city as fd_city,
  1135. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as fd_timezone
  1136. FROM ocean.contacts c WHERE o.final_desination::text = c.contact_id::text) fd ON true
  1137. where (lower(tracking_no) = '" . strtolower($reference_number) . "'
  1138. or lower(booking_no) = '" . strtolower($reference_number) . "'
  1139. or lower(h_bol) = '" . strtolower($reference_number) . "'
  1140. or lower(m_bol) = '" . strtolower($reference_number) . "'
  1141. or lower(po_no) = '" . strtolower($reference_number) . "'
  1142. or lower(quote_no) = '" . strtolower($reference_number) . "'
  1143. or lower(invoice_no) = '" . strtolower($reference_number) . "'
  1144. or exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number'))";
  1145. $ocean_arr = common::excuteListSql($sql);
  1146. if(empty($ocean_arr)){
  1147. $sql = $this->trackingSfsSql($reference_number);
  1148. $ocean_arr = common::excuteListSql($sql);
  1149. if(empty($ocean_arr)){
  1150. $data = array("msg" =>"No matches");
  1151. return $data;
  1152. }
  1153. }
  1154. if(!empty($ocean_arr) && count($ocean_arr) > 1){
  1155. $data = array("msg" =>"Multiple results");
  1156. return $data;
  1157. }
  1158. $ocean = $ocean_arr[0];
  1159. //获取对应uncode 对应的时间
  1160. $uncodes = $ocean['fport_of_loading_un'].";".$ocean['mport_of_discharge_un'];
  1161. $codeinfo = common::getCityPortsInfo($uncodes);
  1162. //处理transportInfo信息数据
  1163. $transportInfo = array("Tracking No." =>$ocean['_tracking_no'],"status"=>$ocean['new_status'],"mode" => "Ocean Freight",
  1164. "origin" =>$ocean['shippr_uncode'],"destination" =>$ocean['consignee_uncode'],
  1165. "etd" =>$ocean['f_etd'],"atd" =>$ocean['atd'],
  1166. "etd_timezone" =>$codeinfo[$ocean['fport_of_loading_un']],
  1167. "atd_timezone" =>$codeinfo[$ocean['fport_of_loading_un']],
  1168. "eta" =>$ocean['m_eta'],"ata" =>$ocean['ata'],
  1169. "eta_timezone" =>$codeinfo[$ocean['mport_of_discharge_un']],
  1170. "ata_timezone" =>$codeinfo[$ocean['mport_of_discharge_un']]);
  1171. $data['transportInfo'] = $transportInfo;
  1172. //处理basicInfo信息数据
  1173. $vessel = utils::outDisplayForMerge($ocean['f_vessel'],$ocean['m_vessel']);
  1174. $voyage = utils::outDisplayForMerge($ocean['f_voyage'],$ocean['m_voyage']);
  1175. $basicInfo = array("MAWB/MBL No." =>$ocean['m_bol'],"HAWB/HBOL" => $ocean['h_bol'],"Carrier_Booking_No" =>$ocean['booking_no'],
  1176. "PO_NO" =>$ocean['po_no'],"Vessel/Airline" =>$vessel,"Voyage/Filght" =>$voyage,
  1177. "Incoterm" =>$ocean['incoterms'],"Service_Type" =>$ocean['service']);
  1178. //处理 拼接地址 ocean表单exp 字段无法精准分割电话和地址信息,只能从contacts表里查询
  1179. $shipper_address = common::retStationInfo($ocean['sh_address_1'], $ocean['sh_address_2'], $ocean['sh_address_3'], $ocean['sh_address_4'],
  1180. $ocean['sh_city'], $ocean['sh_state'], $ocean['sh_zipcode'], $ocean['sh_country']);
  1181. $consignee_address = common::retStationInfo($ocean['cn_address_1'], $ocean['cn_address_2'], $ocean['cn_address_3'], $ocean['cn_address_4'],
  1182. $ocean['cn_city'], $ocean['cn_state'], $ocean['cn_zipcode'], $ocean['cn_country']);
  1183. $origin_address = common::retStationInfo($ocean['aa_address_1'], $ocean['aa_address_2'], $ocean['aa_address_3'], $ocean['aa_address_4'],
  1184. $ocean['aa_city'], $ocean['aa_state'], $ocean['aa_zipcode'], $ocean['aa_country']);
  1185. $destination_address = common::retStationInfo($ocean['dd_address_1'], $ocean['dd_address_2'], $ocean['dd_address_3'], $ocean['dd_address_4'],
  1186. $ocean['dd_city'], $ocean['dd_state'], $ocean['dd_zipcode'], $ocean['dd_country']);
  1187. $shipperPartners = array("company" =>$ocean['sh_company'],"address"=>$shipper_address,"phone"=>$ocean['sh_phone']);
  1188. $consigneePartners = array("company" =>$ocean['cn_company'],"address"=>$consignee_address,"phone"=>$ocean['cn_phone']);
  1189. $originPartners = array("company" =>$ocean['aa_company'],"address"=>$origin_address,"phone"=>$ocean['aa_phone']);
  1190. $destinationPartners = array("company" =>$ocean['dd_company'],"address"=>$destination_address,"phone"=>$ocean['dd_phone']);
  1191. $businessPartners = array("shipper"=>$shipperPartners,"consignee" => $consigneePartners,"origin" => $originPartners,"destination" => $destinationPartners);
  1192. //处理marksAndDescription
  1193. $marksAndDescription = array("marks"=>$ocean['marks'],"description"=>$ocean['description']);
  1194. $sql = "SELECT " . column::getInstance()->getSearchSql('Ocean_Container') . " ,net_lbs from oc_container where lower(serial_no) = '" . strtolower($ocean['serial_no']) . "'";
  1195. $rss = common::excuteListSql($sql);
  1196. $quantity_unit = array();
  1197. $g_weight_tolal = 0;
  1198. $ch_weight_tolal = 0;
  1199. $ch_weight_tolal_grs_lbs = 0;
  1200. $cbm_tolal = 0;
  1201. foreach ($rss as $key => $rs) {
  1202. $unit = $rs['unit'];
  1203. if (array_key_exists($unit, $quantity_unit)) {
  1204. $quantity_unit[$unit] = $quantity_unit[$unit] + $rs['qty'];
  1205. } else {
  1206. $quantity_unit[$unit] = $rs['qty'];
  1207. }
  1208. $g_weight_tolal += $rs['grs_kgs'];
  1209. $ch_weight_tolal += $rs['net_lbs'];
  1210. $ch_weight_tolal_grs_lbs += $rs['grs_lbs'];
  1211. $cbm_tolal += $rs['cbm'];
  1212. }
  1213. $quantity_tolal = "";
  1214. foreach($quantity_unit as $uk => $uv){
  1215. $quantity_tolal.=$uv." ".$uk." ";
  1216. }
  1217. $ch_weight_tolal = empty($ch_weight_tolal) ? $ch_weight_tolal_grs_lbs : $ch_weight_tolal;
  1218. //Packing 不确定信息
  1219. $packing = array("Quantity/Unit"=>$quantity_tolal,"G. Weight" => $g_weight_tolal." KGS","Ch. Weight" => $ch_weight_tolal." LBS","Volume" => $cbm_tolal." CBM");
  1220. /* Container Status */
  1221. //数据库里配置好
  1222. //$sql = "SELECT " . column::getInstance()->getSearchSql('Ocean_Container_Status') . " from oc_container_v where lower(serial_no) = '" . strtolower($serial_no) . "'";
  1223. $serial_no = $ocean["serial_no"];
  1224. $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
  1225. WHERE o.serial_no='$serial_no'";
  1226. $ctnr_data = common::excuteListSql($ctnr_sql);
  1227. foreach ($ctnr_data as $cd){
  1228. //存在柜号为空的数据情况
  1229. if(empty($cd['ctnr'])){
  1230. continue;
  1231. }
  1232. $ctnr_status_sql = "select s.source_id, s.event_base as event,
  1233. to_char(to_timestamp(s.event_date, 'YYYYMMDD'), 'YYYY-MM-DD') as eventdate,
  1234. to_char(to_timestamp(s.event_time, 'HH24MI'), 'HH24:MI') as eventtime,
  1235. e.description,s.event_type as eventtype,
  1236. s.event_code as eventcode, s.event_city as eventcity,
  1237. (select timezonecode from public.city_ports where ctrycitycode = s.event_code) as timezone,
  1238. (select uncity from public.ports where uncode = s.event_code) as uncity,
  1239. case when event ='I' then 'IFFREC'::text
  1240. when event ='AE' then 'IFFONB'::text
  1241. when event ='VD' then 'IFFDEP'::text
  1242. when event ='EB' or event ='VA' then 'IFFARR'::text
  1243. when event ='UV' then 'IFFUND'::text
  1244. when event ='VA' then 'IFFAFD'::text
  1245. when event ='AV' then 'IFFCTA'::text
  1246. when event ='CT' then 'IFFICC'::text
  1247. when event ='OA' or event ='D' then 'IFFPPD'::text
  1248. when event ='EE' then 'IFFECP'::text
  1249. else '' ::text
  1250. end as milestone_code
  1251. from public.ra_online_container_status_v s
  1252. left join ra_online_edi_event e on s.event_base = e.ra_name
  1253. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1254. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1255. order by to_timestamp(s.event_date, 'YYYYMMDD') asc,
  1256. to_timestamp(s.event_time, 'HH24MI') asc,e.ra_order asc";
  1257. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1258. //记录所有的信息
  1259. $EDI315TimeAndLocation = array();
  1260. foreach($ctnr_status as $event){
  1261. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1262. //如果存在EB 的EB 的优先级最高
  1263. continue;
  1264. }
  1265. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1266. //如果存在OA 的OA 的优先级最高
  1267. continue;
  1268. }
  1269. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone'],"location"=>$event['uncity']);
  1270. }
  1271. }
  1272. //Milestones 数据信息待定
  1273. $Milestones = common::getMilestonesInfo($ocean,$EDI315TimeAndLocation);
  1274. global $_COPYRIGHT;
  1275. $data = array('transportInfo' => $transportInfo,
  1276. 'basicInfo' => $basicInfo,
  1277. 'businessPartners' => $businessPartners,
  1278. 'packing' => $packing,
  1279. 'marksAndDescription' => $marksAndDescription,
  1280. 'Milestones' => $Milestones,
  1281. 'copyright' =>$_COPYRIGHT);
  1282. return array("msg" =>"success","data" =>$data);
  1283. }
  1284. private function trackingSfsSql($reference_number){
  1285. $sql = "SELECT m_eta as _m_eta, h_bol as _h_bol, m_bol as _m_bol,job_no as _job_bol,
  1286. o.* ,sh.*, cn.* ,aa.*,dd.*,fd.*,oo.*,
  1287. (select uncode from ports where code = o.port_of_transshipment) as port_of_transshipment_un
  1288. from sfs.ocean o
  1289. LEFT JOIN LATERAL ( SELECT tracking_no as _tracking_no,shippr_uncode,shipper_city,
  1290. consignee_uncode,consignee_city,incoterms,
  1291. fport_of_loading_un,
  1292. mport_of_discharge_un,
  1293. place_of_receipt_un,
  1294. place_of_delivery_un,
  1295. (select timezonecode from public.city_ports where ctrycitycode = oo.fport_of_loading_un limit 1) as pol_timezone,
  1296. (select uncity from public.ports where uncode = oo.fport_of_loading_un limit 1) as pol_uncity,
  1297. (select timezonecode from public.city_ports where ctrycitycode = oo.mport_of_discharge_un limit 1) as mpod_timezone,
  1298. (select uncity from public.ports where uncode = oo.mport_of_discharge_un limit 1) as mpod_uncity,
  1299. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_receipt_un limit 1) as por_timezone,
  1300. (select uncity from public.ports where uncode = oo.place_of_receipt_un limit 1) as por_uncity,
  1301. (select timezonecode from public.city_ports where ctrycitycode = oo.place_of_delivery_un limit 1) as pod_timezone,
  1302. (select uncity from public.ports where uncode = oo.place_of_delivery_un limit 1) as pod_uncity,
  1303. po_no as _po_no,
  1304. CASE
  1305. 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
  1306. 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
  1307. WHEN (m_iffdep is not null and m_iffarr is null and m_iffafd is null) THEN 'Departure'::text
  1308. WHEN (m_iffarr is not null and m_iffafd is null) THEN 'Arrived'::text
  1309. WHEN (m_iffafd is not null) THEN 'Completed'::text
  1310. ELSE 'Created'::text
  1311. END AS new_status
  1312. FROM public.online_ocean oo WHERE oo.serial_no::text = o.serial_no::text) oo ON true
  1313. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1314. address_1 as cn_address_1,
  1315. address_2 as cn_address_2,
  1316. address_3 as cn_address_3,
  1317. address_4 as cn_address_4,
  1318. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1319. phone_1 as cn_phone
  1320. FROM sfs.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1321. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1322. address_1 as sh_address_1,
  1323. address_2 as sh_address_2,
  1324. address_3 as sh_address_3,
  1325. address_4 as sh_address_4,
  1326. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1327. phone_1 as sh_phone
  1328. FROM sfs.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1329. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1330. address_1 as aa_address_1,
  1331. address_2 as aa_address_2,
  1332. address_3 as aa_address_3,
  1333. address_4 as aa_address_4,
  1334. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1335. phone_1 as aa_phone,
  1336. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as aa_timezone
  1337. FROM sfs.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1338. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1339. address_1 as dd_address_1,
  1340. address_2 as dd_address_2,
  1341. address_3 as dd_address_3,
  1342. address_4 as dd_address_4,
  1343. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1344. phone_1 as dd_phone,
  1345. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as dd_timezone
  1346. FROM sfs.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1347. LEFT JOIN LATERAL ( SELECT
  1348. city as fd_city,
  1349. (select timezonecode from public.city_ports where ctrycitycode = LEFT(country, 2) || COALESCE(city_code,'') limit 1) as fd_timezone
  1350. FROM sfs.contacts c WHERE o.final_desination::text = c.contact_id::text) fd ON true
  1351. where (lower(tracking_no) = '" . strtolower($reference_number) . "'
  1352. or lower(booking_no) = '" . strtolower($reference_number) . "'
  1353. or lower(h_bol) = '" . strtolower($reference_number) . "'
  1354. or lower(m_bol) = '" . strtolower($reference_number) . "'
  1355. or lower(po_no) = '" . strtolower($reference_number) . "'
  1356. or lower(quote_no) = '" . strtolower($reference_number) . "'
  1357. or lower(invoice_no) = '" . strtolower($reference_number) . "'
  1358. or exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number'))";
  1359. return $sql;
  1360. }
  1361. private function AES_encrypted($encrypted_string){
  1362. $key = 'fT5!R1k$7Mv@4Q9X'; // 16 bytes key
  1363. $iv = '1234567890123456'; // 16 bytes IV
  1364. $decrypted = openssl_decrypt(base64_decode($encrypted_string), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  1365. return $decrypted;
  1366. }
  1367. }
  1368. ?>