Request.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. namespace app;
  12. use Spatie\Macroable\Macroable;
  13. /**
  14. * Class Request
  15. * @package app
  16. * @method tokenData() 获取token信息
  17. * @method user(string $key = null) 获取用户信息
  18. * @method uid() 获取用户uid
  19. * @method isAdminLogin() 后台登陆状态
  20. * @method adminId() 后台管理员id
  21. * @method adminInfo() 后台管理信息
  22. * @method kefuId() 客服id
  23. * @method kefuInfo() 客服信息
  24. */
  25. class Request extends \think\Request
  26. {
  27. use Macroable;
  28. /**
  29. * 不过滤变量名
  30. * @var array
  31. */
  32. protected $except = ['menu_path', 'api_url', 'unique_auth',
  33. 'description', 'custom_form', 'content', 'tableField', 'url'];
  34. /**
  35. * 获取请求的数据
  36. * @param array $params
  37. * @param bool $suffix
  38. * @param bool $filter
  39. * @return array
  40. */
  41. public function more(array $params, bool $suffix = false, bool $filter = true): array
  42. {
  43. $p = [];
  44. $i = 0;
  45. foreach ($params as $param) {
  46. if (!is_array($param)) {
  47. $p[$suffix == true ? $i++ : $param] = $this->filterWord(is_string($this->param($param)) ? trim($this->param($param)) : $this->param($param), $filter && !in_array($param, $this->except));
  48. } else {
  49. if (!isset($param[1])) $param[1] = null;
  50. if (!isset($param[2])) $param[2] = '';
  51. if (is_array($param[0])) {
  52. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  53. $keyName = $param[0][0];
  54. } else {
  55. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  56. $keyName = $param[0];
  57. }
  58. $p[$suffix == true ? $i++ : ($param[3] ?? $keyName)] = $this->filterWord(
  59. is_string($this->param($name, $param[1], $param[2])) ?
  60. trim($this->param($name, $param[1], $param[2])) :
  61. $this->param($name, $param[1], $param[2]),
  62. $filter && !in_array($keyName, $this->except));
  63. }
  64. }
  65. return $p;
  66. }
  67. /**
  68. * 过滤接受的参数
  69. * @param $str
  70. * @param bool $filter
  71. * @return array|mixed|string|string[]
  72. */
  73. public function filterWord($str, bool $filter = true)
  74. {
  75. if (!$str || !$filter) return $str;
  76. // 把数据过滤
  77. $farr = [
  78. "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
  79. "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
  80. '/phar/is',
  81. "/select|join|where|drop|like|modify|rename|insert|update|table|database|alter|truncate|\'|\/\*|\.\.\/|\.\/|union|into|load_file|outfile/is"
  82. ];
  83. if (is_array($str)) {
  84. foreach ($str as &$v) {
  85. if (is_array($v)) {
  86. foreach ($v as &$vv) {
  87. if (!is_array($vv)) {
  88. $vv = $this->replaceWord($farr, $vv);
  89. }
  90. }
  91. } else {
  92. $v = $this->replaceWord($farr, $v);
  93. }
  94. }
  95. } else {
  96. $str = $this->replaceWord($farr, $str);
  97. }
  98. return $str;
  99. }
  100. /**
  101. * 替换
  102. * @param $farr
  103. * @param $str
  104. * @return array|string|string[]|null
  105. * @author: 吴汐
  106. * @email: 442384644@qq.com
  107. * @date: 2023/9/19
  108. */
  109. public function replaceWord($farr, $str)
  110. {
  111. if (filter_var($str, FILTER_VALIDATE_URL)) {
  112. $url = parse_url($str);
  113. $host = $url['scheme'] . '://' . $url['host'];
  114. $str = $host . preg_replace($farr, '', str_replace($host, '', $str));
  115. } else {
  116. $str = preg_replace($farr, '', $str);
  117. }
  118. return $str;
  119. }
  120. /**
  121. * 获取get参数
  122. * @param array $params
  123. * @param bool $suffix
  124. * @param bool $filter
  125. * @return array
  126. */
  127. public function getMore(array $params, bool $suffix = false, bool $filter = true): array
  128. {
  129. return $this->more($params, $suffix, $filter);
  130. }
  131. /**
  132. * 获取post参数
  133. * @param array $params
  134. * @param bool $suffix
  135. * @param bool $filter
  136. * @return array
  137. */
  138. public function postMore(array $params, bool $suffix = false, bool $filter = true): array
  139. {
  140. return $this->more($params, $suffix, $filter);
  141. }
  142. /**
  143. * 获取用户访问端
  144. * @return array|string|null
  145. */
  146. public function getFromType()
  147. {
  148. return $this->header('Form-type', '');
  149. }
  150. /**
  151. * 当前访问端
  152. * @param string $terminal
  153. * @return bool
  154. */
  155. public function isTerminal(string $terminal)
  156. {
  157. return strtolower($this->getFromType()) === $terminal;
  158. }
  159. /**
  160. * 是否是H5端
  161. * @return bool
  162. */
  163. public function isH5()
  164. {
  165. return $this->isTerminal('h5');
  166. }
  167. /**
  168. * 是否是微信端
  169. * @return bool
  170. */
  171. public function isWechat()
  172. {
  173. return $this->isTerminal('wechat');
  174. }
  175. /**
  176. * 是否是小程序端
  177. * @return bool
  178. */
  179. public function isRoutine()
  180. {
  181. return $this->isTerminal('routine');
  182. }
  183. /**
  184. * 是否是app端
  185. * @return bool
  186. */
  187. public function isApp()
  188. {
  189. return $this->isTerminal('app');
  190. }
  191. /**
  192. * 是否是app端
  193. * @return bool
  194. */
  195. public function isPc()
  196. {
  197. return $this->isTerminal('pc');
  198. }
  199. }