Request.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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'];
  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. file_put_contents(__DIR__ . '/../debug_replaceWord.log', "Input: " . var_export($str, true) . "\n", FILE_APPEND);
  112. $url = parse_url($str);
  113. file_put_contents(__DIR__ . '/../debug_replaceWord.log', "parse_url: " . var_export($url, true) . "\n", FILE_APPEND);
  114. if ($url && isset($url['host']) && $url['host'] != '') {
  115. if (!isset($url['scheme'])) {
  116. $url['scheme'] = 'http';
  117. }
  118. $path = isset($url['path']) ? $url['path'] : '';
  119. $str = $url['scheme'] . '://' . $url['host'] . preg_replace($farr, '', $path);
  120. } else {
  121. $str = preg_replace($farr, '', $str);
  122. }
  123. file_put_contents(__DIR__ . '/../debug_replaceWord.log', "Output: " . var_export($str, true) . "\n\n", FILE_APPEND);
  124. return $str;
  125. }
  126. /**
  127. * 获取get参数
  128. * @param array $params
  129. * @param bool $suffix
  130. * @param bool $filter
  131. * @return array
  132. */
  133. public function getMore(array $params, bool $suffix = false, bool $filter = true): array
  134. {
  135. return $this->more($params, $suffix, $filter);
  136. }
  137. /**
  138. * 获取post参数
  139. * @param array $params
  140. * @param bool $suffix
  141. * @param bool $filter
  142. * @return array
  143. */
  144. public function postMore(array $params, bool $suffix = false, bool $filter = true): array
  145. {
  146. return $this->more($params, $suffix, $filter);
  147. }
  148. /**
  149. * 获取用户访问端
  150. * @return array|string|null
  151. */
  152. public function getFromType()
  153. {
  154. return $this->header('Form-type', '');
  155. }
  156. /**
  157. * 当前访问端
  158. * @param string $terminal
  159. * @return bool
  160. */
  161. public function isTerminal(string $terminal)
  162. {
  163. return strtolower($this->getFromType()) === $terminal;
  164. }
  165. /**
  166. * 是否是H5端
  167. * @return bool
  168. */
  169. public function isH5()
  170. {
  171. return $this->isTerminal('h5');
  172. }
  173. /**
  174. * 是否是微信端
  175. * @return bool
  176. */
  177. public function isWechat()
  178. {
  179. return $this->isTerminal('wechat');
  180. }
  181. /**
  182. * 是否是小程序端
  183. * @return bool
  184. */
  185. public function isRoutine()
  186. {
  187. return $this->isTerminal('routine');
  188. }
  189. /**
  190. * 是否是app端
  191. * @return bool
  192. */
  193. public function isApp()
  194. {
  195. return $this->isTerminal('app');
  196. }
  197. /**
  198. * 是否是app端
  199. * @return bool
  200. */
  201. public function isPc()
  202. {
  203. return $this->isTerminal('pc');
  204. }
  205. }