130 |
kaklik |
1 |
<?php |
|
|
2 |
/*************************************************************************** |
|
|
3 |
* sql_parse.php |
|
|
4 |
* ------------------- |
|
|
5 |
* begin : Thu May 31, 2001 |
|
|
6 |
* copyright : (C) 2001 The phpBB Group |
|
|
7 |
* email : support@phpbb.com |
|
|
8 |
* |
|
|
9 |
* $Id: sql_parse.php,v 1.8 2002/03/18 23:53:12 psotfx Exp $ |
|
|
10 |
* |
|
|
11 |
****************************************************************************/ |
|
|
12 |
|
|
|
13 |
/*************************************************************************** |
|
|
14 |
* |
|
|
15 |
* This program is free software; you can redistribute it and/or modify |
|
|
16 |
* it under the terms of the GNU General Public License as published by |
|
|
17 |
* the Free Software Foundation; either version 2 of the License, or |
|
|
18 |
* (at your option) any later version. |
|
|
19 |
* |
|
|
20 |
***************************************************************************/ |
|
|
21 |
|
|
|
22 |
/*************************************************************************** |
|
|
23 |
* |
|
|
24 |
* These functions are mainly for use in the db_utilities under the admin |
|
|
25 |
* however in order to make these functions available elsewhere, specifically |
|
|
26 |
* in the installation phase of phpBB I have seperated out a couple of |
|
|
27 |
* functions into this file. JLH |
|
|
28 |
* |
|
|
29 |
\***************************************************************************/ |
|
|
30 |
|
|
|
31 |
// |
|
|
32 |
// remove_comments will strip the sql comment lines out of an uploaded sql file |
|
|
33 |
// specifically for mssql and postgres type files in the install.... |
|
|
34 |
// |
|
|
35 |
function remove_comments(&$output) |
|
|
36 |
{ |
|
|
37 |
$lines = explode("\n", $output); |
|
|
38 |
$output = ""; |
|
|
39 |
|
|
|
40 |
// try to keep mem. use down |
|
|
41 |
$linecount = count($lines); |
|
|
42 |
|
|
|
43 |
$in_comment = false; |
|
|
44 |
for($i = 0; $i < $linecount; $i++) |
|
|
45 |
{ |
|
|
46 |
if( preg_match("/^\/\*/", preg_quote($lines[$i])) ) |
|
|
47 |
{ |
|
|
48 |
$in_comment = true; |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
if( !$in_comment ) |
|
|
52 |
{ |
|
|
53 |
$output .= $lines[$i] . "\n"; |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
if( preg_match("/\*\/$/", preg_quote($lines[$i])) ) |
|
|
57 |
{ |
|
|
58 |
$in_comment = false; |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
unset($lines); |
|
|
63 |
return $output; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
// |
|
|
67 |
// remove_remarks will strip the sql comment lines out of an uploaded sql file |
|
|
68 |
// |
|
|
69 |
function remove_remarks($sql) |
|
|
70 |
{ |
|
|
71 |
$lines = explode("\n", $sql); |
|
|
72 |
|
|
|
73 |
// try to keep mem. use down |
|
|
74 |
$sql = ""; |
|
|
75 |
|
|
|
76 |
$linecount = count($lines); |
|
|
77 |
$output = ""; |
|
|
78 |
|
|
|
79 |
for ($i = 0; $i < $linecount; $i++) |
|
|
80 |
{ |
|
|
81 |
if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0)) |
|
|
82 |
{ |
|
|
83 |
if ($lines[$i][0] != "#") |
|
|
84 |
{ |
|
|
85 |
$output .= $lines[$i] . "\n"; |
|
|
86 |
} |
|
|
87 |
else |
|
|
88 |
{ |
|
|
89 |
$output .= "\n"; |
|
|
90 |
} |
|
|
91 |
// Trading a bit of speed for lower mem. use here. |
|
|
92 |
$lines[$i] = ""; |
|
|
93 |
} |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
return $output; |
|
|
97 |
|
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
// |
|
|
101 |
// split_sql_file will split an uploaded sql file into single sql statements. |
|
|
102 |
// Note: expects trim() to have already been run on $sql. |
|
|
103 |
// |
|
|
104 |
function split_sql_file($sql, $delimiter) |
|
|
105 |
{ |
|
|
106 |
// Split up our string into "possible" SQL statements. |
|
|
107 |
$tokens = explode($delimiter, $sql); |
|
|
108 |
|
|
|
109 |
// try to save mem. |
|
|
110 |
$sql = ""; |
|
|
111 |
$output = array(); |
|
|
112 |
|
|
|
113 |
// we don't actually care about the matches preg gives us. |
|
|
114 |
$matches = array(); |
|
|
115 |
|
|
|
116 |
// this is faster than calling count($oktens) every time thru the loop. |
|
|
117 |
$token_count = count($tokens); |
|
|
118 |
for ($i = 0; $i < $token_count; $i++) |
|
|
119 |
{ |
|
|
120 |
// Don't wanna add an empty string as the last thing in the array. |
|
|
121 |
if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0))) |
|
|
122 |
{ |
|
|
123 |
// This is the total number of single quotes in the token. |
|
|
124 |
$total_quotes = preg_match_all("/'/", $tokens[$i], $matches); |
|
|
125 |
// Counts single quotes that are preceded by an odd number of backslashes, |
|
|
126 |
// which means they're escaped quotes. |
|
|
127 |
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches); |
|
|
128 |
|
|
|
129 |
$unescaped_quotes = $total_quotes - $escaped_quotes; |
|
|
130 |
|
|
|
131 |
// If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal. |
|
|
132 |
if (($unescaped_quotes % 2) == 0) |
|
|
133 |
{ |
|
|
134 |
// It's a complete sql statement. |
|
|
135 |
$output[] = $tokens[$i]; |
|
|
136 |
// save memory. |
|
|
137 |
$tokens[$i] = ""; |
|
|
138 |
} |
|
|
139 |
else |
|
|
140 |
{ |
|
|
141 |
// incomplete sql statement. keep adding tokens until we have a complete one. |
|
|
142 |
// $temp will hold what we have so far. |
|
|
143 |
$temp = $tokens[$i] . $delimiter; |
|
|
144 |
// save memory.. |
|
|
145 |
$tokens[$i] = ""; |
|
|
146 |
|
|
|
147 |
// Do we have a complete statement yet? |
|
|
148 |
$complete_stmt = false; |
|
|
149 |
|
|
|
150 |
for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++) |
|
|
151 |
{ |
|
|
152 |
// This is the total number of single quotes in the token. |
|
|
153 |
$total_quotes = preg_match_all("/'/", $tokens[$j], $matches); |
|
|
154 |
// Counts single quotes that are preceded by an odd number of backslashes, |
|
|
155 |
// which means they're escaped quotes. |
|
|
156 |
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches); |
|
|
157 |
|
|
|
158 |
$unescaped_quotes = $total_quotes - $escaped_quotes; |
|
|
159 |
|
|
|
160 |
if (($unescaped_quotes % 2) == 1) |
|
|
161 |
{ |
|
|
162 |
// odd number of unescaped quotes. In combination with the previous incomplete |
|
|
163 |
// statement(s), we now have a complete statement. (2 odds always make an even) |
|
|
164 |
$output[] = $temp . $tokens[$j]; |
|
|
165 |
|
|
|
166 |
// save memory. |
|
|
167 |
$tokens[$j] = ""; |
|
|
168 |
$temp = ""; |
|
|
169 |
|
|
|
170 |
// exit the loop. |
|
|
171 |
$complete_stmt = true; |
|
|
172 |
// make sure the outer loop continues at the right point. |
|
|
173 |
$i = $j; |
|
|
174 |
} |
|
|
175 |
else |
|
|
176 |
{ |
|
|
177 |
// even number of unescaped quotes. We still don't have a complete statement. |
|
|
178 |
// (1 odd and 1 even always make an odd) |
|
|
179 |
$temp .= $tokens[$j] . $delimiter; |
|
|
180 |
// save memory. |
|
|
181 |
$tokens[$j] = ""; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
} // for.. |
|
|
185 |
} // else |
|
|
186 |
} |
|
|
187 |
} |
|
|
188 |
|
|
|
189 |
return $output; |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
?> |