login.class.php 83 KB

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