login.class.php 67 KB

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