common.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. use app\services\pay\PayServices;
  13. use crmeb\services\CacheService;
  14. use crmeb\services\HttpService;
  15. use Fastknife\Service\ClickWordCaptchaService;
  16. use think\exception\ValidateException;
  17. use crmeb\services\FormBuilder as Form;
  18. use app\services\other\UploadService;
  19. use Fastknife\Service\BlockPuzzleCaptchaService;
  20. use app\services\system\lang\LangTypeServices;
  21. use app\services\system\lang\LangCodeServices;
  22. use app\services\system\lang\LangCountryServices;
  23. use think\facade\Config;
  24. use think\facade\Log;
  25. use think\facade\Db;
  26. if (!function_exists('crmebLog')) {
  27. /**
  28. * CRMEB Log 日志
  29. * @param $msg
  30. * @author 吴汐
  31. * @email 442384644@qq.com
  32. * @date 2023/03/03
  33. */
  34. function crmebLog($msg)
  35. {
  36. Log::write($msg, 'crmeb');
  37. }
  38. }
  39. if (!function_exists('getWorkerManUrl')) {
  40. /**
  41. * 获取客服数据
  42. * @return mixed
  43. */
  44. function getWorkerManUrl()
  45. {
  46. $ws = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'wss://' : 'ws://';
  47. $host = $_SERVER['HTTP_HOST'];
  48. $data['admin'] = $ws . $host . '/notice';
  49. $data['chat'] = $ws . $host . '/msg';
  50. return $data;
  51. }
  52. }
  53. if (!function_exists('object2array')) {
  54. /**
  55. * 对象转数组
  56. * @param $object
  57. * @return array|mixed
  58. */
  59. function object2array($object)
  60. {
  61. $array = [];
  62. if (is_object($object)) {
  63. foreach ($object as $key => $value) {
  64. $array[$key] = $value;
  65. }
  66. } else {
  67. $array = $object;
  68. }
  69. return $array;
  70. }
  71. }
  72. if (!function_exists('exception')) {
  73. /**
  74. * 抛出异常处理
  75. * @param $msg
  76. * @param int $code
  77. * @param string $exception
  78. * @throws \think\Exception
  79. */
  80. function exception($msg, $code = 0, $exception = '')
  81. {
  82. $e = $exception ?: '\think\Exception';
  83. throw new $e($msg, $code);
  84. }
  85. }
  86. if (!function_exists('sys_config')) {
  87. /**
  88. * 获取系统单个配置
  89. * @param string $name
  90. * @param string $default
  91. * @return string
  92. */
  93. function sys_config(string $name, $default = '')
  94. {
  95. if (empty($name))
  96. return $default;
  97. $sysConfig = app('sysConfig')->get($name);
  98. if (is_array($sysConfig)) {
  99. foreach ($sysConfig as &$item) {
  100. if (strpos($item, '/uploads/system/') !== false || strpos($item, '/statics/system_images/') !== false) $item = set_file_url($item);
  101. }
  102. } else {
  103. if (strpos($sysConfig, '/uploads/system/') !== false || strpos($sysConfig, '/statics/system_images/') !== false) $sysConfig = set_file_url($sysConfig);
  104. }
  105. $config = is_array($sysConfig) ? $sysConfig : trim($sysConfig);
  106. if ($config === '' || $config === false) {
  107. return $default;
  108. } else {
  109. return $config;
  110. }
  111. }
  112. }
  113. if (!function_exists('sys_data')) {
  114. /**
  115. * 获取系统单个数据
  116. * @param string $name
  117. * @return string
  118. */
  119. function sys_data(string $name, int $limit = 0)
  120. {
  121. return app('sysGroupData')->getData($name, $limit);
  122. }
  123. }
  124. if (!function_exists('filter_emoji')) {
  125. // 过滤掉emoji表情
  126. function filter_emoji($str)
  127. {
  128. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  129. '/./u',
  130. function (array $match) {
  131. return strlen($match[0]) >= 4 ? '' : $match[0];
  132. },
  133. $str);
  134. return $str;
  135. }
  136. }
  137. if (!function_exists('str_middle_replace')) {
  138. /** TODO 系统未使用
  139. * @param string $string 需要替换的字符串
  140. * @param int $start 开始的保留几位
  141. * @param int $end 最后保留几位
  142. * @return string
  143. */
  144. function str_middle_replace($string, $start, $end)
  145. {
  146. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  147. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  148. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  149. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  150. }
  151. }
  152. if (!function_exists('sensitive_words_filter')) {
  153. /**
  154. * 敏感词过滤
  155. *
  156. * @param string
  157. * @return string
  158. */
  159. function sensitive_words_filter($str)
  160. {
  161. if (!$str) return '';
  162. $file = app()->getAppPath() . 'public/statics/plug/censorwords/CensorWords';
  163. $words = file($file);
  164. foreach ($words as $word) {
  165. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  166. if (!$word) continue;
  167. $ret = preg_match("/$word/", $str, $match);
  168. if ($ret) {
  169. return $match[0];
  170. }
  171. }
  172. return '';
  173. }
  174. }
  175. if (!function_exists('make_path')) {
  176. /**
  177. * 上传路径转化,默认路径
  178. * @param $path
  179. * @param int $type
  180. * @param bool $force
  181. * @return string
  182. */
  183. function make_path($path, int $type = 2, bool $force = false)
  184. {
  185. $path = DS . ltrim(rtrim($path));
  186. switch ($type) {
  187. case 1:
  188. $path .= DS . date('Y');
  189. break;
  190. case 2:
  191. $path .= DS . date('Y') . DS . date('m');
  192. break;
  193. case 3:
  194. $path .= DS . date('Y') . DS . date('m') . DS . date('d');
  195. break;
  196. }
  197. try {
  198. if (is_dir(app()->getRootPath() . 'public' . DS . 'uploads' . $path) == true || mkdir(app()->getRootPath() . 'public' . DS . 'uploads' . $path, 0777, true) == true) {
  199. return trim(str_replace(DS, '/', $path), '.');
  200. } else return '';
  201. } catch (\Exception $e) {
  202. if ($force)
  203. throw new \Exception($e->getMessage());
  204. // return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS . 'attach' . DS;
  205. return '';
  206. }
  207. }
  208. }
  209. if (!function_exists('curl_file_exist')) {
  210. /**
  211. * CURL 检测远程文件是否在
  212. * @param $url
  213. * @return bool
  214. */
  215. function curl_file_exist($url)
  216. {
  217. $ch = curl_init();
  218. try {
  219. curl_setopt($ch, CURLOPT_URL, $url);
  220. curl_setopt($ch, CURLOPT_HEADER, 1);
  221. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  222. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  223. $contents = curl_exec($ch);
  224. if (preg_match("/404/", $contents)) return false;
  225. if (preg_match("/403/", $contents)) return false;
  226. return true;
  227. } catch (\Exception $e) {
  228. return false;
  229. }
  230. }
  231. }
  232. if (!function_exists('set_file_url')) {
  233. /**
  234. * 设置附加路径
  235. * @param $url
  236. * @return bool
  237. */
  238. function set_file_url($image, $siteUrl = '')
  239. {
  240. if (!strlen(trim($siteUrl))) $siteUrl = sys_config('site_url');
  241. if (!$image) return $image;
  242. if (is_array($image)) {
  243. foreach ($image as &$item) {
  244. $domainTop1 = substr($item, 0, 4);
  245. $domainTop2 = substr($item, 0, 2);
  246. if ($domainTop1 != 'http' && $domainTop2 != '//')
  247. $item = $siteUrl . str_replace('\\', '/', $item);
  248. }
  249. } else {
  250. $domainTop1 = substr($image, 0, 4);
  251. $domainTop2 = substr($image, 0, 2);
  252. if ($domainTop1 != 'http' && $domainTop2 != '//')
  253. $image = $siteUrl . str_replace('\\', '/', $image);
  254. }
  255. return $image;
  256. }
  257. }
  258. if (!function_exists('set_http_type')) {
  259. /**
  260. * 修改 https 和 http
  261. * @param $url $url 域名
  262. * @param int $type 0 返回https 1 返回 http
  263. * @return string
  264. */
  265. function set_http_type($url, $type = 0)
  266. {
  267. $domainTop = substr($url, 0, 5);
  268. if ($type) {
  269. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  270. } else {
  271. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  272. }
  273. return $url;
  274. }
  275. }
  276. if (!function_exists('check_card')) {
  277. /**
  278. * 身份证验证
  279. * @param $card
  280. * @return bool
  281. */
  282. function check_card($card)
  283. {
  284. $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 => "国外 "];
  285. $tip = "";
  286. $match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
  287. $pass = true;
  288. if (!$card || !preg_match($match, $card)) {
  289. //身份证格式错误
  290. $pass = false;
  291. } else if (!$city[substr($card, 0, 2)]) {
  292. //地址错误
  293. $pass = false;
  294. } else {
  295. //18位身份证需要验证最后一位校验位
  296. if (strlen($card) == 18) {
  297. $card = str_split($card);
  298. //∑(ai×Wi)(mod 11)
  299. //加权因子
  300. $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  301. //校验位
  302. $parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
  303. $sum = 0;
  304. $ai = 0;
  305. $wi = 0;
  306. for ($i = 0; $i < 17; $i++) {
  307. $ai = $card[$i];
  308. $wi = $factor[$i];
  309. $sum += $ai * $wi;
  310. }
  311. $last = $parity[$sum % 11];
  312. if ($parity[$sum % 11] != $card[17]) {
  313. // $tip = "校验位错误";
  314. $pass = false;
  315. }
  316. } else {
  317. $pass = false;
  318. }
  319. }
  320. if (!$pass) return false;/* 身份证格式错误*/
  321. return true;/* 身份证格式正确*/
  322. }
  323. }
  324. if (!function_exists('check_link')) {
  325. /**
  326. * 地址验证
  327. * @param string $link
  328. * @return false|int
  329. */
  330. function check_link(string $link)
  331. {
  332. return preg_match("/^(http|https|ftp):\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\”])*$/", $link);
  333. }
  334. }
  335. if (!function_exists('check_phone')) {
  336. /**
  337. * 手机号验证
  338. * @param $phone
  339. * @return false|int
  340. */
  341. function check_phone($phone)
  342. {
  343. return preg_match("/^1[3456789]\d{9}$/", $phone);
  344. }
  345. }
  346. if (!function_exists('anonymity')) {
  347. /**
  348. * 匿名处理处理用户昵称
  349. * @param $name
  350. * @return string
  351. */
  352. function anonymity($name, $type = 1)
  353. {
  354. if ($type == 1) {
  355. return mb_substr($name, 0, 1, 'UTF-8') . '**' . mb_substr($name, -1, 1, 'UTF-8');
  356. } else {
  357. $strLen = mb_strlen($name, 'UTF-8');
  358. $min = 3;
  359. if ($strLen <= 1)
  360. return '*';
  361. if ($strLen <= $min)
  362. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $min - 1);
  363. else
  364. return mb_substr($name, 0, 1, 'UTF-8') . str_repeat('*', $strLen - 1) . mb_substr($name, -1, 1, 'UTF-8');
  365. }
  366. }
  367. }
  368. if (!function_exists('sort_list_tier')) {
  369. /**
  370. * 分级排序
  371. * @param $data
  372. * @param int $pid
  373. * @param string $field
  374. * @param string $pk
  375. * @param string $html
  376. * @param int $level
  377. * @param bool $clear
  378. * @return array
  379. */
  380. function sort_list_tier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|-----', $level = 1, $clear = true)
  381. {
  382. static $list = [];
  383. if ($clear) $list = [];
  384. foreach ($data as $k => $res) {
  385. if ($res[$field] == $pid) {
  386. $res['html'] = str_repeat($html, $level);
  387. $list[] = $res;
  388. unset($data[$k]);
  389. sort_list_tier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
  390. }
  391. }
  392. return $list;
  393. }
  394. }
  395. if (!function_exists('sort_city_tier')) {
  396. /**
  397. * 城市数据整理
  398. * @param $data
  399. * @param int $pid
  400. * @param string $field
  401. * @param string $pk
  402. * @param string $html
  403. * @param int $level
  404. * @param bool $clear
  405. * @return array
  406. */
  407. function sort_city_tier($data, $pid = 0, $navList = [])
  408. {
  409. foreach ($data as $k => $menu) {
  410. if ($menu['parent_id'] == $pid) {
  411. unset($menu['parent_id']);
  412. unset($data[$k]);
  413. $menu['c'] = sort_city_tier($data, $menu['v']);
  414. $navList[] = $menu;
  415. }
  416. }
  417. return $navList;
  418. }
  419. }
  420. if (!function_exists('time_tran')) {
  421. /**
  422. * 时间戳人性化转化
  423. * @param $time
  424. * @return string
  425. */
  426. function time_tran($time)
  427. {
  428. $t = time() - $time;
  429. $f = array(
  430. '31536000' => '年',
  431. '2592000' => '个月',
  432. '604800' => '星期',
  433. '86400' => '天',
  434. '3600' => '小时',
  435. '60' => '分钟',
  436. '1' => '秒'
  437. );
  438. foreach ($f as $k => $v) {
  439. if (0 != $c = floor($t / (int)$k)) {
  440. return $c . $v . '前';
  441. }
  442. }
  443. }
  444. }
  445. if (!function_exists('url_to_path')) {
  446. /**
  447. * url转换路径
  448. * @param $url
  449. * @return string
  450. */
  451. function url_to_path($url)
  452. {
  453. $path = trim(str_replace('/', DS, $url), DS);
  454. if (0 !== strripos($path, 'public'))
  455. $path = 'public' . DS . $path;
  456. return app()->getRootPath() . $path;
  457. }
  458. }
  459. if (!function_exists('path_to_url')) {
  460. /**
  461. * 路径转url路径
  462. * @param $path
  463. * @return string
  464. */
  465. function path_to_url($path)
  466. {
  467. return trim(str_replace(DS, '/', $path), '.');
  468. }
  469. }
  470. if (!function_exists('image_to_base64')) {
  471. /**
  472. * 获取图片转为base64
  473. * @param string $avatar
  474. * @return bool|string
  475. */
  476. function image_to_base64($avatar = '', $timeout = 9)
  477. {
  478. $avatar = str_replace('https', 'http', $avatar);
  479. try {
  480. $url = parse_url($avatar);
  481. if ($url['scheme'] . '://' . $url['host'] == sys_config('site_url')) {
  482. return "data:image/jpeg;base64," . base64_encode(file_get_contents(public_path() . substr($url['path'], 1)));
  483. }
  484. $url = $url['host'];
  485. $header = [
  486. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
  487. 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
  488. 'Accept-Encoding: gzip, deflate, br',
  489. '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',
  490. 'Host:' . $url
  491. ];
  492. $dir = pathinfo($url);
  493. $host = $dir['dirname'];
  494. $refer = $host . '/';
  495. $curl = curl_init();
  496. curl_setopt($curl, CURLOPT_REFERER, $refer);
  497. curl_setopt($curl, CURLOPT_URL, $avatar);
  498. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  499. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  500. curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
  501. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
  502. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  503. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  504. $data = curl_exec($curl);
  505. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  506. curl_close($curl);
  507. if ($code == 200) {
  508. return "data:image/jpeg;base64," . base64_encode($data);
  509. } else {
  510. return false;
  511. }
  512. } catch (\Exception $e) {
  513. return false;
  514. }
  515. }
  516. }
  517. if (!function_exists('put_image')) {
  518. /**
  519. * 获取图片转为base64
  520. * @param string $avatar
  521. * @return bool|string
  522. */
  523. function put_image($url, $filename = '')
  524. {
  525. if ($url == '') {
  526. return false;
  527. }
  528. try {
  529. if ($filename == '') {
  530. $ext = pathinfo($url);
  531. if ($ext['extension'] != "jpg" && $ext['extension'] != "png" && $ext['extension'] != "jpeg") {
  532. return false;
  533. }
  534. $filename = time() . "." . $ext['extension'];
  535. }
  536. //文件保存路径
  537. ob_start();
  538. $url = str_replace('phar://', '', $url);
  539. readfile($url);
  540. $img = ob_get_contents();
  541. ob_end_clean();
  542. $path = 'uploads/qrcode';
  543. $fp2 = fopen($path . '/' . $filename, 'a');
  544. fwrite($fp2, $img);
  545. fclose($fp2);
  546. return $path . '/' . $filename;
  547. } catch (\Exception $e) {
  548. return false;
  549. }
  550. }
  551. }
  552. if (!function_exists('debug_file')) {
  553. /**
  554. * 文件调试
  555. * @param $content
  556. */
  557. function debug_file($content, string $fileName = 'error', string $ext = 'txt')
  558. {
  559. $msg = '[' . date('Y-m-d H:i:s', time()) . '] [ DEBUG ] ';
  560. $pach = app()->getRuntimePath();
  561. file_put_contents($pach . $fileName . '.' . $ext, $msg . print_r($content, true) . "\r\n", FILE_APPEND);
  562. }
  563. }
  564. if (!function_exists('sql_filter')) {
  565. /**
  566. * sql 参数过滤
  567. * @param string $str
  568. * @return mixed
  569. */
  570. function sql_filter(string $str)
  571. {
  572. $filter = ['select ', 'insert ', 'update ', 'delete ', 'drop', 'truncate ', 'declare', 'xp_cmdshell', '/add', ' or ', 'exec', 'create', 'chr', 'mid', ' and ', 'execute'];
  573. $toupper = array_map(function ($str) {
  574. return strtoupper($str);
  575. }, $filter);
  576. return str_replace(array_merge($filter, $toupper, ['%20']), '', $str);
  577. }
  578. }
  579. if (!function_exists('filter_str')) {
  580. /**
  581. * 过滤字符串敏感字符
  582. * @param $str
  583. * @return array|mixed|string|string[]|null
  584. */
  585. function filter_str($str)
  586. {
  587. $rules = [
  588. '/\.\./', // 禁用包含 ../ 的参数
  589. '/\<\?/', // 禁止 php 脚本出现
  590. '/\bor\b.*=.*/i', // 匹配 'or 1=1',防止 SQL 注入(注意边界词 \b 和不区分大小写 i 修饰符)
  591. '/(select[\s\S]*?)(from|limit)/i', // 防止 SQL 注入
  592. '/(union[\s\S]*?select)/i', // 防止 SQL 注入
  593. '/(having|updatexml|extractvalue)/i', // 防止 SQL 注入
  594. '/sleep\((\s*)(\d*)(\s*)\)/i', // 防止 SQL 盲注
  595. '/benchmark\((.*)\,(.*)\)/i', // 防止 SQL 盲注
  596. '/base64_decode\(/i', // 防止 SQL 变种注入
  597. '/(?:from\W+information_schema\W)/i', // 注意这里的 (?:...) 是不合法的,应该是 (?:...) 表示非捕获组,但通常我们不需要这个
  598. '/(?:current_|user|database|schema|connection_id)\s*\(/i', // 防止 SQL 注入(注意去掉了不必要的 (?:...))
  599. '/(?:etc\/\W*passwd)/i', // 防止窥探 Linux 用户信息
  600. '/into(\s+)(?:dump|out)file\s*/i', // 禁用 MySQL 导出函数
  601. '/group\s+by.+\(/i', // 防止 SQL 注入
  602. '/(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(/i', // 禁用 webshell 相关某些函数
  603. '/(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/\//i', // 防止一些协议攻击(注意协议后的三个斜杠)
  604. '/\$_(GET|POST|COOKIE|FILES|SESSION|ENV|GLOBALS|SERVER)\[/i', // 禁用一些内置变量,注意 PHP 变量名通常是大写的
  605. '/<(iframe|script|body|img|layer|div|meta|style|base|object|input)/i', // 防止 XSS 标签植入
  606. '/(onmouseover|onerror|onload|onclick)\=/i', // 防止 XSS 事件植入
  607. '/\|\|.*?(?:ls|pwd|whoami|ll|ifconfig|ipconfig|&&|chmod|cd|mkdir|rmdir|cp|mv)/i', // 防止执行 shell(注意去掉了不合适的 ifconfog)
  608. '/\sand\s+.*=.*/i' // 匹配 and 1=1
  609. ];
  610. if (filter_var($str, FILTER_VALIDATE_URL)) {
  611. $url = parse_url($str);
  612. if (!isset($url['scheme'])) return $str;
  613. $host = $url['scheme'] . '://' . $url['host'];
  614. $str = $host . preg_replace($rules, '', str_replace($host, '', $str));
  615. } else {
  616. $str = preg_replace($rules, '', $str);
  617. }
  618. return $str;
  619. }
  620. }
  621. if (!function_exists('is_brokerage_statu')) {
  622. /**
  623. * 是否能成为推广人
  624. * @param float $price
  625. * @return bool
  626. */
  627. function is_brokerage_statu(float $price)
  628. {
  629. if (!sys_config('brokerage_func_status')) {
  630. return false;
  631. }
  632. $storeBrokerageStatus = sys_config('store_brokerage_statu', 1);
  633. if ($storeBrokerageStatus == 1) {
  634. return false;
  635. } else if ($storeBrokerageStatus == 2) {
  636. return false;
  637. } else {
  638. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  639. return $price >= $storeBrokeragePrice;
  640. }
  641. }
  642. }
  643. if (!function_exists('array_unique_fb')) {
  644. /**
  645. * 二维数组去掉重复值
  646. * @param $array
  647. * @return array
  648. */
  649. function array_unique_fb($array)
  650. {
  651. $out = array();
  652. foreach ($array as $key => $value) {
  653. if (!in_array($value, $out)) {
  654. $out[$key] = $value;
  655. }
  656. }
  657. $out = array_values($out);
  658. return $out;
  659. }
  660. }
  661. if (!function_exists('get_crmeb_version')) {
  662. /**
  663. * 获取CRMEB系统版本号
  664. * @param string $default
  665. * @return string
  666. */
  667. function get_crmeb_version($default = 'v1.0.0')
  668. {
  669. try {
  670. $version = parse_ini_file(app()->getRootPath() . '.version');
  671. return $version['version'] ?? $default;
  672. } catch (\Throwable $e) {
  673. return $default;
  674. }
  675. }
  676. }
  677. if (!function_exists('get_file_link')) {
  678. /**
  679. * 获取文件带域名的完整路径
  680. * @param string $link
  681. * @return string
  682. */
  683. function get_file_link(string $link)
  684. {
  685. if (!$link) {
  686. return '';
  687. }
  688. if (substr($link, 0, 4) === "http" || substr($link, 0, 2) === "//") {
  689. return $link;
  690. } else {
  691. return app()->request->domain() . $link;
  692. }
  693. }
  694. }
  695. if (!function_exists('tidy_tree')) {
  696. /**
  697. * 格式化分类
  698. * @param $menusList
  699. * @param int $pid
  700. * @param array $navList
  701. * @return array
  702. */
  703. function tidy_tree($menusList, $pid = 0, $navList = [])
  704. {
  705. foreach ($menusList as $k => $menu) {
  706. if ($menu['parent_id'] == $pid) {
  707. unset($menusList[$k]);
  708. $menu['children'] = tidy_tree($menusList, $menu['id']);
  709. if ($menu['children']) $menu['expand'] = true;
  710. $navList[] = $menu;
  711. }
  712. }
  713. return $navList;
  714. }
  715. }
  716. if (!function_exists('create_form')) {
  717. /**
  718. * 表单生成方法
  719. * @param string $title
  720. * @param array $field
  721. * @param $url
  722. * @param string $method
  723. * @return array
  724. * @throws \FormBuilder\Exception\FormBuilderException
  725. */
  726. function create_form(string $title, array $field, $url, string $method = 'POST')
  727. {
  728. $form = Form::createForm((string)$url);//提交地址
  729. $form->setMethod($method);//提交方式
  730. $form->setRule($field);//表单字段
  731. $form->setTitle($title);//表单标题
  732. $rules = $form->formRule();
  733. $title = $form->getTitle();
  734. $action = $form->getAction();
  735. $method = $form->getMethod();
  736. $info = '';
  737. $status = true;
  738. $methodData = ['POST', 'PUT', 'GET', 'DELETE'];
  739. if (!in_array(strtoupper($method), $methodData)) {
  740. throw new ValidateException('请求方式有误');
  741. }
  742. return compact('rules', 'title', 'action', 'method', 'info', 'status');
  743. }
  744. }
  745. if (!function_exists('msectime')) {
  746. /**
  747. * 获取毫秒数
  748. * @return float
  749. */
  750. function msectime()
  751. {
  752. list($msec, $sec) = explode(' ', microtime());
  753. return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  754. }
  755. }
  756. if (!function_exists('array_bc_sum')) {
  757. /**
  758. * 获取一维数组的总合高精度
  759. * @param array $data
  760. * @return string
  761. */
  762. function array_bc_sum(array $data)
  763. {
  764. $sum = '0';
  765. foreach ($data as $item) {
  766. $sum = bcadd($sum, (string)$item, 2);
  767. }
  768. return $sum;
  769. }
  770. }
  771. if (!function_exists('get_tree_children')) {
  772. /**
  773. * tree 子菜单
  774. * @param array $data 数据
  775. * @param string $childrenname 子数据名
  776. * @param string $keyName 数据key名
  777. * @param string $pidName 数据上级key名
  778. * @return array
  779. */
  780. function get_tree_children(array $data, string $childrenname = 'children', string $keyName = 'id', string $pidName = 'pid')
  781. {
  782. $list = array();
  783. foreach ($data as $value) {
  784. $list[$value[$keyName]] = $value;
  785. }
  786. $tree = array(); //格式化好的树
  787. foreach ($list as $item) {
  788. if (isset($list[$item[$pidName]])) {
  789. $list[$item[$pidName]][$childrenname][] = &$list[$item[$keyName]];
  790. } else {
  791. $tree[] = &$list[$item[$keyName]];
  792. }
  793. }
  794. return $tree;
  795. }
  796. }
  797. if (!function_exists('get_tree_children_value')) {
  798. function get_tree_children_value(array $data, $value, string $childrenname = 'children', string $keyName = 'id')
  799. {
  800. static $childrenValue = [];
  801. foreach ($data as $item) {
  802. $childrenData = $item[$childrenname] ?? [];
  803. if (count($childrenData)) {
  804. return get_tree_children_value($childrenData, $childrenname, $keyName);
  805. } else {
  806. if ($item[$keyName] == $value) {
  807. $childrenValue[] = $item['value'];
  808. }
  809. }
  810. }
  811. return $childrenValue;
  812. }
  813. }
  814. if (!function_exists('get_tree_value')) {
  815. /**
  816. * 获取
  817. * @param array $data
  818. * @param int|string $value
  819. * @return array
  820. */
  821. function get_tree_value(array $data, $value)
  822. {
  823. // static $childrenValue = [];
  824. // foreach ($data as &$item) {
  825. // if ($item['value'] == $value) {
  826. // $childrenValue[] = $item['value'];
  827. // if ($item['pid']) {
  828. // $value = $item['pid'];
  829. // unset($item);
  830. // return get_tree_value($data, $value);
  831. // }
  832. // }
  833. // }
  834. // return $childrenValue;
  835. $childrenValue = []; // 用于存储找到的子值的数组
  836. foreach ($data as $item) {
  837. if ($item['value'] == $value) { // 如果当前项的'value'键与给定值匹配
  838. $childrenValue[] = $item['value']; // 将当前值添加到子值数组中
  839. if ($item['pid']) { // 如果当前项有'pid'值,表示有父项
  840. // 递归调用get_tree_value函数,并将父项的'pid'值作为新的$value参数
  841. $childrenValue = array_merge($childrenValue, get_tree_value($data, $item['pid']));
  842. }
  843. }
  844. }
  845. return $childrenValue; // 返回包含所有子值的数组
  846. }
  847. }
  848. if (!function_exists('get_image_thumb')) {
  849. /**
  850. * 获取缩略图
  851. * @param $filePath
  852. * @param string $type all|big|mid|small
  853. * @param bool $is_remote_down
  854. * @return mixed|string|string[]
  855. */
  856. function get_image_thumb($filePath, string $type = 'all', bool $is_remote_down = false)
  857. {
  858. if (!$filePath || !is_string($filePath) || strpos($filePath, '?') !== false) return $filePath;
  859. try {
  860. $upload = UploadService::getOssInit($filePath, $is_remote_down);
  861. $fileArr = explode('/', $filePath);
  862. $data = $upload->thumb($filePath, end($fileArr), $type);
  863. $image = $type == 'all' ? $data : $data[$type] ?? $filePath;
  864. } catch (\Throwable $e) {
  865. $image = $filePath;
  866. }
  867. $data = parse_url($image);
  868. if (!isset($data['host']) && (substr($image, 0, 2) == './' || substr($image, 0, 1) == '/')) {//不是完整地址
  869. $image = sys_config('site_url') . $image;
  870. }
  871. //请求是https 图片是http 需要改变图片地址
  872. if (strpos(request()->domain(), 'https:') !== false && strpos($image, 'https:') === false) {
  873. $image = str_replace('http:', 'https:', $image);
  874. }
  875. return $image;
  876. }
  877. }
  878. if (!function_exists('get_thumb_water')) {
  879. /**
  880. * 处理数组获取缩略图、水印
  881. * @param $list
  882. * @param string $type
  883. * @param array|string[] $field 1、['image','images'] type 取值参数:type 2、['small'=>'image','mid'=>'images'] type 取field数组的key
  884. * @param bool $is_remote_down
  885. * @return array|mixed|string|string[]
  886. */
  887. function get_thumb_water($list, string $type = 'small', array $field = ['image'], bool $is_remote_down = false)
  888. {
  889. if (!$list || !$field) return $list;
  890. $baseType = $type;
  891. $data = $list;
  892. if (is_string($list)) {
  893. $field = [$type => 'image'];
  894. $data = ['image' => $list];
  895. }
  896. if (is_array($data)) {
  897. foreach ($field as $type => $key) {
  898. if (is_integer($type)) {//索引数组,默认type
  899. $type = $baseType;
  900. }
  901. //一维数组
  902. if (isset($data[$key])) {
  903. if (is_array($data[$key])) {
  904. $path_data = [];
  905. foreach ($data[$key] as $k => $path) {
  906. $path_data[] = get_image_thumb($path, $type, $is_remote_down);
  907. }
  908. $data[$key] = $path_data;
  909. } else {
  910. $data[$key] = get_image_thumb($data[$key], $type, $is_remote_down);
  911. }
  912. } else {
  913. foreach ($data as &$item) {
  914. if (!isset($item[$key]))
  915. continue;
  916. if (is_array($item[$key])) {
  917. $path_data = [];
  918. foreach ($item[$key] as $k => $path) {
  919. $path_data[] = get_image_thumb($path, $type, $is_remote_down);
  920. }
  921. $item[$key] = $path_data;
  922. } else {
  923. $item[$key] = get_image_thumb($item[$key], $type, $is_remote_down);
  924. }
  925. }
  926. }
  927. }
  928. }
  929. return is_string($list) ? ($data['image'] ?? '') : $data;
  930. }
  931. }
  932. if (!function_exists('getLang')) {
  933. /**
  934. * 多语言
  935. * @param $code
  936. * @param array $replace
  937. * @return array|string|string[]
  938. */
  939. function getLang($code, array $replace = [])
  940. {
  941. //确保获取语言的时候不会报错
  942. try {
  943. /** @var LangCountryServices $langCountryServices */
  944. $langCountryServices = app()->make(LangCountryServices::class);
  945. /** @var LangTypeServices $langTypeServices */
  946. $langTypeServices = app()->make(LangTypeServices::class);
  947. /** @var LangCodeServices $langCodeServices */
  948. $langCodeServices = app()->make(LangCodeServices::class);
  949. $request = app()->request;
  950. //获取接口传入的语言类型
  951. if (!$range = $request->header('cb-lang')) {
  952. //没有传入则使用系统默认语言显示
  953. $range = CacheService::remember('range_name', function () use ($langTypeServices) {
  954. return $langTypeServices->value(['is_default' => 1], 'file_name');
  955. });
  956. if (!$range) {
  957. //系统没有设置默认语言的话,根据浏览器语言显示,如果浏览器语言在库中找不到,则使用简体中文
  958. if ($request->header('accept-language') !== null) {
  959. $range = explode(',', $request->header('accept-language'))[0];
  960. } else {
  961. $range = 'zh-CN';
  962. }
  963. }
  964. }
  965. // 获取type_id
  966. $typeId = CacheService::remember('type_id_' . $range, function () use ($langCountryServices, $range) {
  967. return $langCountryServices->value(['code' => $range], 'type_id') ?: 1;
  968. }, 3600);
  969. // 获取类型
  970. $langData = CacheService::remember('lang_type_data', function () use ($langTypeServices) {
  971. return $langTypeServices->getColumn(['status' => 1, 'is_del' => 0], 'file_name', 'id');
  972. }, 3600);
  973. // 获取缓存key
  974. $langStr = 'lang_' . str_replace('-', '_', $langData[$typeId]);
  975. //读取当前语言的语言包
  976. $lang = CacheService::remember($langStr, function () use ($typeId, $range, $langCodeServices) {
  977. return $langCodeServices->getColumn(['type_id' => $typeId, 'is_admin' => 1], 'lang_explain', 'code');
  978. }, 3600);
  979. //获取返回文字
  980. $message = (string)($lang[$code] ?? 'Code Error');
  981. //替换变量
  982. if (!empty($replace) && is_array($replace)) {
  983. // 关联索引解析
  984. $key = array_keys($replace);
  985. foreach ($key as &$v) {
  986. $v = "{:{$v}}";
  987. }
  988. $message = str_replace($key, $replace, $message);
  989. }
  990. return $message;
  991. } catch (\Throwable $e) {
  992. Log::error('获取语言code:' . $code . '发成错误,错误原因是:' . json_encode([
  993. 'file' => $e->getFile(),
  994. 'message' => $e->getMessage(),
  995. 'line' => $e->getLine()
  996. ]));
  997. return $code;
  998. }
  999. }
  1000. }
  1001. if (!function_exists('aj_captcha_check_one')) {
  1002. /**
  1003. * 验证滑块1次验证
  1004. * @param string $token
  1005. * @param string $pointJson
  1006. * @return bool
  1007. */
  1008. function aj_captcha_check_one(string $captchaType, string $token, string $pointJson)
  1009. {
  1010. aj_get_serevice($captchaType)->check($token, $pointJson);
  1011. return true;
  1012. }
  1013. }
  1014. if (!function_exists('aj_captcha_check_two')) {
  1015. /**
  1016. * 验证滑块2次验证
  1017. * @param string $token
  1018. * @param string $pointJson
  1019. * @return bool
  1020. */
  1021. function aj_captcha_check_two(string $captchaType, string $captchaVerification)
  1022. {
  1023. aj_get_serevice($captchaType)->verificationByEncryptCode($captchaVerification);
  1024. return true;
  1025. }
  1026. }
  1027. if (!function_exists('aj_captcha_create')) {
  1028. /**
  1029. * 创建验证码
  1030. * @return array
  1031. */
  1032. function aj_captcha_create(string $captchaType)
  1033. {
  1034. return aj_get_serevice($captchaType)->get();
  1035. }
  1036. }
  1037. if (!function_exists('aj_get_serevice')) {
  1038. /**
  1039. * @param string $captchaType
  1040. * @return ClickWordCaptchaService|BlockPuzzleCaptchaService
  1041. */
  1042. function aj_get_serevice(string $captchaType)
  1043. {
  1044. $config = Config::get('ajcaptcha');
  1045. switch ($captchaType) {
  1046. case "clickWord":
  1047. $service = new ClickWordCaptchaService($config);
  1048. break;
  1049. case "blockPuzzle":
  1050. $service = new BlockPuzzleCaptchaService($config);
  1051. break;
  1052. default:
  1053. throw new ValidateException('captchaType参数不正确!');
  1054. }
  1055. return $service;
  1056. }
  1057. }
  1058. if (!function_exists('out_push')) {
  1059. /**
  1060. * 默认数据推送
  1061. * @param string $pushUrl
  1062. * @param array $data
  1063. * @param string $tip
  1064. * @return bool
  1065. */
  1066. function out_push(string $pushUrl, array $data, string $tip = ''): bool
  1067. {
  1068. $param = json_encode($data, JSON_UNESCAPED_UNICODE);
  1069. $res = HttpService::postRequest($pushUrl, $param, ['Content-Type:application/json', 'Content-Length:' . strlen($param)]);
  1070. $res = $res ? json_decode($res, true) : [];
  1071. if (!$res || !isset($res['code']) || $res['code'] != 0) {
  1072. \think\facade\Log::error(['msg' => $tip . '推送失败', 'data' => $res]);
  1073. return false;
  1074. }
  1075. return true;
  1076. }
  1077. }
  1078. if (!function_exists('dump_sql')) {
  1079. /**
  1080. * 打印sql
  1081. * @param string $pushUrl
  1082. * @param array $data
  1083. * @param string $tip
  1084. * @return bool
  1085. */
  1086. function dump_sql()
  1087. {
  1088. Db::listen(function ($sql) {
  1089. var_dump($sql);
  1090. });
  1091. }
  1092. }
  1093. if (!function_exists('stringToIntArray')) {
  1094. /**
  1095. * 处理ids等并过滤参数
  1096. * @param string $string
  1097. * @param string $separator
  1098. * @return array
  1099. */
  1100. function stringToIntArray(string $string, string $separator = ',')
  1101. {
  1102. return !empty($string) ? array_unique(array_diff(array_map('intval', explode($separator, $string)), [0])) : [];
  1103. }
  1104. }