common.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 = '')
  36. {
  37. if (empty($name))
  38. return $default;
  39. $config = trim(app('sysConfig')->get($name));
  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 string
  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('sensitive_words_filter')) {
  87. /**
  88. * 敏感词过滤
  89. *
  90. * @param string
  91. * @return string
  92. */
  93. function sensitive_words_filter($str)
  94. {
  95. if (!$str) return '';
  96. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  97. $words = file($file);
  98. foreach ($words as $word) {
  99. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  100. if (!$word) continue;
  101. $ret = preg_match("/$word/", $str, $match);
  102. if ($ret) {
  103. return $match[0];
  104. }
  105. }
  106. return '';
  107. }
  108. }
  109. if (!function_exists('make_path')) {
  110. /**
  111. * 上传路径转化,默认路径
  112. * @param $path
  113. * @param int $type
  114. * @param bool $force
  115. * @return string
  116. */
  117. function make_path($path, int $type = 2, bool $force = false)
  118. {
  119. $path = DS . ltrim(rtrim($path));
  120. switch ($type) {
  121. case 1:
  122. $path .= DS . date('Y');
  123. break;
  124. case 2:
  125. $path .= DS . date('Y') . DS . date('m');
  126. break;
  127. case 3:
  128. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  129. break;
  130. }
  131. try {
  132. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  133. return trim(str_replace(DS, '/', $path), '.');
  134. } else return '';
  135. } catch (\Exception $e) {
  136. if ($force)
  137. throw new \Exception($e->getMessage());
  138. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  139. }
  140. }
  141. }
  142. if (!function_exists('curl_file_exist')) {
  143. /**
  144. * CURL 检测远程文件是否在
  145. * @param $url
  146. * @return bool
  147. */
  148. function curl_file_exist($url)
  149. {
  150. $ch = curl_init();
  151. try {
  152. curl_setopt($ch, CURLOPT_URL, $url);
  153. curl_setopt($ch, CURLOPT_HEADER, 1);
  154. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  155. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  156. $contents = curl_exec($ch);
  157. if (preg_match("/404/", $contents)) return false;
  158. if (preg_match("/403/", $contents)) return false;
  159. return true;
  160. } catch (\Exception $e) {
  161. return false;
  162. }
  163. }
  164. }
  165. if (!function_exists('set_file_url')) {
  166. /**
  167. * 设置附加路径
  168. * @param $url
  169. * @return bool
  170. */
  171. function set_file_url($image, $siteUrl = '')
  172. {
  173. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  174. $domainTop = substr($image, 0, 4);
  175. if ($domainTop == 'http') return $image;
  176. $image = str_replace('\\', '/', $image);
  177. return $siteUrl . $image;
  178. }
  179. }
  180. if (!function_exists('set_http_type')) {
  181. /**
  182. * 修改 https 和 http
  183. * @param $url $url 域名
  184. * @param int $type 0 返回https 1 返回 http
  185. * @return string
  186. */
  187. function set_http_type($url, $type = 0)
  188. {
  189. $domainTop = substr($url, 0, 5);
  190. if ($type) {
  191. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  192. } else {
  193. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  194. }
  195. return $url;
  196. }
  197. }
  198. if (!function_exists('check_card')) {
  199. /**
  200. * 身份证验证
  201. * @param $card
  202. * @return bool
  203. */
  204. function check_card($card)
  205. {
  206. $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 => "国外 "];
  207. $tip = "";
  208. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  209. $pass = true;
  210. if (!$card || !preg_match($match, $card)) {
  211. //身份证格式错误
  212. $pass = false;
  213. } else if (!$city[substr($card, 0, 2)]) {
  214. //地址错误
  215. $pass = false;
  216. } else {
  217. //18位身份证需要验证最后一位校验位
  218. if (strlen($card) == 18) {
  219. $card = str_split($card);
  220. //∑(ai×Wi)(mod 11)
  221. //加权因子
  222. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  223. //校验位
  224. $parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  225. $sum = 0;
  226. $ai = 0;
  227. $wi = 0;
  228. for ($i = 0; $i < 17; $i++) {
  229. $ai = $card[$i];
  230. $wi = $factor[$i];
  231. $sum += $ai * $wi;
  232. }
  233. $last = $parity[$sum % 11];
  234. if ($parity[$sum % 11] != $card[17]) {
  235. // $tip = "校验位错误";
  236. $pass = false;
  237. }
  238. } else {
  239. $pass = false;
  240. }
  241. }
  242. if (!$pass) return false;/* 身份证格式错误*/
  243. return true;/* 身份证格式正确*/
  244. }
  245. }
  246. if (!function_exists('anonymity')) {
  247. /**
  248. * 匿名处理处理用户昵称
  249. * @param $name
  250. * @return string
  251. */
  252. function anonymity($name)
  253. {
  254. $strLen = mb_strlen($name, 'UTF-8');
  255. $min = 3;
  256. if ($strLen <= 1)
  257. return '*';
  258. if ($strLen <= $min)
  259. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $min - 1);
  260. else
  261. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $strLen - 1) . mb_substr($name, -1, 1, 'UTF-8');
  262. }
  263. }
  264. if (!function_exists('sort_list_tier')) {
  265. /**
  266. * 分级排序
  267. * @param $data
  268. * @param int $pid
  269. * @param string $field
  270. * @param string $pk
  271. * @param string $html
  272. * @param int $level
  273. * @param bool $clear
  274. * @return array
  275. */
  276. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  277. {
  278. static $list = [];
  279. if ($clear) $list = [];
  280. foreach ($data as $k => $res) {
  281. if ($res[$field] == $pid) {
  282. $res['html'] = str_repeat($html, $level);
  283. $list[] = $res;
  284. unset($data[$k]);
  285. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  286. }
  287. }
  288. return $list;
  289. }
  290. }
  291. if (!function_exists('time_tran')) {
  292. /**
  293. * 时间戳人性化转化
  294. * @param $time
  295. * @return string
  296. */
  297. function time_tran($time)
  298. {
  299. $t = time() - $time;
  300. $f = array(
  301. '31536000' => '年',
  302. '2592000' => '个月',
  303. '604800' => '星期',
  304. '86400' => '天',
  305. '3600' => '小时',
  306. '60' => '分钟',
  307. '1' => '秒'
  308. );
  309. foreach ($f as $k => $v) {
  310. if (0 != $c = floor($t / (int)$k)) {
  311. return $c . $v . '前';
  312. }
  313. }
  314. }
  315. }
  316. if (!function_exists('url_to_path')) {
  317. /**
  318. * url转换路径
  319. * @param $url
  320. * @return string
  321. */
  322. function url_to_path($url)
  323. {
  324. $path = trim(str_replace('/', DS, $url), DS);
  325. if (0 !== strripos($path, 'public'))
  326. $path = 'public' . DS . $path;
  327. return app()->getRootPath() . $path;
  328. }
  329. }
  330. if (!function_exists('path_to_url')) {
  331. /**
  332. * 路径转url路径
  333. * @param $path
  334. * @return string
  335. */
  336. function path_to_url($path)
  337. {
  338. return trim(str_replace(DS, '/', $path), '.');
  339. }
  340. }
  341. if (!function_exists('image_to_base64')) {
  342. /**
  343. * 获取图片转为base64
  344. * @param string $avatar
  345. * @return bool|string
  346. */
  347. function image_to_base64($avatar = '', $timeout = 9)
  348. {
  349. try {
  350. $url = parse_url($avatar);
  351. $url = $url['host'];
  352. $header = [
  353. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  354. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  355. 'Accept-Encoding: gzip, deflate, br',
  356. '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',
  357. 'Host:' . $url
  358. ];
  359. $dir = pathinfo($url);
  360. $host = $dir['dirname'];
  361. $refer = $host . '/';
  362. $curl = curl_init();
  363. curl_setopt($curl, CURLOPT_REFERER, $refer);
  364. curl_setopt($curl, CURLOPT_URL, $avatar);
  365. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  366. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  367. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  368. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  369. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  370. $data = curl_exec($curl);
  371. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  372. curl_close($curl);
  373. if ($code == 200) {
  374. return "data:image/jpeg;base64," . base64_encode($data);
  375. } else {
  376. return false;
  377. }
  378. } catch (\Exception $e) {
  379. return false;
  380. }
  381. }
  382. }
  383. if (!function_exists('put_image')) {
  384. /**
  385. * 获取图片转为base64
  386. * @param string $avatar
  387. * @return bool|string
  388. */
  389. function put_image($url, $filename = '')
  390. {
  391. if ($url == '') {
  392. return false;
  393. }
  394. try {
  395. if ($filename == '') {
  396. $ext = pathinfo($url);
  397. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  398. return false;
  399. }
  400. $filename = time() . "." . $ext['extension'];
  401. }
  402. //文件保存路径
  403. ob_start();
  404. readfile($url);
  405. $img = ob_get_contents();
  406. ob_end_clean();
  407. $path = 'uploads/qrcode';
  408. $fp2 = fopen($path . '/' . $filename, 'a');
  409. fwrite($fp2, $img);
  410. fclose($fp2);
  411. return $path . '/' . $filename;
  412. } catch (\Exception $e) {
  413. return false;
  414. }
  415. }
  416. }
  417. if (!function_exists('debug_file')) {
  418. /**
  419. * 文件调试
  420. * @param $content
  421. */
  422. function debug_file($content, string $fileName = 'error', string $ext = 'txt')
  423. {
  424. $msg = '[' . date('Y-m-d H:i:s', time()) . '] [ DEBUG ] ';
  425. $pach = app()->getRuntimePath();
  426. file_put_contents($pach . $fileName . '.' . $ext, $msg . print_r($content, true) . "\r\n", FILE_APPEND);
  427. }
  428. }
  429. if (!function_exists('sql_filter')) {
  430. /**
  431. * sql 参数过滤
  432. * @param string $str
  433. * @return mixed
  434. */
  435. function sql_filter(string $str)
  436. {
  437. $filter = ['select ', 'insert ', 'update ', 'delete ', 'drop', 'truncate ', 'declare', 'xp_cmdshell', '/add', ' or ', 'exec', 'create', 'chr', 'mid', ' and ', 'execute'];
  438. $toupper = array_map(function ($str) {
  439. return strtoupper($str);
  440. }, $filter);
  441. return str_replace(array_merge($filter, $toupper, ['%20']), '', $str);
  442. }
  443. }
  444. if (!function_exists('is_brokerage_statu')) {
  445. /**
  446. * 是否能成为推广人
  447. * @param float $price
  448. * @return bool
  449. */
  450. function is_brokerage_statu(float $price)
  451. {
  452. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  453. if ($storeBrokerageStatus == 1) {
  454. return false;
  455. } else {
  456. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  457. return $price >= $storeBrokeragePrice;
  458. }
  459. }
  460. }
  461. if (!function_exists('array_unique_fb')) {
  462. /**
  463. * 二维数组去掉重复值
  464. * @param $array
  465. * @return array
  466. */
  467. function array_unique_fb($array)
  468. {
  469. $out = array();
  470. foreach ($array as $key => $value) {
  471. if (!in_array($value, $out)) {
  472. $out[$key] = $value;
  473. }
  474. }
  475. $out = array_values($out);
  476. return $out;
  477. }
  478. }
  479. if (!function_exists('not_empty_check')) {
  480. /**
  481. * 非空验证
  482. * @param $param
  483. * @return bool
  484. */
  485. function not_empty_check($param)
  486. {
  487. if (is_array($param)) {
  488. return !(count($param) <= 0);
  489. } else {
  490. if ($param == '') {
  491. return false;
  492. }
  493. if ($param == null) {
  494. return false;
  495. }
  496. return true;
  497. }
  498. }
  499. }
  500. if (!function_exists('mobile_check')) {
  501. /**
  502. * 电话验证
  503. * @param $param
  504. * @return bool
  505. */
  506. function mobile_check($param)
  507. {
  508. if (!preg_match("/^1[3456789]\d{9}$/", $param)) {
  509. return false;
  510. } else {
  511. return true;
  512. }
  513. }
  514. }
  515. if (!function_exists('do_request')) {
  516. /**
  517. * CURL 请求接口
  518. * @param string $url 请求地址
  519. * @param array $data 请求参数
  520. * @param array $header 请求头
  521. * @param bool $post true:post请求 false:get请求
  522. * @param bool $json post请求时 请求数据打包方式是否为json
  523. * @param int $format 数据打包为json格式时打包的格式值 来自json_encode
  524. * @param bool $form post请求时 请求数据是否为表单 同时为false时为http提交 优先级高于json
  525. * @return bool|false|string
  526. */
  527. function do_request($url, $data, $header = null, $post = true, $json = false, $format = 0, $form = false)
  528. {
  529. $curl = curl_init();
  530. curl_setopt($curl, CURLOPT_URL, $url);
  531. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  532. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  533. if ($post) {
  534. curl_setopt($curl, CURLOPT_POST, 1);
  535. if (!$json && !$form) {
  536. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  537. } else if ($json && !$form) {
  538. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $format));
  539. } else {
  540. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  541. }
  542. }
  543. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  544. if ($header) {
  545. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  546. curl_setopt($curl, CURLOPT_HEADER, 0);
  547. }
  548. $result = curl_exec($curl);
  549. if (curl_errno($curl)) {
  550. return json_encode(['status' => curl_errno($curl), 'msg' => '请求失败']);
  551. }
  552. curl_close($curl);
  553. return $result;
  554. }
  555. }