dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明。使用时需部分修改,你懂的

1.XSS过滤

  1. function XSSClean($val) {
  2. global $cfg_soft_lang;
  3. if($cfg_soft_lang=='gb2312') gb2utf8($val);
  4. if (is_array($val))
  5. {
  6. while (list($key) = each($val))
  7. {
  8. if(in_array($key,array('tags','body','dede_fields','dede_addonfields','dopost','introduce'))) continue;
  9. $val[$key] = XSSClean($val[$key]);
  10. }
  11. return $val;
  12. }
  13. $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  14. $search = 'abcdefghijklmnopqrstuvwxyz';
  15. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  16. $search .= '1234567890!@#$%^&*()';
  17. $search .= '~`";:?+/={}[]-_|\'\\';
  18. for ($i = 0; $i < strlen($search); $i++) {
  19. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  20. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  21. }
  22. $val = str_replace("`","‘",$val);
  23. $val = str_replace("'","‘",$val);
  24. $val = str_replace("\"","“",$val);
  25. $val = str_replace(",",",",$val);
  26. $val = str_replace("(","(",$val);
  27. $val = str_replace(")",")",$val);
  28. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  29. $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');
  30. $ra = array_merge($ra1, $ra2);
  31. $found = true;
  32. while ($found == true) {
  33. $val_before = $val;
  34. for ($i = 0; $i < sizeof($ra); $i++) {
  35. $pattern = '/';
  36. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  37. if ($j > 0) {
  38. $pattern .= '(';
  39. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  40. $pattern .= '|';
  41. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  42. $pattern .= ')*';
  43. }
  44. $pattern .= $ra[$i][$j];
  45. }
  46. $pattern .= '/i';
  47. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
  48. $val = preg_replace($pattern, $replacement, $val);
  49. if ($val_before == $val) {
  50. $found = false;
  51. }
  52. }
  53. }
  54. if($cfg_soft_lang=='gb2312') utf82gb($val);
  55. return $val;
  56. }
  57. $_GET = XSSClean($_GET);
  58. $_POST = XSSClean($_POST);
  59. $_REQUEST = XSSClean($_REQUEST);
  60. $_COOKIE = XSSClean($_COOKIE);

2.编码函数

  1. $UC2GBTABLE = $CODETABLE = $BIG5_DATA = $GB_DATA = '';
  2. $GbkUniDic = null;
  3. /**
  4. * UTF-8 转GB编码
  5. *
  6. * @access public
  7. * @param string $utfstr 需要转换的字符串
  8. * @return string
  9. */
  10. if ( ! function_exists('utf82gb'))
  11. {
  12. function utf82gb($utfstr)
  13. {
  14. if(function_exists('iconv'))
  15. {
  16. return iconv('utf-8','gbk//ignore',$utfstr);
  17. }
  18. global $UC2GBTABLE;
  19. $okstr = "";
  20. if(trim($utfstr)=="")
  21. {
  22. return $utfstr;
  23. }
  24. if(empty($UC2GBTABLE))
  25. {
  26. $filename = DEDEINC."/data/gb2312-utf8.dat";
  27. $fp = fopen($filename,"r");
  28. while($l = fgets($fp,15))
  29. {
  30. $UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
  31. }
  32. fclose($fp);
  33. }
  34. $okstr = "";
  35. $ulen = strlen($utfstr);
  36. for($i=0;$i<$ulen;$i++)
  37. {
  38. $c = $utfstr[$i];
  39. $cb = decbin(ord($utfstr[$i]));
  40. if(strlen($cb)==8)
  41. {
  42. $csize = strpos(decbin(ord($cb)),"0");
  43. for($j=0;$j < $csize;$j++)
  44. {
  45. $i++; $c .= $utfstr[$i];
  46. }
  47. $c = utf82u($c);
  48. if(isset($UC2GBTABLE[$c]))
  49. {
  50. $c = dechex($UC2GBTABLE[$c]+0x8080);
  51. $okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
  52. }
  53. else
  54. {
  55. $okstr .= "&#".$c.";";
  56. }
  57. }
  58. else
  59. {
  60. $okstr .= $c;
  61. }
  62. }
  63. $okstr = trim($okstr);
  64. return $okstr;
  65. }
  66. }
  67. /**
  68. * GB转UTF-8编码
  69. *
  70. * @access public
  71. * @param string $gbstr gbk的字符串
  72. * @return string
  73. */
  74. if ( ! function_exists('gb2utf8'))
  75. {
  76. function gb2utf8($gbstr)
  77. {
  78. if(function_exists('iconv'))
  79. {
  80. return iconv('gbk','utf-8//ignore',$gbstr);
  81. }
  82. global $CODETABLE;
  83. if(trim($gbstr)=="")
  84. {
  85. return $gbstr;
  86. }
  87. if(empty($CODETABLE))
  88. {
  89. $filename = DEDEINC."/data/gb2312-utf8.dat";
  90. $fp = fopen($filename,"r");
  91. while ($l = fgets($fp,15))
  92. {
  93. $CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6);
  94. }
  95. fclose($fp);
  96. }
  97. $ret = "";
  98. $utf8 = "";
  99. while ($gbstr != '')
  100. {
  101. if (ord(substr($gbstr, 0, 1)) > 0x80)
  102. {
  103. $thisW = substr($gbstr, 0, 2);
  104. $gbstr = substr($gbstr, 2, strlen($gbstr));
  105. $utf8 = "";
  106. @$utf8 = u2utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080]));
  107. if($utf8!="")
  108. {
  109. for ($i = 0;$i < strlen($utf8);$i += 3)
  110. $ret .= chr(substr($utf8, $i, 3));
  111. }
  112. }
  113. else
  114. {
  115. $ret .= substr($gbstr, 0, 1);
  116. $gbstr = substr($gbstr, 1, strlen($gbstr));
  117. }
  118. }
  119. return $ret;
  120. }
  121. }
  122. /**
  123. * Unicode转utf8
  124. *
  125. * @access public
  126. * @param string $c Unicode的字符串内容
  127. * @return string
  128. */
  129. if ( ! function_exists('u2utf8'))
  130. {
  131. function u2utf8($c)
  132. {
  133. for ($i = 0;$i < count($c);$i++)
  134. {
  135. $str = "";
  136. }
  137. if ($c < 0x80)
  138. {
  139. $str .= $c;
  140. }
  141. else if ($c < 0x800)
  142. {
  143. $str .= (0xC0 | $c >> 6);
  144. $str .= (0x80 | $c & 0x3F);
  145. }
  146. else if ($c < 0x10000)
  147. {
  148. $str .= (0xE0 | $c >> 12);
  149. $str .= (0x80 | $c >> 6 & 0x3F);
  150. $str .= (0x80 | $c & 0x3F);
  151. }
  152. else if ($c < 0x200000)
  153. {
  154. $str .= (0xF0 | $c >> 18);
  155. $str .= (0x80 | $c >> 12 & 0x3F);
  156. $str .= (0x80 | $c >> 6 & 0x3F);
  157. $str .= (0x80 | $c & 0x3F);
  158. }
  159. return $str;
  160. }
  161. }
  162. /**
  163. * utf8转Unicode
  164. *
  165. * @access public
  166. * @param string $c UTF-8的字符串信息
  167. * @return string
  168. */
  169. if ( ! function_exists('utf82u'))
  170. {
  171. function utf82u($c)
  172. {
  173. switch(strlen($c))
  174. {
  175. case 1:
  176. return ord($c);
  177. case 2:
  178. $n = (ord($c[0]) & 0x3f) << 6;
  179. $n += ord($c[1]) & 0x3f;
  180. return $n;
  181. case 3:
  182. $n = (ord($c[0]) & 0x1f) << 12;
  183. $n += (ord($c[1]) & 0x3f) << 6;
  184. $n += ord($c[2]) & 0x3f;
  185. return $n;
  186. case 4:
  187. $n = (ord($c[0]) & 0x0f) << 18;
  188. $n += (ord($c[1]) & 0x3f) << 12;
  189. $n += (ord($c[2]) & 0x3f) << 6;
  190. $n += ord($c[3]) & 0x3f;
  191. return $n;
  192. }
  193. }
  194. }
  195. /**
  196. * Big5码转换成GB码
  197. *
  198. * @access public
  199. * @param string $Text 字符串内容
  200. * @return string
  201. */
  202. if ( ! function_exists('big52gb'))
  203. {
  204. function big52gb($Text)
  205. {
  206. if(function_exists('iconv'))
  207. {
  208. return iconv('big5','gbk//ignore',$Text);
  209. }
  210. global $BIG5_DATA;
  211. if(empty($BIG5_DATA))
  212. {
  213. $filename = DEDEINC."/data/big5-gb.dat";
  214. $fp = fopen($filename, "rb");
  215. $BIG5_DATA = fread($fp,filesize($filename));
  216. fclose($fp);
  217. }
  218. $max = strlen($Text)-1;
  219. for($i=0;$i<$max;$i++)
  220. {
  221. $h = ord($Text[$i]);
  222. if($h>=0x80)
  223. {
  224. $l = ord($Text[$i+1]);
  225. if($h==161 && $l==64)
  226. {
  227. $gbstr = " ";
  228. }
  229. else
  230. {
  231. $p = ($h-160)*510+($l-1)*2;
  232. $gbstr = $BIG5_DATA[$p].$BIG5_DATA[$p+1];
  233. }
  234. $Text[$i] = $gbstr[0];
  235. $Text[$i+1] = $gbstr[1];
  236. $i++;
  237. }
  238. }
  239. return $Text;
  240. }
  241. }
  242. /**
  243. * GB码转换成Big5码
  244. *
  245. * @access public
  246. * @param string $Text 字符串内容
  247. * @return string
  248. */
  249. if ( ! function_exists('gb2big5'))
  250. {
  251. function gb2big5($Text)
  252. {
  253. if(function_exists('iconv'))
  254. {
  255. return iconv('gbk','big5//ignore',$Text);
  256. }
  257. global $GB_DATA;
  258. if(empty($GB_DATA))
  259. {
  260. $filename = DEDEINC."/data/gb-big5.dat";
  261. $fp = fopen($filename, "rb");
  262. $gb = fread($fp,filesize($filename));
  263. fclose($fp);
  264. }
  265. $max = strlen($Text)-1;
  266. for($i=0;$i<$max;$i++)
  267. {
  268. $h = ord($Text[$i]);
  269. if($h>=0x80)
  270. {
  271. $l = ord($Text[$i+1]);
  272. if($h==161 && $l==64)
  273. {
  274. $big = " ";
  275. }
  276. else
  277. {
  278. $p = ($h-160)*510+($l-1)*2;
  279. $big = $GB_DATA[$p].$GB_DATA[$p+1];
  280. }
  281. $Text[$i] = $big[0];
  282. $Text[$i+1] = $big[1];
  283. $i++;
  284. }
  285. }
  286. return $Text;
  287. }
  288. }
  289. /**
  290. * unicode url编码转gbk编码函数
  291. *
  292. * @access public
  293. * @param string $str 转换的内容
  294. * @return string
  295. */
  296. if ( ! function_exists('UnicodeUrl2Gbk'))
  297. {
  298. function UnicodeUrl2Gbk($str)
  299. {
  300. //载入对照词典
  301. if(!isset($GLOBALS['GbkUniDic']))
  302. {
  303. $fp = fopen(DEDEINC.'/data/gbk-unicode.dat','rb');
  304. while(!feof($fp))
  305. {
  306. $GLOBALS['GbkUniDic'][bin2hex(fread($fp,2))] = fread($fp,2);
  307. }
  308. fclose($fp);
  309. }
  310. //处理字符串
  311. $str = str_replace('$#$','+',$str);
  312. $glen = strlen($str);
  313. $okstr = "";
  314. for($i=0; $i < $glen; $i++)
  315. {
  316. if($glen-$i > 4)
  317. {
  318. if($str[$i]=='%' && $str[$i+1]=='u')
  319. {
  320. $uni = strtolower(substr($str,$i+2,4));
  321. $i = $i+5;
  322. if(isset($GLOBALS['GbkUniDic'][$uni]))
  323. {
  324. $okstr .= $GLOBALS['GbkUniDic'][$uni];
  325. }
  326. else
  327. {
  328. $okstr .= "&#".hexdec('0x'.$uni).";";
  329. }
  330. }
  331. else
  332. {
  333. $okstr .= $str[$i];
  334. }
  335. }
  336. else
  337. {
  338. $okstr .= $str[$i];
  339. }
  340. }
  341. return $okstr;
  342. }
  343. }
  344. /**
  345. * 自动转换字符集 支持数组转换
  346. *
  347. * @access public
  348. * @param string $str 转换的内容
  349. * @return string
  350. */
  351. if ( ! function_exists('AutoCharset'))
  352. {
  353. function AutoCharset($fContents, $from='gbk', $to='utf-8')
  354. {
  355. $from = strtoupper($from)=='UTF8'? 'utf-8' : $from;
  356. $to = strtoupper($to)=='UTF8'? 'utf-8' : $to;
  357. if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
  358. //如果编码相同或者非字符串标量则不转换
  359. return $fContents;
  360. }
  361. if(is_string($fContents) )
  362. {
  363. if(function_exists('mb_convert_encoding'))
  364. {
  365. return mb_convert_encoding ($fContents, $to, $from);
  366. } elseif (function_exists('iconv'))
  367. {
  368. return iconv($from, $to, $fContents);
  369. } else {
  370. return $fContents;
  371. }
  372. }
  373. elseif(is_array($fContents))
  374. {
  375. foreach ( $fContents as $key => $val )
  376. {
  377. $_key = AutoCharset($key,$from,$to);
  378. $fContents[$_key] = AutoCharset($val,$from,$to);
  379. if($key != $_key )
  380. unset($fContents[$key]);
  381. }
  382. return $fContents;
  383. }
  384. else{
  385. return $fContents;
  386. }
  387. }
  388. }

