login.class.php 84 KB

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