PageRenderTime 288ms CodeModel.GetById 13ms RepoModel.GetById 2ms app.codeStats 0ms

/xampp/phpMyAdmin/libraries/import/sql.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 287 lines | 208 code | 17 blank | 62 comment | 99 complexity | 0ea2195ae2cdf419fdb2242b3383bd8b MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * SQL import plugin for phpMyAdmin
  5. *
  6. * @version $Id: sql.php 11387 2008-07-14 15:28:58Z lem9 $
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. *
  13. */
  14. if (isset($plugin_list)) {
  15. $plugin_list['sql'] = array(
  16. 'text' => 'strSQL',
  17. 'extension' => 'sql',
  18. 'options_text' => 'strOptions',
  19. );
  20. $compats = PMA_DBI_getCompatibilities();
  21. if (count($compats) > 0) {
  22. $values = array();
  23. foreach($compats as $val) {
  24. $values[$val] = $val;
  25. }
  26. $plugin_list['sql']['options'] = array(
  27. array(
  28. 'type' => 'select',
  29. 'name' => 'compatibility',
  30. 'text' => 'strSQLCompatibility',
  31. 'values' => $values,
  32. 'doc' => array(
  33. 'manual_MySQL_Database_Administration',
  34. 'Server_SQL_mode',
  35. ),
  36. ),
  37. );
  38. }
  39. /* We do not define function when plugin is just queried for information above */
  40. return;
  41. }
  42. $buffer = '';
  43. // Defaults for parser
  44. $sql = '';
  45. $start_pos = 0;
  46. $i = 0;
  47. $len= 0;
  48. $big_value = 2147483647;
  49. if (isset($_POST['sql_delimiter'])) {
  50. $sql_delimiter = $_POST['sql_delimiter'];
  51. } else {
  52. $sql_delimiter = ';';
  53. }
  54. // Handle compatibility option
  55. if (isset($_REQUEST['sql_compatibility'])) {
  56. PMA_DBI_try_query('SET SQL_MODE="' . $_REQUEST['sql_compatibility'] . '"');
  57. }
  58. /**
  59. * will be set in PMA_importGetNextChunk()
  60. *
  61. * @global boolean $GLOBALS['finished']
  62. */
  63. $GLOBALS['finished'] = false;
  64. while (!($GLOBALS['finished'] && $i >= $len) && !$error && !$timeout_passed) {
  65. $data = PMA_importGetNextChunk();
  66. if ($data === FALSE) {
  67. // subtract data we didn't handle yet and stop processing
  68. $offset -= strlen($buffer);
  69. break;
  70. } elseif ($data === TRUE) {
  71. // Handle rest of buffer
  72. } else {
  73. // Append new data to buffer
  74. $buffer .= $data;
  75. // free memory
  76. unset($data);
  77. // Do not parse string when we're not at the end and don't have ; inside
  78. if ((strpos($buffer, $sql_delimiter, $i) === FALSE) && !$GLOBALS['finished']) {
  79. continue;
  80. }
  81. }
  82. // Current length of our buffer
  83. $len = strlen($buffer);
  84. // Grab some SQL queries out of it
  85. while ($i < $len) {
  86. $found_delimiter = false;
  87. // Find first interesting character
  88. $old_i = $i;
  89. // this is about 7 times faster that looking for each sequence i
  90. // one by one with strpos()
  91. if (preg_match('/(\'|"|#|-- |\/\*|`|(?i)DELIMITER)/', $buffer, $matches, PREG_OFFSET_CAPTURE, $i)) {
  92. // in $matches, index 0 contains the match for the complete
  93. // expression but we don't use it
  94. $first_position = $matches[1][1];
  95. } else {
  96. $first_position = $big_value;
  97. }
  98. /**
  99. * @todo we should not look for a delimiter that might be
  100. * inside quotes (or even double-quotes)
  101. */
  102. // the cost of doing this one with preg_match() would be too high
  103. $first_sql_delimiter = strpos($buffer, $sql_delimiter, $i);
  104. if ($first_sql_delimiter === FALSE) {
  105. $first_sql_delimiter = $big_value;
  106. } else {
  107. $found_delimiter = true;
  108. }
  109. // set $i to the position of the first quote, comment.start or delimiter found
  110. $i = min($first_position, $first_sql_delimiter);
  111. if ($i == $big_value) {
  112. // none of the above was found in the string
  113. $i = $old_i;
  114. if (!$GLOBALS['finished']) {
  115. break;
  116. }
  117. // at the end there might be some whitespace...
  118. if (trim($buffer) == '') {
  119. $buffer = '';
  120. $len = 0;
  121. break;
  122. }
  123. // We hit end of query, go there!
  124. $i = strlen($buffer) - 1;
  125. }
  126. // Grab current character
  127. $ch = $buffer[$i];
  128. // Quotes
  129. if (strpos('\'"`', $ch) !== FALSE) {
  130. $quote = $ch;
  131. $endq = FALSE;
  132. while (!$endq) {
  133. // Find next quote
  134. $pos = strpos($buffer, $quote, $i + 1);
  135. // No quote? Too short string
  136. if ($pos === FALSE) {
  137. // We hit end of string => unclosed quote, but we handle it as end of query
  138. if ($GLOBALS['finished']) {
  139. $endq = TRUE;
  140. $i = $len - 1;
  141. }
  142. $found_delimiter = false;
  143. break;
  144. }
  145. // Was not the quote escaped?
  146. $j = $pos - 1;
  147. while ($buffer[$j] == '\\') $j--;
  148. // Even count means it was not escaped
  149. $endq = (((($pos - 1) - $j) % 2) == 0);
  150. // Skip the string
  151. $i = $pos;
  152. if ($first_sql_delimiter < $pos) {
  153. $found_delimiter = false;
  154. }
  155. }
  156. if (!$endq) {
  157. break;
  158. }
  159. $i++;
  160. // Aren't we at the end?
  161. if ($GLOBALS['finished'] && $i == $len) {
  162. $i--;
  163. } else {
  164. continue;
  165. }
  166. }
  167. // Not enough data to decide
  168. if ((($i == ($len - 1) && ($ch == '-' || $ch == '/'))
  169. || ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-')
  170. || ($ch == '/' && $buffer[$i + 1] == '*')))) && !$GLOBALS['finished']) {
  171. break;
  172. }
  173. // Comments
  174. if ($ch == '#'
  175. || ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-'
  176. && (($i < ($len - 2) && $buffer[$i + 2] <= ' ')
  177. || ($i == ($len - 1) && $GLOBALS['finished'])))
  178. || ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*')
  179. ) {
  180. // Copy current string to SQL
  181. if ($start_pos != $i) {
  182. $sql .= substr($buffer, $start_pos, $i - $start_pos);
  183. }
  184. // Skip the rest
  185. $j = $i;
  186. $i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i);
  187. // didn't we hit end of string?
  188. if ($i === FALSE) {
  189. if ($GLOBALS['finished']) {
  190. $i = $len - 1;
  191. } else {
  192. break;
  193. }
  194. }
  195. // Skip *
  196. if ($ch == '/') {
  197. // Check for MySQL conditional comments and include them as-is
  198. if ($buffer[$j + 2] == '!') {
  199. $comment = substr($buffer, $j + 3, $i - $j - 3);
  200. if (preg_match('/^[0-9]{5}/', $comment, $version)) {
  201. if ($version[0] <= PMA_MYSQL_INT_VERSION) {
  202. $sql .= substr($comment, 5);
  203. }
  204. } else {
  205. $sql .= $comment;
  206. }
  207. }
  208. $i++;
  209. }
  210. // Skip last char
  211. $i++;
  212. // Next query part will start here
  213. $start_pos = $i;
  214. // Aren't we at the end?
  215. if ($i == $len) {
  216. $i--;
  217. } else {
  218. continue;
  219. }
  220. }
  221. // Change delimiter, if redefined, and skip it (don't send to server!)
  222. if (strtoupper(substr($buffer, $i, 9)) == "DELIMITER"
  223. && ($buffer[$i + 9] <= ' ')
  224. && ($i < $len - 11)
  225. && strpos($buffer, "\n", $i + 11) !== FALSE) {
  226. $new_line_pos = strpos($buffer, "\n", $i + 10);
  227. $sql_delimiter = substr($buffer, $i + 10, $new_line_pos - $i - 10);
  228. $i = $new_line_pos + 1;
  229. // Next query part will start here
  230. $start_pos = $i;
  231. continue;
  232. }
  233. // End of SQL
  234. if ($found_delimiter || ($GLOBALS['finished'] && ($i == $len - 1))) {
  235. $tmp_sql = $sql;
  236. if ($start_pos < $len) {
  237. $length_to_grab = $i - $start_pos;
  238. if (! $found_delimiter) {
  239. $length_to_grab++;
  240. }
  241. $tmp_sql .= substr($buffer, $start_pos, $length_to_grab);
  242. unset($length_to_grab);
  243. }
  244. // Do not try to execute empty SQL
  245. if (! preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
  246. $sql = $tmp_sql;
  247. PMA_importRunQuery($sql, substr($buffer, 0, $i + strlen($sql_delimiter)));
  248. $buffer = substr($buffer, $i + strlen($sql_delimiter));
  249. // Reset parser:
  250. $len = strlen($buffer);
  251. $sql = '';
  252. $i = 0;
  253. $start_pos = 0;
  254. // Any chance we will get a complete query?
  255. //if ((strpos($buffer, ';') === FALSE) && !$GLOBALS['finished']) {
  256. if ((strpos($buffer, $sql_delimiter) === FALSE) && !$GLOBALS['finished']) {
  257. break;
  258. }
  259. } else {
  260. $i++;
  261. $start_pos = $i;
  262. }
  263. }
  264. } // End of parser loop
  265. } // End of import loop
  266. // Commit any possible data in buffers
  267. PMA_importRunQuery('', substr($buffer, 0, $len));
  268. PMA_importRunQuery();
  269. ?>