3.修复浏览器XSS hack的函数

  1. /**
  2. * XSS hack的函数
  3. *
  4. * @param string $val 需要处理的内容
  5. * @return string
  6. */
  7. if ( ! function_exists('RemoveXSS'))
  8. {
  9. function RemoveXSS($val) {
  10. $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  11. $search = 'abcdefghijklmnopqrstuvwxyz';
  12. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  13. $search .= '1234567890!@#$%^&*()';
  14. $search .= '~`";:?+/={}[]-_|\'\\';
  15. for ($i = 0; $i < strlen($search); $i++) {
  16. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  17. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  18. }
  19. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  20. $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');
  21. $ra = array_merge($ra1, $ra2);
  22. $found = true;
  23. while ($found == true) {
  24. $val_before = $val;
  25. for ($i = 0; $i < sizeof($ra); $i++) {
  26. $pattern = '/';
  27. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  28. if ($j > 0) {
  29. $pattern .= '(';
  30. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  31. $pattern .= '|';
  32. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  33. $pattern .= ')*';
  34. }
  35. $pattern .= $ra[$i][$j];
  36. }
  37. $pattern .= '/i';
  38. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2);
  39. $val = preg_replace($pattern, $replacement, $val);
  40. if ($val_before == $val) {
  41. $found = false;
  42. }
  43. }
  44. }
  45. return $val;
  46. }
  47. }

