login.class.php 96 KB

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