login.class.php 82 KB

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