safe.func.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 (strpos($value, '.') !== false) {
  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_gpc_string($value, $default = '') {
  31. $value = safe_bad_str_replace($value);
  32. $value = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', '&\\1', $value);
  33. if (empty($value) && $default != $value) {
  34. $value = $default;
  35. }
  36. return $value;
  37. }
  38. function safe_gpc_path($value, $default = '') {
  39. $path = safe_gpc_string($value);
  40. $path = str_replace(array('..', '..\\', '\\\\' ,'\\', '..\\\\'), '', $path);
  41. if (empty($path) || $path != $value) {
  42. $path = $default;
  43. }
  44. return $path;
  45. }
  46. function safe_gpc_array($value, $default = array()) {
  47. if (empty($value) || !is_array($value)) {
  48. return $default;
  49. }
  50. foreach ($value as &$row) {
  51. if (is_numeric($row)) {
  52. $row = safe_gpc_int($row);
  53. } elseif (is_array($row)) {
  54. $row = safe_gpc_array($row, $default);
  55. } else {
  56. $row = safe_gpc_string($row);
  57. }
  58. }
  59. return $value;
  60. }
  61. function safe_gpc_boolean($value) {
  62. return boolval($value);
  63. }
  64. function safe_gpc_html($value, $default = '') {
  65. if (empty($value) || !is_string($value)) {
  66. return $default;
  67. }
  68. $value = safe_bad_str_replace($value);
  69. $value = safe_remove_xss($value);
  70. if (empty($value) && $value != $default) {
  71. $value = $default;
  72. }
  73. return $value;
  74. }
  75. function safe_gpc_sql($value, $operator = 'ENCODE', $default = '') {
  76. if (empty($value) || !is_string($value)) {
  77. return $default;
  78. }
  79. $value = trim(strtolower($value));
  80. $badstr = array(
  81. '_', '%', "'", chr(39),
  82. 'select', 'join', 'union',
  83. 'where', 'insert', 'delete',
  84. 'update', 'like', 'drop',
  85. 'create', 'modify', 'rename',
  86. 'alter', 'cast',
  87. );
  88. $newstr = array(
  89. '\_', '\%', "''", '&#39;',
  90. 'sel&#101;ct"', 'jo&#105;n', 'un&#105;on',
  91. 'wh&#101;re', 'ins&#101;rt', 'del&#101;te',
  92. 'up&#100;ate', 'lik&#101;', 'dro&#112',
  93. 'cr&#101;ate', 'mod&#105;fy', 'ren&#097;me"',
  94. 'alt&#101;r', 'ca&#115;',
  95. );
  96. if ($operator == 'ENCODE') {
  97. $value = str_replace($badstr, $newstr, $value);
  98. } else {
  99. $value = str_replace($newstr, $badstr, $value);
  100. }
  101. return $value;
  102. }
  103. function safe_gpc_url($value, $strict_domain = true, $default = '') {
  104. global $_W;
  105. if (empty($value) || !is_string($value)) {
  106. return $default;
  107. }
  108. $value = urldecode($value);
  109. if (starts_with($value, './')) {
  110. return $value;
  111. }
  112. if ($strict_domain) {
  113. if (starts_with($value, $_W['siteroot'])) {
  114. return $value;
  115. }
  116. return $default;
  117. }
  118. if (starts_with($value, 'http') || starts_with($value, '//')) {
  119. return $value;
  120. }
  121. return $default;
  122. }
  123. function safe_remove_xss($val) {
  124. $val = preg_replace('/([\x0e-\x19])/', '', $val);
  125. $search = 'abcdefghijklmnopqrstuvwxyz';
  126. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  127. $search .= '1234567890!@#$%^&*()';
  128. $search .= '~`";:?+/={}[]-_|\'\\';
  129. for ($i = 0; $i < strlen($search); $i++) {
  130. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val);
  131. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val);
  132. }
  133. preg_match_all('/href=[\'|\"](.*?)[\'|\"]|src=[\'|\"](.*?)[\'|\"]/i', $val, $matches);
  134. $url_list = array_merge($matches[1], $matches[2]);
  135. $encode_url_list = array();
  136. if (!empty($url_list)) {
  137. foreach ($url_list as $key => $url) {
  138. $val = str_replace($url, 'we7_' . $key . '_we7placeholder', $val);
  139. $encode_url_list[] = $url;
  140. }
  141. }
  142. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'frameset', 'ilayer', 'bgsound', 'base');
  143. $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');
  144. $ra = array_merge($ra1, $ra2);
  145. $found = true;
  146. while ($found == true) {
  147. $val_before = $val;
  148. for ($i = 0; $i < sizeof($ra); $i++) {
  149. $pattern = '/';
  150. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  151. if ($j > 0) {
  152. $pattern .= '(';
  153. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  154. $pattern .= '|';
  155. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  156. $pattern .= ')*';
  157. }
  158. $pattern .= $ra[$i][$j];
  159. }
  160. $pattern .= '/i';
  161. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
  162. $val = preg_replace($pattern, $replacement, $val);
  163. if ($val_before == $val) {
  164. $found = false;
  165. }
  166. }
  167. }
  168. if (!empty($encode_url_list) && is_array($encode_url_list)) {
  169. foreach ($encode_url_list as $key => $url) {
  170. $val = str_replace('we7_' . $key . '_we7placeholder', $url, $val);
  171. }
  172. }
  173. return $val;
  174. }
  175. function safe_bad_str_replace($string) {
  176. if (empty($string)) {
  177. return '';
  178. }
  179. $badstr = array("\0", "%00", "%3C", "%3E", '<?', '<%', '<?php', '{php', '{if', '{loop', '../');
  180. $newstr = array('_', '_', '&lt;', '&gt;', '_', '_', '_', '_', '_', '_', '.._');
  181. $string = str_replace($badstr, $newstr, $string);
  182. return $string;
  183. }
  184. function safe_check_password($password) {
  185. $setting = setting_load('register');
  186. if (!$setting['register']['safe']) {
  187. return true;
  188. }
  189. preg_match(PASSWORD_STRONG_REGULAR, $password, $out);
  190. if (empty($out)) {
  191. return error(-1, PASSWORD_STRONG_STATE);
  192. } else {
  193. return true;
  194. }
  195. }