login.class.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. CASE
  1060. WHEN cc.status is null THEN 'Created'::text
  1061. ELSE cc.status::text
  1062. END AS new_status
  1063. from public.ocean o
  1064. left join LATERAL (select case when a.code='IFFBCF' then 'Created'
  1065. when a.code='IFFCPU' then 'Cargo Received'
  1066. when a.code='IFFREC' then 'Cargo Received'
  1067. when a.code='IFFDEP' then 'Departed'
  1068. when a.code='IFFARR' then 'Arrived'
  1069. when a.code='IFFAFD' then 'Completed'
  1070. else 'Created' END as status
  1071. from ocean_milestone a
  1072. where a.serial_no=o.serial_no
  1073. and a.code in (select regexp_split_to_table('IFFBCF,IFFCPU,IFFREC,IFFDEP,IFFARR,IFFAFD', ','))
  1074. and a.act_date is not null
  1075. order by id desc limit 1) cc on true
  1076. LEFT JOIN LATERAL ( SELECT incoterms as incoterms,shippr_uncode,shipper_city,
  1077. consignee_uncode,consignee_city
  1078. FROM online_ocean WHERE online_ocean.serial_no = o.serial_no) oo ON true
  1079. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1080. address_1 as cn_address_1,
  1081. address_2 as cn_address_2,
  1082. address_3 as cn_address_3,
  1083. address_4 as cn_address_4,
  1084. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1085. phone_1 as cn_phone
  1086. FROM ocean.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1087. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1088. address_1 as sh_address_1,
  1089. address_2 as sh_address_2,
  1090. address_3 as sh_address_3,
  1091. address_4 as sh_address_4,
  1092. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1093. phone_1 as sh_phone
  1094. FROM ocean.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1095. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1096. address_1 as aa_address_1,
  1097. address_2 as aa_address_2,
  1098. address_3 as aa_address_3,
  1099. address_4 as aa_address_4,
  1100. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1101. phone_1 as aa_phone
  1102. FROM ocean.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1103. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1104. address_1 as dd_address_1,
  1105. address_2 as dd_address_2,
  1106. address_3 as dd_address_3,
  1107. address_4 as dd_address_4,
  1108. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1109. phone_1 as dd_phone
  1110. FROM ocean.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1111. where (lower(tracking_no) = '" . strtolower($reference_number) . "'
  1112. or lower(booking_no) = '" . strtolower($reference_number) . "'
  1113. or lower(h_bol) = '" . strtolower($reference_number) . "'
  1114. or lower(m_bol) = '" . strtolower($reference_number) . "'
  1115. or lower(po_no) = '" . strtolower($reference_number) . "'
  1116. or lower(quote_no) = '" . strtolower($reference_number) . "'
  1117. or lower(invoice_no) = '" . strtolower($reference_number) . "'
  1118. or exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number'))";
  1119. $ocean_arr = common::excuteListSql($sql);
  1120. error_log($sql);
  1121. if(empty($ocean_arr)){
  1122. $sql = $this->trackingSfsSql($reference_number);
  1123. $ocean_arr = common::excuteListSql($sql);
  1124. if(empty($ocean_arr)){
  1125. $data = array("msg" =>"No matches");
  1126. return $data;
  1127. }
  1128. }
  1129. if(!empty($ocean_arr) && count($ocean_arr) > 1){
  1130. $data = array("msg" =>"Multiple results");
  1131. return $data;
  1132. }
  1133. $ocean = $ocean_arr[0];
  1134. //处理transportInfo信息数据
  1135. $transportInfo = array("Tracking No." =>$ocean['tracking_no'],"status"=>$ocean['new_status'],"mode" => "Ocean Freight",
  1136. "origin" =>$ocean['origin_station'],"destination" =>$ocean['destination_station'],
  1137. "etd" =>$ocean['f_etd'],"atd" =>"","eta" =>$ocean['m_eta'],"ata" =>"");
  1138. //处理basicInfo信息数据
  1139. $vessel = utils::outDisplayForMerge($ocean['f_vessel'],$ocean['m_vessel']);
  1140. $voyage = utils::outDisplayForMerge($ocean['f_voyage'],$ocean['m_voyage']);
  1141. $basicInfo = array("MAWB/MBL No." =>$ocean['m_bol'],"HAWB/HBOL" => $ocean['h_bol'],"Carrier_Booking_No" =>$ocean['carrier_booking'],
  1142. "PO_NO" =>$ocean['po_no'],"Vessel/Airline" =>$vessel,"Voyage/Filght" =>$voyage,
  1143. "Incoterm" =>$ocean['incoterms'],"Service_Type" =>$ocean['service']);
  1144. //处理 拼接地址 ocean表单exp 字段无法精准分割电话和地址信息,只能从contacts表里查询
  1145. $shipper_address = common::retStationInfo($ocean['sh_address_1'], $ocean['sh_address_2'], $ocean['sh_address_3'], $ocean['sh_address_4'],
  1146. $ocean['sh_city'], $ocean['sh_state'], $ocean['sh_zipcode'], $ocean['sh_country']);
  1147. $consignee_address = common::retStationInfo($ocean['cn_address_1'], $ocean['cn_address_2'], $ocean['cn_address_3'], $ocean['cn_address_4'],
  1148. $ocean['cn_city'], $ocean['cn_state'], $ocean['cn_zipcode'], $ocean['cn_country']);
  1149. $origin_address = common::retStationInfo($ocean['aa_address_1'], $ocean['aa_address_2'], $ocean['aa_address_3'], $ocean['aa_address_4'],
  1150. $ocean['aa_city'], $ocean['aa_state'], $ocean['aa_zipcode'], $ocean['aa_country']);
  1151. $destination_address = common::retStationInfo($ocean['dd_address_1'], $ocean['dd_address_2'], $ocean['dd_address_3'], $ocean['dd_address_4'],
  1152. $ocean['dd_city'], $ocean['dd_state'], $ocean['dd_zipcode'], $ocean['dd_country']);
  1153. $shipperPartners = array("company" =>$ocean['sh_company'],"address"=>$shipper_address,"phone"=>$ocean['sh_phone']);
  1154. $consigneePartners = array("company" =>$ocean['cn_company'],"address"=>$consignee_address,"phone"=>$ocean['cn_phone']);
  1155. $originPartners = array("company" =>$ocean['aa_company'],"address"=>$origin_address,"phone"=>$ocean['aa_phone']);
  1156. $destinationPartners = array("company" =>$ocean['dd_company'],"address"=>$destination_address,"phone"=>$ocean['dd_phone']);
  1157. $businessPartners = array("shipper"=>$shipperPartners,"consignee" => $consigneePartners,"origin" => $originPartners,"destination" => $destinationPartners);
  1158. //处理marksAndDescription
  1159. $marksAndDescription = array("marks"=>$ocean['marks'],"description"=>$ocean['description']);
  1160. $sql = "SELECT " . column::getInstance()->getSearchSql('Ocean_Container') . " from oc_container where lower(serial_no) = '" . strtolower($ocean['serial_no']) . "'";
  1161. $rss = common::excuteListSql($sql);
  1162. $quantity_tolal = 0;
  1163. $packages = "CTN";
  1164. $g_weight_tolal = 0;
  1165. $ch_weight_tolal = 0;
  1166. $cbm_tolal = 0;
  1167. foreach ($rss as $key => $rs) {
  1168. $quantity_tolal += $rs['qty'];
  1169. $g_weight_tolal += $rs['grs_kgs'];
  1170. $ch_weight_tolal += $rs['grs_lbs'];
  1171. $cbm_tolal += $rs['cbm'];
  1172. $packages = " ".$rs['unit'];
  1173. }
  1174. //Packing 不确定信息
  1175. $packing = array("Quantity/Unit"=>$quantity_tolal.$packages,"G. Weight" => $g_weight_tolal." KGS","Ch. Weight" => $ch_weight_tolal." LBS","Volume" => $cbm_tolal." CBM");
  1176. //Milestones 数据信息待定
  1177. $Milestones = common::getMilestonesInfo($ocean);
  1178. global $_COPYRIGHT;
  1179. $data = array('transportInfo' => $transportInfo,
  1180. 'basicInfo' => $basicInfo,
  1181. 'businessPartners' => $businessPartners,
  1182. 'packing' => $packing,
  1183. 'marksAndDescription' => $marksAndDescription,
  1184. 'Milestones' => $Milestones,
  1185. 'copyright' =>$_COPYRIGHT);
  1186. return array("msg" =>"success","data" =>$data);
  1187. }
  1188. private function trackingSfsSql($reference_number){
  1189. $sql = "SELECT o.* ,sh.*, cn.* ,aa.*,dd.* ,
  1190. CASE
  1191. WHEN cc.status is null THEN 'Created'::text
  1192. ELSE cc.status::text
  1193. END AS new_status
  1194. from sfs.ocean o
  1195. left join LATERAL (select case when a.code='IFFBCF' then 'Created'
  1196. when a.code='IFFCPU' then 'Cargo Received'
  1197. when a.code='IFFREC' then 'Cargo Received'
  1198. when a.code='IFFDEP' then 'Departed'
  1199. when a.code='IFFARR' then 'Arrived'
  1200. when a.code='IFFAFD' then 'Completed'
  1201. else 'Created' END as status
  1202. from ocean_milestone a
  1203. where a.serial_no=o.serial_no
  1204. and a.code in (select regexp_split_to_table('IFFBCF,IFFCPU,IFFREC,IFFDEP,IFFARR,IFFAFD', ','))
  1205. order by id desc limit 1) cc on true
  1206. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1207. address_1 as cn_address_1,
  1208. address_2 as cn_address_2,
  1209. address_3 as cn_address_3,
  1210. address_4 as cn_address_4,
  1211. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1212. phone_1 as cn_phone
  1213. FROM sfs.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1214. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1215. address_1 as sh_address_1,
  1216. address_2 as sh_address_2,
  1217. address_3 as sh_address_3,
  1218. address_4 as sh_address_4,
  1219. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1220. phone_1 as sh_phone
  1221. FROM sfs.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1222. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1223. address_1 as aa_address_1,
  1224. address_2 as aa_address_2,
  1225. address_3 as aa_address_3,
  1226. address_4 as aa_address_4,
  1227. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1228. phone_1 as aa_phone
  1229. FROM sfs.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1230. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1231. address_1 as dd_address_1,
  1232. address_2 as dd_address_2,
  1233. address_3 as dd_address_3,
  1234. address_4 as dd_address_4,
  1235. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1236. phone_1 as dd_phone
  1237. FROM sfs.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1238. where (lower(tracking_no) = '" . strtolower($reference_number) . "'
  1239. or lower(booking_no) = '" . strtolower($reference_number) . "'
  1240. or lower(h_bol) = '" . strtolower($reference_number) . "'
  1241. or lower(m_bol) = '" . strtolower($reference_number) . "'
  1242. or lower(po_no) = '" . strtolower($reference_number) . "'
  1243. or lower(quote_no) = '" . strtolower($reference_number) . "'
  1244. or lower(invoice_no) = '" . strtolower($reference_number) . "'
  1245. or exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number'))";
  1246. return $sql;
  1247. }
  1248. private function AES_encrypted($encrypted_string){
  1249. $key = 'fT5!R1k$7Mv@4Q9X'; // 16 bytes key
  1250. $iv = '1234567890123456'; // 16 bytes IV
  1251. $decrypted = openssl_decrypt(base64_decode($encrypted_string), 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv);
  1252. return $decrypted;
  1253. }
  1254. }
  1255. ?>