login.class.php 84 KB

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