login.class.php 84 KB

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