[ Index ]
 

Code source de phpMyAdmin 2.10.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libraries/ -> import.lib.php (source)

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


Généré le : Mon Nov 26 15:18:20 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics