Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

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