import.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Bookmark;
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\Encoding;
  11. use PhpMyAdmin\File;
  12. use PhpMyAdmin\Import;
  13. use PhpMyAdmin\ParseAnalyze;
  14. use PhpMyAdmin\Plugins;
  15. use PhpMyAdmin\Plugins\ImportPlugin;
  16. use PhpMyAdmin\Response;
  17. use PhpMyAdmin\Sql;
  18. use PhpMyAdmin\Url;
  19. use PhpMyAdmin\Util;
  20. /* Enable LOAD DATA LOCAL INFILE for LDI plugin */
  21. if (isset($_POST['format']) && $_POST['format'] == 'ldi') {
  22. define('PMA_ENABLE_LDI', 1);
  23. }
  24. /**
  25. * Get the variables sent or posted to this script and a core script
  26. */
  27. require_once 'libraries/common.inc.php';
  28. if (isset($_POST['show_as_php'])) {
  29. $GLOBALS['show_as_php'] = $_POST['show_as_php'];
  30. }
  31. // If there is a request to 'Simulate DML'.
  32. if (isset($_POST['simulate_dml'])) {
  33. Import::handleSimulateDmlRequest();
  34. exit;
  35. }
  36. $response = Response::getInstance();
  37. $sql = new Sql();
  38. // If it's a refresh console bookmarks request
  39. if (isset($_GET['console_bookmark_refresh'])) {
  40. $response->addJSON(
  41. 'console_message_bookmark', PhpMyAdmin\Console::getBookmarkContent()
  42. );
  43. exit;
  44. }
  45. // If it's a console bookmark add request
  46. if (isset($_POST['console_bookmark_add'])) {
  47. if (isset($_POST['label']) && isset($_POST['db'])
  48. && isset($_POST['bookmark_query']) && isset($_POST['shared'])
  49. ) {
  50. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  51. $bookmarkFields = array(
  52. 'bkm_database' => $_POST['db'],
  53. 'bkm_user' => $cfgBookmark['user'],
  54. 'bkm_sql_query' => $_POST['bookmark_query'],
  55. 'bkm_label' => $_POST['label']
  56. );
  57. $isShared = ($_POST['shared'] == 'true' ? true : false);
  58. $bookmark = Bookmark::createBookmark(
  59. $GLOBALS['dbi'],
  60. $GLOBALS['cfg']['Server']['user'],
  61. $bookmarkFields,
  62. $isShared
  63. );
  64. if ($bookmark !== false && $bookmark->save()) {
  65. $response->addJSON('message', __('Succeeded'));
  66. $response->addJSON('data', $bookmarkFields);
  67. $response->addJSON('isShared', $isShared);
  68. } else {
  69. $response->addJSON('message', __('Failed'));
  70. }
  71. die();
  72. } else {
  73. $response->addJSON('message', __('Incomplete params'));
  74. die();
  75. }
  76. }
  77. $format = '';
  78. /**
  79. * Sets globals from $_POST
  80. */
  81. $post_params = array(
  82. 'charset_of_file',
  83. 'format',
  84. 'import_type',
  85. 'is_js_confirmed',
  86. 'MAX_FILE_SIZE',
  87. 'message_to_show',
  88. 'noplugin',
  89. 'skip_queries',
  90. 'local_import_file'
  91. );
  92. foreach ($post_params as $one_post_param) {
  93. if (isset($_POST[$one_post_param])) {
  94. $GLOBALS[$one_post_param] = $_POST[$one_post_param];
  95. }
  96. }
  97. // reset import messages for ajax request
  98. $_SESSION['Import_message']['message'] = null;
  99. $_SESSION['Import_message']['go_back_url'] = null;
  100. // default values
  101. $GLOBALS['reload'] = false;
  102. // Use to identify current cycle is executing
  103. // a multiquery statement or stored routine
  104. if (!isset($_SESSION['is_multi_query'])) {
  105. $_SESSION['is_multi_query'] = false;
  106. }
  107. $ajax_reload = array();
  108. // Are we just executing plain query or sql file?
  109. // (eg. non import, but query box/window run)
  110. if (! empty($sql_query)) {
  111. // apply values for parameters
  112. if (! empty($_POST['parameterized'])
  113. && ! empty($_POST['parameters'])
  114. && is_array($_POST['parameters'])
  115. ) {
  116. $parameters = $_POST['parameters'];
  117. foreach ($parameters as $parameter => $replacement) {
  118. $quoted = preg_quote($parameter, '/');
  119. // making sure that :param does not apply values to :param1
  120. $sql_query = preg_replace(
  121. '/' . $quoted . '([^a-zA-Z0-9_])/',
  122. $GLOBALS['dbi']->escapeString($replacement) . '${1}',
  123. $sql_query
  124. );
  125. // for parameters the appear at the end of the string
  126. $sql_query = preg_replace(
  127. '/' . $quoted . '$/',
  128. $GLOBALS['dbi']->escapeString($replacement),
  129. $sql_query
  130. );
  131. }
  132. }
  133. // run SQL query
  134. $import_text = $sql_query;
  135. $import_type = 'query';
  136. $format = 'sql';
  137. $_SESSION['sql_from_query_box'] = true;
  138. // If there is a request to ROLLBACK when finished.
  139. if (isset($_POST['rollback_query'])) {
  140. Import::handleRollbackRequest($import_text);
  141. }
  142. // refresh navigation and main panels
  143. if (preg_match('/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i', $sql_query)) {
  144. $GLOBALS['reload'] = true;
  145. $ajax_reload['reload'] = true;
  146. }
  147. // refresh navigation panel only
  148. if (preg_match(
  149. '/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i',
  150. $sql_query
  151. )) {
  152. $ajax_reload['reload'] = true;
  153. }
  154. // do a dynamic reload if table is RENAMED
  155. // (by sending the instruction to the AJAX response handler)
  156. if (preg_match(
  157. '/^RENAME\s+TABLE\s+(.*?)\s+TO\s+(.*?)($|;|\s)/i',
  158. $sql_query,
  159. $rename_table_names
  160. )) {
  161. $ajax_reload['reload'] = true;
  162. $ajax_reload['table_name'] = PhpMyAdmin\Util::unQuote(
  163. $rename_table_names[2]
  164. );
  165. }
  166. $sql_query = '';
  167. } elseif (! empty($sql_file)) {
  168. // run uploaded SQL file
  169. $import_file = $sql_file;
  170. $import_type = 'queryfile';
  171. $format = 'sql';
  172. unset($sql_file);
  173. } elseif (! empty($_POST['id_bookmark'])) {
  174. // run bookmark
  175. $import_type = 'query';
  176. $format = 'sql';
  177. }
  178. // If we didn't get any parameters, either user called this directly, or
  179. // upload limit has been reached, let's assume the second possibility.
  180. if ($_POST == array() && $_GET == array()) {
  181. $message = PhpMyAdmin\Message::error(
  182. __(
  183. 'You probably tried to upload a file that is too large. Please refer ' .
  184. 'to %sdocumentation%s for a workaround for this limit.'
  185. )
  186. );
  187. $message->addParam('[doc@faq1-16]');
  188. $message->addParam('[/doc]');
  189. // so we can obtain the message
  190. $_SESSION['Import_message']['message'] = $message->getDisplay();
  191. $_SESSION['Import_message']['go_back_url'] = $GLOBALS['goto'];
  192. $response->setRequestStatus(false);
  193. $response->addJSON('message', $message);
  194. exit; // the footer is displayed automatically
  195. }
  196. // Add console message id to response output
  197. if (isset($_POST['console_message_id'])) {
  198. $response->addJSON('console_message_id', $_POST['console_message_id']);
  199. }
  200. /**
  201. * Sets globals from $_POST patterns, for import plugins
  202. * We only need to load the selected plugin
  203. */
  204. if (! in_array(
  205. $format,
  206. array(
  207. 'csv',
  208. 'ldi',
  209. 'mediawiki',
  210. 'ods',
  211. 'shp',
  212. 'sql',
  213. 'xml'
  214. )
  215. )
  216. ) {
  217. // this should not happen for a normal user
  218. // but only during an attack
  219. Core::fatalError('Incorrect format parameter');
  220. }
  221. $post_patterns = array(
  222. '/^force_file_/',
  223. '/^' . $format . '_/'
  224. );
  225. Core::setPostAsGlobal($post_patterns);
  226. // Check needed parameters
  227. PhpMyAdmin\Util::checkParameters(array('import_type', 'format'));
  228. // We don't want anything special in format
  229. $format = Core::securePath($format);
  230. if (strlen($table) > 0 && strlen($db) > 0) {
  231. $urlparams = array('db' => $db, 'table' => $table);
  232. } elseif (strlen($db) > 0) {
  233. $urlparams = array('db' => $db);
  234. } else {
  235. $urlparams = array();
  236. }
  237. // Create error and goto url
  238. if ($import_type == 'table') {
  239. $goto = 'tbl_import.php';
  240. } elseif ($import_type == 'database') {
  241. $goto = 'db_import.php';
  242. } elseif ($import_type == 'server') {
  243. $goto = 'server_import.php';
  244. } else {
  245. if (empty($goto) || !preg_match('@^(server|db|tbl)(_[a-z]*)*\.php$@i', $goto)) {
  246. if (strlen($table) > 0 && strlen($db) > 0) {
  247. $goto = 'tbl_structure.php';
  248. } elseif (strlen($db) > 0) {
  249. $goto = 'db_structure.php';
  250. } else {
  251. $goto = 'server_sql.php';
  252. }
  253. }
  254. }
  255. $err_url = $goto . Url::getCommon($urlparams);
  256. $_SESSION['Import_message']['go_back_url'] = $err_url;
  257. // Avoid setting selflink to 'import.php'
  258. // problem similar to bug 4276
  259. if (basename($_SERVER['SCRIPT_NAME']) === 'import.php') {
  260. $_SERVER['SCRIPT_NAME'] = $goto;
  261. }
  262. if (strlen($db) > 0) {
  263. $GLOBALS['dbi']->selectDb($db);
  264. }
  265. Util::setTimeLimit();
  266. if (! empty($cfg['MemoryLimit'])) {
  267. ini_set('memory_limit', $cfg['MemoryLimit']);
  268. }
  269. $timestamp = time();
  270. if (isset($_POST['allow_interrupt'])) {
  271. $maximum_time = ini_get('max_execution_time');
  272. } else {
  273. $maximum_time = 0;
  274. }
  275. // set default values
  276. $timeout_passed = false;
  277. $error = false;
  278. $read_multiply = 1;
  279. $finished = false;
  280. $offset = 0;
  281. $max_sql_len = 0;
  282. $file_to_unlink = '';
  283. $sql_query = '';
  284. $sql_query_disabled = false;
  285. $go_sql = false;
  286. $executed_queries = 0;
  287. $run_query = true;
  288. $charset_conversion = false;
  289. $reset_charset = false;
  290. $bookmark_created = false;
  291. $result = false;
  292. $msg = 'Sorry an unexpected error happened!';
  293. // Bookmark Support: get a query back from bookmark if required
  294. if (! empty($_POST['id_bookmark'])) {
  295. $id_bookmark = (int)$_POST['id_bookmark'];
  296. switch ($_POST['action_bookmark']) {
  297. case 0: // bookmarked query that have to be run
  298. $bookmark = Bookmark::get(
  299. $GLOBALS['dbi'],
  300. $GLOBALS['cfg']['Server']['user'],
  301. $db,
  302. $id_bookmark,
  303. 'id',
  304. isset($_POST['action_bookmark_all'])
  305. );
  306. if (! empty($_POST['bookmark_variable'])) {
  307. $import_text = $bookmark->applyVariables(
  308. $_POST['bookmark_variable']
  309. );
  310. } else {
  311. $import_text = $bookmark->getQuery();
  312. }
  313. // refresh navigation and main panels
  314. if (preg_match(
  315. '/^(DROP)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i',
  316. $import_text
  317. )) {
  318. $GLOBALS['reload'] = true;
  319. $ajax_reload['reload'] = true;
  320. }
  321. // refresh navigation panel only
  322. if (preg_match(
  323. '/^(CREATE|ALTER)\s+(VIEW|TABLE|DATABASE|SCHEMA)\s+/i',
  324. $import_text
  325. )
  326. ) {
  327. $ajax_reload['reload'] = true;
  328. }
  329. break;
  330. case 1: // bookmarked query that have to be displayed
  331. $bookmark = Bookmark::get(
  332. $GLOBALS['dbi'],
  333. $GLOBALS['cfg']['Server']['user'],
  334. $db,
  335. $id_bookmark
  336. );
  337. $import_text = $bookmark->getQuery();
  338. if ($response->isAjax()) {
  339. $message = PhpMyAdmin\Message::success(__('Showing bookmark'));
  340. $response->setRequestStatus($message->isSuccess());
  341. $response->addJSON('message', $message);
  342. $response->addJSON('sql_query', $import_text);
  343. $response->addJSON('action_bookmark', $_POST['action_bookmark']);
  344. exit;
  345. } else {
  346. $run_query = false;
  347. }
  348. break;
  349. case 2: // bookmarked query that have to be deleted
  350. $bookmark = Bookmark::get(
  351. $GLOBALS['dbi'],
  352. $GLOBALS['cfg']['Server']['user'],
  353. $db,
  354. $id_bookmark
  355. );
  356. if (! empty($bookmark)) {
  357. $bookmark->delete();
  358. if ($response->isAjax()) {
  359. $message = PhpMyAdmin\Message::success(
  360. __('The bookmark has been deleted.')
  361. );
  362. $response->setRequestStatus($message->isSuccess());
  363. $response->addJSON('message', $message);
  364. $response->addJSON('action_bookmark', $_POST['action_bookmark']);
  365. $response->addJSON('id_bookmark', $id_bookmark);
  366. exit;
  367. } else {
  368. $run_query = false;
  369. $error = true; // this is kind of hack to skip processing the query
  370. }
  371. }
  372. break;
  373. }
  374. } // end bookmarks reading
  375. // Do no run query if we show PHP code
  376. if (isset($GLOBALS['show_as_php'])) {
  377. $run_query = false;
  378. $go_sql = true;
  379. }
  380. // We can not read all at once, otherwise we can run out of memory
  381. $memory_limit = trim(ini_get('memory_limit'));
  382. // 2 MB as default
  383. if (empty($memory_limit)) {
  384. $memory_limit = 2 * 1024 * 1024;
  385. }
  386. // In case no memory limit we work on 10MB chunks
  387. if ($memory_limit == -1) {
  388. $memory_limit = 10 * 1024 * 1024;
  389. }
  390. // Calculate value of the limit
  391. $memoryUnit = mb_strtolower(substr($memory_limit, -1));
  392. if ('m' == $memoryUnit) {
  393. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024;
  394. } elseif ('k' == $memoryUnit) {
  395. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024;
  396. } elseif ('g' == $memoryUnit) {
  397. $memory_limit = (int)substr($memory_limit, 0, -1) * 1024 * 1024 * 1024;
  398. } else {
  399. $memory_limit = (int)$memory_limit;
  400. }
  401. // Just to be sure, there might be lot of memory needed for uncompression
  402. $read_limit = $memory_limit / 8;
  403. // handle filenames
  404. if (isset($_FILES['import_file'])) {
  405. $import_file = $_FILES['import_file']['tmp_name'];
  406. }
  407. if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) {
  408. // sanitize $local_import_file as it comes from a POST
  409. $local_import_file = Core::securePath($local_import_file);
  410. $import_file = PhpMyAdmin\Util::userDir($cfg['UploadDir'])
  411. . $local_import_file;
  412. /*
  413. * Do not allow symlinks to avoid security issues
  414. * (user can create symlink to file he can not access,
  415. * but phpMyAdmin can).
  416. */
  417. if (@is_link($import_file)) {
  418. $import_file = 'none';
  419. }
  420. } elseif (empty($import_file) || ! is_uploaded_file($import_file)) {
  421. $import_file = 'none';
  422. }
  423. // Do we have file to import?
  424. if ($import_file != 'none' && ! $error) {
  425. /**
  426. * Handle file compression
  427. */
  428. $import_handle = new File($import_file);
  429. $import_handle->checkUploadedFile();
  430. if ($import_handle->isError()) {
  431. Import::stop($import_handle->getError());
  432. }
  433. $import_handle->setDecompressContent(true);
  434. $import_handle->open();
  435. if ($import_handle->isError()) {
  436. Import::stop($import_handle->getError());
  437. }
  438. } elseif (! $error) {
  439. if (! isset($import_text) || empty($import_text)) {
  440. $message = PhpMyAdmin\Message::error(
  441. __(
  442. 'No data was received to import. Either no file name was ' .
  443. 'submitted, or the file size exceeded the maximum size permitted ' .
  444. 'by your PHP configuration. See [doc@faq1-16]FAQ 1.16[/doc].'
  445. )
  446. );
  447. Import::stop($message);
  448. }
  449. }
  450. // so we can obtain the message
  451. //$_SESSION['Import_message'] = $message->getDisplay();
  452. // Convert the file's charset if necessary
  453. if (Encoding::isSupported() && isset($charset_of_file)) {
  454. if ($charset_of_file != 'utf-8') {
  455. $charset_conversion = true;
  456. }
  457. } elseif (isset($charset_of_file) && $charset_of_file != 'utf-8') {
  458. $GLOBALS['dbi']->query('SET NAMES \'' . $charset_of_file . '\'');
  459. // We can not show query in this case, it is in different charset
  460. $sql_query_disabled = true;
  461. $reset_charset = true;
  462. }
  463. // Something to skip? (because timeout has passed)
  464. if (! $error && isset($_POST['skip'])) {
  465. $original_skip = $skip = intval($_POST['skip']);
  466. while ($skip > 0 && ! $finished) {
  467. Import::getNextChunk($skip < $read_limit ? $skip : $read_limit);
  468. // Disable read progressivity, otherwise we eat all memory!
  469. $read_multiply = 1;
  470. $skip -= $read_limit;
  471. }
  472. unset($skip);
  473. }
  474. // This array contain the data like numberof valid sql queries in the statement
  475. // and complete valid sql statement (which affected for rows)
  476. $sql_data = array('valid_sql' => array(), 'valid_queries' => 0);
  477. if (! $error) {
  478. /* @var $import_plugin ImportPlugin */
  479. $import_plugin = Plugins::getPlugin(
  480. "import",
  481. $format,
  482. 'libraries/classes/Plugins/Import/',
  483. $import_type
  484. );
  485. if ($import_plugin == null) {
  486. $message = PhpMyAdmin\Message::error(
  487. __('Could not load import plugins, please check your installation!')
  488. );
  489. Import::stop($message);
  490. } else {
  491. // Do the real import
  492. try {
  493. $default_fk_check = PhpMyAdmin\Util::handleDisableFKCheckInit();
  494. $import_plugin->doImport($sql_data);
  495. PhpMyAdmin\Util::handleDisableFKCheckCleanup($default_fk_check);
  496. } catch (Exception $e) {
  497. PhpMyAdmin\Util::handleDisableFKCheckCleanup($default_fk_check);
  498. throw $e;
  499. }
  500. }
  501. }
  502. if (isset($import_handle)) {
  503. $import_handle->close();
  504. }
  505. // Cleanup temporary file
  506. if ($file_to_unlink != '') {
  507. unlink($file_to_unlink);
  508. }
  509. // Reset charset back, if we did some changes
  510. if ($reset_charset) {
  511. $GLOBALS['dbi']->query('SET CHARACTER SET ' . $GLOBALS['charset_connection']);
  512. $GLOBALS['dbi']->setCollation($collation_connection);
  513. }
  514. // Show correct message
  515. if (! empty($id_bookmark) && $_POST['action_bookmark'] == 2) {
  516. $message = PhpMyAdmin\Message::success(__('The bookmark has been deleted.'));
  517. $display_query = $import_text;
  518. $error = false; // unset error marker, it was used just to skip processing
  519. } elseif (! empty($id_bookmark) && $_POST['action_bookmark'] == 1) {
  520. $message = PhpMyAdmin\Message::notice(__('Showing bookmark'));
  521. } elseif ($bookmark_created) {
  522. $special_message = '[br]' . sprintf(
  523. __('Bookmark %s has been created.'),
  524. htmlspecialchars($_POST['bkm_label'])
  525. );
  526. } elseif ($finished && ! $error) {
  527. // Do not display the query with message, we do it separately
  528. $display_query = ';';
  529. if ($import_type != 'query') {
  530. $message = PhpMyAdmin\Message::success(
  531. '<em>'
  532. . _ngettext(
  533. 'Import has been successfully finished, %d query executed.',
  534. 'Import has been successfully finished, %d queries executed.',
  535. $executed_queries
  536. )
  537. . '</em>'
  538. );
  539. $message->addParam($executed_queries);
  540. if (! empty($import_notice)) {
  541. $message->addHtml($import_notice);
  542. }
  543. if (! empty($local_import_file)) {
  544. $message->addText('(' . $local_import_file . ')');
  545. } else {
  546. $message->addText('(' . $_FILES['import_file']['name'] . ')');
  547. }
  548. }
  549. }
  550. // Did we hit timeout? Tell it user.
  551. if ($timeout_passed) {
  552. $urlparams['timeout_passed'] = '1';
  553. $urlparams['offset'] = $GLOBALS['offset'];
  554. if (isset($local_import_file)) {
  555. $urlparams['local_import_file'] = $local_import_file;
  556. }
  557. $importUrl = $err_url = $goto . Url::getCommon($urlparams);
  558. $message = PhpMyAdmin\Message::error(
  559. __(
  560. 'Script timeout passed, if you want to finish import,'
  561. . ' please %sresubmit the same file%s and import will resume.'
  562. )
  563. );
  564. $message->addParamHtml('<a href="' . $importUrl . '">');
  565. $message->addParamHtml('</a>');
  566. if ($offset == 0 || (isset($original_skip) && $original_skip == $offset)) {
  567. $message->addText(
  568. __(
  569. 'However on last run no data has been parsed,'
  570. . ' this usually means phpMyAdmin won\'t be able to'
  571. . ' finish this import unless you increase php time limits.'
  572. )
  573. );
  574. }
  575. }
  576. // if there is any message, copy it into $_SESSION as well,
  577. // so we can obtain it by AJAX call
  578. if (isset($message)) {
  579. $_SESSION['Import_message']['message'] = $message->getDisplay();
  580. }
  581. // Parse and analyze the query, for correct db and table name
  582. // in case of a query typed in the query window
  583. // (but if the query is too large, in case of an imported file, the parser
  584. // can choke on it so avoid parsing)
  585. $sqlLength = mb_strlen($sql_query);
  586. if ($sqlLength <= $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
  587. list(
  588. $analyzed_sql_results,
  589. $db,
  590. $table_from_sql
  591. ) = ParseAnalyze::sqlQuery($sql_query, $db);
  592. // @todo: possibly refactor
  593. extract($analyzed_sql_results);
  594. if ($table != $table_from_sql && !empty($table_from_sql)) {
  595. $table = $table_from_sql;
  596. }
  597. }
  598. // There was an error?
  599. if (isset($my_die)) {
  600. foreach ($my_die as $key => $die) {
  601. PhpMyAdmin\Util::mysqlDie(
  602. $die['error'], $die['sql'], false, $err_url, $error
  603. );
  604. }
  605. }
  606. if ($go_sql) {
  607. if (! empty($sql_data) && ($sql_data['valid_queries'] > 1)) {
  608. $_SESSION['is_multi_query'] = true;
  609. $sql_queries = $sql_data['valid_sql'];
  610. } else {
  611. $sql_queries = array($sql_query);
  612. }
  613. $html_output = '';
  614. foreach ($sql_queries as $sql_query) {
  615. // parse sql query
  616. list(
  617. $analyzed_sql_results,
  618. $db,
  619. $table_from_sql
  620. ) = ParseAnalyze::sqlQuery($sql_query, $db);
  621. // @todo: possibly refactor
  622. extract($analyzed_sql_results);
  623. // Check if User is allowed to issue a 'DROP DATABASE' Statement
  624. if ($sql->hasNoRightsToDropDatabase(
  625. $analyzed_sql_results, $cfg['AllowUserDropDatabase'], $GLOBALS['dbi']->isSuperuser()
  626. )) {
  627. PhpMyAdmin\Util::mysqlDie(
  628. __('"DROP DATABASE" statements are disabled.'),
  629. '',
  630. false,
  631. $_SESSION['Import_message']['go_back_url']
  632. );
  633. return;
  634. } // end if
  635. if ($table != $table_from_sql && !empty($table_from_sql)) {
  636. $table = $table_from_sql;
  637. }
  638. $html_output .= $sql->executeQueryAndGetQueryResponse(
  639. $analyzed_sql_results, // analyzed_sql_results
  640. false, // is_gotofile
  641. $db, // db
  642. $table, // table
  643. null, // find_real_end
  644. null, // sql_query_for_bookmark - see below
  645. null, // extra_data
  646. null, // message_to_show
  647. null, // message
  648. null, // sql_data
  649. $goto, // goto
  650. $pmaThemeImage, // pmaThemeImage
  651. null, // disp_query
  652. null, // disp_message
  653. null, // query_type
  654. $sql_query, // sql_query
  655. null, // selectedTables
  656. null // complete_query
  657. );
  658. }
  659. // sql_query_for_bookmark is not included in Sql::executeQueryAndGetQueryResponse
  660. // since only one bookmark has to be added for all the queries submitted through
  661. // the SQL tab
  662. if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
  663. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  664. $sql->storeTheQueryAsBookmark(
  665. $db, $cfgBookmark['user'],
  666. $_POST['sql_query'], $_POST['bkm_label'],
  667. isset($_POST['bkm_replace']) ? $_POST['bkm_replace'] : null
  668. );
  669. }
  670. $response->addJSON('ajax_reload', $ajax_reload);
  671. $response->addHTML($html_output);
  672. exit();
  673. } elseif ($result) {
  674. // Save a Bookmark with more than one queries (if Bookmark label given).
  675. if (! empty($_POST['bkm_label']) && ! empty($import_text)) {
  676. $cfgBookmark = Bookmark::getParams($GLOBALS['cfg']['Server']['user']);
  677. $sql->storeTheQueryAsBookmark(
  678. $db, $cfgBookmark['user'],
  679. $_POST['sql_query'], $_POST['bkm_label'],
  680. isset($_POST['bkm_replace']) ? $_POST['bkm_replace'] : null
  681. );
  682. }
  683. $response->setRequestStatus(true);
  684. $response->addJSON('message', PhpMyAdmin\Message::success($msg));
  685. $response->addJSON(
  686. 'sql_query',
  687. PhpMyAdmin\Util::getMessage($msg, $sql_query, 'success')
  688. );
  689. } elseif ($result == false) {
  690. $response->setRequestStatus(false);
  691. $response->addJSON('message', PhpMyAdmin\Message::error($msg));
  692. } else {
  693. $active_page = $goto;
  694. include '' . $goto;
  695. }
  696. // If there is request for ROLLBACK in the end.
  697. if (isset($_POST['rollback_query'])) {
  698. $GLOBALS['dbi']->query('ROLLBACK');
  699. }