login.class.php 87 KB

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