PageRenderTime 1058ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/sql_uploads.php

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