PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cpg1.6.x/include/sql_parse.php

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