PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/includes/sql_parse.php

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