PageRenderTime 74ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/wind/utility/WindSecurity.php

https://github.com/cuijinquan/nextwind
PHP | 121 lines | 55 code | 9 blank | 57 comment | 18 complexity | 35db9cc62096b56a04335b1cef150ce1 MD5 | raw file
  1. <?php
  2. /**
  3. * 字符、路径过滤等安全处理
  4. *
  5. * @author Qiong Wu <papa0924@gmail.com>
  6. * @copyright ©2003-2103 phpwind.com
  7. * @license http://www.windframework.com
  8. * @version $Id: WindSecurity.php 3904 2013-01-08 07:01:26Z yishuo $
  9. * @package utility
  10. */
  11. class WindSecurity {
  12. /**
  13. * 转义输出字符串
  14. *
  15. * @param string $str 被转义的字符串
  16. * @return string
  17. */
  18. public static function escapeHTML($str, $charset = 'ISO-8859-1') {
  19. if (!is_string($str)) return $str;
  20. return htmlspecialchars($str, ENT_QUOTES, $charset);
  21. }
  22. /**
  23. * 转义字符串
  24. *
  25. * @param array $array 被转移的数组
  26. * @return array
  27. */
  28. public static function escapeArrayHTML($array) {
  29. if (!is_array($array) || count($array) > 100) return $array;
  30. $_tmp = array();
  31. foreach ($array as $key => $value) {
  32. is_string($key) && $key = self::escapeHTML($key);
  33. $_tmp[$key] = self::escapeHTML($value);
  34. }
  35. return $_tmp;
  36. }
  37. /**
  38. * 字符串加密
  39. *
  40. * @param string $str 需要加密的字符串
  41. * @param string $key 密钥
  42. * @return string 加密后的结果
  43. */
  44. public static function encrypt($str, $key, $iv = '') {
  45. if (!$key || !is_string($key)) throw new WindException(
  46. '[utility.WindSecurity.encrypt] security key is required. ', WindException::ERROR_PARAMETER_TYPE_ERROR);
  47. if (!$str || !is_string($str)) throw new WindException(
  48. '[utility.WindSecurity.encrypt] security string is required.', WindException::ERROR_PARAMETER_TYPE_ERROR);
  49. $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
  50. $iv = substr(md5($iv ? $iv : $key), -$size);
  51. $pad = $size - (strlen($str) % $size);
  52. $str .= str_repeat(chr($pad), $pad);
  53. @$data = mcrypt_cbc(MCRYPT_DES, $key, $str, MCRYPT_ENCRYPT, $iv);
  54. return base64_encode($data);
  55. }
  56. /**
  57. * 解密字符串
  58. *
  59. * @param string $str 解密的字符串
  60. * @param string $key 密钥
  61. * @return string 解密后的结果
  62. */
  63. public static function decrypt($str, $key, $iv = '') {
  64. if (!$str || !is_string($str)) throw new WindException(
  65. '[utility.WindSecurity.decrypt] security string is required.', WindException::ERROR_PARAMETER_TYPE_ERROR);
  66. if (!$key || !is_string($key)) throw new WindException(
  67. '[utility.WindSecurity.decrypt] security key is required.', WindException::ERROR_PARAMETER_TYPE_ERROR);
  68. $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
  69. $iv = substr(md5($iv ? $iv : $key), -$size);
  70. $str = base64_decode($str);
  71. @$str = mcrypt_cbc(MCRYPT_DES, $key, $str, MCRYPT_DECRYPT, $iv);
  72. $pad = ord($str{strlen($str) - 1});
  73. if ($pad > strlen($str)) return false;
  74. if (strspn($str, chr($pad), strlen($str) - $pad) != $pad) return false;
  75. return substr($str, 0, -1 * $pad);
  76. }
  77. /**
  78. * 创建token令牌串
  79. * 创建token令牌串,用于避免表单重复提交等.
  80. * 使用当前的sessionID以及当前时间戳,生成唯一一串令牌串,并返回.
  81. *
  82. * @deprecated
  83. *
  84. * @return string
  85. */
  86. public static function createToken() {
  87. return self::generateGUID();
  88. }
  89. /**
  90. * 获取唯一标识符串,标识符串的长度为16个字节,128位.
  91. * 根据当前时间与sessionID,混合生成一个唯一的串.
  92. *
  93. * @return string GUID串,16个字节
  94. */
  95. public static function generateGUID() {
  96. return substr(md5(WindUtility::generateRandStr(8) . microtime()), -16);
  97. }
  98. /**
  99. * 路径检查转义
  100. *
  101. * @param string $fileName 被检查的路径
  102. * @param boolean $ifCheck 是否需要检查文件名,默认为false
  103. * @return string
  104. */
  105. public static function escapePath($filePath, $ifCheck = false) {
  106. $_tmp = array("'" => '', '#' => '', '=' => '', '`' => '', '$' => '', '%' => '', '&' => '', ';' => '');
  107. $_tmp['://'] = $_tmp["\0"] = '';
  108. $ifCheck && $_tmp['..'] = '';
  109. if (strtr($filePath, $_tmp) == $filePath) return preg_replace('/[\/\\\]{1,}/i', '/', $filePath);
  110. throw new WindException('[utility.WindSecurity.escapePath] file path is illegal');
  111. }
  112. }