login.class.php 74 KB

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