safe.func.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function safe_gpc_int($value, $default = 0) {
  8. if (false !== strpos($value, '.')) {
  9. $value = floatval($value);
  10. $default = floatval($default);
  11. } else {
  12. $value = intval($value);
  13. $default = intval($default);
  14. }
  15. if (empty($value) && $default != $value) {
  16. $value = $default;
  17. }
  18. return $value;
  19. }
  20. function safe_gpc_belong($value, $allow = array(), $default = '') {
  21. if (empty($allow)) {
  22. return $default;
  23. }
  24. if (in_array($value, $allow, true)) {
  25. return $value;
  26. } else {
  27. return $default;
  28. }
  29. }
  30. function safe_rule_alpha($value) {
  31. return is_scalar($value) && 1 === preg_match('/^[A-Za-z]+$/', (string)$value);
  32. }
  33. function safe_rule_alpha_num($value) {
  34. return is_scalar($value) && 1 === preg_match('/^[A-Za-z0-9]+$/', (string)$value);
  35. }
  36. function safe_rule_alpha_dash($value) {
  37. return is_scalar($value) && 1 === preg_match('/^[A-Za-z0-9\-_]+$/', (string)$value);
  38. }
  39. function safe_rule_chs_alpha_dash($value) {
  40. return is_scalar($value) && 1 === preg_match('/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\(\)()\/\-\_\.\s\*,,。;;—::#!!@$+~=]+$/u', (string)$value);
  41. }
  42. function safe_rule_url($value) {
  43. return is_scalar($value) && 1 === preg_match('/^((ht|f)tps?):\/\/([\w-]+(\.[\w-]+)*\/?)+([\x{4e00}-\x{9fa5}\w\-\.,@?^=;%&:\/~\+#\[\]]*)?$/u', (string)$value);
  44. }
  45. function safe_rule_path($value) {
  46. return is_scalar($value) && 1 === preg_match('/^(\.\/|\/|[a-zA-Z0-9])[a-zA-Z0-9\(\)()\/\-\_\.\s\*,,。;;—::#!!@$+~~=]+$/', (string)$value);
  47. }
  48. function safe_rule_mobile($value) {
  49. return is_scalar($value) && 1 === preg_match(REGULAR_MOBILE, (string)$value);
  50. }
  51. function safe_rule_color($value) {
  52. return is_scalar($value) && 1 === preg_match('/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', (string)$value);
  53. }
  54. function safe_gpc_string($value, $default = '', $rule = '') {
  55. if (empty($value)) {
  56. return $value;
  57. }
  58. $rule = in_array($rule, array('path', 'url', 'mobile', 'alpha', 'alpha_num', 'alpha_dash', 'color', 'chs_alpha_dash')) ? $rule : 'chs_alpha_dash';
  59. switch ($rule) {
  60. case 'path':
  61. $value = safe_rule_path($value) ? $value : $default;
  62. break;
  63. case 'url':
  64. $value = safe_rule_url($value) ? $value : $default;
  65. break;
  66. case 'mobile':
  67. $value = safe_rule_mobile($value) ? $value : $default;
  68. break;
  69. case 'alpha':
  70. $value = safe_rule_alpha($value) ? $value : $default;
  71. break;
  72. case 'alpha_num':
  73. $value = safe_rule_alpha_num($value) ? $value : $default;
  74. break;
  75. case 'alpha_dash':
  76. $value = safe_rule_alpha_dash($value) ? $value : $default;
  77. break;
  78. case 'color':
  79. $value = safe_rule_color($value) ? $value : $default;
  80. break;
  81. case 'chs_alpha_dash':
  82. $value = safe_rule_chs_alpha_dash($value) ? $value : $default;
  83. break;
  84. }
  85. return $value;
  86. }
  87. function safe_gpc_path($value, $default = '') {
  88. if (starts_with($value, 'http') || starts_with($value, 'ftp')) {
  89. $path = safe_gpc_string($value, '', 'url');
  90. } else {
  91. $path = safe_gpc_string($value, '', 'path');
  92. }
  93. $path = str_replace(array('..', '..\\', '\\\\', '\\', '..\\\\'), '', $path);
  94. if (empty($path) || $path != $value) {
  95. $path = $default;
  96. }
  97. return $path;
  98. }
  99. function safe_gpc_array($value, $default = array()) {
  100. if (empty($value) || !is_array($value)) {
  101. return $default;
  102. }
  103. foreach ($value as &$row) {
  104. if (is_array($row)) {
  105. $row = safe_gpc_array($row, $default);
  106. } elseif (0 === strpos($row, 'http') || 0 === strpos($row, './')) {
  107. $row = safe_gpc_url($row, false);
  108. } else {
  109. $row = safe_gpc_string($row);
  110. }
  111. }
  112. return $value;
  113. }
  114. function safe_gpc_boolean($value) {
  115. return boolval($value);
  116. }
  117. function safe_gpc_html($value, $default = '') {
  118. if (empty($value) || !is_string($value)) {
  119. return $default;
  120. }
  121. $value = safe_bad_str_replace($value);
  122. $value = safe_remove_xss($value);
  123. if (empty($value) && $value != $default) {
  124. $value = $default;
  125. }
  126. return $value;
  127. }
  128. function safe_gpc_sql($value, $operator = 'ENCODE', $default = '') {
  129. if (empty($value) || !is_string($value)) {
  130. return $default;
  131. }
  132. $value = trim(strtolower($value));
  133. $badstr = array(
  134. '_', '%', "'", chr(39),
  135. 'select', 'join', 'union',
  136. 'where', 'insert', 'delete',
  137. 'update', 'like', 'drop',
  138. 'create', 'modify', 'rename',
  139. 'alter', 'cast',
  140. );
  141. $newstr = array(
  142. '\_', '\%', "''", '&#39;',
  143. 'sel&#101;ct"', 'jo&#105;n', 'un&#105;on',
  144. 'wh&#101;re', 'ins&#101;rt', 'del&#101;te',
  145. 'up&#100;ate', 'lik&#101;', 'dro&#112',
  146. 'cr&#101;ate', 'mod&#105;fy', 'ren&#097;me"',
  147. 'alt&#101;r', 'ca&#115;',
  148. );
  149. if ('ENCODE' == $operator) {
  150. $value = str_replace($badstr, $newstr, $value);
  151. } else {
  152. $value = str_replace($newstr, $badstr, $value);
  153. }
  154. return $value;
  155. }
  156. function safe_gpc_url($value, $strict_domain = true, $default = '') {
  157. global $_W;
  158. if (empty($value) || !is_string($value)) {
  159. return $default;
  160. }
  161. $value = urldecode($value);
  162. if (starts_with($value, './')) {
  163. return $value;
  164. }
  165. $value = safe_gpc_string($value, '', 'url');
  166. if ($strict_domain) {
  167. if (starts_with($value, array($_W['siteroot'], 'https://cdn.w7.cc', 'http://cdn.w7.cc', 'https://console.w7.cc', 'http://console.w7.cc', 'https://api.w7.cc', 'http://api.w7.cc', 'https://passport.w7.cc', 'http://passport.w7.cc'))) {
  168. return $value;
  169. }
  170. return $default;
  171. }
  172. if (starts_with($value, 'http') || starts_with($value, '//')) {
  173. return $value;
  174. }
  175. return $default;
  176. }
  177. function safe_remove_xss($val) {
  178. $val = preg_replace('/([\x0e-\x19])/', '', $val);
  179. $search = 'abcdefghijklmnopqrstuvwxyz';
  180. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  181. $search .= '1234567890!@#$%^&*()';
  182. $search .= '~`";:?+/={}[]-_|\'\\';
  183. for ($i = 0; $i < strlen($search); ++$i) {
  184. $val = preg_replace('/(&#[xX]0{0,8}' . dechex(ord($search[$i])) . ';?)/i', $search[$i], $val);
  185. $val = preg_replace('/(&#0{0,8}' . ord($search[$i]) . ';?)/', $search[$i], $val);
  186. }
  187. preg_match_all('/href=[\'|\"](.*?)[\'|\"]|src=[\'|\"](.*?)[\'|\"]/i', $val, $matches);
  188. $url_list = array_merge($matches[1], $matches[2]);
  189. $encode_url_list = array();
  190. if (!empty($url_list)) {
  191. foreach ($url_list as $key => $url) {
  192. $val = str_replace($url, 'we7_' . $key . '_we7placeholder', $val);
  193. $encode_url_list[] = $url;
  194. }
  195. }
  196. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'frameset', 'ilayer', 'bgsound', 'base');
  197. $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload', '@import');
  198. $ra = array_merge($ra1, $ra2);
  199. $found = true;
  200. while (true == $found) {
  201. $val_before = $val;
  202. for ($i = 0; $i < sizeof($ra); ++$i) {
  203. $pattern = '/';
  204. for ($j = 0; $j < strlen($ra[$i]); ++$j) {
  205. if ($j > 0) {
  206. $pattern .= '(';
  207. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  208. $pattern .= '|';
  209. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  210. $pattern .= ')*';
  211. }
  212. $pattern .= $ra[$i][$j];
  213. }
  214. $pattern .= '/i';
  215. $replacement = substr($ra[$i], 0, 2) . '<x>' . substr($ra[$i], 2);
  216. $val = preg_replace($pattern, $replacement, $val);
  217. if ($val_before == $val) {
  218. $found = false;
  219. }
  220. }
  221. }
  222. if (!empty($encode_url_list) && is_array($encode_url_list)) {
  223. foreach ($encode_url_list as $key => $url) {
  224. $val = str_replace('we7_' . $key . '_we7placeholder', $url, $val);
  225. }
  226. }
  227. return $val;
  228. }
  229. function safe_bad_str_replace($string) {
  230. if (empty($string)) {
  231. return '';
  232. }
  233. $badstr = array("\0", '%00', '%3C', '%3E', '<?', '<%', '<?php', '{php', '{if', '{loop', '../', '%0D%0A');
  234. $newstr = array('_', '_', '&lt;', '&gt;', '_', '_', '_', '_', '_', '_', '.._', '_');
  235. $string = str_replace($badstr, $newstr, $string);
  236. return $string;
  237. }
  238. function safe_check_password($password) {
  239. global $_W;
  240. if (empty($_W['setting']['register']['safe'])) {
  241. $regex = '/[a-zA-Z0-9~!@#\$\^\&\*\(\)]{6,16}/';
  242. $message = '密码为6-16个字符,只允许大写字母、小写字母、数字及特殊字符:~!@#$^&*()';
  243. } else {
  244. $regex = '/(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9~!@#\$\^\&\*\(\)]{8,16}/';
  245. $message = '密码为8-16个字符,至少1个大写字母、1个小写字母和1个数字,可包含特殊字符:~!@#$^&*(),其他字符不被允许';
  246. }
  247. preg_match($regex, $password, $out);
  248. if (empty($out)) {
  249. return error(-1, $message);
  250. } else {
  251. if ($out[0] != $password) {
  252. return error(-1, $message);
  253. }
  254. return $password;
  255. }
  256. }