login.class.php 84 KB

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