PageRenderTime 90ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ASTRA_Demo_Server/udrive/home/admin/www/phpMyAdmin/libraries/import/sql.php

https://github.com/shafiqissani/ASTRA-College-Website
PHP | 242 lines | 195 code | 8 blank | 39 comment | 100 complexity | c57c870dd567b979ac825092469c1638 MD5 | raw file
  1. <?php
  2. /* $Id: sql.php 9110 2006-06-13 15:10:08Z lem9 $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. /* SQL import plugin for phpMyAdmin */
  5. if (isset($plugin_list)) {
  6. $plugin_list['sql'] = array(
  7. 'text' => 'strSQL',
  8. 'extension' => 'sql',
  9. 'options_text' => 'strSQLImportOptions',
  10. );
  11. $compats = PMA_DBI_getCompatibilities();
  12. if (count($compats) > 0) {
  13. $values = array();
  14. foreach($compats as $val) {
  15. $values[$val] = $val;
  16. }
  17. $plugin_list['sql']['options'] = array(
  18. array('type' => 'select', 'name' => 'compatibility', 'text' => 'strSQLCompatibility', 'values' => $values, 'doc' => array('manual_MySQL_Database_Administration', 'Server_SQL_mode'))
  19. );
  20. }
  21. } else {
  22. /* We do not define function when plugin is just queried for information above */
  23. $buffer = '';
  24. // Defaults for parser
  25. $sql = '';
  26. $start_pos = 0;
  27. $i = 0;
  28. if (isset($_POST['sql_delimiter'])) {
  29. $sql_delimiter = $_POST['sql_delimiter'];
  30. } else {
  31. $sql_delimiter = ';';
  32. }
  33. // Handle compatibility option
  34. if (isset($_REQUEST['sql_compatibility'])) {
  35. PMA_DBI_try_query('SET SQL_MODE="' . $_REQUEST['sql_compatibility'] . '"');
  36. }
  37. while (!($finished && $i >= $len) && !$error && !$timeout_passed) {
  38. $data = PMA_importGetNextChunk();
  39. if ($data === FALSE) {
  40. // subtract data we didn't handle yet and stop processing
  41. $offset -= strlen($buffer);
  42. break;
  43. } elseif ($data === TRUE) {
  44. // Handle rest of buffer
  45. } else {
  46. // Append new data to buffer
  47. $buffer .= $data;
  48. // Do not parse string when we're not at the end and don't have ; inside
  49. if ((strpos($buffer, $sql_delimiter) === FALSE) && !$finished) {
  50. continue;
  51. }
  52. }
  53. // Current length of our buffer
  54. $len = strlen($buffer);
  55. // Grab some SQL queries out of it
  56. while ($i < $len) {
  57. $found_delimiter = false;
  58. // Find first interesting character, several strpos seem to be faster than simple loop in php:
  59. //while (($i < $len) && (strpos('\'";#-/', $buffer[$i]) === FALSE)) $i++;
  60. //if ($i == $len) break;
  61. $oi = $i;
  62. $p1 = strpos($buffer, '\'', $i);
  63. if ($p1 === FALSE) {
  64. $p1 = 2147483647;
  65. }
  66. $p2 = strpos($buffer, '"', $i);
  67. if ($p2 === FALSE) {
  68. $p2 = 2147483647;
  69. }
  70. $p3 = strpos($buffer, $sql_delimiter, $i);
  71. if ($p3 === FALSE) {
  72. $p3 = 2147483647;
  73. } else {
  74. $found_delimiter = true;
  75. }
  76. $p4 = strpos($buffer, '#', $i);
  77. if ($p4 === FALSE) {
  78. $p4 = 2147483647;
  79. }
  80. $p5 = strpos($buffer, '--', $i);
  81. if ($p5 === FALSE || $p5 >= ($len - 2) || $buffer[$p5 + 2] > ' ') {
  82. $p5 = 2147483647;
  83. }
  84. $p6 = strpos($buffer, '/*', $i);
  85. if ($p6 === FALSE) {
  86. $p6 = 2147483647;
  87. }
  88. $p7 = strpos($buffer, '`', $i);
  89. if ($p7 === FALSE) {
  90. $p7 = 2147483647;
  91. }
  92. $i = min ($p1, $p2, $p3, $p4, $p5, $p6, $p7);
  93. unset($p1, $p2, $p3, $p4, $p5, $p6, $p7);
  94. if ($i == 2147483647) {
  95. $i = $oi;
  96. if (!$finished) {
  97. break;
  98. }
  99. // at the end there might be some whitespace...
  100. if (trim($buffer) == '') {
  101. $buffer = '';
  102. $len = 0;
  103. break;
  104. }
  105. // We hit end of query, go there!
  106. $i = strlen($buffer) - 1;
  107. }
  108. // Grab current character
  109. $ch = $buffer[$i];
  110. // Quotes
  111. if (!(strpos('\'"`', $ch) === FALSE)) {
  112. $quote = $ch;
  113. $endq = FALSE;
  114. while (!$endq) {
  115. // Find next quote
  116. $pos = strpos($buffer, $quote, $i + 1);
  117. // No quote? Too short string
  118. if ($pos === FALSE) {
  119. // We hit end of string => unclosed quote, but we handle it as end of query
  120. if ($finished) {
  121. $endq = TRUE;
  122. $i = $len - 1;
  123. }
  124. break;
  125. }
  126. // Was not the quote escaped?
  127. $j = $pos - 1;
  128. while ($buffer[$j] == '\\') $j--;
  129. // Even count means it was not escaped
  130. $endq = (((($pos - 1) - $j) % 2) == 0);
  131. // Skip the string
  132. $i = $pos;
  133. }
  134. if (!$endq) {
  135. break;
  136. }
  137. $i++;
  138. // Aren't we at the end?
  139. if ($finished && $i == $len) {
  140. $i--;
  141. } else {
  142. continue;
  143. }
  144. }
  145. // Not enough data to decide
  146. if ((($i == ($len - 1) && ($ch == '-' || $ch == '/'))
  147. || ($i == ($len - 2) && (($ch == '-' && $buffer[$i + 1] == '-') || ($ch == '/' && $buffer[$i + 1] == '*')))
  148. ) && !$finished) {
  149. break;
  150. }
  151. // Comments
  152. if ($ch == '#'
  153. || ($i < ($len - 1) && $ch == '-' && $buffer[$i + 1] == '-' && (($i < ($len - 2) && $buffer[$i + 2] <= ' ') || ($i == ($len - 1) && $finished)))
  154. || ($i < ($len - 1) && $ch == '/' && $buffer[$i + 1] == '*')
  155. ) {
  156. // Copy current string to SQL
  157. if ($start_pos != $i) {
  158. $sql .= substr($buffer, $start_pos, $i - $start_pos);
  159. }
  160. // Skip the rest
  161. $j = $i;
  162. $i = strpos($buffer, $ch == '/' ? '*/' : "\n", $i);
  163. // didn't we hit end of string?
  164. if ($i === FALSE) {
  165. if ($finished) {
  166. $i = $len - 1;
  167. } else {
  168. break;
  169. }
  170. }
  171. // Skip *
  172. if ($ch == '/') {
  173. // Check for MySQL conditional comments and include them as-is
  174. if ($buffer[$j + 2] == '!') {
  175. $comment = substr($buffer, $j + 3, $i - $j - 3);
  176. if (preg_match('/^[0-9]{5}/', $comment, $version)) {
  177. if ($version[0] <= PMA_MYSQL_INT_VERSION) {
  178. $sql .= substr($comment, 5);
  179. }
  180. } else {
  181. $sql .= $comment;
  182. }
  183. }
  184. $i++;
  185. }
  186. // Skip last char
  187. $i++;
  188. // Next query part will start here
  189. $start_pos = $i;
  190. // Aren't we at the end?
  191. if ($i == $len) {
  192. $i--;
  193. } else {
  194. continue;
  195. }
  196. }
  197. // End of SQL
  198. if ($found_delimiter || ($finished && ($i == $len - 1))) {
  199. $tmp_sql = $sql;
  200. if ($start_pos < $len) {
  201. $length_to_grab = $i - $start_pos;
  202. if (!$found_delimiter) {
  203. $length_to_grab++;
  204. }
  205. $tmp_sql .= substr($buffer, $start_pos, $length_to_grab);
  206. unset($length_to_grab);
  207. }
  208. // Do not try to execute empty SQL
  209. if (!preg_match('/^([\s]*;)*$/', trim($tmp_sql))) {
  210. $sql = $tmp_sql;
  211. PMA_importRunQuery($sql, substr($buffer, 0, $i + strlen($sql_delimiter)));
  212. $buffer = substr($buffer, $i + strlen($sql_delimiter));
  213. // Reset parser:
  214. $len = strlen($buffer);
  215. $sql = '';
  216. $i = 0;
  217. $start_pos = 0;
  218. // Any chance we will get a complete query?
  219. //if ((strpos($buffer, ';') === FALSE) && !$finished) {
  220. if ((strpos($buffer, $sql_delimiter) === FALSE) && !$finished) {
  221. break;
  222. }
  223. } else {
  224. $i++;
  225. $start_pos = $i;
  226. }
  227. }
  228. } // End of parser loop
  229. } // End of import loop
  230. // Commit any possible data in buffers
  231. PMA_importRunQuery('', substr($buffer, 0, $len));
  232. PMA_importRunQuery();
  233. }
  234. ?>