login.class.php 87 KB

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