login.class.php 85 KB

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