login.class.php 98 KB

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