login.class.php 87 KB

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