4.字符操作函数

  1. <?php if(!defined('DEDEINC')) exit('dedecms');
  2. //拼音的缓冲数组
  3. $pinyins = Array();
  4. /**
  5. * 中文截取2,单字节截取模式
  6. * 如果是request的内容,必须使用这个函数
  7. *
  8. * @access public
  9. * @param string $str 需要截取的字符串
  10. * @param int $slen 截取的长度
  11. * @param int $startdd 开始标记处
  12. * @return string
  13. */
  14. if ( ! function_exists('cn_substrR'))
  15. {
  16. function cn_substrR($str, $slen, $startdd=0)
  17. {
  18. $str = cn_substr(stripslashes($str), $slen, $startdd);
  19. return addslashes($str);
  20. }
  21. }
  22. /**
  23. * 中文截取2,单字节截取模式
  24. *
  25. * @access public
  26. * @param string $str 需要截取的字符串
  27. * @param int $slen 截取的长度
  28. * @param int $startdd 开始标记处
  29. * @return string
  30. */
  31. if ( ! function_exists('cn_substr'))
  32. {
  33. function cn_substr($str, $slen, $startdd=0)
  34. {
  35. global $cfg_soft_lang;
  36. if($cfg_soft_lang=='utf-8')
  37. {
  38. return cn_substr_utf8($str, $slen, $startdd);
  39. }
  40. $restr = '';
  41. $c = '';
  42. $str_len = strlen($str);
  43. if($str_len < $startdd+1)
  44. {
  45. return '';
  46. }
  47. if($str_len < $startdd + $slen || $slen==0)
  48. {
  49. $slen = $str_len - $startdd;
  50. }
  51. $enddd = $startdd + $slen - 1;
  52. for($i=0;$i<$str_len;$i++)
  53. {
  54. if($startdd==0)
  55. {
  56. $restr .= $c;
  57. }
  58. else if($i > $startdd)
  59. {
  60. $restr .= $c;
  61. }
  62. if(ord($str[$i])>0x80)
  63. {
  64. if($str_len>$i+1)
  65. {
  66. $c = $str[$i].$str[$i+1];
  67. }
  68. $i++;
  69. }
  70. else
  71. {
  72. $c = $str[$i];
  73. }
  74. if($i >= $enddd)
  75. {
  76. if(strlen($restr)+strlen($c)>$slen)
  77. {
  78. break;
  79. }
  80. else
  81. {
  82. $restr .= $c;
  83. break;
  84. }
  85. }
  86. }
  87. return $restr;
  88. }
  89. }
  90. /**
  91. * utf-8中文截取,单字节截取模式
  92. *
  93. * @access public
  94. * @param string $str 需要截取的字符串
  95. * @param int $slen 截取的长度
  96. * @param int $startdd 开始标记处
  97. * @return string
  98. */
  99. if ( ! function_exists('cn_substr_utf8'))
  100. {
  101. function cn_substr_utf8($str, $length, $start=0)
  102. {
  103. if(strlen($str) < $start+1)
  104. {
  105. return '';
  106. }
  107. preg_match_all("/./su", $str, $ar);
  108. $str = '';
  109. $tstr = '';
  110. //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
  111. for($i=0; isset($ar[0][$i]); $i++)
  112. {
  113. if(strlen($tstr) < $start)
  114. {
  115. $tstr .= $ar[0][$i];
  116. }
  117. else
  118. {
  119. if(strlen($str) < $length + strlen($ar[0][$i]) )
  120. {
  121. $str .= $ar[0][$i];
  122. }
  123. else
  124. {
  125. break;
  126. }
  127. }
  128. }
  129. return $str;
  130. }
  131. }
  132. /**
  133. * HTML转换为文本
  134. *
  135. * @param string $str 需要转换的字符串
  136. * @param string $r 如果$r=0直接返回内容,否则需要使用反斜线引用字符串
  137. * @return string
  138. */
  139. if ( ! function_exists('Html2Text'))
  140. {
  141. function Html2Text($str,$r=0)
  142. {
  143. if(!function_exists('SpHtml2Text'))
  144. {
  145. require_once(DEDEINC."/inc/inc_fun_funString.php");
  146. }
  147. if($r==0)
  148. {
  149. return SpHtml2Text($str);
  150. }
  151. else
  152. {
  153. $str = SpHtml2Text(stripslashes($str));
  154. return addslashes($str);
  155. }
  156. }
  157. }
  158. /**
  159. * 文本转HTML
  160. *
  161. * @param string $txt 需要转换的文本内容
  162. * @return string
  163. */
  164. if ( ! function_exists('Text2Html'))
  165. {
  166. function Text2Html($txt)
  167. {
  168. $txt = str_replace(" ", " ", $txt);
  169. $txt = str_replace("<", "&lt;", $txt);
  170. $txt = str_replace(">", "&gt;", $txt);
  171. $txt = preg_replace("/[\r\n]{1,}/isU", "<br/>\r\n", $txt);
  172. return $txt;
  173. }
  174. }
  175. /**
  176. * 获取半角字符
  177. *
  178. * @param string $fnum 数字字符串
  179. * @return string
  180. */
  181. if ( ! function_exists('GetAlabNum'))
  182. {
  183. function GetAlabNum($fnum)
  184. {
  185. $nums = array("0","1","2","3","4","5","6","7","8","9");
  186. //$fnums = "0123456789";
  187. $fnums = array("0","1","2","3","4","5","6","7","8","9");
  188. $fnum = str_replace($nums, $fnums, $fnum);
  189. $fnum = preg_replace("/[^0-9\.-]/", '', $fnum);
  190. if($fnum=='')
  191. {
  192. $fnum=0;
  193. }
  194. return $fnum;
  195. }
  196. }
  197. /**
  198. * 获取拼音以gbk编码为准
  199. *
  200. * @access public
  201. * @param string $str 字符串信息
  202. * @param int $ishead 是否取头字母
  203. * @param int $isclose 是否关闭字符串资源
  204. * @return string
  205. */
  206. if ( ! function_exists('GetPinyin'))
  207. {
  208. function GetPinyin($str, $ishead=0, $isclose=1)
  209. {
  210. global $cfg_soft_lang;
  211. if(!function_exists('SpGetPinyin'))
  212. {
  213. //全局函数仅是inc_fun_funAdmin.php文件中函数的一个映射
  214. require_once(DEDEINC."/inc/inc_fun_funAdmin.php");
  215. }
  216. if($cfg_soft_lang=='utf-8')
  217. {
  218. return SpGetPinyin(utf82gb($str), $ishead, $isclose);
  219. }
  220. else
  221. {
  222. return SpGetPinyin($str, $ishead, $isclose);
  223. }
  224. }
  225. }
  226. /**
  227. * 将实体html代码转换成标准html代码(兼容php4)
  228. *
  229. * @access public
  230. * @param string $str 字符串信息
  231. * @param long $options 替换的字符集
  232. * @return string
  233. */
  234. if ( ! function_exists('htmlspecialchars_decode'))
  235. {
  236. function htmlspecialchars_decode($str, $options=ENT_COMPAT) {
  237. $trans = get_html_translation_table(HTML_SPECIALCHARS, $options);
  238. $decode = ARRAY();
  239. foreach ($trans AS $char=>$entity) {
  240. $decode[$entity] = $char;
  241. }
  242. $str = strtr($str, $decode);
  243. return $str;
  244. }
  245. }
  246. if ( ! function_exists('ubb'))
  247. {
  248. function ubb($Text) {
  249. $Text=trim($Text);
  250. //$Text=htmlspecialchars($Text);
  251. //$Text=ereg_replace("\n","<br>",$Text);
  252. $Text=preg_replace("/\\t/is"," ",$Text);
  253. $Text=preg_replace("/\[hr\]/is","<hr>",$Text);
  254. $Text=preg_replace("/\[separator\]/is","<br/>",$Text);
  255. $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
  256. $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
  257. $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
  258. $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
  259. $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
  260. $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
  261. $Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
  262. //$Text=preg_replace("/\[url=([^\[]*)\](.+?)\[\/url\]/is","<a href=\\1 target='_blank'>\\2</a>",$Text);
  263. $Text=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"\\1\" target='_blank'>\\1</a>",$Text);
  264. $Text=preg_replace("/\[url=(http:\/\/.+?)\](.+?)\[\/url\]/is","<a href='\\1' target='_blank'>\\2</a>",$Text);
  265. $Text=preg_replace("/\[url=(.+?)\](.+?)\[\/url\]/is","<a href=\\1>\\2</a>",$Text);
  266. $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1>",$Text);
  267. $Text=preg_replace("/\[img\s(.+?)\](.+?)\[\/img\]/is","<img \\1 src=\\2>",$Text);
  268. $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text);
  269. $Text=preg_replace("/\[colorTxt\](.+?)\[\/colorTxt\]/eis","color_txt('\\1')",$Text);
  270. $Text=preg_replace("/\[style=(.+?)\](.+?)\[\/style\]/is","<div class='\\1'>\\2</div>",$Text);
  271. $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text);
  272. $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
  273. $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
  274. $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
  275. $Text=preg_replace("/\[emot\](.+?)\[\/emot\]/eis","emot('\\1')",$Text);
  276. $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href='mailto:\\1'>\\1</a>",$Text);
  277. $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
  278. $Text=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$Text);
  279. $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
  280. $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is","<blockquote>引用:<div style='border:1px solid silver;background:#EFFFDF;color:#393939;padding:5px' >\\1</div></blockquote>", $Text);
  281. $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div style='text-align: left; color: darkgreen; margin-left: 5%'><br><br>--------------------------<br>\\1<br>--------------------------</div>", $Text);
  282. return $Text;
  283. }
  284. }

