login.class.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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['belong_schemas'])) {
  45. $rs['belong_schemas'] = "public";
  46. }
  47. if (empty($rs['main_schemas'])) {
  48. $rs['main_schemas'] = "public";
  49. }
  50. if (!empty($rs)) {
  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. 'msg' => 'no_active',
  65. 'login_version' => $rs["login_version"],
  66. 'data' => $data,
  67. 'desc' => "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. 'msg' => 'no_online',
  98. 'login_version' => $rs["login_version"],
  99. 'data' => '',
  100. 'desc' => '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. 'msg' => 'no_active',
  125. 'login_version' => $rs["login_version"],
  126. 'data' => '',
  127. 'desc' => '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. 'msg' => $tar,
  138. 'login_version' => $rs["login_version"],
  139. 'data' => '',
  140. 'desc' => $tar
  141. );
  142. common::echo_json_encode(500, $data);
  143. exit();
  144. }
  145. if (empty($rs['last_pwd_change'])) {
  146. $data = array(
  147. 'msg' => 'first_login',
  148. 'login_version' => $rs["login_version"],
  149. 'data' => '',
  150. 'desc' => '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. 'msg' => 'login user email is empty',
  212. 'login_version' => $rs["login_version"],
  213. 'desc' => '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. 'msg' => 'database_error',
  364. 'login_version' => $rs["login_version"],
  365. 'desc' => 'database_error'
  366. );
  367. common::echo_json_encode(500, $data);
  368. exit();
  369. }
  370. } else {
  371. $data = array(
  372. 'msg' => 'no_exist',
  373. 'login_version' => $rs["login_version"],
  374. 'desc' => '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(6);
  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 = 14;
  452. $y = 12;
  453. for ($i = 0; $i < 6; $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 ($_SESSION['captcha'] != $verifcation_code) {
  475. $data = array(
  476. 'msg' => 'verifcation_error',
  477. 'data' => ''
  478. );
  479. common::echo_json_encode(500, $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. $data = array(
  500. 'msg' => $msg,
  501. );
  502. common::echo_json_encode(200, $data);
  503. exit();
  504. }
  505. public function generateCaptcha($length = 6) {
  506. $captcha = '';
  507. $chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
  508. for ($i = 0; $i < $length; $i++) {
  509. // 随机选择字母或数字
  510. $char = $chars[mt_rand(0, strlen($chars) - 1)];
  511. $captcha .= strval($char);
  512. }
  513. return $captcha;
  514. }
  515. public function do_login_auto() {
  516. $uname = common::check_input($_GET['u']);
  517. $password = common::check_input($_GET['p']);
  518. $login_error_times = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Login_Error_Times'");
  519. $lock_user_seconds = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Lock_User_Seconds'");
  520. if (!empty($uname) || !empty($password)) {
  521. } else {
  522. $uname = common::check_input($_POST['uname']);
  523. $sql = $this->getLoginSql($uname);
  524. $rs = common::excuteObjectSql($sql);
  525. if (empty($rs['belong_schemas'])) {
  526. $rs['belong_schemas'] = "public";
  527. }
  528. if (empty($rs['main_schemas'])) {
  529. $rs['main_schemas'] = "public";
  530. }
  531. if (!empty($rs)) {
  532. //验证employee是否active
  533. if (!empty($rs["employee_id"]) && $rs["employee_id_active"] != "t") {
  534. if (strtolower(Soure) =='topocean'){
  535. if (strtolower($rs['user_type']) == "employee" && utils::endWith($rs['email'], "cn")) {
  536. $data = "<a href='mailto:lilyyang@topocean.com.cn'>lilyyang@topocean.com.cn</a>";
  537. }else{
  538. $data = "<a href='mailto:winnie@topocean.com'>winnie@topocean.com</a>";
  539. }
  540. }
  541. if (strtolower(Soure) =='apex'){
  542. $data = "<a href='mailto:maria.wang@apexshipping.com.cn'>maria.wang@apexshipping.com.cn</a>";
  543. }
  544. $data = array(
  545. 'msg' => 'no_active',
  546. 'login_version' => $rs["login_version"],
  547. 'data' => $data
  548. );
  549. common::echo_json_encode(500, $data);
  550. $this->failedLogin($uname, 'Employee not active');
  551. exit();
  552. }
  553. //add 处理登录状态
  554. $userInfo = common::check_input($_COOKIE['userInfo']);
  555. $noCheckPwd = false;
  556. if (!empty($userInfo)) {
  557. $userInfoSplit = explode("_", $userInfo);
  558. if ($uname == $userInfoSplit[0]) {
  559. if ($userInfoSplit[1] == md5($rs['password'])) {
  560. $noCheckPwd = true;
  561. }
  562. }
  563. }
  564. //if (!$noCheckPwd) {
  565. if ($rs['error_login_count'] > $login_error_times && $rs['second'] < $lock_user_seconds) {
  566. $data = array(
  567. 'msg' => 'error_times',
  568. 'login_version' => $rs["login_version"],
  569. 'data' => ceil(($lock_user_seconds - $rs['second']) / 60)
  570. );
  571. common::echo_json_encode(500, $data);
  572. $this->failedLogin($uname, 'Failed login too times');
  573. exit();
  574. }
  575. if ($rs['is_online'] != 't') {
  576. if (strtolower($rs['user_type']) != "employee") {
  577. $data =array(
  578. 'msg' => 'no_online',
  579. 'login_version' => $rs["login_version"],
  580. 'data' => ''
  581. );
  582. common::echo_json_encode(500, $data);
  583. $this->failedLogin($uname, 'Online is not active');
  584. exit();
  585. }
  586. }
  587. //if ($rs['decrypt_password'] != $_POST['psw']) {
  588. if ($noCheckPwd) {
  589. }else{
  590. if ($rs['password'] != $_POST['psw']) {
  591. 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) . "'");
  592. $data = array(
  593. 'msg' => 'password_error',
  594. 'login_version' => $rs["login_version"],
  595. 'data' => ''
  596. );
  597. common::echo_json_encode(500, $data);
  598. $this->failedLogin($uname, 'Password is wrong');
  599. exit();
  600. }
  601. }
  602. if ($rs['online_active'] != 't') {
  603. $data = array(
  604. 'msg' => 'no_active',
  605. 'login_version' => $rs["login_version"],
  606. 'data' => ''
  607. );
  608. common::echo_json_encode(500, $data);
  609. $this->failedLogin($uname, 'Online is not active');
  610. exit();
  611. }
  612. //check password length
  613. $tar = utils::checkPassword($rs['password']);
  614. if (!empty($tar)) {
  615. $data = array(
  616. 'msg' => $tar,
  617. 'login_version' => $rs["login_version"],
  618. 'data' => ''
  619. );
  620. common::echo_json_encode(500, $data);
  621. exit();
  622. }
  623. if (empty($rs['last_pwd_change'])) {
  624. $data = array(
  625. 'msg' => 'first_login',
  626. 'login_version' => $rs["login_version"],
  627. 'data' => ''
  628. );
  629. common::echo_json_encode(500, $data);
  630. exit();
  631. }
  632. //}
  633. //get more infor by employee_id or contact_id
  634. $sql = '';
  635. $diffdate = $rs['last_pwd_change_date'];
  636. $user_type = $rs['user_type'];
  637. //if user is customer, check company
  638. if (strtolower($user_type) == 'customer') {
  639. $company = $rs['company_name'];
  640. } else {
  641. if (!empty($rs['station']))
  642. $company = $rs['station'];
  643. }
  644. if (strtolower($uname) == 'ra.admin') {
  645. $company = 'Admin';
  646. }
  647. // get system config
  648. $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')";
  649. $rs1s = common::excuteListSql($sql);
  650. foreach ($rs1s as $rs1) {
  651. if ($rs1['ra_name'] == 'employee_session_timeout')
  652. $EMPLOYEE_SESSION_TIMEOUT = $rs1['ra_value'];
  653. if ($rs1['ra_name'] == 'customer_session_timeout')
  654. $CUSTOMER_SESSION_TIMEOUT = $rs1['ra_value'];
  655. if ($rs1['ra_name'] == 'password_change_alert')
  656. $PASSWORD_CHANGE_ALERT = $rs1['ra_value'];
  657. if ($rs1['ra_name'] == 'employee_password_change_cycle')
  658. $EMPLOYEE_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  659. if ($rs1['ra_name'] == 'customer_password_change_cycle')
  660. $CUSTOMER_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  661. }
  662. $sql="select item_value from config where item='passwordChangePeriod'";
  663. $pcp = common::excuteObjectSql($sql);
  664. $passwordChangePeriod = json_decode($pcp["item_value"],true);
  665. if (strtolower($rs['user_type']) == 'employee') {
  666. $PASSWORD_CHANGE_CYCLE = $EMPLOYEE_PASSWORD_CHANGE_CYCLE;
  667. $SESSION_TIMEOUT = $EMPLOYEE_SESSION_TIMEOUT;
  668. //如果有新配置,则采用新配置
  669. if (!empty($pcp)) {
  670. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Employee"]["days"];
  671. $PASSWORD_CHANGE_ALERT = $passwordChangePeriod["Employee"]["advanceDays"];
  672. }
  673. } else {
  674. $PASSWORD_CHANGE_CYCLE = $CUSTOMER_PASSWORD_CHANGE_CYCLE;
  675. $SESSION_TIMEOUT = $CUSTOMER_SESSION_TIMEOUT;
  676. //如果有新配置,则采用新配置
  677. if (!empty($pcp)) {
  678. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Customer"]["days"];
  679. $PASSWORD_CHANGE_ALERT = $passwordChangePeriod["Customer"]["advanceDays"];
  680. }
  681. }
  682. /* if ($diffdate > $PASSWORD_CHANGE_CYCLE) {// Timeout
  683. echo json_encode(array(
  684. 'msg' => 'password_require_change',
  685. 'login_version' => $rs["login_version"],
  686. 'data' => ''
  687. ));
  688. $this->failedLogin($uname, 'Required password change');
  689. exit();
  690. }*/
  691. $loginName = $rs['user_login'];
  692. $email = $rs['email'];
  693. if ($diffdate > $PASSWORD_CHANGE_CYCLE) {// Timeout
  694. if(empty($email)){
  695. $data = array(
  696. 'status' => '0',
  697. 'msg' => 'login user email is empty',
  698. 'login_version' => $rs["login_version"],
  699. 'data' => ''
  700. );
  701. common::echo_json_encode(500, $data);
  702. exit();
  703. }else{
  704. //$this -> passwordExpires($loginName,$email);
  705. }
  706. }
  707. if ($diffdate == $PASSWORD_CHANGE_CYCLE) {// Due today
  708. $login_tmp = array(
  709. 'msg' => 'today',
  710. 'login_version' => $rs["login_version"],
  711. 'data' => ''
  712. );
  713. } elseif ($diffdate >= ($PASSWORD_CHANGE_CYCLE - $PASSWORD_CHANGE_ALERT)) {// Password expires soon, JS Tips
  714. $login_tmp = array(
  715. 'msg' => 'last',
  716. 'login_version' => $rs["login_version"],
  717. 'data' => $PASSWORD_CHANGE_CYCLE - $diffdate,
  718. 'is_only_vgm' => $rs["is_only_vgm"]
  719. );
  720. }
  721. //insert into log table
  722. $ip = common::ip();
  723. $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() . "')";
  724. if (common::excuteUpdateSql($sql)) {
  725. 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) . "'");
  726. //自动登录。为了方便调用,先注销掉
  727. if (isset($login_tmp)) {
  728. //common::echo_json_encode(500, $login_tmp);
  729. } else {
  730. $data = array(
  731. 'msg' => 'success',
  732. 'login_version' => $rs["login_version"],
  733. 'data' => '',
  734. 'is_only_vgm' => $rs["is_only_vgm"]
  735. );
  736. //common::echo_json_encode("200", $data);
  737. }
  738. $online_user = $rs;
  739. $online_user['user_login'] = $uname;
  740. $online_user['company'] = $company;
  741. $online_user['password'] = "";
  742. if (!_isAdmin()) {
  743. if ($rs["is_only_vgm"] == "t") {//VGM用户写死
  744. $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), ',')";
  745. $rrrs = common::excuteOneSql($sql);
  746. } else {
  747. $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)) . "'), ',')";
  748. $rrrs = common::excuteOneSql($sql);
  749. if (strtolower($rs['user_type']) == "employee" && empty($rrrs)) {
  750. $sql = "select array_to_string(ARRAY(select url_action from public.ra_online_permission where is_customer = true order by order_by asc), ',')";
  751. $rrrs = common::excuteOneSql($sql);
  752. }
  753. }
  754. $online_user['permission'] = $rrrs;
  755. }
  756. if (!empty($online_user['docdownload']))
  757. $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)";
  758. else {
  759. $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";
  760. if (strtolower($online_user['user_type']) == "customer")
  761. $sql .= " and client_display = true";
  762. $sql .= " group by display_name order by min(id)";
  763. }
  764. $online_user['view_doc_type'] = common::excuteListSql($sql);
  765. if (!empty($online_user['view_air_file_format']))
  766. $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)";
  767. else {
  768. $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";
  769. if (strtolower($online_user['user_type']) == "customer")
  770. $sql .= " and client_display = true";
  771. $sql .= " group by display_name order by min(id)";
  772. }
  773. $online_user['view_air_doc_type'] = common::excuteListSql($sql);
  774. //补充aci and ams CustomerLogin station - public
  775. $ocean_station_temp = $online_user['ocean_station'];
  776. $online_user['session_ocean_station'] = $this->getOriginOrAgent($ocean_station_temp);
  777. $ocean_agent_temp = $online_user['ocean_agent'];
  778. $online_user['session_ocean_agent'] = $this->getOriginOrAgent($ocean_agent_temp);
  779. $_SESSION['ONLINE_USER'] = $online_user;
  780. $_SESSION['LAST_OPERATE_TIME'] = time();
  781. $_SESSION['SESSION_TIMEOUT'] = $SESSION_TIMEOUT;
  782. //判断是否记录密码 add
  783. if ($_POST['rememberpwd'] === 'true') {
  784. if (!$noCheckPwd) {
  785. $user_info = $uname . "_" . md5($rs['password']);
  786. setcookie('userInfo', $user_info, time() + 30 * 24 * 3600, "/");
  787. }
  788. } else {
  789. setcookie('userInfo', '', time() - 1, "/");
  790. }
  791. if ($rs['is_super'] == "t") {
  792. $schemas_list = common::excuteListSql("select * from schemas_list");
  793. } else {
  794. $schemas_list = common::excuteListSql("select * from schemas_list where schemas_name=any(regexp_split_to_array('" . $rs['belong_schemas'] . "'::text, ';'::text))");
  795. }
  796. ///if (count($schemas_list) > 1) {
  797. foreach ($schemas_list as $sk => $sv) {
  798. if ($sv['schemas_name'] == "public") {
  799. continue;
  800. }
  801. $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,
  802. 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,
  803. 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,
  804. 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) . "'");
  805. if (empty($ttdd)) {
  806. unset($schemas_list[$sk]);
  807. continue;
  808. }
  809. if (!empty($ttdd['docdownload'])) {
  810. $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)";
  811. } else {
  812. $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";
  813. if (strtolower($ttdd['user_type']) == "customer")
  814. $sql .= " and client_display = true";
  815. $sql .= " group by display_name order by min(id)";
  816. }
  817. $ttdd['view_doc_type'] = common::excuteListSql($sql);
  818. if (!empty($ttdd['view_air_file_format'])) {
  819. $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)";
  820. } else {
  821. $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";
  822. if (strtolower($ttdd['user_type']) == "customer")
  823. $sql .= " and client_display = true";
  824. $sql .= " group by display_name order by min(id)";
  825. }
  826. $ttdd['view_air_doc_type'] = common::excuteListSql($sql);
  827. //补充aci and ams CustomerLogin station - other like sfs
  828. $ocean_station_temp = $ttdd['ocean_station'];
  829. $ttdd['session_ocean_station'] = $this->getOriginOrAgent($ocean_station_temp);
  830. $ocean_agent_temp = $ttdd['ocean_agent'];
  831. $ttdd['session_ocean_agent'] = $this->getOriginOrAgent($ocean_agent_temp);
  832. $_SESSION[$sv['schemas_name'] . '_ONLINE_USER'] = $ttdd;
  833. }
  834. //}
  835. $_SESSION['schemas_list'] = $schemas_list;
  836. //不再返回登录页面,直接跳转
  837. if (isset($_GET['up'])) {
  838. if($_GET['v'] == 'new'){
  839. header("Location: main_new_version.php?action=main");
  840. }else{
  841. header("Location: main.php?action=main");
  842. }
  843. }
  844. //自动登录。为了方便调用,先注销掉
  845. //exit();
  846. } else {
  847. $data = array(
  848. 'msg' => 'database_error',
  849. 'login_version' => $rs["login_version"],
  850. 'data' => ''
  851. );
  852. common::echo_json_encode(500, $data);
  853. exit();
  854. }
  855. } else {
  856. $data = array(
  857. 'msg' => 'no_exist',
  858. 'login_version' => $rs["login_version"],
  859. 'data' => ''
  860. );
  861. common::echo_json_encode(500, $data);
  862. exit();
  863. }
  864. }
  865. }
  866. private function failedLogin($uname, $company){
  867. $ip = common::ip();
  868. 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() . "')");
  869. }
  870. //重置密码
  871. public function passwordExpires($loginName,$email,$uname){
  872. $result = array(
  873. 'msg' => "passwordExpires",
  874. 'uname' => $uname,
  875. );
  876. common::echo_json_encode(400, $result);
  877. exit();
  878. }
  879. //submit change form -- change expires password
  880. public function update_pwd_expires(){
  881. $loginName = common::check_input($_POST['uname']);
  882. $old_password = common::check_input($_POST['old_password']);
  883. $password = common::check_input($_POST['password']);
  884. //首先校验验证码 展示注销掉
  885. // $verifcation_code =
  886. // $verifcation_code = common::check_input($_POST['verifcation_code']);
  887. // if (strtolower($_SESSION['captcha']) != strtolower($verifcation_code)) {
  888. // $data = array(
  889. // 'msg' => 'verifcation_error',
  890. // 'data' => ''
  891. // );
  892. // common::echo_json_encode(400, $data);
  893. // exit();
  894. // }
  895. $sql = "select ra_password as password from ra_online_user where lower(user_login) = '" . strtolower($loginName) . "'";
  896. $rs = common::excuteObjectSql($sql);
  897. $str = '';
  898. if (!empty($rs)) {
  899. if ($rs['password'] != $old_password) {
  900. $str = "Old password is incorrect!";
  901. }
  902. } else {
  903. $str = "Old password is incorrect!";
  904. }
  905. if(!empty($str)){
  906. $data = array(
  907. 'msg' => $str,
  908. 'data' => ''
  909. );
  910. common::echo_json_encode(400, $data);
  911. exit();
  912. }
  913. //验证通过,进行修改密码
  914. $msg = $this->updateExpirePassword($loginName, $password);
  915. if($msg == "success"){
  916. $data = array(
  917. 'msg' => "success",
  918. 'data' => ''
  919. );
  920. common::echo_json_encode(200, $data);
  921. exit();
  922. } else {
  923. $data = array(
  924. 'msg' => $msg,
  925. 'data' => ''
  926. );
  927. common::echo_json_encode(400, $data);
  928. exit();
  929. }
  930. }
  931. //更新密码
  932. public function updateExpirePassword($login,$new_password) {
  933. $str = common::checkPasswordRule($login, $new_password);
  934. //更新密码,擦除expire pwd痕迹
  935. if (empty($str)) {
  936. $sql = "UPDATE public.ra_online_user SET ra_password = '" . common::check_input($new_password) . "',password_new=redant_encode('".$new_password."'),
  937. last_pwd_change = now(),password_expires_keycode = null,password_expires_time = null
  938. WHERE lower(user_login) = '" . common::check_input(strtolower($login)) . "';";
  939. $rls = common::excuteUpdateSql($sql);
  940. if (!$rls) {
  941. $str = "Database Error, Try Later.";
  942. } else {
  943. $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());";
  944. common::excuteUpdateSql($sql);
  945. $str = "success";
  946. }
  947. }
  948. return $str;
  949. }
  950. public function getOriginOrAgent($ocean_station_temp){
  951. $session_ocean_station = "";
  952. if (strtolower($ocean_station_temp) == 'all'){
  953. $session_ocean_station = $ocean_station_temp;
  954. }
  955. if (!(strtolower($ocean_station_temp) == 'all' || empty($ocean_station_temp))){
  956. if (utils::checkExist($ocean_station_temp, ";")) {
  957. $ost = str_replace(";","','",strtolower($ocean_station_temp));
  958. $sql="select kerry_station_id,contact_id from ocean.contacts where coalesce(kerry_station_id,'') <>''and lower(contact_id) in ('".$ost."')";
  959. $tar = common::excuteListSql($sql);
  960. foreach ($tar as $tk => $tv) {
  961. $tar[$tv['contact_id']] = $tv['kerry_station_id'];
  962. }
  963. $_tt = explode(";", $ocean_station_temp);
  964. foreach ($_tt as $vv) {
  965. if (!empty($vv)){
  966. $session_ocean_station .= trim($vv).';';
  967. if (!empty($tar[$vv])) {
  968. $session_ocean_station .= $tar[trim($vv)].';';
  969. }
  970. }
  971. }
  972. } else {
  973. $session_ocean_station .= trim($ocean_station_temp).';';
  974. $temp_contacts = common::excuteObjectSql("select kerry_station_id from ocean.contacts where lower(contact_id)='".strtolower(common::check_input($ocean_station_temp))."'");
  975. if (!empty($temp_contacts['kerry_station_id'])){
  976. $session_ocean_station .= $temp_contacts['kerry_station_id'].';';
  977. }
  978. }
  979. }
  980. return $session_ocean_station;
  981. }
  982. public function logout() {
  983. unset($_SESSION['ONLINE_USER']);
  984. unset($_SESSION['LAST_OPERATE_TIME']);
  985. unset($_SESSION['SESSION_TIMEOUT']);
  986. common::sessionDestroy();
  987. $data = array("msg" =>"logout Successful");
  988. common::echo_json_encode(200, $data);
  989. exit();
  990. }
  991. public function tracking_checked(){
  992. $reference_number = common::check_input($_POST['reference_number']);
  993. $is_verify = common::check_input($_POST['verifcation_code']);
  994. if($this->signUpAndTrackingChecked($is_verify)){
  995. $data = array("msg" =>"visit limit");
  996. common::echo_json_encode(200, $data);
  997. exit();
  998. }else{
  999. $data = $this->getTrackingInfo($reference_number);
  1000. common::echo_json_encode(200, $data);
  1001. exit();
  1002. }
  1003. }
  1004. private function signUpAndTrackingChecked($is_verify){
  1005. $Tracking_Search_Count = common::excuteOneSql("select ra_value from ra_online_config where ra_name='Tracking_Search_Count'");
  1006. $ip = common::ip();
  1007. global $db;
  1008. $db->StartTrans();
  1009. $db->Execute("update tracking_login_record set visit_count = 1, visit_time=now() "
  1010. . " 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));
  1011. $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");
  1012. if(empty($ipInfo)){
  1013. $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));
  1014. }else{
  1015. if ($ipInfo['visit_count'] > $Tracking_Search_Count){
  1016. if(strtolower($_SESSION['captcha']) == strtolower($is_verify) || true ){
  1017. //归零验证次数
  1018. $db->Execute("update tracking_login_record set visit_count = 1 "
  1019. . " 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));
  1020. }else{
  1021. return TRUE;
  1022. }
  1023. }else{
  1024. $db->Execute("update tracking_login_record set visit_count = visit_count::integer + 1 "
  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. }
  1027. }
  1028. if ($db->CompleteTrans() === FALSE) {
  1029. //出错拦截
  1030. return TRUE;
  1031. } else {
  1032. return FALSE;
  1033. }
  1034. }
  1035. private function getTrackingInfo($reference_number){
  1036. $sql = "SELECT o.* ,sh.*, cn.* ,aa.*,dd.* ,cc.status as new_status
  1037. from public.ocean o
  1038. left join LATERAL (select case when a.code='IFFBCF' then 'Booked'
  1039. when a.code='IFFCPU' then 'Cargo Received'
  1040. when a.code='IFFREC' then 'Cargo Received'
  1041. when a.code='IFFDEP' then 'Departed'
  1042. when a.code='IFFARR' then 'Arrived'
  1043. when a.code='IFFAFD' then 'Completed'
  1044. else 'Booked' END as status,
  1045. from ocean_milestone a
  1046. where a.serial_no=o.serial_no
  1047. and a.code in (select regexp_split_to_table('IFFBCF,IFFCPU,IFFREC,IFFDEP,IFFARR,IFFAFD', ','))
  1048. order by id desc limit 1) cc on true
  1049. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1050. address_1 as cn_address_1,
  1051. address_2 as cn_address_2,
  1052. address_3 as cn_address_3,
  1053. address_4 as cn_address_4,
  1054. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1055. phone_1 as cn_phone
  1056. FROM ocean.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1057. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1058. address_1 as sh_address_1,
  1059. address_2 as sh_address_2,
  1060. address_3 as sh_address_3,
  1061. address_4 as sh_address_4,
  1062. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1063. phone_1 as sh_phone
  1064. FROM ocean.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1065. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1066. address_1 as aa_address_1,
  1067. address_2 as aa_address_2,
  1068. address_3 as aa_address_3,
  1069. address_4 as aa_address_4,
  1070. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1071. phone_1 as aa_phone
  1072. FROM ocean.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1073. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1074. address_1 as dd_address_1,
  1075. address_2 as dd_address_2,
  1076. address_3 as dd_address_3,
  1077. address_4 as dd_address_4,
  1078. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1079. phone_1 as dd_phone
  1080. FROM ocean.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1081. where (lower(tracking_no) = '" . strtolower($reference_number) . "'
  1082. or lower(booking_no) = '" . strtolower($reference_number) . "'
  1083. or lower(h_bol) = '" . strtolower($reference_number) . "'
  1084. or lower(m_bol) = '" . strtolower($reference_number) . "'
  1085. or lower(po_no) = '" . strtolower($reference_number) . "'
  1086. or lower(quote_no) = '" . strtolower($reference_number) . "'
  1087. or lower(invoice_no) = '" . strtolower($reference_number) . "'
  1088. or exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number'))";
  1089. $ocean_arr = common::excuteListSql($sql);
  1090. if(empty($ocean_arr)){
  1091. $ocean_arr = $this->trackingSfsSql($reference_number);
  1092. if(empty($ocean_arr)){
  1093. $data = array("msg" =>"No matches");
  1094. return $data;
  1095. }
  1096. }
  1097. if(!empty($ocean_arr) && count($ocean_arr) > 1){
  1098. $data = array("msg" =>"Multiple results");
  1099. return $data;
  1100. }
  1101. $ocean = $ocean_arr[0];
  1102. //处理transportInfo信息数据
  1103. $transportInfo = array("Tracking No." =>$ocean['tracking_no'],"status"=>$ocean['new_status'],"mode" => "Ocean Freight",
  1104. "origin" =>$ocean['origin_station'],"destination" =>$ocean['destination_station'],
  1105. "etd" =>$ocean['f_etd'],"atd" =>"","eta" =>$ocean['m_eta'],"ata" =>"");
  1106. //处理basicInfo信息数据
  1107. $vessel = utils::outDisplayForMerge($ocean['f_vessel'],$ocean['m_vessel']);
  1108. $voyage = utils::outDisplayForMerge($ocean['f_voyage'],$ocean['m_voyage']);
  1109. $basicInfo = array("MAWB/MBL No." =>$ocean['m_bol'],"HAWB/HBOL" => $ocean['h_bol'],"Carrier_Booking_No" =>$ocean['carrier_booking'],
  1110. "PO_NO" =>$ocean['po_no'],"Vessel/Airline" =>$vessel,"Voyage/Filght" =>$voyage,
  1111. "Incoterm" =>$ocean['terms'],"Service_Type" =>$ocean['service']);
  1112. //处理 拼接地址 ocean表单exp 字段无法精准分割电话和地址信息,只能从contacts表里查询
  1113. $shipper_address = common::retStationInfo($ocean['sh_address_1'], $ocean['sh_address_2'], $ocean['sh_address_3'], $ocean['sh_address_4'],
  1114. $ocean['sh_city'], $ocean['sh_state'], $ocean['sh_zipcode'], $ocean['sh_country']);
  1115. $consignee_address = common::retStationInfo($ocean['cn_address_1'], $ocean['cn_address_2'], $ocean['cn_address_3'], $ocean['cn_address_4'],
  1116. $ocean['cn_city'], $ocean['cn_state'], $ocean['cn_zipcode'], $ocean['cn_country']);
  1117. $origin_address = common::retStationInfo($ocean['aa_address_1'], $ocean['aa_address_2'], $ocean['aa_address_3'], $ocean['aa_address_4'],
  1118. $ocean['aa_city'], $ocean['aa_state'], $ocean['aa_zipcode'], $ocean['aa_country']);
  1119. $destination_address = common::retStationInfo($ocean['dd_address_1'], $ocean['dd_address_2'], $ocean['dd_address_3'], $ocean['dd_address_4'],
  1120. $ocean['dd_city'], $ocean['dd_state'], $ocean['dd_zipcode'], $ocean['dd_country']);
  1121. $shipperPartners = array("company" =>$ocean['sh_company'],"address"=>$shipper_address,"phone"=>$ocean['sh_phone']);
  1122. $consigneePartners = array("company" =>$ocean['cn_company'],"address"=>$consignee_address,"phone"=>$ocean['cn_phone']);
  1123. $originPartners = array("company" =>$ocean['aa_company'],"address"=>$origin_address,"phone"=>$ocean['aa_phone']);
  1124. $destinationPartners = array("company" =>$ocean['dd_company'],"address"=>$destination_address,"phone"=>$ocean['dd_phone']);
  1125. $businessPartners = array("shipper"=>$shipperPartners,"consignee" => $consigneePartners,"origin" => $originPartners,"destination" => $destinationPartners);
  1126. //处理marksAndDescription
  1127. $marksAndDescription = array("marks"=>$ocean['marks'],"description"=>$ocean['description']);
  1128. $sql = "SELECT " . column::getInstance()->getSearchSql('Ocean_Container') . " from oc_container where lower(serial_no) = '" . strtolower($ocean['serial_no']) . "'";
  1129. $rss = common::excuteListSql($sql);
  1130. $quantity_tolal = 0;
  1131. $packages = "CTN";
  1132. $g_weight_tolal = 0;
  1133. $ch_weight_tolal = 0;
  1134. $cbm_tolal = 0;
  1135. foreach ($rss as $key => $rs) {
  1136. $quantity_tolal += $rs['qty'];
  1137. $g_weight_tolal += $rs['grs_kgs'];
  1138. $ch_weight_tolal += $rs['grs_lbs'];
  1139. $cbm_tolal += $rs['cbm'];
  1140. $packages = " ".$rs['unit'];
  1141. }
  1142. //Packing 不确定信息
  1143. $packing = array("Quantity/Unit"=>$quantity_tolal.$packages,"G. Weight" => $g_weight_tolal." KGS","Ch. Weight" => $ch_weight_tolal." LBS","Volume" => $cbm_tolal." CBM");
  1144. //Milestones 数据信息待定
  1145. $Milestones = common::getMilestonesInfo($ocean);
  1146. global $_COPYRIGHT;
  1147. $data = array('transportInfo' => $transportInfo,
  1148. 'basicInfo' => $basicInfo,
  1149. 'businessPartners' => $businessPartners,
  1150. 'packing' => $packing,
  1151. 'marksAndDescription' => $marksAndDescription,
  1152. 'Milestones' => $Milestones,
  1153. 'copyright' =>$_COPYRIGHT);
  1154. return array("msg" =>"success","data" =>$data);
  1155. }
  1156. private function trackingSfsSql($reference_number){
  1157. $sql = "SELECT o.* ,sh.*, cn.* ,aa.*,dd.* ,cc.status as new_status
  1158. from sfs.ocean o
  1159. left join LATERAL (select case when a.code='IFFBCF' then 'Booked'
  1160. when a.code='IFFCPU' then 'Cargo Received'
  1161. when a.code='IFFREC' then 'Cargo Received'
  1162. when a.code='IFFDEP' then 'Departed'
  1163. when a.code='IFFARR' then 'Arrived'
  1164. when a.code='IFFAFD' then 'Completed'
  1165. else 'Booked' END as status,
  1166. from ocean_milestone a
  1167. where a.serial_no=o.serial_no
  1168. and a.code in (select regexp_split_to_table('IFFBCF,IFFCPU,IFFREC,IFFDEP,IFFARR,IFFAFD', ','))
  1169. order by id desc limit 1) cc on true
  1170. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1171. address_1 as cn_address_1,
  1172. address_2 as cn_address_2,
  1173. address_3 as cn_address_3,
  1174. address_4 as cn_address_4,
  1175. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1176. phone_1 as cn_phone
  1177. FROM sfs.contacts c WHERE o.consignee::text = c.contact_id::text) cn ON true
  1178. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1179. address_1 as sh_address_1,
  1180. address_2 as sh_address_2,
  1181. address_3 as sh_address_3,
  1182. address_4 as sh_address_4,
  1183. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1184. phone_1 as sh_phone
  1185. FROM sfs.contacts c WHERE o.shipper::text = c.contact_id::text) sh ON true
  1186. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1187. address_1 as aa_address_1,
  1188. address_2 as aa_address_2,
  1189. address_3 as aa_address_3,
  1190. address_4 as aa_address_4,
  1191. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1192. phone_1 as aa_phone
  1193. FROM sfs.contacts c WHERE o.origin_station::text = c.contact_id::text) aa ON true
  1194. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1195. address_1 as dd_address_1,
  1196. address_2 as dd_address_2,
  1197. address_3 as dd_address_3,
  1198. address_4 as dd_address_4,
  1199. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1200. phone_1 as dd_phone
  1201. FROM sfs.contacts c WHERE o.destination_station::text = c.contact_id::text) dd ON true
  1202. where (lower(tracking_no) = '" . strtolower($reference_number) . "'
  1203. or lower(booking_no) = '" . strtolower($reference_number) . "'
  1204. or lower(h_bol) = '" . strtolower($reference_number) . "'
  1205. or lower(m_bol) = '" . strtolower($reference_number) . "'
  1206. or lower(po_no) = '" . strtolower($reference_number) . "'
  1207. or lower(quote_no) = '" . strtolower($reference_number) . "'
  1208. or lower(invoice_no) = '" . strtolower($reference_number) . "'
  1209. or exists (select 1 from oc_container oc where o.serial_no = oc.serial_no and oc.ctnr = '$reference_number'))";
  1210. return $sql;
  1211. }
  1212. }
  1213. ?>