security.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * 安全防范
  4. * 进行sql注入过滤,xss过滤和csrf过滤
  5. *
  6. * 令牌调用方式
  7. * 输出:直接在模板上调用getToken
  8. * 验证:在验证位置调用checkToken
  9. *
  10. * @package
  11. */
  12. defined('InShopNC') or exit('Access Invalid!');
  13. class Security
  14. {
  15. /**
  16. * 取得令牌内容
  17. * 自动输出html 隐藏域
  18. *
  19. * @param
  20. * @return void 字符串形式的返回结果
  21. */
  22. public static function getToken(){
  23. $token = encrypt(TIMESTAMP,md5(MD5_KEY));
  24. echo "<input type='hidden' name='formhash' value='". $token ."' />";
  25. }
  26. public static function getTokenValue(){
  27. return encrypt(TIMESTAMP,md5(MD5_KEY));
  28. }
  29. /**
  30. * 判断令牌是否正确
  31. *
  32. * @param
  33. * @return bool 布尔类型的返回结果
  34. */
  35. public static function checkToken(){
  36. $data = decrypt($_POST['formhash'],md5(MD5_KEY));
  37. return $data && (TIMESTAMP - $data < 5400);
  38. }
  39. /**
  40. * 将字符 & " < > 替换
  41. *
  42. * @param unknown_type $string
  43. * @return unknown
  44. */
  45. public static function fliterHtmlSpecialChars($string) {
  46. $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
  47. str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
  48. return $string;
  49. }
  50. /**
  51. * 参数过滤
  52. *
  53. * @param array 参数内容
  54. * @param array $ignore 被忽略过滤的元素
  55. * @return array 数组形式的返回结果
  56. *
  57. */
  58. public static function getAddslashesForInput($array,$ignore=array()){
  59. if (!function_exists('htmlawed')) require(BASE_CORE_PATH.'/framework/function/htmlawed.php');
  60. if (!empty($array))
  61. {
  62. foreach ($array as $k => $v) {
  63. // while (list($k,$v) = each($array)) {
  64. if (is_string($v)) {
  65. if (get_magic_quotes_gpc()) {
  66. $v = stripslashes($v);
  67. }
  68. if($k != 'statistics_code') {
  69. if (!in_array($k,$ignore)){
  70. //如果不是编辑器,则转义< > & "
  71. $v = self::fliterHtmlSpecialChars($v);
  72. } else {
  73. $v = htmlawed($v,array('safe'=>1));
  74. }
  75. if($k == 'ref_url') {
  76. $v = str_replace('&amp;','&',$v);
  77. }
  78. }
  79. $array[$k] = addslashes(trim($v));
  80. } else if (is_array($v)) {
  81. if($k == 'statistics_code') {
  82. $array[$k] = $v;
  83. } else {
  84. $array[$k] = self::getAddslashesForInput($v);
  85. }
  86. }
  87. }
  88. return $array;
  89. }else {
  90. return false;
  91. }
  92. }
  93. public static function getAddSlashes($array){
  94. if (!empty($array)){
  95. // while (list($k,$v) = each($array)) {
  96. foreach ($array as $k => $v) {
  97. if (is_string($v)) {
  98. if (!get_magic_quotes_gpc()) {
  99. $v = addslashes($v);
  100. }
  101. } else if (is_array($v)) {
  102. $array[$k] = self::getAddSlashes($v);
  103. }
  104. }
  105. return $array;
  106. }else {
  107. return false;
  108. }
  109. }
  110. }