login.class.php 85 KB

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