部分内容引自:

http://www.neatstudio.com/show-378-1.shtml

附件下载地址:

http://files.cnblogs.com/mengdejun/data.zip

dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)的更多相关文章

  1. SQL_字符操作函数

    原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  2. ctype.h库函数----字符操作函数

    在c++中使用时:  #include <cctype> 字符判断函数 1.isalnum函数--判断是否是英文字母或数字字符,如果是,则返回非0值,如果不是,则返回0. 函数参数 :可以 ...

  3. C语言-字符操作函数

    1字符数组的初始化: 1.1 char string={'c','h','i','n','a'} 1.2char string={"china"}或者去掉{}即char strin ...

  4. ACM编程技巧--常用字符操作函数

    字符串与基本数据类型的转换 int sscanf(buff,"%d%d",&a,&b); //返回值是参数个数 int sprintf(buff,"%d% ...

  5. js cookie创建读取删除函数封装

    js cookie创建读取删除函数封装 一.总结 都是为了方便操作,这样弄了很方便 1.创建cookie的函数封装的作用:方便设置过期时间expire,方便设置键和值 2.查询cookie的数据封装的 ...

  6. MYSQL常用操作函数的封装

    1.mysql常用函数封装文件:mysql.func.php <?php /** * 连接MYSQL函数 * @param string $host * @param string $usern ...

  7. linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words

    1.1       字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...

  8. 跟着ALEX 学python day3集合 文件操作 函数和函数式编程 内置函数

    声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/  一. 集合 集合是一个无序的,不重复的数据组合,主要作用如下 1.去重 把一个列表变成集合 ,就自动去重 ...

  9. 文件操作(FILE)与常用文件操作函数

    文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...

随机推荐

  1. 设计模式--命令模式Command(对象行为型)

    一.命令模式 将一个请求封装为一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能. (1)Command类:是一个抽象类,类中对需要执行的命令进行 ...

  2. java反射

    知识点1:获取类字节码的三种形式 1.Class date = Date.class;//根据类名获取字节码 2.Date date= new Date(); date.getClass();//对象 ...

  3. 在js中获取在css中设置的background-image值

    1. html部分 <div class="bg-color-two" id="bg_color_two" onclick="setBg(thi ...

  4. 关于Response.Redirect 端口不一致的跳转

    如果内网和外网的端口号设置的不相同,那在使用Response.Redirect跳转的时候会无法成功.需要做以下设置: <system.web> <httpRuntime useFul ...

  5. MySql卸载重新安装出现Start service没有响应的解决办法(64位)

    昨天因为自己手欠,不小心把mysql卸载了,于是又得重新安装,但是每次到了最后一步就报没有响应,于是就去寻找解决办法,如下就是啦! 安装Mysql卸载后又重新安装,每次到最后Start service ...

  6. 读取Simulink中Dataset类型的数据

    http://files.cnblogs.com/files/pursuiting/%E5%80%92%E7%AB%8B%E6%91%86%E6%8E%A7%E5%88%B6%E7%B3%BB%E7% ...

  7. Linux更改计算机名称

    1.修改:vim /etc/hosts 2.修改:vim /etc/sysconfig/network 3.重启:reboot 如不重启可以输入:hostname  新改的计算机名称,然后su

  8. JS中,!=, !== 和 !的区别和使用场景

    var num = 1; var str = '1'; var test = 1;   test == num   //true 相同类型 相同值 test === num  //true 相同类型 ...

  9. tif文件导入postgresql

    raster2pgsql -I -F -N -999  文件名  数据库名 | psql -U postgres -d postgres

  10. laravel5笔记

    数据库表创建E:\PHP\learnlaravel5>php artisan migrate 创建modelE:\PHP\learnlaravel5>php artisan make:mo ...