home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / import.lib.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  11.0 KB  |  298 lines

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