common.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. if (!function_exists('exception')) {
  13. /**
  14. * 抛出异常处理
  15. *
  16. * @param string $msg 异常消息
  17. * @param integer $code 异常代码 默认为0
  18. * @param string $exception 异常类
  19. *
  20. * @throws Exception
  21. */
  22. function exception($msg, $code = 0, $exception = '')
  23. {
  24. $e = $exception ?: '\think\Exception';
  25. throw new $e($msg, $code);
  26. }
  27. }
  28. if (!function_exists('sys_config')) {
  29. /**
  30. * 获取系统单个配置
  31. * @param string $name
  32. * @param string $default
  33. * @return string
  34. */
  35. function sys_config(string $name, $default = '', $cache = false)
  36. {
  37. if (empty($name))
  38. return $default;
  39. $config = trim(app('sysConfig')->get($name, $default, $cache));
  40. if ($config === '' || $config === false) {
  41. return $default;
  42. } else {
  43. return $config;
  44. }
  45. }
  46. }
  47. if (!function_exists('sys_data')) {
  48. /**
  49. * 获取系统单个配置
  50. * @param string $name
  51. * @return mixed
  52. */
  53. function sys_data(string $name, int $limit = 0)
  54. {
  55. return app('sysGroupData')->getData($name, $limit);
  56. }
  57. }
  58. if (!function_exists('filter_emoji')) {
  59. // 过滤掉emoji表情
  60. function filter_emoji($str)
  61. {
  62. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  63. '/./u',
  64. function (array $match) {
  65. return strlen($match[0]) >= 4 ? '' : $match[0];
  66. },
  67. $str);
  68. return $str;
  69. }
  70. }
  71. if (!function_exists('str_middle_replace')) {
  72. /** TODO 系统未使用
  73. * @param string $string 需要替换的字符串
  74. * @param int $start 开始的保留几位
  75. * @param int $end 最后保留几位
  76. * @return string
  77. */
  78. function str_middle_replace($string, $start, $end)
  79. {
  80. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  81. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  82. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  83. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  84. }
  85. }
  86. if (!function_exists('not_empty_check')) {
  87. /**
  88. * 非空验证
  89. * @param $param
  90. * @return bool
  91. */
  92. function not_empty_check($param)
  93. {
  94. if (is_array($param)) {
  95. return !(count($param) <= 0);
  96. } else {
  97. if ($param == '') {
  98. return false;
  99. }
  100. if ($param == null) {
  101. return false;
  102. }
  103. return true;
  104. }
  105. }
  106. }
  107. if (!function_exists('sensitive_words_filter')) {
  108. /**
  109. * 敏感词过滤
  110. *
  111. * @param string
  112. * @return string
  113. */
  114. function sensitive_words_filter($str)
  115. {
  116. if (!$str) return '';
  117. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  118. $words = file($file);
  119. foreach ($words as $word) {
  120. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  121. if (!$word) continue;
  122. $ret = preg_match("/$word/", $str, $match);
  123. if ($ret) {
  124. return $match[0];
  125. }
  126. }
  127. return '';
  128. }
  129. }
  130. if (!function_exists('make_path')) {
  131. /**
  132. * 上传路径转化,默认路径
  133. * @param $path
  134. * @param int $type
  135. * @param bool $force
  136. * @return string
  137. */
  138. function make_path($path, int $type = 2, bool $force = false)
  139. {
  140. $path = DS . ltrim(rtrim($path));
  141. switch ($type) {
  142. case 1:
  143. $path .= DS . date('Y');
  144. break;
  145. case 2:
  146. $path .= DS . date('Y') . DS . date('m');
  147. break;
  148. case 3:
  149. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  150. break;
  151. }
  152. try {
  153. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  154. return trim(str_replace(DS, '/', $path), '.');
  155. } else return '';
  156. } catch (\Exception $e) {
  157. if ($force)
  158. throw new \Exception($e->getMessage());
  159. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  160. }
  161. }
  162. }
  163. if (!function_exists('curl_file_exist')) {
  164. /**
  165. * CURL 检测远程文件是否在
  166. * @param $url
  167. * @return bool
  168. */
  169. function curl_file_exist($url)
  170. {
  171. $ch = curl_init();
  172. try {
  173. curl_setopt($ch, CURLOPT_URL, $url);
  174. curl_setopt($ch, CURLOPT_HEADER, 1);
  175. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  176. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  177. $contents = curl_exec($ch);
  178. if (preg_match("/404/", $contents)) return false;
  179. if (preg_match("/403/", $contents)) return false;
  180. return true;
  181. } catch (\Exception $e) {
  182. return false;
  183. }
  184. }
  185. }
  186. if (!function_exists('set_file_url')) {
  187. /**
  188. * 设置附加路径
  189. * @param $url
  190. * @return bool
  191. */
  192. function set_file_url($image, $siteUrl = '')
  193. {
  194. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  195. $domainTop = substr($image, 0, 4);
  196. if ($domainTop == 'http') return $image;
  197. $image = str_replace('\\', '/', $image);
  198. return $siteUrl . $image;
  199. }
  200. }
  201. if (!function_exists('set_http_type')) {
  202. /**
  203. * 修改 https 和 http
  204. * @param $url $url 域名
  205. * @param int $type 0 返回https 1 返回 http
  206. * @return string
  207. */
  208. function set_http_type($url, $type = 0)
  209. {
  210. $domainTop = substr($url, 0, 5);
  211. if ($type) {
  212. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  213. } else {
  214. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  215. }
  216. return $url;
  217. }
  218. }
  219. if (!function_exists('check_card')) {
  220. /**
  221. * 身份证验证
  222. * @param $card
  223. * @return bool
  224. */
  225. function check_card($card)
  226. {
  227. $city = [11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "内蒙古", 21 => "辽宁", 22 => "吉林", 23 => "黑龙江 ", 31 => "上海", 32 => "江苏", 33 => "浙江", 34 => "安徽", 35 => "福建", 36 => "江西", 37 => "山东", 41 => "河南", 42 => "湖北 ", 43 => "湖南", 44 => "广东", 45 => "广西", 46 => "海南", 50 => "重庆", 51 => "四川", 52 => "贵州", 53 => "云南", 54 => "西藏 ", 61 => "陕西", 62 => "甘肃", 63 => "青海", 64 => "宁夏", 65 => "新疆", 71 => "台湾", 81 => "香港", 82 => "澳门", 91 => "国外 "];
  228. $tip = "";
  229. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  230. $pass = true;
  231. if (!$card || !preg_match($match, $card)) {
  232. //身份证格式错误
  233. $pass = false;
  234. } else if (!$city[substr($card, 0, 2)]) {
  235. //地址错误
  236. $pass = false;
  237. } else {
  238. //18位身份证需要验证最后一位校验位
  239. if (strlen($card) == 18) {
  240. $card = str_split($card);
  241. //∑(ai×Wi)(mod 11)
  242. //加权因子
  243. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  244. //校验位
  245. $parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  246. $sum = 0;
  247. $ai = 0;
  248. $wi = 0;
  249. for ($i = 0; $i < 17; $i++) {
  250. $ai = $card[$i];
  251. $wi = $factor[$i];
  252. $sum += $ai * $wi;
  253. }
  254. $last = $parity[$sum % 11];
  255. if ($parity[$sum % 11] != $card[17]) {
  256. // $tip = "校验位错误";
  257. $pass = false;
  258. }
  259. } else {
  260. $pass = false;
  261. }
  262. }
  263. if (!$pass) return false;/* 身份证格式错误*/
  264. return true;/* 身份证格式正确*/
  265. }
  266. }
  267. if (!function_exists('anonymity')) {
  268. /**
  269. * 匿名处理处理用户昵称
  270. * @param $name
  271. * @return string
  272. */
  273. function anonymity($name)
  274. {
  275. $strLen = mb_strlen($name, 'UTF-8');
  276. $min = 3;
  277. if ($strLen <= 1)
  278. return '*';
  279. if ($strLen <= $min)
  280. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $min - 1);
  281. else
  282. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $strLen - 1) . mb_substr($name, -1, 1, 'UTF-8');
  283. }
  284. }
  285. if (!function_exists('sort_list_tier')) {
  286. /**
  287. * 分级排序
  288. * @param $data
  289. * @param int $pid
  290. * @param string $field
  291. * @param string $pk
  292. * @param string $html
  293. * @param int $level
  294. * @param bool $clear
  295. * @return array
  296. */
  297. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  298. {
  299. static $list = [];
  300. if ($clear) $list = [];
  301. foreach ($data as $k => $res) {
  302. if ($res[$field] == $pid) {
  303. $res['html'] = str_repeat($html, $level);
  304. $list[] = $res;
  305. unset($data[$k]);
  306. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  307. }
  308. }
  309. return $list;
  310. }
  311. }
  312. if (!function_exists('time_tran')) {
  313. /**
  314. * 时间戳人性化转化
  315. * @param $time
  316. * @return string
  317. */
  318. function time_tran($time)
  319. {
  320. $t = time() - $time;
  321. $f = array(
  322. '31536000' => '年',
  323. '2592000' => '个月',
  324. '604800' => '星期',
  325. '86400' => '天',
  326. '3600' => '小时',
  327. '60' => '分钟',
  328. '1' => '秒'
  329. );
  330. foreach ($f as $k => $v) {
  331. if (0 != $c = floor($t / (int)$k)) {
  332. return $c . $v . '前';
  333. }
  334. }
  335. }
  336. }
  337. if (!function_exists('url_to_path')) {
  338. /**
  339. * url转换路径
  340. * @param $url
  341. * @return string
  342. */
  343. function url_to_path($url)
  344. {
  345. $path = trim(str_replace('/', DS, $url), DS);
  346. if (0 !== strripos($path, 'public'))
  347. $path = 'public' . DS . $path;
  348. return app()->getRootPath() . $path;
  349. }
  350. }
  351. if (!function_exists('path_to_url')) {
  352. /**
  353. * 路径转url路径
  354. * @param $path
  355. * @return string
  356. */
  357. function path_to_url($path)
  358. {
  359. return trim(str_replace(DS, '/', $path), '.');
  360. }
  361. }
  362. if (!function_exists('image_to_base64')) {
  363. /**
  364. * 获取图片转为base64
  365. * @param string $avatar
  366. * @return bool|string
  367. */
  368. function image_to_base64($avatar = '', $timeout = 9)
  369. {
  370. try {
  371. $url = parse_url($avatar);
  372. $url = $url['host'];
  373. $header = [
  374. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  375. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  376. 'Accept-Encoding: gzip, deflate, br',
  377. 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  378. 'Host:' . $url
  379. ];
  380. $dir = pathinfo($url);
  381. $host = $dir['dirname'];
  382. $refer = $host . '/';
  383. $curl = curl_init();
  384. curl_setopt($curl, CURLOPT_REFERER, $refer);
  385. curl_setopt($curl, CURLOPT_URL, $avatar);
  386. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  387. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  388. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  389. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  390. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  391. $data = curl_exec($curl);
  392. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  393. curl_close($curl);
  394. if ($code == 200) {
  395. return "data:image/jpeg;base64," . base64_encode($data);
  396. } else {
  397. return false;
  398. }
  399. } catch (\Exception $e) {
  400. return false;
  401. }
  402. }
  403. }
  404. if (!function_exists('put_image')) {
  405. /**
  406. * 获取图片转为base64
  407. * @param string $avatar
  408. * @return bool|string
  409. */
  410. function put_image($url, $filename = '')
  411. {
  412. if ($url == '') {
  413. return false;
  414. }
  415. try {
  416. if ($filename == '') {
  417. $ext = pathinfo($url);
  418. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  419. return false;
  420. }
  421. $filename = time() . "." . $ext['extension'];
  422. }
  423. //文件保存路径
  424. ob_start();
  425. readfile($url);
  426. $img = ob_get_contents();
  427. ob_end_clean();
  428. $path = 'uploads/qrcode';
  429. $fp2 = fopen($path . '/' . $filename, 'a');
  430. fwrite($fp2, $img);
  431. fclose($fp2);
  432. return $path . '/' . $filename;
  433. } catch (\Exception $e) {
  434. return false;
  435. }
  436. }
  437. }
  438. if (!function_exists('debug_file')) {
  439. /**
  440. * 文件调试
  441. * @param $content
  442. */
  443. function debug_file($content, string $fileName = 'error', string $ext = 'txt')
  444. {
  445. $msg = '[' . date('Y-m-d H:i:s', time()) . '] [ DEBUG ] ';
  446. $pach = app()->getRuntimePath();
  447. file_put_contents($pach . $fileName . '.' . $ext, $msg . print_r($content, true) . "\r\n", FILE_APPEND);
  448. }
  449. }
  450. if (!function_exists('sql_filter')) {
  451. /**
  452. * sql 参数过滤
  453. * @param string $str
  454. * @return mixed
  455. */
  456. function sql_filter(string $str)
  457. {
  458. $filter = ['select ', 'insert ', 'update ', 'delete ', 'drop', 'truncate ', 'declare', 'xp_cmdshell', '/add', ' or ', 'exec', 'create', 'chr', 'mid', ' and ', 'execute'];
  459. $toupper = array_map(function ($str) {
  460. return strtoupper($str);
  461. }, $filter);
  462. return str_replace(array_merge($filter, $toupper, ['%20']), '', $str);
  463. }
  464. }
  465. if (!function_exists('is_brokerage_statu')) {
  466. /**
  467. * 是否能成为推广人
  468. * @param float $price
  469. * @return bool
  470. */
  471. function is_brokerage_statu(float $price)
  472. {
  473. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  474. if ($storeBrokerageStatus == 1) {
  475. return false;
  476. } else {
  477. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  478. return $price >= $storeBrokeragePrice;
  479. }
  480. }
  481. }
  482. if (!function_exists('array_unique_fb')) {
  483. /**
  484. * 二维数组去掉重复值
  485. * @param $array
  486. * @return array
  487. */
  488. function array_unique_fb($array)
  489. {
  490. $out = array();
  491. foreach ($array as $key => $value) {
  492. if (!in_array($value, $out)) {
  493. $out[$key] = $value;
  494. }
  495. }
  496. $out = array_values($out);
  497. return $out;
  498. }
  499. }