$type = _backup_partial; $type_name = 'partial'; } else { $type = _backup_db; $type_name = 'db'; } $type_ext = _backupExt($type); // nazev souboru $fname = @str_replace('.', '_', _getDomain()); if ($fname == '') { $fname = _mysql_db; } $fname .= '_' . date("Y_m_d"); // komprese $can_compress = extension_loaded('zlib'); $memlimit = _phpIniLimit('memory_limit'); $should_compress = !isset($memlimit) || $memlimit > 10485760; // velikosti $sizes = array(); // velikost databaze $sizes['db'] = 10240; // +- 10kB struktura databaze $q = DB::query('SHOW TABLE STATUS LIKE \'' . _mysql_prefix . '-%\''); while ($r = DB::row($q)) { $sizes['db'] += $r['Data_length']; } // velikost slozek if ($type !== _backup_db) { if ($type === _backup_full) { $dirs = array('admin', 'pictures', 'plugins', 'remote', 'require', 'upload'); } else {
/** * Import data to the database * @param KZipStream|string $stream KZipStream instance or file path * @return array array(true, skipped_tables) on success, array(false, err_msg) on failure */ public function importData($stream) { // prepare global $_lang; $err = null; $this->_import_tmap = array(); // rather ugly hack to use existing file path as KZipStream if (is_string($stream)) { $file = $stream; $stream = new KZipStream(null, array(KZip::FILE_TOADD, $file, null)); unset($file); } // vars $null = chr(0); $nullv = chr(1); $version = ''; // import process do { // read header $offset = 0; while (true) { ++$offset; $byte = $stream->read(1); if ($byte === $null) { // header read break; } else { $version .= $byte; } if ($offset > 32) { $err = $_lang['dbdump']['dataerror']; break 2; } } // check version if (!_checkVersion('database', $version)) { $err = $_lang['dbdump']['badversion']; break; } // find local tables $tables = array(); $q = DB::query('SHOW TABLES LIKE \'' . _mysql_prefix . '-%\''); while ($r = DB::rown($q)) { $tables[$r[0]] = true; } DB::free($q); unset($r); // determine maximum query size $max_size = DB::query('SHOW VARIABLES LIKE \'max_allowed_packet\''); if (DB::size($max_size) !== 1) { $err = $_lang['dbdump']['maxpacket']; break; } $max_size = DB::result($max_size, 0, 1); $max_size -= 128; $max_size = floor(($max_size - 128) * 0.9); // adjust maximum query size to available memory $memlimit = _phpIniLimit('memory_limit'); if (isset($memlimit)) { $avail_mem = $memlimit - memory_get_usage() - 131072; if ($max_size > $avail_mem) { $max_size = $avail_mem; } unset($avail_mem); } if ($max_size < 32768) { $err = $_lang['dbdump']['memory']; break; } // turn off auto_increment for zero values DB::query('SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"'); // prepare $reset = true; $skipped_tables = array(); $stream_buffer = ''; $stream_buffer_i = 0; // import data while (true) { // reset? if ($reset) { $phase = 0; $table = ''; $column = ''; $columns = array(); $columns_size = 0; $values = array(); $value = ''; $value_counter = 0; $sql = ''; $sql_len = 0; $sql_buffer = ''; $sql_buffer_len = 0; $skipping_table = false; $reset = false; } // get 1 byte if (!isset($stream_buffer[$stream_buffer_i])) { if ($stream->eof()) { break; } $stream_buffer = $stream->read(); $stream_buffer_i = 0; if ($stream_buffer === '') { break; } } $byte = $stream_buffer[$stream_buffer_i]; ++$stream_buffer_i; // phase switch ($phase) { /* -- reading table name -- */ case 0: // end of table name? if ($byte === $null) { // read column list $phase = 1; if (!isset($tables[_mysql_prefix . '-' . $table])) { $skipping_table = true; $skipped_tables[] = $table; } break; } // znak nazvu tabulky $table .= $byte; break; /* -- reading column list -- */ /* -- reading column list -- */ case 1: // end of column if ($byte === $null) { if ($column === '') { // end of list, process columns if (!$skipping_table) { $columns = '`' . implode('`,`', $columns) . '`'; } // begin to read rows $phase = 2; } else { // end of column if (!$skipping_table) { $columns[] = $column; } ++$columns_size; $column = ''; } break; } // column name char $column .= $byte; break; /* -- reading row data -- */ /* -- reading row data -- */ case 2: // end of value? if ($byte === $null) { if ($value_counter === 0 && $value === '') { // end of all rows, reset $reset = true; // import remaining data if ($sql_buffer !== '' && !$skipping_table) { $import = $this->_db_import($table, $columns, $sql_buffer, $sql_buffer_len); if (isset($import)) { $err = _htmlStr($import); break 3; } } } else { // end of value ++$value_counter; $values[] = $value; $value = ''; // end of one row? if ($value_counter === $columns_size) { if (!$skipping_table) { // build part of the SQL query $sql = '('; for ($i = 0, $lastcol = $columns_size - 1; isset($values[$i]); ++$i) { if ($values[$i] === $nullv) { $sql .= 'NULL'; } else { $sql .= '\'' . $values[$i] . '\''; } if ($i !== $lastcol) { $sql .= ','; } } $sql .= ')'; // execute query or use buffer $sql_len = strlen($sql); if ($sql_buffer_len + $sql_len + 1 >= $max_size) { $this->_db_import($table, $columns, $sql_buffer, $sql_buffer_len); if (isset($import)) { $err = _htmlStr($import); break 3; } } else { // separate if ($sql_buffer !== '') { $sql_buffer .= ','; ++$sql_buffer_len; } // add query to buffer $sql_buffer .= $sql; $sql_buffer_len += $sql_len; } // clean up $sql = ''; $sql_len = 0; } $value_counter = 0; $values = array(); } } break; } // value char $value .= $byte; break; } } // restore sql_mode DB::query('SET SQL_MODE=""'); } while (false); // void truncate map $this->_import_tmap = null; // return if (!isset($err)) { return array(true, $skipped_tables); } return array(false, $err); }
/** * Zjistit maximalni moznou celkovou velikost uploadu * @param bool $get_mb ziskat cislo j*z prevedene na megabajty * @return number|null cislo v B/mB nebo null (= neomezeno, resp. neznamy limit) */ function _getUploadLimit($get_mb = false) { static $result; if (!isset($result)) { $limit_lowest = null; $opts = array('upload_max_filesize', 'post_max_size', 'memory_limit'); for ($i = 0; isset($opts[$i]); ++$i) { $limit = _phpIniLimit($opts[$i]); if (isset($limit) && (!isset($limit_lowest) || $limit < $limit_lowest)) { $limit_lowest = $limit; } } if (isset($limit_lowest)) { $result = $get_mb ? round($limit_lowest / 1048576, 1) : $limit_lowest; } else { $result = null; } } return $result; }
/** * Nacteni obrazku ze souboru * * Mozne klice v $limit: * * filesize maximalni velikost souboru v bajtech * dimensions max. rozmery ve formatu array(x => max_sirka, y => max_vyska) * memory maximalni procento zbyvajici dostupne pameti, ktere muze byt vyuzito (vychozi je 0.75) a je treba pocitat s +- odchylkou * * @param string $filepath realna cesta k souboru * @param array $limit volby omezeni * @param string|null $filename pouzity nazev souboru (pokud se lisi od $filepath) * @return array v pripade uspechu array(true, kod, resource, pripona), jinak array(false, kod, zprava, pripona) */ function _pictureLoad($filepath, $limit = array(), $filename = null) { // vychozi nastaveni static $limit_default = array('filesize' => null, 'dimensions' => null, 'memory' => 0.75); // vlozeni vychoziho nastaveni $limit += $limit_default; // proces $code = 0; do { /* -------- kontroly a nacteni -------- */ // zjisteni nazvu souboru if (null === $filename) { $filename = basename($filepath); } // zjisteni pripony $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); // kontrola pripony if (!in_array($ext, SL::$imageExt) || !_isSafeFile($filepath) || !_isSafeFile($filename)) { // nepovolena pripona $code = 1; break; } // kontrola velikosti souboru $size = @filesize($filepath); if ($size === false) { // soubor nenalezen $code = 2; break; } if (isset($limit['filesize']) && $size > $limit['filesize']) { // prekrocena datova velikost $code = 3; break; } // kontrola podpory formatu if (!_checkGD($ext)) { // nepodporovany format $code = 4; break; } // zjisteni informaci o obrazku $imageInfo = getimagesize($filepath); if (isset($imageInfo['channels'])) { $channels = $imageInfo['channels']; } else { switch ($ext) { case 'png': $channels = 4; break; default: $channels = 3; break; } } if (false === $imageInfo || 0 == $imageInfo[0] || 0 == $imageInfo[1]) { $code = 5; break; } // kontrola dostupne pameti if ($memlimit = _phpIniLimit('memory_limit')) { $availMem = floor($limit['memory'] * ($memlimit - memory_get_usage())); $requiredMem = ceil(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $channels / 8 + 65536) * 1.65); if ($requiredMem > $availMem) { // nedostatek pameti $code = 5; break; } } // nacteni rozmeru $x = $imageInfo[0]; $y = $imageInfo[1]; // kontrola rozmeru if (isset($limit['dimensions']) && ($x > $limit['dimensions']['x'] || $y > $limit['dimensions']['y'])) { $code = 6; break; } // pokus o nacteni obrazku switch ($ext) { case 'jpg': case 'jpeg': $res = @imagecreatefromjpeg($filepath); break; case 'png': $res = @imagecreatefrompng($filepath); break; case 'gif': $res = @imagecreatefromgif($filepath); break; } // kontrola nacteni if (!is_resource($res)) { $code = 5; break; } // vsechno je ok, vratit vysledek return array(true, $code, $res, $ext); } while (false); // chyba global $_lang; $output = array(false, $code, $_lang['pic.load.' . $code], $ext); // uprava vystupu switch ($code) { case 3: $output[2] = str_replace('*maxkb*', round($limit['filesize'] / 1024), $output[2]); break; case 5: $lastError = error_get_last(); if (null !== $lastError && !empty($lastError['message'])) { $output[2] .= " {$_lang['global.error']}: " . _htmlStr($lastError['message']); } break; case 6: $output[2] = str_replace(array('*maxw*', '*maxh*'), array($limit['dimensions']['x'], $limit['dimensions']['y']), $output[2]); break; } // navrat return $output; }