C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\turbodbadmin\php\sql_parse.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php

    
class turboSqlParser
    
{
        var 
$debug;
        function 
oob()
        {
            return (
$this->|| $this->>= $this->l);
        }
        
        function 
search($inRegEx)
        {
            
//if ($this->debug)
            //    print_r('searching for: ' . $inRegEx . '<br>');
            
$matches false;
            
preg_match($inRegEx$this->s$matchesPREG_OFFSET_CAPTURE$this->p0);
            
$this->= ($matches $matches[0][1] : -1);
            if (
$this->oob())
             return 
false;
            
//$this->d = substr($this->s, $this->p, 1);
            
$this->$this->s{$this->p};
            if (
$this->debug)
                
print_r('found: ' $this->' at ' $this->'<br>');
            return 
true;
        }
        
        function 
tokenize($inD 0)
        {
            
$this->+= $inD;
            if (
$this->$this->p0)
            {
                
$tok substr($this->s$this->p0$this->$this->p0);
                
$this->.= $tok;
                if (
$this->debug && $this->&& $tok
                    
print_r('token: [' $this->']<br>');
                
//$this->s = substr($this->s, $this->p);
                //$this->l = strlen($this->s);
                
$this->p0 $this->p;
            }
        }
            
        function 
pusht()
        {
            if (
$this->t)
                
array_push($this->r$this->t);
        }
        
        function 
pushToken($inD 0)
        {
            
$this->tokenize($inD);
            
$this->pusht();
            
$this->'';
        }
        
        function 
start($inS)
        {
            
$this->debug false;
            
$this->$inS;
            
$this->strlen($this->s);
            
$this->= array();
            
$this->'';
            
$this->p0 0;
            
$this->0;
            
$this->t0 0;
            
$this->'';
            
$this->analyze();
        }
        
        function 
finish()
        {
            
// FIXME: SJM please review. 1 character was being removed from end of last token
            // so added logic to prevent a negative length param for substr
            
if ($this->$this->p0 0)
                
$this->.= substr($this->s$this->p0$this->$this->p0);
            else    
                
$this->.= substr($this->s$this->p0);
            
$this->pusht();
        }
        
        function 
do_separator()
        {
            if (
$this->debug)
                
print_r('doSeparator:<br>');
            
$this->tokenize(1);
        }
        
        function 
do_escape()
        {
            if (
$this->debug)
                
print_r('doEscape:<br>');
            
$this->tokenize(2);
        }
        
        function 
do_sql_comment()
        {
            if (
$this->debug)
                
print_r('doDashComment:<br>');
            
$this->tokenize(($this->== '-' 1));
            if (
$this->search('/[\n]/'))
                
$this->tokenize(1);
        }
        
        function 
do_c_comment()
        {
            if (
$this->debug)
                
print_r('doCComment:<br>');
            
$this->tokenize(2);
            
$n 1;
            while (
$n && $this->search('/\/\*|\*\//'))
            {
                switch (
$this->d) {
                    case 
'/':
                        
$n++;
                        break;
                    case 
'*':
                        
$n--;
                        break;
                }
                
$this->tokenize(2);
            }
        }
         
        function 
do_literal()
        {
            if (
$this->debug)
                
print_r('doLiteral:<br>');
            
$this->tokenize(1);
            
$rx "/" $this->"|\\\\" "/";
            while (
$this->search($rx))
            {
                switch (
$this->d) {
                    case 
'\\':
                        
$this->do_escape();
                        break;
                    default:
                        
$this->tokenize(1);
                        return;
                }
            }
        } 
        
        function 
analyze()
        {
            while (
$this->search("/--|\/\*|[#'\"`\\;]/"))
            {
                if (
$this->== '\\')
                    
$this->do_escape();
                else
                {
                    
$this->pushToken();
                    switch (
$this->d)
                    {
                        case 
';':
                            
$this->do_separator();
                            break;
                        case 
'/'
                            
$this->do_c_comment();
                            break;
                        case 
'-'
                        case 
'#':
                            
$this->do_sql_comment();
                            break;
                        default: 
                            
$this->do_literal();
                            break;
                    }
                    
$this->pushToken();
                }
            }
            
$this->finish();
        }

        function 
split_sql($inSql)
        {
            
$t ';';
            
$this->start($inSql);
            
$tokens $this->r;
            
$result = array();
            
$x = array();
            foreach (
$tokens as $token)
            {
                if (
$token != $t)
                    
array_push($x$token);
                else
                {
                    
$b trim(join(''$x));
                    if (
$b)
                        
array_push($result$b);
                    
$x = array();
                }
            }
            
            
$b trim(join(''$x));
            if (
$b && $b != $t)
                
array_push($result$b);
            return 
$result;    
        }
        
        function 
is_bad_command($inCmd)
        {
            
$matches null;
            
$this->start($inCmd);
            foreach (
$this->as $token)
            {
                
preg_match('/^(--|\/\*|[#\'"`\\;])/'$token$matches);
                if (
count($matches))
                    continue;
                
preg_match('/DROP.*DATABASE/i'$token$matches);    
                if (
count($matches))
                    return 
true;
                
preg_match('/LOAD|DATA\\s|INFILE/i'$token$matches);    
                if (
count($matches))
                    return 
true;    
            }
            return 
false;
        }
    }
    
    
$sql_parser = new turboSqlParser();
    
/*
    $sql = file_get_contents('edfromtheweb_backup.sql');
    //$sql = 'SELECT * FROM testing_database.baseball';
    $time = time();
    $queries = $sql_parser->split_sql($sql);
    $time = time() - $time;
    echo('<pre>');
    print_r(strval($time) . "\n");
    print_r($queries);
    echo('</pre>');
    */
?>