Heim  >  Artikel  >  Backend-Entwicklung  >  DZX的过滤函数封装提取分享

DZX的过滤函数封装提取分享

WBOY
WBOYOriginal
2016-07-25 09:01:271043Durchsuche
新装了个dzx,忽然想起来服务器上是php5.4.10。禁用且不支持*_magic_quotes_*函数。想看看dzx是怎么做的。翻了翻代码。没有发现addslashes。然后提取了dzx的过滤函数。share一下。没什么用。可以看看。


所有函数定义,均在/source/class/discuz/discuz_database.php

DB类定义在/source/class/class_core.php最下方
  1. function quote($str, $noarray = false) {
  2. if (is_string($str))
  3. return '\'' . addcslashes($str, "\n\r\\'\"\032") . '\'';
  4. if (is_int($str) or is_float($str))
  5. return '\'' . $str . '\'';
  6. if (is_array($str)) {
  7. if($noarray === false) {
  8. foreach ($str as &$v) {
  9. $v = self::quote($v, true);
  10. }
  11. return $str;
  12. } else {
  13. return '\'\'';
  14. }
  15. }
  16. if (is_bool($str))
  17. return $str ? '1' : '0';
  18. return '\'\'';
  19. }
复制代码
  1. function quote_field($field) {
  2. if (is_array($field)) {
  3. foreach ($field as $k => $v) {
  4. $field[$k] = self::quote_field($v);
  5. }
  6. } else {
  7. if (strpos($field, '`') !== false)
  8. $field = str_replace('`', '', $field);
  9. $field = '`' . $field . '`';
  10. }
  11. return $field;
  12. }
复制代码
  1. function format($sql, $arg) {
  2. $count = substr_count($sql, '%');
  3. if (!$count) {
  4. return $sql;
  5. } elseif ($count > count($arg)) {
  6. throw new DbException('SQL string format error! This SQL need "' . $count . '" vars to replace into.', 0, $sql);
  7. }
  8. $len = strlen($sql);
  9. $i = $find = 0;
  10. $ret = '';
  11. while ($i if ($sql{$i} == '%') {
  12. $next = $sql{$i + 1};
  13. if ($next == 't') {
  14. $ret .= self::table($arg[$find]);
  15. } elseif ($next == 's') {
  16. $ret .= self::quote(is_array($arg[$find]) ? serialize($arg[$find]) : (string) $arg[$find]);
  17. } elseif ($next == 'f') {
  18. $ret .= sprintf('%F', $arg[$find]);
  19. } elseif ($next == 'd') {
  20. $ret .= dintval($arg[$find]);
  21. } elseif ($next == 'i') {
  22. $ret .= $arg[$find];
  23. } elseif ($next == 'n') {
  24. if (!empty($arg[$find])) {
  25. $ret .= is_array($arg[$find]) ? implode(',', self::quote($arg[$find])) : self::quote($arg[$find]);
  26. } else {
  27. $ret .= '0';
  28. }
  29. } else {
  30. $ret .= self::quote($arg[$find]);
  31. }
  32. $i++;
  33. $find++;
  34. } else {
  35. $ret .= $sql{$i};
  36. }
  37. $i++;
  38. }
  39. if ($i $ret .= substr($sql, $i);
  40. }
  41. return $ret;
  42. }
  43. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:使数组按指定的KEY值排序 Nächster Artikel:获取操作系统