PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/phpMyAdmin/import.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 412 lines | 318 code | 43 blank | 51 comment | 86 complexity | 020e20e6adba773596bd0e4286f7028b MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Core script for import, this is just the glue around all other stuff
  5. *
  6. * @uses PMA_Bookmark_getList()
  7. * @version $Id: import.php 11934 2008-11-21 17:35:47Z lem9 $
  8. */
  9. /**
  10. * Get the variables sent or posted to this script and a core script
  11. */
  12. require_once './libraries/common.inc.php';
  13. $GLOBALS['js_include'][] = 'functions.js';
  14. // default values
  15. $GLOBALS['reload'] = false;
  16. // Are we just executing plain query or sql file? (eg. non import, but query box/window run)
  17. if (!empty($sql_query)) {
  18. // run SQL query
  19. $import_text = $sql_query;
  20. $import_type = 'query';
  21. $format = 'sql';
  22. // refresh left frame on changes in table or db structure
  23. if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
  24. $GLOBALS['reload'] = true;
  25. }
  26. $sql_query = '';
  27. } elseif (!empty($sql_localfile)) {
  28. // run SQL file on server
  29. $local_import_file = $sql_localfile;
  30. $import_type = 'queryfile';
  31. $format = 'sql';
  32. unset($sql_localfile);
  33. } elseif (!empty($sql_file)) {
  34. // run uploaded SQL file
  35. $import_file = $sql_file;
  36. $import_type = 'queryfile';
  37. $format = 'sql';
  38. unset($sql_file);
  39. } elseif (!empty($id_bookmark)) {
  40. // run bookmark
  41. $import_type = 'query';
  42. $format = 'sql';
  43. }
  44. // If we didn't get any parameters, either user called this directly, or
  45. // upload limit has been reached, let's assume the second possibility.
  46. if ($_POST == array() && $_GET == array()) {
  47. require_once './libraries/header.inc.php';
  48. $message = PMA_Message::error('strUploadLimit');
  49. $message->addParam('[a@./Documentation.html#faq1_16@_blank]');
  50. $message->addParam('[/a]');
  51. $message->display();
  52. require './libraries/footer.inc.php';
  53. }
  54. // Check needed parameters
  55. PMA_checkParameters(array('import_type', 'format'));
  56. // We don't want anything special in format
  57. $format = PMA_securePath($format);
  58. // Import functions
  59. require_once './libraries/import.lib.php';
  60. // Create error and goto url
  61. if ($import_type == 'table') {
  62. $err_url = 'tbl_import.php?' . PMA_generate_common_url($db, $table);
  63. $goto = 'tbl_import.php';
  64. } elseif ($import_type == 'database') {
  65. $err_url = 'db_import.php?' . PMA_generate_common_url($db);
  66. $goto = 'db_import.php';
  67. } elseif ($import_type == 'server') {
  68. $err_url = 'server_import.php?' . PMA_generate_common_url();
  69. $goto = 'server_import.php';
  70. } else {
  71. if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
  72. if (strlen($table) && strlen($db)) {
  73. $goto = 'tbl_structure.php';
  74. } elseif (strlen($db)) {
  75. $goto = 'db_structure.php';
  76. } else {
  77. $goto = 'server_sql.php';
  78. }
  79. }
  80. if (strlen($table) && strlen($db)) {
  81. $common = PMA_generate_common_url($db, $table);
  82. } elseif (strlen($db)) {
  83. $common = PMA_generate_common_url($db);
  84. } else {
  85. $common = PMA_generate_common_url();
  86. }
  87. $err_url = $goto
  88. . '?' . $common
  89. . (preg_match('@^tbl_[a-z]*\.php$@', $goto) ? '&amp;table=' . urlencode($table) : '');
  90. }
  91. if (strlen($db)) {
  92. PMA_DBI_select_db($db);
  93. }
  94. @set_time_limit($cfg['ExecTimeLimit']);
  95. if (!empty($cfg['MemoryLimit'])) {
  96. @ini_set('memory_limit', $cfg['MemoryLimit']);
  97. }
  98. $timestamp = time();
  99. if (isset($allow_interrupt)) {
  100. $maximum_time = ini_get('max_execution_time');
  101. } else {
  102. $maximum_time = 0;
  103. }
  104. // set default values
  105. $timeout_passed = FALSE;
  106. $error = FALSE;
  107. $read_multiply = 1;
  108. $finished = FALSE;
  109. $offset = 0;
  110. $max_sql_len = 0;
  111. $file_to_unlink = '';
  112. $sql_query = '';
  113. $sql_query_disabled = FALSE;
  114. $go_sql = FALSE;
  115. $executed_queries = 0;
  116. $run_query = TRUE;
  117. $charset_conversion = FALSE;
  118. $reset_charset = FALSE;
  119. $bookmark_created = FALSE;
  120. // Bookmark Support: get a query back from bookmark if required
  121. if (!empty($id_bookmark)) {
  122. require_once './libraries/bookmark.lib.php';
  123. switch ($action_bookmark) {
  124. case 0: // bookmarked query that have to be run
  125. $import_text = PMA_Bookmark_get($db, $id_bookmark, 'id', isset($action_bookmark_all));
  126. if (isset($bookmark_variable) && !empty($bookmark_variable)) {
  127. $import_text = preg_replace('|/\*(.*)\[VARIABLE\](.*)\*/|imsU', '${1}' . PMA_sqlAddslashes($bookmark_variable) . '${2}', $import_text);
  128. }
  129. // refresh left frame on changes in table or db structure
  130. if (preg_match('/^(CREATE|ALTER|DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $import_text)) {
  131. $GLOBALS['reload'] = true;
  132. }
  133. break;
  134. case 1: // bookmarked query that have to be displayed
  135. $import_text = PMA_Bookmark_get($db, $id_bookmark);
  136. $run_query = FALSE;
  137. break;
  138. case 2: // bookmarked query that have to be deleted
  139. $import_text = PMA_Bookmark_get($db, $id_bookmark);
  140. PMA_Bookmark_delete($db, $id_bookmark);
  141. $run_query = FALSE;
  142. $error = TRUE; // this is kind of hack to skip processing the query
  143. break;
  144. }
  145. } // end bookmarks reading
  146. // Do no run query if we show PHP code
  147. if (isset($GLOBALS['show_as_php'])) {
  148. $run_query = FALSE;
  149. $go_sql = TRUE;
  150. }
  151. // Store the query as a bookmark before executing it if bookmarklabel was given
  152. if (!empty($bkm_label) && !empty($import_text)) {
  153. require_once './libraries/bookmark.lib.php';
  154. $bfields = array(
  155. 'dbase' => $db,
  156. 'user' => $cfg['Bookmark']['user'],
  157. 'query' => urlencode($import_text),
  158. 'label' => $bkm_label
  159. );
  160. // Should we replace bookmark?
  161. if (isset($bkm_replace)) {
  162. $bookmarks = PMA_Bookmark_getList($db);
  163. foreach ($bookmarks as $key => $val) {
  164. if ($val == $bkm_label) {
  165. PMA_Bookmark_delete($db, $key);
  166. }
  167. }
  168. }
  169. PMA_Bookmark_save($bfields, isset($bkm_all_users));
  170. $bookmark_created = TRUE;
  171. } // end store bookmarks
  172. // We can not read all at once, otherwise we can run out of memory
  173. $memory_limit = trim(@ini_get('memory_limit'));
  174. // 2 MB as default
  175. if (empty($memory_limit)) {
  176. $memory_limit = 2 * 1024 * 1024;
  177. }
  178. // In case no memory limit we work on 10MB chunks
  179. if ($memory_limit == -1) {
  180. $memory_limit = 10 * 1024 * 1024;
  181. }
  182. // Calculate value of the limit
  183. if (strtolower(substr($memory_limit, -1)) == 'm') {
  184. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
  185. } elseif (strtolower(substr($memory_limit, -1)) == 'k') {
  186. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
  187. } elseif (strtolower(substr($memory_limit, -1)) == 'g') {
  188. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
  189. } else {
  190. $memory_limit = (int)$memory_limit;
  191. }
  192. $read_limit = $memory_limit / 8; // Just to be sure, there might be lot of memory needed for uncompression
  193. // handle filenames
  194. if (!empty($local_import_file) && !empty($cfg['UploadDir'])) {
  195. // sanitize $local_import_file as it comes from a POST
  196. $local_import_file = PMA_securePath($local_import_file);
  197. $import_file = PMA_userDir($cfg['UploadDir']) . $local_import_file;
  198. } elseif (empty($import_file) || !is_uploaded_file($import_file)) {
  199. $import_file = 'none';
  200. }
  201. // Do we have file to import?
  202. if ($import_file != 'none' && !$error) {
  203. // work around open_basedir and other limitations
  204. $open_basedir = @ini_get('open_basedir');
  205. // If we are on a server with open_basedir, we must move the file
  206. // before opening it. The doc explains how to create the "./tmp"
  207. // directory
  208. if (!empty($open_basedir)) {
  209. $tmp_subdir = (PMA_IS_WINDOWS ? '.\\tmp\\' : './tmp/');
  210. if (is_writable($tmp_subdir)) {
  211. $import_file_new = $tmp_subdir . basename($import_file);
  212. if (move_uploaded_file($import_file, $import_file_new)) {
  213. $import_file = $import_file_new;
  214. $file_to_unlink = $import_file_new;
  215. }
  216. }
  217. }
  218. /**
  219. * Handle file compression
  220. * @todo duplicate code exists in File.class.php
  221. */
  222. $compression = PMA_detectCompression($import_file);
  223. if ($compression === FALSE) {
  224. $message = PMA_Message::error('strFileCouldNotBeRead');
  225. $error = TRUE;
  226. } else {
  227. switch ($compression) {
  228. case 'application/bzip2':
  229. if ($cfg['BZipDump'] && @function_exists('bzopen')) {
  230. $import_handle = @bzopen($import_file, 'r');
  231. } else {
  232. $message = PMA_Message::error('strUnsupportedCompressionDetected');
  233. $message->addParam($compression);
  234. $error = TRUE;
  235. }
  236. break;
  237. case 'application/gzip':
  238. if ($cfg['GZipDump'] && @function_exists('gzopen')) {
  239. $import_handle = @gzopen($import_file, 'r');
  240. } else {
  241. $message = PMA_Message::error('strUnsupportedCompressionDetected');
  242. $message->addParam($compression);
  243. $error = TRUE;
  244. }
  245. break;
  246. case 'application/zip':
  247. if ($cfg['ZipDump'] && @function_exists('zip_open')) {
  248. include_once './libraries/zip_extension.lib.php';
  249. $result = PMA_getZipContents($import_file);
  250. if (! empty($result['error'])) {
  251. $message = PMA_Message::rawError($result['error']);
  252. $error = TRUE;
  253. } else {
  254. $import_text = $result['data'];
  255. }
  256. } else {
  257. $message = PMA_Message::error('strUnsupportedCompressionDetected');
  258. $message->addParam($compression);
  259. $error = TRUE;
  260. }
  261. break;
  262. case 'none':
  263. $import_handle = @fopen($import_file, 'r');
  264. break;
  265. default:
  266. $message = PMA_Message::error('strUnsupportedCompressionDetected');
  267. $message->addParam($compression);
  268. $error = TRUE;
  269. break;
  270. }
  271. }
  272. if (!$error && $import_handle === FALSE) {
  273. $message = PMA_Message::error('strFileCouldNotBeRead');
  274. $error = TRUE;
  275. }
  276. } elseif (!$error) {
  277. if (!isset($import_text) || empty($import_text)) {
  278. $message = PMA_Message::error('strNoDataReceived');
  279. $error = TRUE;
  280. }
  281. }
  282. // Convert the file's charset if necessary
  283. if ($cfg['AllowAnywhereRecoding'] && isset($charset_of_file)) {
  284. if ($charset_of_file != $charset) {
  285. $charset_conversion = TRUE;
  286. }
  287. } elseif (isset($charset_of_file) && $charset_of_file != 'utf8') {
  288. PMA_DBI_query('SET NAMES \'' . $charset_of_file . '\'');
  289. // We can not show query in this case, it is in different charset
  290. $sql_query_disabled = TRUE;
  291. $reset_charset = TRUE;
  292. }
  293. // Something to skip?
  294. if (!$error && isset($skip)) {
  295. $original_skip = $skip;
  296. while ($skip > 0) {
  297. PMA_importGetNextChunk($skip < $read_limit ? $skip : $read_limit);
  298. $read_multiply = 1; // Disable read progresivity, otherwise we eat all memory!
  299. $skip -= $read_limit;
  300. }
  301. unset($skip);
  302. }
  303. if (!$error) {
  304. // Check for file existance
  305. if (!file_exists('./libraries/import/' . $format . '.php')) {
  306. $error = TRUE;
  307. $message = PMA_Message::error('strCanNotLoadImportPlugins');
  308. } else {
  309. // Do the real import
  310. $plugin_param = $import_type;
  311. require './libraries/import/' . $format . '.php';
  312. }
  313. }
  314. if (! $error && FALSE !== $import_handle && NULL !== $import_handle) {
  315. fclose($import_handle);
  316. }
  317. // Cleanup temporary file
  318. if ($file_to_unlink != '') {
  319. unlink($file_to_unlink);
  320. }
  321. // Reset charset back, if we did some changes
  322. if ($reset_charset) {
  323. PMA_DBI_query('SET CHARACTER SET utf8');
  324. PMA_DBI_query('SET SESSION collation_connection =\'' . $collation_connection . '\'');
  325. }
  326. // Show correct message
  327. if (!empty($id_bookmark) && $action_bookmark == 2) {
  328. $message = PMA_Message::success('strBookmarkDeleted');
  329. $display_query = $import_text;
  330. $error = FALSE; // unset error marker, it was used just to skip processing
  331. } elseif (!empty($id_bookmark) && $action_bookmark == 1) {
  332. $message = PMA_Message::notice('strShowingBookmark');
  333. } elseif ($bookmark_created) {
  334. $special_message = '[br]' . sprintf($strBookmarkCreated, htmlspecialchars($bkm_label));
  335. } elseif ($finished && !$error) {
  336. if ($import_type == 'query') {
  337. $message = PMA_Message::success();
  338. } else {
  339. $message = PMA_Message::success('strImportSuccessfullyFinished');
  340. $message->addParam($executed_queries);
  341. }
  342. }
  343. // Did we hit timeout? Tell it user.
  344. if ($timeout_passed) {
  345. $message = PMA_Message::error('strTimeoutPassed');
  346. if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
  347. $message->addString('strTimeoutNothingParsed');
  348. }
  349. }
  350. // Parse and analyze the query, for correct db and table name
  351. // in case of a query typed in the query window
  352. require_once './libraries/parse_analyze.lib.php';
  353. // There was an error?
  354. if (isset($my_die)) {
  355. foreach ($my_die AS $key => $die) {
  356. PMA_mysqlDie($die['error'], $die['sql'], '', $err_url, $error);
  357. }
  358. }
  359. if ($go_sql) {
  360. require './sql.php';
  361. } else {
  362. $active_page = $goto;
  363. require './' . $goto;
  364. }
  365. exit();
  366. ?>