safe.func.php 6.4 KB

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