PageRenderTime 59ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/shafiqissani/ASTRA-College-Website
PHP | 283 lines | 210 code | 21 blank | 52 comment | 85 complexity | b99180bce13687b2d61c8b6c5280cb84 MD5 | raw file
  1. <?php
  2. /* $Id: import.lib.php 9905 2007-02-01 17:33:29Z lem9 $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. /* Library that provides common import functions that are used by import plugins */
  5. // We need to know something about user
  6. require_once('./libraries/check_user_privileges.lib.php');
  7. // We do this check
  8. define('PMA_CHK_DROP', 1);
  9. /**
  10. * Check whether timeout is getting close
  11. *
  12. * @return boolean true if timeout is close
  13. * @access public
  14. */
  15. function PMA_checkTimeout()
  16. {
  17. global $timestamp, $maximum_time, $timeout_passed;
  18. if ($maximum_time == 0) {
  19. return FALSE;
  20. } elseif ($timeout_passed) {
  21. return TRUE;
  22. /* 5 in next row might be too much */
  23. } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
  24. $timeout_passed = TRUE;
  25. return TRUE;
  26. } else {
  27. return FALSE;
  28. }
  29. }
  30. /**
  31. * Detects what compression filse uses
  32. *
  33. * @param string filename to check
  34. * @return string MIME type of compression, none for none
  35. * @access public
  36. */
  37. function PMA_detectCompression($filepath)
  38. {
  39. $file = @fopen($filepath, 'rb');
  40. if (!$file) {
  41. return FALSE;
  42. }
  43. $test = fread($file, 4);
  44. $len = strlen($test);
  45. fclose($file);
  46. if ($len >= 2 && $test[0] == chr(31) && $test[1] == chr(139)) {
  47. return 'application/gzip';
  48. }
  49. if ($len >= 3 && substr($test, 0, 3) == 'BZh') {
  50. return 'application/bzip2';
  51. }
  52. if ($len >= 4 && $test == "PK\003\004") {
  53. return 'application/zip';
  54. }
  55. return 'none';
  56. }
  57. /**
  58. * Runs query inside import buffer. This is needed to allow displaying
  59. * of last SELECT, SHOW or HANDLER results and similar nice stuff.
  60. *
  61. * @param string query to run
  62. * @param string query to display, this might be commented
  63. * @param bool whether to use control user for queries
  64. * @access public
  65. */
  66. function PMA_importRunQuery($sql = '', $full = '', $controluser = false)
  67. {
  68. global $import_run_buffer, $go_sql, $complete_query, $display_query, $sql_query, $cfg, $my_die, $error, $reload, $finished, $timeout_passed, $skip_queries, $executed_queries, $max_sql_len, $read_multiply, $cfg, $sql_query_disabled, $db, $run_query, $is_superuser, $message, $show_error_header;
  69. $read_multiply = 1;
  70. if (isset($import_run_buffer)) {
  71. // Should we skip something?
  72. if ($skip_queries > 0) {
  73. $skip_queries--;
  74. } else {
  75. if (!empty($import_run_buffer['sql']) && trim($import_run_buffer['sql']) != '') {
  76. $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
  77. if (!$sql_query_disabled) {
  78. $sql_query .= $import_run_buffer['full'];
  79. }
  80. if (!$cfg['AllowUserDropDatabase']
  81. && !$is_superuser
  82. && preg_match('@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?DATABASE @i', $import_run_buffer['sql'])) {
  83. $message = $GLOBALS['strNoDropDatabases'];
  84. $show_error_header = TRUE;
  85. $error = TRUE;
  86. } else {
  87. $executed_queries++;
  88. if ($run_query && $finished && empty($sql) && !$error && (
  89. (!empty($import_run_buffer['sql']) && preg_match('/^[\s]*(SELECT|SHOW|HANDLER)/i', $import_run_buffer['sql'])) ||
  90. ($executed_queries == 1)
  91. )) {
  92. $go_sql = TRUE;
  93. if (!$sql_query_disabled) {
  94. $complete_query = $sql_query;
  95. $display_query = $sql_query;
  96. } else {
  97. $complete_query = '';
  98. $display_query = '';
  99. }
  100. $sql_query = $import_run_buffer['sql'];
  101. } elseif ($run_query) {
  102. if ($controluser) {
  103. $result = PMA_query_as_cu($import_run_buffer['sql']);
  104. } else {
  105. $result = PMA_DBI_try_query($import_run_buffer['sql']);
  106. }
  107. $msg = '# ';
  108. if ($result === FALSE) { // execution failed
  109. if (!isset($my_die)) {
  110. $my_die = array();
  111. }
  112. $my_die[] = array('sql' => $import_run_buffer['full'], 'error' => PMA_DBI_getError());
  113. if ($cfg['VerboseMultiSubmit']) {
  114. $msg .= $GLOBALS['strError'];
  115. }
  116. if (!$cfg['IgnoreMultiSubmitErrors']) {
  117. $error = TRUE;
  118. return;
  119. }
  120. } elseif ($cfg['VerboseMultiSubmit']) {
  121. $a_num_rows = (int)@PMA_DBI_num_rows($result);
  122. $a_aff_rows = (int)@PMA_DBI_affected_rows();
  123. if ($a_num_rows > 0) {
  124. $msg .= $GLOBALS['strRows'] . ': ' . $a_num_rows;
  125. } elseif ($a_aff_rows > 0) {
  126. $a_rows =
  127. $msg .= $GLOBALS['strAffectedRows'] . ' ' . $a_aff_rows;
  128. } else {
  129. $msg .= $GLOBALS['strEmptyResultSet'];
  130. }
  131. }
  132. if (!$sql_query_disabled) {
  133. $sql_query .= $msg . "\n";
  134. }
  135. // If a 'USE <db>' SQL-clause was found and the query succeeded, set our current $db to the new one
  136. if ($result != FALSE && preg_match('@^[\s]*USE[[:space:]]*([\S]+)@i', $import_run_buffer['sql'], $match)) {
  137. $db = trim($match[1]);
  138. $db = trim($db,';'); // for example, USE abc;
  139. $reload = TRUE;
  140. }
  141. if ($result != FALSE && preg_match('@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)?(TABLE|DATABASE)[[:space:]]+(.+)@im', $import_run_buffer['sql'])) {
  142. $reload = TRUE;
  143. }
  144. } // end run query
  145. } // end if not DROP DATABASE
  146. } // end non empty query
  147. elseif (!empty($import_run_buffer['full'])) {
  148. if ($go_sql) {
  149. $complete_query .= $import_run_buffer['full'];
  150. $display_query .= $import_run_buffer['full'];
  151. } else {
  152. if (!$sql_query_disabled) {
  153. $sql_query .= $import_run_buffer['full'];
  154. }
  155. }
  156. }
  157. // check length of query unless we decided to pass it to sql.php
  158. if (!$go_sql) {
  159. if ($cfg['VerboseMultiSubmit'] && !empty($sql_query)) {
  160. if (strlen($sql_query) > 50000 || $executed_queries > 50 || $max_sql_len > 1000) {
  161. $sql_query = '';
  162. $sql_query_disabled = TRUE;
  163. }
  164. } else {
  165. if (strlen($sql_query) > 10000 || $executed_queries > 10 || $max_sql_len > 500) {
  166. $sql_query = '';
  167. $sql_query_disabled = TRUE;
  168. }
  169. }
  170. }
  171. } // end do query (no skip)
  172. } // end buffer exists
  173. // Do we have something to push into buffer?
  174. if (!empty($sql) || !empty($full)) {
  175. $import_run_buffer = array('sql' => $sql, 'full' => $full);
  176. } else {
  177. unset($GLOBALS['import_run_buffer']);
  178. }
  179. }
  180. /**
  181. * Returns next part of imported file/buffer
  182. *
  183. * @param integer size of buffer to read (this is maximal size
  184. * function will return)
  185. * @return string part of file/buffer
  186. * @access public
  187. */
  188. function PMA_importGetNextChunk($size = 32768)
  189. {
  190. global $import_file, $import_text, $finished, $compression, $import_handle, $offset, $charset_conversion, $charset_of_file, $charset, $read_multiply, $read_limit;
  191. // Add some progression while reading large amount of data
  192. if ($read_multiply <= 8) {
  193. $size *= $read_multiply;
  194. } else {
  195. $size *= 8;
  196. }
  197. $read_multiply++;
  198. // We can not read too much
  199. if ($size > $read_limit) {
  200. $size = $read_limit;
  201. }
  202. if (PMA_checkTimeout()) {
  203. return FALSE;
  204. }
  205. if ($finished) {
  206. return TRUE;
  207. }
  208. if ($import_file == 'none') {
  209. // Well this is not yet supported and tested, but should return content of textarea
  210. if (strlen($import_text) < $size) {
  211. $finished = TRUE;
  212. return $import_text;
  213. } else {
  214. $r = substr($import_text, 0, $size);
  215. $offset += $size;
  216. $import_text = substr($import_text, $size);
  217. return $r;
  218. }
  219. }
  220. switch ($compression) {
  221. case 'application/bzip2':
  222. $result = bzread($import_handle, $size);
  223. $finished = feof($import_handle);
  224. break;
  225. case 'application/gzip':
  226. $result = gzread($import_handle, $size);
  227. $finished = feof($import_handle);
  228. break;
  229. case 'application/zip':
  230. $result = substr($import_text, 0, $size);
  231. $import_text = substr($import_text, $size);
  232. $finished = empty($import_text);
  233. break;
  234. case 'none':
  235. $result = fread($import_handle, $size);
  236. $finished = feof($import_handle);
  237. break;
  238. }
  239. $offset += $size;
  240. if ($charset_conversion) {
  241. return PMA_convert_string($charset_of_file, $charset, $result);
  242. } else {
  243. /**
  244. * Skip possible byte order marks (I do not think we need more
  245. * charsets, but feel free to add more, you can use wikipedia for
  246. * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
  247. *
  248. * @todo BOM could be used for charset autodetection
  249. */
  250. if ($offset == $size) {
  251. // UTF-8
  252. if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
  253. $result = substr($result, 3);
  254. // UTF-16 BE, LE
  255. } elseif (strncmp($result, "\xFE\xFF", 2) == 0 || strncmp($result, "\xFF\xFE", 2) == 0) {
  256. $result = substr($result, 2);
  257. }
  258. }
  259. return $result;
  260. }
  261. }
  262. ?>