Home >Backend Development >PHP Tutorial >DZX filter function encapsulation extraction sharing

DZX filter function encapsulation extraction sharing

WBOY
WBOYOriginal
2016-07-25 09:01:271087browse
I newly installed dzx, and suddenly I remembered that the server is php5.4.10. The *_magic_quotes_* functions are disabled and not supported. Want to see how dzx does it. I looked through the code. No addslashes found. Then the filter function of dzx was extracted. Share it. It's of no use. You can take a look.


All function definitions are in /source/class/discuz/discuz_database.php

The DB class is defined at the bottom of /source/class/class_core.php
  1. function quote($str, $noarray = false) {
  2. if (is_string($str))
  3. return ''' . addcslashes($str, "nr\'"
  4. function quote_field($field) {
  5. if (is_array($field)) {
  6. foreach ($field as $k => $v) {
  7. $field[$k] = self::quote_field($ v);
  8. }
  9. } else {
  10. if (strpos($field, '`') !== false)
  11. $field = str_replace(''', '', $field);
  12. $field = '`' . $field . '`';
  13. }
  14. return $field;
  15. }
  16. Copy code
function format($sql, $arg) {
$count = substr_count($sql, '%');
if (!$count) {
return $sql;
    } elseif ($count > count ($arg)) {
  1. throw new DbException('SQL string format error! This SQL need "' . $count . '" vars to replace into.', 0, $sql);
  2. }
  3. $len = strlen( $sql);
  4. $i = $find = 0;
  5. $ret = '';
  6. while ($i <= $len && $find < $count) {
  7. if ($sql{$i} == '%') {
  8. $next = $sql{$i + 1};
  9. if ($next == 't') {
  10. $ret .= self::table($arg[$find]);
  11. } elseif ($next == 's') {
$ret .= self::quote(is_array($arg[$find]) ? serialize($arg[$find]) : (string) $arg[$find] );
} elseif ($next == 'f') { $ret .= sprintf('%F', $arg[$find]); } elseif ($next == 'd') {
$ ret .= dintval($arg[$find]);
} elseif ($next == 'i') {
$ret .= $arg[$find];
    } elseif ($next == 'n') {
  1. if (!empty($arg[$find])) {
  2. $ret .= is_array($arg[$find]) ? implode(',', self::quote($arg[$find])) : self::quote($arg[$find]);
  3. } else {
  4. $ret .= '0';
  5. }
  6. } else {
  7. $ret .= self::quote($arg[$find]) ;
  8. }
  9. $i++;
  10. $find++;
  11. } else {
  12. $ret .= $sql{$i};
  13. }
  14. $i++;
  15. }
  16. if ($i < $len) {
  17. $ret . = substr($sql, $i);
  18. }
  19. return $ret;
  20. }
  21. }
  22. Copy code
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn