login.class.php 88 KB

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