common.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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('filterEmoji')) {
  29. // 过滤掉emoji表情
  30. function filterEmoji($str)
  31. {
  32. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  33. '/./u',
  34. function (array $match) {
  35. return strlen($match[0]) >= 4 ? '' : $match[0];
  36. },
  37. $str);
  38. return $str;
  39. }
  40. }
  41. if (!function_exists('strReplace')) {
  42. /**
  43. * @param string $string 需要替换的字符串
  44. * @param int $start 开始的保留几位
  45. * @param int $end 最后保留几位
  46. * @return string
  47. */
  48. function strReplace($string, $start, $end)
  49. {
  50. $strlen = mb_strlen($string, 'UTF-8');//获取字符串长度
  51. $firstStr = mb_substr($string, 0, $start, 'UTF-8');//获取第一位
  52. $lastStr = mb_substr($string, -1, $end, 'UTF-8');//获取最后一位
  53. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($string, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  54. }
  55. }
  56. if (!function_exists('sensitive_words_filter')) {
  57. /**
  58. * 敏感词过滤
  59. *
  60. * @param string
  61. * @return string
  62. */
  63. function sensitive_words_filter($str)
  64. {
  65. if (!$str) return '';
  66. $file = app()->getAppPath() . 'public/static/plug/censorwords/CensorWords';
  67. $words = file($file);
  68. foreach ($words as $word) {
  69. $word = str_replace(array("\r\n", "\r", "\n", "/", "<", ">", "=", " "), '', $word);
  70. if (!$word) continue;
  71. $ret = preg_match("/$word/", $str, $match);
  72. if ($ret) {
  73. return $match[0];
  74. }
  75. }
  76. return '';
  77. }
  78. }
  79. if (!function_exists('make_path')) {
  80. /**
  81. * 上传路径转化,默认路径
  82. * @param $path
  83. * @param int $type
  84. * @param bool $force
  85. * @return string
  86. */
  87. function make_path($path, int $type = 2, bool $force = false)
  88. {
  89. $path = DS.ltrim(rtrim($path));
  90. switch ($type){
  91. case 1:
  92. $path .= DS.date('Y');
  93. break;
  94. case 2:
  95. $path .= DS.date('Y').DS.date('m');
  96. break;
  97. case 3:
  98. $path .= DS.date('Y').DS.date('m').DS.date('d');
  99. break;
  100. }
  101. try{
  102. if (is_dir(app()->getRootPath().'public'.DS.'uploads'.$path) == true || mkdir(app()->getRootPath().'public'.DS.'uploads'.$path, 0777, true) == true) {
  103. return trim(str_replace(DS, '/',$path),'.');
  104. }else return '';
  105. }catch (\Exception $e){
  106. if($force)
  107. throw new \Exception($e->getMessage());
  108. return '无法创建文件夹,请检查您的上传目录权限:' . app()->getRootPath() . 'public' . DS . 'uploads' . DS. 'attach' . DS;
  109. }
  110. }
  111. }
  112. if(!function_exists('sysConfig'))
  113. {
  114. /**
  115. * 获取系统单个配置
  116. * @param string $name
  117. * @return string | null
  118. */
  119. function sysConfig(string $name)
  120. {
  121. if(empty($name))
  122. return null;
  123. return app('sysConfig')->get($name);
  124. }
  125. }
  126. if(!function_exists('sysData'))
  127. {
  128. /**
  129. * 获取系统单个配置
  130. * @param string $name
  131. * @return string
  132. */
  133. function sysData($name)
  134. {
  135. return app('sysGroupData')->getData($name);
  136. }
  137. }