1: <?php
2:
3: /**
4: * You may not change or alter any portion of this comment or credits
5: * of supporting developers from this source code or any supporting source code
6: * which is considered copyrighted (c) material of the original comment or credit authors.
7: *
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: */
12:
13: /**
14: * provide some utility methods for databases
15: *
16: * PHP version 5.3
17: *
18: * @category Xoops\Class\Database\SqlUtility
19: * @package SqlUtility
20: * @author Kazumi Ono <onokazu@xoops.org>
21: * @copyright 2013 XOOPS Project (http://xoops.org)
22: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23: * @version Release: 2.6.0
24: * @link http://xoops.org
25: * @since 2.6.0
26: */
27:
28: class SqlUtility
29: {
30: /**
31: * Function from phpMyAdmin (http://phpwizard.net/projects/phpMyAdmin/)
32: *
33: * Removes comment and splits large sql files into individual queries
34: *
35: * Last revision: September 23, 2001 - gandon
36: *
37: * @param array &$ret the splitted sql commands
38: * @param string $sql the sql commands
39: *
40: * @return bool always true
41: * @access public
42: */
43: public static function splitMySqlFile(&$ret, $sql)
44: {
45: $sql = trim($sql);
46: $sql_len = strlen($sql);
47: $string_start = '';
48: $in_string = false;
49:
50: for ($i = 0; $i < $sql_len; ++$i) {
51: $char = $sql[$i];
52: if ($in_string) {
53: while (true) {
54: $i = strpos($sql, $string_start, $i);
55: if (!$i) {
56: $ret[] = $sql;
57: return true;
58: } else {
59: if ($string_start === '`' || $sql[$i - 1] !== '\\') {
60: $string_start = '';
61: $in_string = false;
62: break;
63: } else {
64: $j = 2;
65: $escaped_backslash = false;
66: while ($i - $j > 0 && $sql[$i - $j] === '\\') {
67: $escaped_backslash = !$escaped_backslash;
68: ++$j;
69: }
70: if ($escaped_backslash) {
71: $string_start = '';
72: $in_string = false;
73: break;
74: } else {
75: ++$i;
76: }
77: }
78: }
79: }
80: } else {
81: if ($char === ';') {
82: $ret[] = substr($sql, 0, $i);
83: $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
84: $sql_len = strlen($sql);
85: if ($sql_len) {
86: $i = -1;
87: } else {
88: return true;
89: }
90: } else {
91: if (($char === '"') || ($char === '\'') || ($char === '`')) {
92: $in_string = true;
93: $string_start = $char;
94: } else {
95: if ($char === '#' || ($char === ' ' && $i > 1 && $sql[$i - 2] . $sql[$i - 1] == '--')) {
96: $start_of_comment = (($sql[$i] === '#') ? $i : $i - 2);
97: $end_of_comment = (strpos(' ' . $sql, "\012", $i + 2)) ? strpos(' ' . $sql, "\012", $i + 2)
98: : strpos(' ' . $sql, "\015", $i + 2);
99: if (!$end_of_comment) {
100: return true;
101: } else {
102: $sql = substr($sql, 0, $start_of_comment) . ltrim(substr($sql, $end_of_comment));
103: $sql_len = strlen($sql);
104: $i--;
105: }
106: }
107: }
108: }
109: }
110: }
111:
112: if (!empty($sql) && trim($sql) != '') {
113: $ret[] = $sql;
114: }
115: return true;
116: }
117:
118: /**
119: * add a prefix.'_' to all tablenames in a query
120: *
121: * @param string $query valid SQL query string
122: * @param string $prefix prefix to add to all table names
123: *
124: * @return mixed FALSE on failure
125: */
126: public static function prefixQuery($query, $prefix)
127: {
128: $pattern = "/^(INSERT[\s]+INTO|CREATE[\s]+TABLE|ALTER[\s]+TABLE|UPDATE)(\s)+([`]?)([^`\s]+)\\3(\s)+/siU";
129: $pattern2 = "/^(DROP TABLE)(\s)+([`]?)([^`\s]+)\\3(\s)?$/siU";
130: if (preg_match($pattern, $query, $matches)
131: || preg_match($pattern2, $query, $matches)
132: ) {
133: $replace = "\\1 " . $prefix . "_\\4\\5";
134: $matches[0] = preg_replace($pattern, $replace, $query);
135: return $matches;
136: }
137: return false;
138: }
139: }
140: