PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Quản lý website bán phụ tùng oto PHP/phpMyAdmin1/import.php

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