login.class.php 85 KB

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