login.class.php 87 KB

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