import.lib.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Library that provides common import functions that are used by import plugins
  5. *
  6. * @package PhpMyAdmin-Import
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * We need to know something about user
  13. */
  14. require_once './libraries/check_user_privileges.lib.php';
  15. /**
  16. * We do this check, DROP DATABASE does not need to be confirmed elsewhere
  17. */
  18. define('PMA_CHK_DROP', 1);
  19. /**
  20. * Checks whether timeout is getting close
  21. *
  22. * @return boolean true if timeout is close
  23. * @access public
  24. */
  25. function PMA_checkTimeout()
  26. {
  27. global $timestamp, $maximum_time, $timeout_passed;
  28. if ($maximum_time == 0) {
  29. return false;
  30. } elseif ($timeout_passed) {
  31. return true;
  32. /* 5 in next row might be too much */
  33. } elseif ((time() - $timestamp) > ($maximum_time - 5)) {
  34. $timeout_passed = true;
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. }
  40. /**
  41. * Detects what compression the file uses
  42. *
  43. * @param string $filepath filename to check
  44. *
  45. * @return string MIME type of compression, none for none
  46. * @access public
  47. */
  48. function PMA_detectCompression($filepath)
  49. {
  50. $file = @fopen($filepath, 'rb');
  51. if (! $file) {
  52. return false;
  53. }
  54. return PMA_Util::getCompressionMimeType($file);
  55. }
  56. /**
  57. * Runs query inside import buffer. This is needed to allow displaying
  58. * of last SELECT, SHOW or HANDLER results and similar nice stuff.
  59. *
  60. * @param string $sql query to run
  61. * @param string $full query to display, this might be commented
  62. * @param bool $controluser whether to use control user for queries
  63. * @param array &$sql_data SQL parse data storage
  64. *
  65. * @return void
  66. * @access public
  67. */
  68. function PMA_importRunQuery($sql = '', $full = '', $controluser = false,
  69. &$sql_data = array()
  70. ) {
  71. global $import_run_buffer, $go_sql, $complete_query, $display_query,
  72. $sql_query, $my_die, $error, $reload,
  73. $last_query_with_results, $result, $msg,
  74. $skip_queries, $executed_queries, $max_sql_len, $read_multiply,
  75. $cfg, $sql_query_disabled, $db, $run_query, $is_superuser;
  76. $read_multiply = 1;
  77. if (!isset($import_run_buffer)) {
  78. // Do we have something to push into buffer?
  79. $import_run_buffer = PMA_ImportRunQuery_post(
  80. $import_run_buffer, $sql, $full
  81. );
  82. return;
  83. }
  84. // Should we skip something?
  85. if ($skip_queries > 0) {
  86. $skip_queries--;
  87. // Do we have something to push into buffer?
  88. $import_run_buffer = PMA_ImportRunQuery_post(
  89. $import_run_buffer, $sql, $full
  90. );
  91. return;
  92. }
  93. if (! empty($import_run_buffer['sql'])
  94. && trim($import_run_buffer['sql']) != ''
  95. ) {
  96. // USE query changes the database, son need to track
  97. // while running multiple queries
  98. $is_use_query
  99. = (stripos($import_run_buffer['sql'], "use ") !== false)
  100. ? true
  101. : false;
  102. $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql']));
  103. if (! $sql_query_disabled) {
  104. $sql_query .= $import_run_buffer['full'];
  105. }
  106. $pattern = '@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?'
  107. . 'DATABASE @i';
  108. if (! $cfg['AllowUserDropDatabase']
  109. && ! $is_superuser
  110. && preg_match($pattern, $import_run_buffer['sql'])
  111. ) {
  112. $GLOBALS['message'] = PMA_Message::error(
  113. __('"DROP DATABASE" statements are disabled.')
  114. );
  115. $error = true;
  116. } else {
  117. $executed_queries++;
  118. $pattern = '/^[\s]*(SELECT|SHOW|HANDLER)/i';
  119. if ($run_query
  120. && $GLOBALS['finished']
  121. && empty($sql)
  122. && ! $error
  123. && ((! empty($import_run_buffer['sql'])
  124. && preg_match($pattern, $import_run_buffer['sql']))
  125. || ($executed_queries == 1))
  126. ) {
  127. $go_sql = true;
  128. if (! $sql_query_disabled) {
  129. $complete_query = $sql_query;
  130. $display_query = $sql_query;
  131. } else {
  132. $complete_query = '';
  133. $display_query = '';
  134. }
  135. $sql_query = $import_run_buffer['sql'];
  136. $sql_data['valid_sql'][] = $import_run_buffer['sql'];
  137. if (! isset($sql_data['valid_queries'])) {
  138. $sql_data['valid_queries'] = 0;
  139. }
  140. $sql_data['valid_queries']++;
  141. // If a 'USE <db>' SQL-clause was found,
  142. // set our current $db to the new one
  143. list($db, $reload) = PMA_lookForUse(
  144. $import_run_buffer['sql'],
  145. $db,
  146. $reload
  147. );
  148. } elseif ($run_query) {
  149. if ($controluser) {
  150. $result = PMA_queryAsControlUser(
  151. $import_run_buffer['sql']
  152. );
  153. } else {
  154. $result = $GLOBALS['dbi']
  155. ->tryQuery($import_run_buffer['sql']);
  156. }
  157. $msg = '# ';
  158. if ($result === false) { // execution failed
  159. if (! isset($my_die)) {
  160. $my_die = array();
  161. }
  162. $my_die[] = array(
  163. 'sql' => $import_run_buffer['full'],
  164. 'error' => $GLOBALS['dbi']->getError()
  165. );
  166. $msg .= __('Error');
  167. if (! $cfg['IgnoreMultiSubmitErrors']) {
  168. $error = true;
  169. return;
  170. }
  171. } else {
  172. $a_num_rows = (int)@$GLOBALS['dbi']->numRows($result);
  173. $a_aff_rows = (int)@$GLOBALS['dbi']->affectedRows();
  174. if ($a_num_rows > 0) {
  175. $msg .= __('Rows') . ': ' . $a_num_rows;
  176. $last_query_with_results = $import_run_buffer['sql'];
  177. } elseif ($a_aff_rows > 0) {
  178. $message = PMA_Message::getMessageForAffectedRows(
  179. $a_aff_rows
  180. );
  181. $msg .= $message->getMessage();
  182. } else {
  183. $msg .= __(
  184. 'MySQL returned an empty result set (i.e. zero '
  185. . 'rows).'
  186. );
  187. }
  188. if (($a_num_rows > 0) || $is_use_query) {
  189. $sql_data['valid_sql'][] = $import_run_buffer['sql'];
  190. if (! isset($sql_data['valid_queries'])) {
  191. $sql_data['valid_queries'] = 0;
  192. }
  193. $sql_data['valid_queries']++;
  194. }
  195. }
  196. if (! $sql_query_disabled) {
  197. $sql_query .= $msg . "\n";
  198. }
  199. // If a 'USE <db>' SQL-clause was found and the query
  200. // succeeded, set our current $db to the new one
  201. if ($result != false) {
  202. list($db, $reload) = PMA_lookForUse(
  203. $import_run_buffer['sql'],
  204. $db,
  205. $reload
  206. );
  207. }
  208. $pattern = '@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)'
  209. . '?(TABLE|DATABASE)[[:space:]]+(.+)@im';
  210. if ($result != false
  211. && preg_match($pattern, $import_run_buffer['sql'])
  212. ) {
  213. $reload = true;
  214. }
  215. } // end run query
  216. } // end if not DROP DATABASE
  217. // end non empty query
  218. } elseif (! empty($import_run_buffer['full'])) {
  219. if ($go_sql) {
  220. $complete_query .= $import_run_buffer['full'];
  221. $display_query .= $import_run_buffer['full'];
  222. } else {
  223. if (! $sql_query_disabled) {
  224. $sql_query .= $import_run_buffer['full'];
  225. }
  226. }
  227. }
  228. // check length of query unless we decided to pass it to sql.php
  229. // (if $run_query is false, we are just displaying so show
  230. // the complete query in the textarea)
  231. if (! $go_sql && $run_query) {
  232. if (! empty($sql_query)) {
  233. if (strlen($sql_query) > 50000
  234. || $executed_queries > 50
  235. || $max_sql_len > 1000
  236. ) {
  237. $sql_query = '';
  238. $sql_query_disabled = true;
  239. }
  240. }
  241. }
  242. // Do we have something to push into buffer?
  243. $import_run_buffer = PMA_ImportRunQuery_post($import_run_buffer, $sql, $full);
  244. }
  245. /**
  246. * Return import run buffer
  247. *
  248. * @param array $import_run_buffer Buffer of queries for import
  249. * @param string $sql SQL query
  250. * @param string $full Query to display
  251. *
  252. * @return array Buffer of queries for import
  253. */
  254. function PMA_ImportRunQuery_post($import_run_buffer, $sql, $full)
  255. {
  256. if (!empty($sql) || !empty($full)) {
  257. $import_run_buffer = array('sql' => $sql, 'full' => $full);
  258. return $import_run_buffer;
  259. } else {
  260. unset($GLOBALS['import_run_buffer']);
  261. return $import_run_buffer;
  262. }
  263. }
  264. /**
  265. * Looks for the presence of USE to possibly change current db
  266. *
  267. * @param string $buffer buffer to examine
  268. * @param string $db current db
  269. * @param bool $reload reload
  270. *
  271. * @return array (current or new db, whether to reload)
  272. * @access public
  273. */
  274. function PMA_lookForUse($buffer, $db, $reload)
  275. {
  276. if (preg_match('@^[\s]*USE[[:space:]]+([\S]+)@i', $buffer, $match)) {
  277. $db = trim($match[1]);
  278. $db = trim($db, ';'); // for example, USE abc;
  279. // $db must not contain the escape characters generated by backquote()
  280. // ( used in PMA_buildSQL() as: backquote($db_name), and then called
  281. // in PMA_importRunQuery() which in turn calls PMA_lookForUse() )
  282. $db = PMA_Util::unQuote($db);
  283. $reload = true;
  284. }
  285. return(array($db, $reload));
  286. }
  287. /**
  288. * Returns next part of imported file/buffer
  289. *
  290. * @param int $size size of buffer to read
  291. * (this is maximal size function will return)
  292. *
  293. * @return string part of file/buffer
  294. * @access public
  295. */
  296. function PMA_importGetNextChunk($size = 32768)
  297. {
  298. global $compression, $import_handle, $charset_conversion, $charset_of_file,
  299. $read_multiply;
  300. // Add some progression while reading large amount of data
  301. if ($read_multiply <= 8) {
  302. $size *= $read_multiply;
  303. } else {
  304. $size *= 8;
  305. }
  306. $read_multiply++;
  307. // We can not read too much
  308. if ($size > $GLOBALS['read_limit']) {
  309. $size = $GLOBALS['read_limit'];
  310. }
  311. if (PMA_checkTimeout()) {
  312. return false;
  313. }
  314. if ($GLOBALS['finished']) {
  315. return true;
  316. }
  317. if ($GLOBALS['import_file'] == 'none') {
  318. // Well this is not yet supported and tested,
  319. // but should return content of textarea
  320. if (strlen($GLOBALS['import_text']) < $size) {
  321. $GLOBALS['finished'] = true;
  322. return $GLOBALS['import_text'];
  323. } else {
  324. $r = substr($GLOBALS['import_text'], 0, $size);
  325. $GLOBALS['offset'] += $size;
  326. $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
  327. return $r;
  328. }
  329. }
  330. switch ($compression) {
  331. case 'application/bzip2':
  332. $result = bzread($import_handle, $size);
  333. $GLOBALS['finished'] = feof($import_handle);
  334. break;
  335. case 'application/gzip':
  336. $result = gzread($import_handle, $size);
  337. $GLOBALS['finished'] = feof($import_handle);
  338. break;
  339. case 'application/zip':
  340. $result = substr($GLOBALS['import_text'], 0, $size);
  341. $GLOBALS['import_text'] = substr($GLOBALS['import_text'], $size);
  342. $GLOBALS['finished'] = empty($GLOBALS['import_text']);
  343. break;
  344. case 'none':
  345. $result = fread($import_handle, $size);
  346. $GLOBALS['finished'] = feof($import_handle);
  347. break;
  348. }
  349. $GLOBALS['offset'] += $size;
  350. if ($charset_conversion) {
  351. return PMA_convertString($charset_of_file, 'utf-8', $result);
  352. }
  353. /**
  354. * Skip possible byte order marks (I do not think we need more
  355. * charsets, but feel free to add more, you can use wikipedia for
  356. * reference: <http://en.wikipedia.org/wiki/Byte_Order_Mark>)
  357. *
  358. * @todo BOM could be used for charset autodetection
  359. */
  360. if ($GLOBALS['offset'] == $size) {
  361. // UTF-8
  362. if (strncmp($result, "\xEF\xBB\xBF", 3) == 0) {
  363. $result = substr($result, 3);
  364. // UTF-16 BE, LE
  365. } elseif (strncmp($result, "\xFE\xFF", 2) == 0
  366. || strncmp($result, "\xFF\xFE", 2) == 0
  367. ) {
  368. $result = substr($result, 2);
  369. }
  370. }
  371. return $result;
  372. }
  373. /**
  374. * Returns the "Excel" column name (i.e. 1 = "A", 26 = "Z", 27 = "AA", etc.)
  375. *
  376. * This functions uses recursion to build the Excel column name.
  377. *
  378. * The column number (1-26) is converted to the responding
  379. * ASCII character (A-Z) and returned.
  380. *
  381. * If the column number is bigger than 26 (= num of letters in alfabet),
  382. * an extra character needs to be added. To find this extra character,
  383. * the number is divided by 26 and this value is passed to another instance
  384. * of the same function (hence recursion). In that new instance the number is
  385. * evaluated again, and if it is still bigger than 26, it is divided again
  386. * and passed to another instance of the same function. This continues until
  387. * the number is smaller than 26. Then the last called function returns
  388. * the corresponding ASCII character to the function that called it.
  389. * Each time a called function ends an extra character is added to the column name.
  390. * When the first function is reached, the last character is addded and the complete
  391. * column name is returned.
  392. *
  393. * @param int $num the column number
  394. *
  395. * @return string The column's "Excel" name
  396. * @access public
  397. */
  398. function PMA_getColumnAlphaName($num)
  399. {
  400. $A = 65; // ASCII value for capital "A"
  401. $col_name = "";
  402. if ($num > 26) {
  403. $div = (int)($num / 26);
  404. $remain = (int)($num % 26);
  405. // subtract 1 of divided value in case the modulus is 0,
  406. // this is necessary because A-Z has no 'zero'
  407. if ($remain == 0) {
  408. $div--;
  409. }
  410. // recursive function call
  411. $col_name = PMA_getColumnAlphaName($div);
  412. // use modulus as new column number
  413. $num = $remain;
  414. }
  415. if ($num == 0) {
  416. // use 'Z' if column number is 0,
  417. // this is necessary because A-Z has no 'zero'
  418. $col_name .= chr(($A + 26) - 1);
  419. } else {
  420. // convert column number to ASCII character
  421. $col_name .= chr(($A + $num) - 1);
  422. }
  423. return $col_name;
  424. }
  425. /**
  426. * Returns the column number based on the Excel name.
  427. * So "A" = 1, "Z" = 26, "AA" = 27, etc.
  428. *
  429. * Basicly this is a base26 (A-Z) to base10 (0-9) conversion.
  430. * It iterates through all characters in the column name and
  431. * calculates the corresponding value, based on character value
  432. * (A = 1, ..., Z = 26) and position in the string.
  433. *
  434. * @param string $name column name(i.e. "A", or "BC", etc.)
  435. *
  436. * @return int The column number
  437. * @access public
  438. */
  439. function PMA_getColumnNumberFromName($name)
  440. {
  441. if (empty($name)) {
  442. return 0;
  443. }
  444. $name = strtoupper($name);
  445. $num_chars = strlen($name);
  446. $column_number = 0;
  447. for ($i = 0; $i < $num_chars; ++$i) {
  448. // read string from back to front
  449. $char_pos = ($num_chars - 1) - $i;
  450. // convert capital character to ASCII value
  451. // and subtract 64 to get corresponding decimal value
  452. // ASCII value of "A" is 65, "B" is 66, etc.
  453. // Decimal equivalent of "A" is 1, "B" is 2, etc.
  454. $number = (ord($name[$char_pos]) - 64);
  455. // base26 to base10 conversion : multiply each number
  456. // with corresponding value of the position, in this case
  457. // $i=0 : 1; $i=1 : 26; $i=2 : 676; ...
  458. $column_number += $number * PMA_Util::pow(26, $i);
  459. }
  460. return $column_number;
  461. }
  462. /**
  463. * Constants definitions
  464. */
  465. /* MySQL type defs */
  466. define("NONE", 0);
  467. define("VARCHAR", 1);
  468. define("INT", 2);
  469. define("DECIMAL", 3);
  470. define("BIGINT", 4);
  471. define("GEOMETRY", 5);
  472. /* Decimal size defs */
  473. define("M", 0);
  474. define("D", 1);
  475. define("FULL", 2);
  476. /* Table array defs */
  477. define("TBL_NAME", 0);
  478. define("COL_NAMES", 1);
  479. define("ROWS", 2);
  480. /* Analysis array defs */
  481. define("TYPES", 0);
  482. define("SIZES", 1);
  483. define("FORMATTEDSQL", 2);
  484. /**
  485. * Obtains the precision (total # of digits) from a size of type decimal
  486. *
  487. * @param string $last_cumulative_size Size of type decimal
  488. *
  489. * @return int Precision of the given decimal size notation
  490. * @access public
  491. */
  492. function PMA_getDecimalPrecision($last_cumulative_size)
  493. {
  494. return (int)substr($last_cumulative_size, 0, strpos($last_cumulative_size, ","));
  495. }
  496. /**
  497. * Obtains the scale (# of digits to the right of the decimal point)
  498. * from a size of type decimal
  499. *
  500. * @param string $last_cumulative_size Size of type decimal
  501. *
  502. * @return int Scale of the given decimal size notation
  503. * @access public
  504. */
  505. function PMA_getDecimalScale($last_cumulative_size)
  506. {
  507. return (int) substr(
  508. $last_cumulative_size,
  509. (strpos($last_cumulative_size, ",") + 1),
  510. (strlen($last_cumulative_size) - strpos($last_cumulative_size, ","))
  511. );
  512. }
  513. /**
  514. * Obtains the decimal size of a given cell
  515. *
  516. * @param string $cell cell content
  517. *
  518. * @return array Contains the precision, scale, and full size
  519. * representation of the given decimal cell
  520. * @access public
  521. */
  522. function PMA_getDecimalSize($cell)
  523. {
  524. $curr_size = strlen((string)$cell);
  525. $decPos = strpos($cell, ".");
  526. $decPrecision = ($curr_size - 1) - $decPos;
  527. $m = $curr_size - 1;
  528. $d = $decPrecision;
  529. return array($m, $d, ($m . "," . $d));
  530. }
  531. /**
  532. * Obtains the size of the given cell
  533. *
  534. * @param string $last_cumulative_size Last cumulative column size
  535. * @param int $last_cumulative_type Last cumulative column type
  536. * (NONE or VARCHAR or DECIMAL or INT or BIGINT)
  537. * @param int $curr_type Type of the current cell
  538. * (NONE or VARCHAR or DECIMAL or INT or BIGINT)
  539. * @param string $cell The current cell
  540. *
  541. * @return string Size of the given cell in the type-appropriate format
  542. * @access public
  543. *
  544. * @todo Handle the error cases more elegantly
  545. */
  546. function PMA_detectSize($last_cumulative_size, $last_cumulative_type,
  547. $curr_type, $cell
  548. ) {
  549. $curr_size = strlen((string)$cell);
  550. /**
  551. * If the cell is NULL, don't treat it as a varchar
  552. */
  553. if (! strcmp('NULL', $cell)) {
  554. return $last_cumulative_size;
  555. } elseif ($curr_type == VARCHAR) {
  556. /**
  557. * What to do if the current cell is of type VARCHAR
  558. */
  559. /**
  560. * The last cumulative type was VARCHAR
  561. */
  562. if ($last_cumulative_type == VARCHAR) {
  563. if ($curr_size >= $last_cumulative_size) {
  564. return $curr_size;
  565. } else {
  566. return $last_cumulative_size;
  567. }
  568. } elseif ($last_cumulative_type == DECIMAL) {
  569. /**
  570. * The last cumulative type was DECIMAL
  571. */
  572. $oldM = PMA_getDecimalPrecision($last_cumulative_size);
  573. if ($curr_size >= $oldM) {
  574. return $curr_size;
  575. } else {
  576. return $oldM;
  577. }
  578. } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) {
  579. /**
  580. * The last cumulative type was BIGINT or INT
  581. */
  582. if ($curr_size >= $last_cumulative_size) {
  583. return $curr_size;
  584. } else {
  585. return $last_cumulative_size;
  586. }
  587. } elseif (! isset($last_cumulative_type) || $last_cumulative_type == NONE) {
  588. /**
  589. * This is the first row to be analyzed
  590. */
  591. return $curr_size;
  592. } else {
  593. /**
  594. * An error has DEFINITELY occurred
  595. */
  596. /**
  597. * TODO: Handle this MUCH more elegantly
  598. */
  599. return -1;
  600. }
  601. } elseif ($curr_type == DECIMAL) {
  602. /**
  603. * What to do if the current cell is of type DECIMAL
  604. */
  605. /**
  606. * The last cumulative type was VARCHAR
  607. */
  608. if ($last_cumulative_type == VARCHAR) {
  609. /* Convert $last_cumulative_size from varchar to decimal format */
  610. $size = PMA_getDecimalSize($cell);
  611. if ($size[M] >= $last_cumulative_size) {
  612. return $size[M];
  613. } else {
  614. return $last_cumulative_size;
  615. }
  616. } elseif ($last_cumulative_type == DECIMAL) {
  617. /**
  618. * The last cumulative type was DECIMAL
  619. */
  620. $size = PMA_getDecimalSize($cell);
  621. $oldM = PMA_getDecimalPrecision($last_cumulative_size);
  622. $oldD = PMA_getDecimalScale($last_cumulative_size);
  623. /* New val if M or D is greater than current largest */
  624. if ($size[M] > $oldM || $size[D] > $oldD) {
  625. /* Take the largest of both types */
  626. return (string) ((($size[M] > $oldM) ? $size[M] : $oldM)
  627. . "," . (($size[D] > $oldD) ? $size[D] : $oldD));
  628. } else {
  629. return $last_cumulative_size;
  630. }
  631. } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) {
  632. /**
  633. * The last cumulative type was BIGINT or INT
  634. */
  635. /* Convert $last_cumulative_size from int to decimal format */
  636. $size = PMA_getDecimalSize($cell);
  637. if ($size[M] >= $last_cumulative_size) {
  638. return $size[FULL];
  639. } else {
  640. return ($last_cumulative_size . "," . $size[D]);
  641. }
  642. } elseif (! isset($last_cumulative_type) || $last_cumulative_type == NONE) {
  643. /**
  644. * This is the first row to be analyzed
  645. */
  646. /* First row of the column */
  647. $size = PMA_getDecimalSize($cell);
  648. return $size[FULL];
  649. } else {
  650. /**
  651. * An error has DEFINITELY occurred
  652. */
  653. /**
  654. * TODO: Handle this MUCH more elegantly
  655. */
  656. return -1;
  657. }
  658. } elseif ($curr_type == BIGINT || $curr_type == INT) {
  659. /**
  660. * What to do if the current cell is of type BIGINT or INT
  661. */
  662. /**
  663. * The last cumulative type was VARCHAR
  664. */
  665. if ($last_cumulative_type == VARCHAR) {
  666. if ($curr_size >= $last_cumulative_size) {
  667. return $curr_size;
  668. } else {
  669. return $last_cumulative_size;
  670. }
  671. } elseif ($last_cumulative_type == DECIMAL) {
  672. /**
  673. * The last cumulative type was DECIMAL
  674. */
  675. $oldM = PMA_getDecimalPrecision($last_cumulative_size);
  676. $oldD = PMA_getDecimalScale($last_cumulative_size);
  677. $oldInt = $oldM - $oldD;
  678. $newInt = strlen((string)$cell);
  679. /* See which has the larger integer length */
  680. if ($oldInt >= $newInt) {
  681. /* Use old decimal size */
  682. return $last_cumulative_size;
  683. } else {
  684. /* Use $newInt + $oldD as new M */
  685. return (($newInt + $oldD) . "," . $oldD);
  686. }
  687. } elseif ($last_cumulative_type == BIGINT || $last_cumulative_type == INT) {
  688. /**
  689. * The last cumulative type was BIGINT or INT
  690. */
  691. if ($curr_size >= $last_cumulative_size) {
  692. return $curr_size;
  693. } else {
  694. return $last_cumulative_size;
  695. }
  696. } elseif (! isset($last_cumulative_type) || $last_cumulative_type == NONE) {
  697. /**
  698. * This is the first row to be analyzed
  699. */
  700. return $curr_size;
  701. } else {
  702. /**
  703. * An error has DEFINITELY occurred
  704. */
  705. /**
  706. * TODO: Handle this MUCH more elegantly
  707. */
  708. return -1;
  709. }
  710. } else {
  711. /**
  712. * An error has DEFINITELY occurred
  713. */
  714. /**
  715. * TODO: Handle this MUCH more elegantly
  716. */
  717. return -1;
  718. }
  719. }
  720. /**
  721. * Determines what MySQL type a cell is
  722. *
  723. * @param int $last_cumulative_type Last cumulative column type
  724. * (VARCHAR or INT or BIGINT or DECIMAL or NONE)
  725. * @param string $cell String representation of the cell for which
  726. * a best-fit type is to be determined
  727. *
  728. * @return int The MySQL type representation
  729. * (VARCHAR or INT or BIGINT or DECIMAL or NONE)
  730. * @access public
  731. */
  732. function PMA_detectType($last_cumulative_type, $cell)
  733. {
  734. /**
  735. * If numeric, determine if decimal, int or bigint
  736. * Else, we call it varchar for simplicity
  737. */
  738. if (! strcmp('NULL', $cell)) {
  739. if ($last_cumulative_type === null || $last_cumulative_type == NONE) {
  740. return NONE;
  741. }
  742. return $last_cumulative_type;
  743. }
  744. if (is_numeric($cell)) {
  745. if ($cell == (string)(float)$cell
  746. && strpos($cell, ".") !== false
  747. && substr_count($cell, ".") == 1
  748. ) {
  749. return DECIMAL;
  750. }
  751. if (abs($cell) > 2147483647) {
  752. return BIGINT;
  753. }
  754. return INT;
  755. }
  756. return VARCHAR;
  757. }
  758. /**
  759. * Determines if the column types are int, decimal, or string
  760. *
  761. * @param array &$table array(string $table_name, array $col_names, array $rows)
  762. *
  763. * @return array array(array $types, array $sizes)
  764. * @access public
  765. *
  766. * @link http://wiki.phpmyadmin.net/pma/Import
  767. *
  768. * @todo Handle the error case more elegantly
  769. */
  770. function PMA_analyzeTable(&$table)
  771. {
  772. /* Get number of rows in table */
  773. $numRows = count($table[ROWS]);
  774. /* Get number of columns */
  775. $numCols = count($table[COL_NAMES]);
  776. /* Current type for each column */
  777. $types = array();
  778. $sizes = array();
  779. /* Initialize $sizes to all 0's */
  780. for ($i = 0; $i < $numCols; ++$i) {
  781. $sizes[$i] = 0;
  782. }
  783. /* Initialize $types to NONE */
  784. for ($i = 0; $i < $numCols; ++$i) {
  785. $types[$i] = NONE;
  786. }
  787. /* Temp vars */
  788. $curr_type = NONE;
  789. /* If the passed array is not of the correct form, do not process it */
  790. if (!is_array($table)
  791. || is_array($table[TBL_NAME])
  792. || !is_array($table[COL_NAMES])
  793. || !is_array($table[ROWS])
  794. ) {
  795. /**
  796. * TODO: Handle this better
  797. */
  798. return false;
  799. }
  800. /* Analyze each column */
  801. for ($i = 0; $i < $numCols; ++$i) {
  802. /* Analyze the column in each row */
  803. for ($j = 0; $j < $numRows; ++$j) {
  804. /* Determine type of the current cell */
  805. $curr_type = PMA_detectType($types[$i], $table[ROWS][$j][$i]);
  806. /* Determine size of the current cell */
  807. $sizes[$i] = PMA_detectSize(
  808. $sizes[$i],
  809. $types[$i],
  810. $curr_type,
  811. $table[ROWS][$j][$i]
  812. );
  813. /**
  814. * If a type for this column has already been declared,
  815. * only alter it if it was a number and a varchar was found
  816. */
  817. if ($curr_type != NONE) {
  818. if ($curr_type == VARCHAR) {
  819. $types[$i] = VARCHAR;
  820. } else if ($curr_type == DECIMAL) {
  821. if ($types[$i] != VARCHAR) {
  822. $types[$i] = DECIMAL;
  823. }
  824. } else if ($curr_type == BIGINT) {
  825. if ($types[$i] != VARCHAR && $types[$i] != DECIMAL) {
  826. $types[$i] = BIGINT;
  827. }
  828. } else if ($curr_type == INT) {
  829. if ($types[$i] != VARCHAR
  830. && $types[$i] != DECIMAL
  831. && $types[$i] != BIGINT
  832. ) {
  833. $types[$i] = INT;
  834. }
  835. }
  836. }
  837. }
  838. }
  839. /* Check to ensure that all types are valid */
  840. $len = count($types);
  841. for ($n = 0; $n < $len; ++$n) {
  842. if (! strcmp(NONE, $types[$n])) {
  843. $types[$n] = VARCHAR;
  844. $sizes[$n] = '10';
  845. }
  846. }
  847. return array($types, $sizes);
  848. }
  849. /* Needed to quell the beast that is PMA_Message */
  850. $import_notice = null;
  851. /**
  852. * Builds and executes SQL statements to create the database and tables
  853. * as necessary, as well as insert all the data.
  854. *
  855. * @param string $db_name Name of the database
  856. * @param array &$tables Array of tables for the specified database
  857. * @param array &$analyses Analyses of the tables
  858. * @param array &$additional_sql Additional SQL statements to be executed
  859. * @param array $options Associative array of options
  860. *
  861. * @return void
  862. * @access public
  863. *
  864. * @link http://wiki.phpmyadmin.net/pma/Import
  865. */
  866. function PMA_buildSQL($db_name, &$tables, &$analyses = null,
  867. &$additional_sql = null, $options = null
  868. ) {
  869. /* Take care of the options */
  870. if (isset($options['db_collation'])&& ! is_null($options['db_collation'])) {
  871. $collation = $options['db_collation'];
  872. } else {
  873. $collation = "utf8_general_ci";
  874. }
  875. if (isset($options['db_charset']) && ! is_null($options['db_charset'])) {
  876. $charset = $options['db_charset'];
  877. } else {
  878. $charset = "utf8";
  879. }
  880. if (isset($options['create_db'])) {
  881. $create_db = $options['create_db'];
  882. } else {
  883. $create_db = true;
  884. }
  885. /* Create SQL code to handle the database */
  886. $sql = array();
  887. if ($create_db) {
  888. if (PMA_DRIZZLE) {
  889. $sql[] = "CREATE DATABASE IF NOT EXISTS " . PMA_Util::backquote($db_name)
  890. . " COLLATE " . $collation;
  891. } else {
  892. $sql[] = "CREATE DATABASE IF NOT EXISTS " . PMA_Util::backquote($db_name)
  893. . " DEFAULT CHARACTER SET " . $charset . " COLLATE " . $collation;
  894. }
  895. }
  896. /**
  897. * The calling plug-in should include this statement,
  898. * if necessary, in the $additional_sql parameter
  899. *
  900. * $sql[] = "USE " . backquote($db_name);
  901. */
  902. /* Execute the SQL statements create above */
  903. $sql_len = count($sql);
  904. for ($i = 0; $i < $sql_len; ++$i) {
  905. PMA_importRunQuery($sql[$i], $sql[$i]);
  906. }
  907. /* No longer needed */
  908. unset($sql);
  909. /* Run the $additional_sql statements supplied by the caller plug-in */
  910. if ($additional_sql != null) {
  911. /* Clean the SQL first */
  912. $additional_sql_len = count($additional_sql);
  913. /**
  914. * Only match tables for now, because CREATE IF NOT EXISTS
  915. * syntax is lacking or nonexisting for views, triggers,
  916. * functions, and procedures.
  917. *
  918. * See: http://bugs.mysql.com/bug.php?id=15287
  919. *
  920. * To the best of my knowledge this is still an issue.
  921. *
  922. * $pattern = 'CREATE (TABLE|VIEW|TRIGGER|FUNCTION|PROCEDURE)';
  923. */
  924. $pattern = '/CREATE [^`]*(TABLE)/';
  925. $replacement = 'CREATE \\1 IF NOT EXISTS';
  926. /* Change CREATE statements to CREATE IF NOT EXISTS to support
  927. * inserting into existing structures
  928. */
  929. for ($i = 0; $i < $additional_sql_len; ++$i) {
  930. $additional_sql[$i] = preg_replace(
  931. $pattern,
  932. $replacement,
  933. $additional_sql[$i]
  934. );
  935. /* Execute the resulting statements */
  936. PMA_importRunQuery($additional_sql[$i], $additional_sql[$i]);
  937. }
  938. }
  939. if ($analyses != null) {
  940. $type_array = array(
  941. NONE => "NULL",
  942. VARCHAR => "varchar",
  943. INT => "int",
  944. DECIMAL => "decimal",
  945. BIGINT => "bigint",
  946. GEOMETRY => 'geometry'
  947. );
  948. /* TODO: Do more checking here to make sure they really are matched */
  949. if (count($tables) != count($analyses)) {
  950. exit();
  951. }
  952. /* Create SQL code to create the tables */
  953. $tempSQLStr = "";
  954. $num_tables = count($tables);
  955. for ($i = 0; $i < $num_tables; ++$i) {
  956. $num_cols = count($tables[$i][COL_NAMES]);
  957. $tempSQLStr = "CREATE TABLE IF NOT EXISTS "
  958. . PMA_Util::backquote($db_name)
  959. . '.' . PMA_Util::backquote($tables[$i][TBL_NAME]) . " (";
  960. for ($j = 0; $j < $num_cols; ++$j) {
  961. $size = $analyses[$i][SIZES][$j];
  962. if ((int)$size == 0) {
  963. $size = 10;
  964. }
  965. $tempSQLStr .= PMA_Util::backquote($tables[$i][COL_NAMES][$j]) . " "
  966. . $type_array[$analyses[$i][TYPES][$j]];
  967. if ($analyses[$i][TYPES][$j] != GEOMETRY) {
  968. $tempSQLStr .= "(" . $size . ")";
  969. }
  970. if ($j != (count($tables[$i][COL_NAMES]) - 1)) {
  971. $tempSQLStr .= ", ";
  972. }
  973. }
  974. $tempSQLStr .= ")"
  975. . (PMA_DRIZZLE ? "" : " DEFAULT CHARACTER SET " . $charset)
  976. . " COLLATE " . $collation . ";";
  977. /**
  978. * Each SQL statement is executed immediately
  979. * after it is formed so that we don't have
  980. * to store them in a (possibly large) buffer
  981. */
  982. PMA_importRunQuery($tempSQLStr, $tempSQLStr);
  983. }
  984. }
  985. /**
  986. * Create the SQL statements to insert all the data
  987. *
  988. * Only one insert query is formed for each table
  989. */
  990. $tempSQLStr = "";
  991. $col_count = 0;
  992. $num_tables = count($tables);
  993. for ($i = 0; $i < $num_tables; ++$i) {
  994. $num_cols = count($tables[$i][COL_NAMES]);
  995. $num_rows = count($tables[$i][ROWS]);
  996. $tempSQLStr = "INSERT INTO " . PMA_Util::backquote($db_name) . '.'
  997. . PMA_Util::backquote($tables[$i][TBL_NAME]) . " (";
  998. for ($m = 0; $m < $num_cols; ++$m) {
  999. $tempSQLStr .= PMA_Util::backquote($tables[$i][COL_NAMES][$m]);
  1000. if ($m != ($num_cols - 1)) {
  1001. $tempSQLStr .= ", ";
  1002. }
  1003. }
  1004. $tempSQLStr .= ") VALUES ";
  1005. for ($j = 0; $j < $num_rows; ++$j) {
  1006. $tempSQLStr .= "(";
  1007. for ($k = 0; $k < $num_cols; ++$k) {
  1008. // If fully formatted SQL, no need to enclose
  1009. // with aphostrophes, add shalshes etc.
  1010. if ($analyses != null
  1011. && isset($analyses[$i][FORMATTEDSQL][$col_count])
  1012. && $analyses[$i][FORMATTEDSQL][$col_count] == true
  1013. ) {
  1014. $tempSQLStr .= (string) $tables[$i][ROWS][$j][$k];
  1015. } else {
  1016. if ($analyses != null) {
  1017. $is_varchar = ($analyses[$i][TYPES][$col_count] === VARCHAR);
  1018. } else {
  1019. $is_varchar = ! is_numeric($tables[$i][ROWS][$j][$k]);
  1020. }
  1021. /* Don't put quotes around NULL fields */
  1022. if (! strcmp($tables[$i][ROWS][$j][$k], 'NULL')) {
  1023. $is_varchar = false;
  1024. }
  1025. $tempSQLStr .= (($is_varchar) ? "'" : "");
  1026. $tempSQLStr .= PMA_Util::sqlAddSlashes(
  1027. (string) $tables[$i][ROWS][$j][$k]
  1028. );
  1029. $tempSQLStr .= (($is_varchar) ? "'" : "");
  1030. }
  1031. if ($k != ($num_cols - 1)) {
  1032. $tempSQLStr .= ", ";
  1033. }
  1034. if ($col_count == ($num_cols - 1)) {
  1035. $col_count = 0;
  1036. } else {
  1037. $col_count++;
  1038. }
  1039. /* Delete the cell after we are done with it */
  1040. unset($tables[$i][ROWS][$j][$k]);
  1041. }
  1042. $tempSQLStr .= ")";
  1043. if ($j != ($num_rows - 1)) {
  1044. $tempSQLStr .= ",\n ";
  1045. }
  1046. $col_count = 0;
  1047. /* Delete the row after we are done with it */
  1048. unset($tables[$i][ROWS][$j]);
  1049. }
  1050. $tempSQLStr .= ";";
  1051. /**
  1052. * Each SQL statement is executed immediately
  1053. * after it is formed so that we don't have
  1054. * to store them in a (possibly large) buffer
  1055. */
  1056. PMA_importRunQuery($tempSQLStr, $tempSQLStr);
  1057. }
  1058. /* No longer needed */
  1059. unset($tempSQLStr);
  1060. /**
  1061. * A work in progress
  1062. */
  1063. /* Add the viewable structures from $additional_sql
  1064. * to $tables so they are also displayed
  1065. */
  1066. $view_pattern = '@VIEW `[^`]+`\.`([^`]+)@';
  1067. $table_pattern = '@CREATE TABLE IF NOT EXISTS `([^`]+)`@';
  1068. /* Check a third pattern to make sure its not a "USE `db_name`;" statement */
  1069. $regs = array();
  1070. $inTables = false;
  1071. $additional_sql_len = count($additional_sql);
  1072. for ($i = 0; $i < $additional_sql_len; ++$i) {
  1073. preg_match($view_pattern, $additional_sql[$i], $regs);
  1074. if (count($regs) == 0) {
  1075. preg_match($table_pattern, $additional_sql[$i], $regs);
  1076. }
  1077. if (count($regs)) {
  1078. for ($n = 0; $n < $num_tables; ++$n) {
  1079. if (! strcmp($regs[1], $tables[$n][TBL_NAME])) {
  1080. $inTables = true;
  1081. break;
  1082. }
  1083. }
  1084. if (! $inTables) {
  1085. $tables[] = array(TBL_NAME => $regs[1]);
  1086. }
  1087. }
  1088. /* Reset the array */
  1089. $regs = array();
  1090. $inTables = false;
  1091. }
  1092. $params = array('db' => (string)$db_name);
  1093. $db_url = 'db_structure.php' . PMA_URL_getCommon($params);
  1094. $db_ops_url = 'db_operations.php' . PMA_URL_getCommon($params);
  1095. $message = '<br /><br />';
  1096. $message .= '<strong>' . __('The following structures have either been created or altered. Here you can:') . '</strong><br />';
  1097. $message .= '<ul><li>' . __("View a structure's contents by clicking on its name.") . '</li>';
  1098. $message .= '<li>' . __('Change any of its settings by clicking the corresponding "Options" link.') . '</li>';
  1099. $message .= '<li>' . __('Edit structure by following the "Structure" link.') . '</li>';
  1100. $message .= sprintf(
  1101. '<br /><li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">'
  1102. . __('Options') . '</a>)</li>',
  1103. $db_url,
  1104. sprintf(
  1105. __('Go to database: %s'),
  1106. htmlspecialchars(PMA_Util::backquote($db_name))
  1107. ),
  1108. htmlspecialchars($db_name),
  1109. $db_ops_url,
  1110. sprintf(
  1111. __('Edit settings for %s'),
  1112. htmlspecialchars(PMA_Util::backquote($db_name))
  1113. )
  1114. );
  1115. $message .= '<ul>';
  1116. unset($params);
  1117. $num_tables = count($tables);
  1118. for ($i = 0; $i < $num_tables; ++$i) {
  1119. $params = array(
  1120. 'db' => (string) $db_name,
  1121. 'table' => (string) $tables[$i][TBL_NAME]
  1122. );
  1123. $tbl_url = 'sql.php' . PMA_URL_getCommon($params);
  1124. $tbl_struct_url = 'tbl_structure.php' . PMA_URL_getCommon($params);
  1125. $tbl_ops_url = 'tbl_operations.php' . PMA_URL_getCommon($params);
  1126. unset($params);
  1127. if (! PMA_Table::isView($db_name, $tables[$i][TBL_NAME])) {
  1128. $message .= sprintf(
  1129. '<li><a href="%s" title="%s">%s</a> (<a href="%s" title="%s">' . __('Structure') . '</a>) (<a href="%s" title="%s">' . __('Options') . '</a>)</li>',
  1130. $tbl_url,
  1131. sprintf(
  1132. __('Go to table: %s'),
  1133. htmlspecialchars(
  1134. PMA_Util::backquote($tables[$i][TBL_NAME])
  1135. )
  1136. ),
  1137. htmlspecialchars($tables[$i][TBL_NAME]),
  1138. $tbl_struct_url,
  1139. sprintf(
  1140. __('Structure of %s'),
  1141. htmlspecialchars(
  1142. PMA_Util::backquote($tables[$i][TBL_NAME])
  1143. )
  1144. ),
  1145. $tbl_ops_url,
  1146. sprintf(
  1147. __('Edit settings for %s'),
  1148. htmlspecialchars(
  1149. PMA_Util::backquote($tables[$i][TBL_NAME])
  1150. )
  1151. )
  1152. );
  1153. } else {
  1154. $message .= sprintf(
  1155. '<li><a href="%s" title="%s">%s</a></li>',
  1156. $tbl_url,
  1157. sprintf(
  1158. __('Go to view: %s'),
  1159. htmlspecialchars(
  1160. PMA_Util::backquote($tables[$i][TBL_NAME])
  1161. )
  1162. ),
  1163. htmlspecialchars($tables[$i][TBL_NAME])
  1164. );
  1165. }
  1166. }
  1167. $message .= '</ul></ul>';
  1168. global $import_notice;
  1169. $import_notice = $message;
  1170. unset($tables);
  1171. }
  1172. /**
  1173. * Stops the import on (mostly upload/file related) error
  1174. *
  1175. * @param PMA_Message $error_message The error message
  1176. *
  1177. * @return void
  1178. * @access public
  1179. *
  1180. */
  1181. function PMA_stopImport( PMA_Message $error_message )
  1182. {
  1183. global $import_handle, $file_to_unlink;
  1184. // Close open handles
  1185. if ($import_handle !== false && $import_handle !== null) {
  1186. fclose($import_handle);
  1187. }
  1188. // Delete temporary file
  1189. if ($file_to_unlink != '') {
  1190. unlink($file_to_unlink);
  1191. }
  1192. $_SESSION['Import_message']['message'] = $error_message->getDisplay();
  1193. $response = PMA_Response::getInstance();
  1194. $response->isSuccess(false);
  1195. $response->addJSON('message', PMA_Message::error($msg));
  1196. exit;
  1197. }
  1198. ?>