PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/system/Filter.php

https://github.com/monkeycraps/swoole_framework
PHP | 228 lines | 165 code | 8 blank | 55 comment | 23 complexity | 0f4fa270c903cf6be0dd3d5d02cf635f MD5 | raw file
  1. <?php
  2. namespace Swoole;
  3. /**
  4. * 过滤类
  5. * 用于过滤过外部输入的数据,过滤数组或者变量中的不安全字符,以及HTML标签
  6. * @author Tianfeng.Han
  7. * @package SwooleSystem
  8. * @subpackage request_filter
  9. */
  10. class Filter
  11. {
  12. static $error_url;
  13. static $magic_quotes_gpc;
  14. public $mode;
  15. function __construct($mode='deny',$error_url=false)
  16. {
  17. $this->mode = $mode;
  18. self::$error_url = $error_url;
  19. }
  20. function post($param)
  21. {
  22. $this->_check($_POST,$param);
  23. }
  24. function get($param)
  25. {
  26. $this->_check($_GET,$param);
  27. }
  28. function cookie($param)
  29. {
  30. $this->_check($_COOKIE,$param);
  31. }
  32. /**
  33. * 根据提供的参数对数据进行检查
  34. * @param $data
  35. * @param $param
  36. * @return unknown_type
  37. */
  38. function _check(&$data,$param)
  39. {
  40. foreach($param as $k=>$p)
  41. {
  42. if(!isset($data[$k]))
  43. {
  44. if(isset($p['require']) and $p['require']) self::raise('param require');
  45. else continue;
  46. }
  47. if(isset($p['type']))
  48. {
  49. $data[$k] = Validate::$p['type']($data[$k]);
  50. if($data[$k]===false) self::raise();
  51. //最小值参数
  52. if(isset($p['min']) and is_numeric($data[$k]) and $data[$k]<$p['min']) self::raise('num too small');
  53. //最大值参数
  54. if(isset($p['max']) and is_numeric($data[$k]) and $data[$k]>$p['max']) self::raise('num too big');
  55. //最小值参数
  56. if(isset($p['short']) and is_string($data[$k]) and mb_strlen($data[$k])<$p['short']) self::raise('string too short');
  57. //最大值参数
  58. if(isset($p['long']) and is_string($data[$k]) and mb_strlen($data[$k])>$p['long']) self::raise('string too long');
  59. //自定义的正则表达式
  60. if($p['type']=='regx' and isset($p['regx']) and preg_match($p['regx'],$data[$k])===false) self::raise();
  61. }
  62. }
  63. //如果为拒绝模式,所有不在过滤参数$param中的键值都将被删除
  64. if($this->mode=='deny')
  65. {
  66. $allow = array_keys($param);
  67. $have = array_keys($data);
  68. foreach($have as $ha) if(!in_array($ha,$allow)) unset($data[$ha]);
  69. }
  70. }
  71. static function raise($text=false)
  72. {
  73. if(self::$error_url) Swoole_client::redirect(self::$error_url);
  74. if($text) exit($text);
  75. else exit('Web input param error!');
  76. }
  77. /**
  78. * 过滤$_GET $_POST $_REQUEST $_COOKIE
  79. * @return unknown_type
  80. */
  81. static function request()
  82. {
  83. $_POST = Filter::filter_array($_POST);
  84. $_GET = Filter::filter_array($_GET);
  85. $_REQUEST = Filter::filter_array($_REQUEST);
  86. $_COOKIE = Filter::filter_array($_COOKIE);
  87. }
  88. static function safe(&$content)
  89. {
  90. $content = stripslashes($content);
  91. $content = html_entity_decode($content, ENT_QUOTES, \Swoole::$charset);
  92. }
  93. public static function filter_var($var,$type)
  94. {
  95. switch($type)
  96. {
  97. case 'int':
  98. return intval($var);
  99. case 'string':
  100. return htmlspecialchars(strval($var),ENT_QUOTES);
  101. case 'float':
  102. return floatval($var);
  103. default:
  104. return false;
  105. }
  106. }
  107. /**
  108. * 过滤数组
  109. * @param $array
  110. * @return unknown_type
  111. */
  112. public static function filter_array($array)
  113. {
  114. if(!is_array($array))
  115. {
  116. return false;
  117. }
  118. $clean = array();
  119. foreach($array as $key=>$string)
  120. {
  121. if(is_array($string))
  122. {
  123. self::filter_array($string);
  124. }
  125. else
  126. {
  127. if(self::$magic_quotes_gpc and DBCHARSET=='gbk')
  128. {
  129. $string = stripslashes($string);
  130. }
  131. else
  132. {
  133. $string = self::escape($string);
  134. $key = self::escape($key);
  135. }
  136. }
  137. $clean[$key] = $string;
  138. }
  139. return $clean;
  140. }
  141. /**
  142. * 使输入的代码安全
  143. * @param $string
  144. * @return unknown_type
  145. */
  146. public static function escape($string)
  147. {
  148. if(is_numeric($string)) return $string;
  149. $string = htmlspecialchars($string,ENT_QUOTES,\Swoole::$charset);
  150. if(\Swoole::$charset=='gbk') self::gbk_addslash($string);
  151. else self::addslash($string);
  152. return $string;
  153. }
  154. /**
  155. * 移除HTML中的危险代码,如iframe和script
  156. * @param $val
  157. * @return unknown_type
  158. */
  159. public static function remove_xss($content,$allow='')
  160. {
  161. $danger = 'javascript,vbscript,expression,applet,meta,xml,blink,link,style,script,embed,object,iframe,frame,frameset,ilayer,layer,bgsound,title,base';
  162. $event = 'onabort|onactivate|onafterprint|onafterupdate|onbeforeactivate|onbeforecopy|onbeforecut|onbeforedeactivate|onbeforeeditfocus|'.
  163. 'onbeforepaste|onbeforeprint|onbeforeunload|onbeforeupdate|onblur|onbounce|oncellchange|onchange|onclick|oncontextmenu|oncontrolselect|'.
  164. 'oncopy|oncut|ondataavailable|ondatasetchanged|ondatasetcomplete|ondblclick|ondeactivate|ondrag|ondragend|ondragenter|ondragleave|'.
  165. 'ondragover|ondragstart|ondrop|onerror|onerrorupdate|onfilterchange|onfinish|onfocus|onfocusin|onfocusout|onhelp|onkeydown|onkeypress|'.
  166. 'onkeyup|onlayoutcomplete|onload|onlosecapture|onmousedown|onmouseenter|onmouseleave|onmousemove|onmouseout|onmouseover|onmouseup|'.
  167. 'onmousewheel|onmove|onmoveend|onmovestart|onpaste|onpropertychange|onreadystatechange|onreset|onresize|onresizeend|onresizestart|'.
  168. 'onrowenter|onrowexit|onrowsdelete|onrowsinserted|onscroll|onselect|onselectionchange|onselectstart|onstart|onstop|onsubmit|onunload';
  169. if(!empty($allow))
  170. {
  171. $allows = explode(',',$allow);
  172. $danger = str_replace($allow,'',$danger);
  173. }
  174. $danger = str_replace(',','|',$danger);
  175. //替换所有危险标签
  176. $content = preg_replace("/<\s*($danger)[^>]*>[^<]*(<\s*\/\s*\\1\s*>)?/is",'',$content);
  177. //替换所有危险的JS事件
  178. $content = preg_replace("/<([^>]*)($event)\s*\=([^>]*)>/is","<\\1 \\3>",$content);
  179. return $content;
  180. }
  181. /**
  182. * 过滤危险字符
  183. * @param $string
  184. * @return unknown_type
  185. */
  186. public static function addslash(&$string)
  187. {
  188. $string = addslashes($string);
  189. }
  190. /**
  191. * 过滤危险字符,解决GBK漏洞
  192. * @param $string
  193. * @return unknown_type
  194. */
  195. public static function gbk_addslash(&$string)
  196. {
  197. while(true)
  198. {
  199. $i = mb_strpos($text, chr(92),0,"GBK");
  200. if ($i === false) break;
  201. $T = mb_substr($text, 0, $i, "GBK") . chr(92) . chr(92);
  202. $text = substr($text, strlen($T) - 1);
  203. $OK .= $T;
  204. }
  205. $text = $OK . $text;
  206. $text = str_replace(chr(39), chr(92) . chr(39), $text);
  207. $text = str_replace(chr(34), chr(92) . chr(34), $text);
  208. $string = $text;
  209. }
  210. /**
  211. * 移除反斜杠过滤
  212. * @param $string
  213. * @return unknown_type
  214. */
  215. public static function deslash(&$string)
  216. {
  217. $string = stripslashes($string);
  218. }
  219. }
  220. Filter::$magic_quotes_gpc = get_magic_quotes_gpc();