utils.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of utilsclass
  7. *
  8. * @author Administrator
  9. */
  10. class utils {
  11. public static function checkPassword($password,$rule="",$user_login="") {
  12. $str ="";
  13. if (!empty($rule)) {
  14. //是否校验大小写
  15. if (!empty($rule["hasOneUpperChar"])&&$rule["hasOneUpperChar"]) {
  16. if (!preg_match('/[A-Z]/',$password)) {
  17. $str ="Password must contain uppercase letters";
  18. }
  19. }
  20. //是否校验小写
  21. if (!empty($rule["hasOneLowerChar"])&&$rule["hasOneLowerChar"]) {
  22. if (!preg_match('/[a-z]/',$password)) {
  23. $str ="Password must contain lowercase letters";
  24. }
  25. }
  26. //是否存在数字
  27. if (!empty($rule["hasOneNumberChar"])&&$rule["hasOneNumberChar"]) {
  28. if (!preg_match('/[0-9]/',$password)) {
  29. $str ="Password must contain numbers";
  30. }
  31. }
  32. $sql = "select user_type from ra_online_user_roles_rel where upper(user_login)=upper('".$user_login."') and exists(select count(0) from ra_online_user where upper(user_login)=upper('".$user_login."') and is_desktop=true) order by id desc limit 1;";
  33. $user_type = common::excuteOneSql($sql);
  34. if (!empty($user_type)&&$user_type=="Super User") {
  35. if (strlen($password)<$rule["SuperMinLen"]||strlen($password)>$rule["SuperMaxLen"]) {
  36. $str ="Super user password length between ".$rule["SuperMinLen"]." and ".$rule["SuperMaxLen"];
  37. }
  38. }else{
  39. //校验密码长度
  40. if (strlen($password)<$rule["MinLen"]||strlen($password)>$rule["MaxLen"]) {
  41. $str ="Password length between ".$rule["MinLen"]." and ".$rule["MaxLen"];
  42. }
  43. }
  44. return $str;
  45. }else{
  46. if (preg_match('/^\d*$/', $password) || preg_match('/^[a-zA-Z]+$/', $password)) {
  47. $str ="Must include letters and numbers";
  48. }
  49. $len = strlen($password);
  50. $t = substr($password, 0, 1);
  51. for ($i = 1; $i < $len; $i++) {
  52. $t1 = substr($password, $i, 1);
  53. if ($t != $t1) {
  54. return "";
  55. }
  56. }
  57. return "error";
  58. }
  59. }
  60. //隐藏邮箱地址
  61. public static function maskEmail($email) {
  62. $idex = strlen($email) - strrpos($email, ".");
  63. $mask = substr($email, 0, 1) . str_repeat('*', 6) . "@" . str_repeat('*', 3) . substr($email, -$idex);
  64. return $mask;
  65. }
  66. public static function getInSql($str, $not = false, $sep = ";") {
  67. $str = trim($str);
  68. $str = trim($str, $sep);
  69. $str = trim($str);
  70. if (empty($str) && $str !== "0" && $str !== 0)
  71. return "1<>1";
  72. $str = strtolower($str);
  73. if (utils::checkExist($str, $sep)) {
  74. $aa = explode($sep, $str);
  75. $msg = "";
  76. foreach ($aa as $value) {
  77. $value = trim($value);
  78. if (empty($value))
  79. continue;
  80. if (empty($msg))
  81. $msg = "'" . common::check_input($value) . "'";
  82. else
  83. $msg .= ",'" . common::check_input($value) . "'";
  84. }
  85. if ($not !== FALSE)
  86. return " not in (" . $msg . ")";
  87. else
  88. return " in (" . $msg . ")";
  89. } else {
  90. if ($not !== FALSE)
  91. return " != '" . common::check_input(trim($str)) . "'";
  92. else
  93. return " = '" . common::check_input(trim($str)) . "'";
  94. }
  95. }
  96. public static function checkExist($string, $search, $u = TRUE) {
  97. if ($u === TRUE) {
  98. if (stripos($string, $search) !== false)
  99. return TRUE;
  100. }else {
  101. if (strpos($string, $search) !== false)
  102. return TRUE;
  103. }
  104. return FALSE;
  105. }
  106. public static function endWith($string, $end, $u = TRUE) {
  107. if ($u === TRUE) {
  108. $string = strtolower($string);
  109. $end = strtolower($end);
  110. return strrchr($string, $end) == $end;
  111. }
  112. return strrchr($string, $end) == $end;
  113. }
  114. public static function _get($str) {
  115. $rs = isset($_POST[$str]) ? $_POST[$str] : null;
  116. if (empty($rs))
  117. $rs = isset($_GET[$str]) ? $_GET[$str] : null;
  118. return $rs;
  119. }
  120. public static function startWith($string, $start, $u = TRUE) {
  121. if ($u === TRUE)
  122. return stripos($string, $start) === 0;
  123. return strpos($string, $start) === 0;
  124. }
  125. public static function outDisplay($content, $is_time = 'f', $is_first = 'f', $is_boolean = 'f', $excel_export = FALSE) {
  126. if (empty($content) && $content !== 0 && $content !== "0")
  127. return "";
  128. if (strtolower($is_time) == 't')
  129. return utils::dealTimeDisplay($content);
  130. if (strtolower($is_first) == 't') {
  131. if ($excel_export !== FALSE)
  132. return utils::getCompanyName($content);
  133. else
  134. return '<span title="' . $content . '">' . utils::getCompanyName($content) . '</span>';
  135. }
  136. if (strtolower($is_boolean) == 't')
  137. return utils::outTrue($content);
  138. return nl2br($content);
  139. }
  140. public static function _output($value) {
  141. if (empty($value))
  142. return "&nbsp;";
  143. else
  144. return $value;
  145. }
  146. public static function dealTimeDisplay($date) {
  147. if (empty($date))
  148. return "";
  149. if (strlen($date) > 10)
  150. return date("m/d/Y H:i:s", strtotime($date));
  151. return date("m/d/Y", strtotime($date));
  152. }
  153. public static function outDisplayForMerge($frist,$last,$split = "/") {
  154. if (!empty($frist)){
  155. if(!empty($last)){
  156. return $frist.$split.$last;
  157. }else{
  158. return $frist;
  159. }
  160. }else{
  161. return $last;
  162. }
  163. }
  164. public static function outTrue($r) {
  165. if (empty($r))
  166. return "No";
  167. $r = strtolower($r);
  168. if ($r == "t")
  169. return "Yes";
  170. elseif ($r == "f")
  171. return "No";
  172. else
  173. return $r;
  174. }
  175. public static function getCompanyName($detail) {
  176. $detail = nl2br($detail);
  177. if (strpos($detail, '<br />') === FALSE)
  178. return $detail;
  179. return substr($detail, 0, strpos($detail, '<br />'));
  180. }
  181. public static function getEmail($serial_no) {
  182. $ocean = common::excuteObjectSql("select sales_rep, last_user, created_by, order_from, h_bol, consignee, dest_op, agent from public.online_ocean where md5(serial_no)=md5('$serial_no') "
  183. . "order by schem_not_display nulls last limit 1");
  184. $schema = $ocean["order_from"] . ".";
  185. $dest_op_from_agent = common::excuteOneSql("select dest_op_from_agent from " . $schema . "ocean where md5(serial_no)=md5('$serial_no')");
  186. if ($ocean["agent"] == "KYMTL" || $ocean["agent"] == "KYYYZ") {
  187. $email = array();
  188. $email["email"] = "";
  189. if (!empty($dest_op_from_agent)) {
  190. $so_email = common::excuteOneSql("select email from " . $schema . "employee where employee_id='" . $dest_op_from_agent . "' and active=true");
  191. }
  192. if (!empty($so_email)) {
  193. if (empty($email["email"])) {
  194. $email["email"] = $so_email;
  195. } else {
  196. $email["email"] .= ";" . $so_email;
  197. }
  198. }
  199. if (!empty($ocean["sales_rep"])) {
  200. $rep_email = common::excuteOneSql("select email from " . $schema . "employee where lower(salesopcode)='" . strtolower($ocean["sales_rep"]) . "' and active=true");
  201. if (!empty($rep_email)) {
  202. if (empty($email["email"])) {
  203. $email["email"] = $rep_email;
  204. } else {
  205. $email["email"] .= ";" . $rep_email;
  206. }
  207. }
  208. }
  209. } else {
  210. $email = common::excuteObjectSql("select string_agg(e.email, ';') as email, string_agg(e.first_name, ';') as name from " . $schema . "ra_online_user u, " . $schema . "employee e WHERE u.employee_id = e.employee_id and "
  211. . "lower(u.user_login) in ('" . strtolower($ocean["created_by"]) . "', '" . strtolower($ocean["last_user"]) . "')");
  212. if (empty($dest_op_from_agent)) {
  213. if (!empty($ocean["dest_op"])) {
  214. $so_email = common::excuteOneSql("select email from " . $schema . "employee where employee_id='" . $ocean["dest_op"] . "' and active=true");
  215. }
  216. } else {
  217. $so_email = common::excuteOneSql("select email from " . $schema . "employee where employee_id='" . $dest_op_from_agent . "' and active=true");
  218. }
  219. if (empty($so_email)) {
  220. if ($ocean["agent"] == "APEXSFO") {
  221. $so_email = "oid2@apexshipping.com";
  222. }
  223. if ($ocean["agent"] == "APEXLAX") {
  224. $so_email = "laxoid@apexshipping.com";
  225. }
  226. if ($ocean["agent"] == "APEXNYC") {
  227. $so_email = "NYCOID@APEXSHIPPING.COM";
  228. }
  229. if ($ocean["agent"] == "APEXPNW") {
  230. $so_email = "pnwoid@apexshipping.com";
  231. }
  232. if ($ocean["agent"] == "STLUTA") {
  233. $so_email = "starlinkOID@apexshipping.com ";
  234. }
  235. if ($ocean["agent"] == "APEXORD") {
  236. $so_email = "ordoid@apexshipping.com";
  237. }
  238. }
  239. if (!empty($so_email)) {
  240. if (empty($email["email"])) {
  241. $email["email"] = $so_email;
  242. } else {
  243. $email["email"] .= ";" . $so_email;
  244. }
  245. }
  246. if (!empty($ocean["sales_rep"])) {
  247. $rep_email = common::excuteOneSql("select email from " . $schema . "employee where lower(salesopcode)='" . strtolower($ocean["sales_rep"]) . "' and active=true");
  248. if (!empty($rep_email)) {
  249. if (empty($email["email"])) {
  250. $email["email"] = $rep_email;
  251. } else {
  252. $email["email"] .= ";" . $rep_email;
  253. }
  254. }
  255. }
  256. }
  257. $email["h_bol"] = $ocean["h_bol"];
  258. $email["consignee"] = $ocean["consignee"];
  259. return $email;
  260. }
  261. /***
  262. * 过滤json中的某个数据
  263. * @param unknown $json
  264. * @param unknown $search
  265. * @param unknown $replace
  266. * @return mixed
  267. */
  268. public static function jsonFiltration($search,$replace,$json){
  269. //处理json中将斜杠转义问题
  270. $json = str_replace("\\/", "/", $json);
  271. return str_replace($search, $replace, $json);
  272. }
  273. /*
  274. * calculate eta destination by etd port
  275. */
  276. public static function calculate_ETA_Des($serial_no) {
  277. $sql = "SELECT m_eta as eat, mport_of_discharge as poul, place_of_delivery as pod,service from ocean where lower(serial_no) = '" . strtolower($serial_no) . "'";
  278. $rs = common::excuteObjectSql($sql);
  279. $date = "";
  280. if (!empty($rs['eat'])) {
  281. $date = utils::calculate_ETA_Dest($rs['eat'], $rs['poul'], $rs['pod'], $rs['service']);
  282. }
  283. return $date;
  284. }
  285. public static function calculate_ETA_Dest($eta, $poul, $pod, $service) {
  286. if (empty($poul) || empty($pod))
  287. return $eta;
  288. $sql = "SELECT door_days, cy_days
  289. FROM eta_dest
  290. WHERE eta_dest.state::text = ((( SELECT unlocode.state
  291. FROM ports, unlocode
  292. WHERE ports.uncode::text = unlocode.uncode::text AND ports.code::text = '" . common::check_input($pod) . "'
  293. LIMIT 1))::text) AND (','::text || eta_dest.pod::text) ~~* (('%,'::text || '" . common::check_input($poul) . "') || '%'::text)
  294. LIMIT 1";
  295. //$sql = "select door_days, cy_days from eta_dest where state = (select state from ports where code = '" . common::check_input($poul) . "' limit 1) and ','||pod ilike '%," . common::check_input($pod) . "%'";
  296. $rs = common::excuteObjectSql($sql);
  297. if (empty($rs))
  298. return $eta;
  299. if (utils::endWith($service, "cy"))
  300. return common::addDays($eta, $rs['cy_days']);
  301. else
  302. return common::addDays($eta, $rs['door_days']);
  303. }
  304. /*
  305. * password change, email alert
  306. */
  307. public static function sendEmailByPassword($username, $password, $email, $companyname='') {
  308. $sql = "select subject, ra_content as content from ra_online_email_tpl where lower(ra_type) = 'forgotpw'";
  309. $rs = common::excuteObjectSql($sql);
  310. if (!empty($rs)) {
  311. $subject = $rs['subject'];
  312. $content = $rs['content'];
  313. }
  314. if (!empty($subject) && !empty($content)) {
  315. $content = str_replace('<{username}>', $username, $content);
  316. $content = str_replace('<{password}>', $password, $content);
  317. $content = str_replace('<{companyname}>', $companyname, $content);
  318. global $db;
  319. common::excuteUpdateSql("INSERT INTO public.email_record_forgotpassword(type, title, from_email, to_email, content, insert_date,
  320. cc_email) VALUES ('forgot_password', '" . common::check_input($subject) . "', 'US.KApex.Online@kerryapex.com', '" .
  321. common::check_input($email) . "', '" . common::check_input($content) . "', now(), '');");
  322. return "success";
  323. //return Mail::sendMail($email, $subject, $content);
  324. } else
  325. return null;
  326. }
  327. public static function operation_log_records(){
  328. //排除opreation_log操作
  329. if($_REQUEST["action"] == "opreation_log"
  330. || empty($_REQUEST["operate"])
  331. || ($_REQUEST["action"] == "login" && $_REQUEST["operate"] == "verifcation_code")){
  332. return;
  333. }
  334. $user_type = _isCustomerLogin() ? "Customer" : "Employee";
  335. $user_name = _getLoginName();
  336. $operateInfo = utils::getPageByAction($_REQUEST["action"],$_REQUEST["operate"]);
  337. $page = $operateInfo["page"];
  338. $operation = $operateInfo["operate"];
  339. $operation_detail = utils::jsonFiltration("null", "\"\"", json_encode($_REQUEST));
  340. $sql = "INSERT INTO public.customer_service_operation_log(user_type, user_name, page, operation, operation_detail,
  341. operation_time)
  342. VALUES ('$user_type', '$user_name', '$page', '$operation', '$operation_detail', now())";
  343. common::excuteUpdateSql($sql);
  344. }
  345. public static function getPageByAction($action,$operate){
  346. //取消
  347. $operationConvertName = array(
  348. "login=do_login" => array("page" =>"Login","operate"=>"Login"),
  349. "login=logout" => array("page" =>"logout","operate"=>"logout"),
  350. "login=update_pwd_expires" => array("page" =>"Login","operate"=>"Reset password"),
  351. "ocean_booking=search" => array("page" =>"Booking","operate"=>"Search"),
  352. "ocean_booking=setting_display" => array("page" =>"Booking","operate"=>"Customize Coulumns"),
  353. "ocean_booking=detail" => array("page" =>"Booking","operate"=>"Open Detailed Page"),
  354. "ocean_booking=excel" => array("page" =>"Booking","operate"=>"Download"),
  355. "ocean_booking=save_communication" => array("page" =>"Booking","operate"=>"Send Email"),
  356. "login=tracking_checked" => array("page" =>"Tracking","operate"=>"Public tracking"),
  357. "ocean_order=search" => array("page" =>"Tracking","operate"=>"Search"),
  358. "ocean_order=setting_display" => array("page" =>"Tracking","operate"=>"Customize Coulumns"),
  359. "ocean_order=detail" => array("page" =>"Tracking","operate"=>"Open Detailed Page"),
  360. "ocean_order=excel" => array("page" =>"Tracking","operate"=>"Download"),
  361. "ocean_booking=save_communication" => array("page" =>"Tracking","operate"=>"Send Email"),
  362. "password=" => array("page" =>"Profile","operate"=>"Change password"));
  363. return $operationConvertName[$action."=".$operate];
  364. }
  365. public static function calculateTicks($minValue, $maxValue, $targetTickCount = 10) {
  366. $tickSpacing = ($maxValue - $minValue);
  367. $tickSpacing = intval($tickSpacing);
  368. $len = strlen($tickSpacing);
  369. if($len >=2 ){
  370. $interval = ceil($tickSpacing/pow(10,$len-2)/10) *pow(10,$len-2);
  371. }else{
  372. $interval = ceil($tickSpacing/10);
  373. }
  374. return $interval;
  375. }
  376. public static function uuid() {
  377. return strtoupper(md5(uniqid("", TRUE) . mt_rand()));
  378. }
  379. public static function count($variable){
  380. if (is_array($variable)) {
  381. $count = count($variable);
  382. } else {
  383. $count = 0;
  384. }
  385. return $count;
  386. }
  387. public static function implode($variable){
  388. $variable = isset($variable) && is_array($variable) ? $variable : array();
  389. return implode(',', $variable);
  390. }
  391. public static function in_array($str, $arr){
  392. if (is_array($arr)) {
  393. return in_array($str, $arr);
  394. } else {
  395. return false;
  396. }
  397. }
  398. }
  399. ?>