protected function getLines() { $handle = bzopen($this->file, "r"); if ($handle) { $decompressedData = ''; while (true) { do { if (feof($handle)) { bzclose($handle); return; } $decompressedData .= bzread($handle, 8192); $key = strpos($decompressedData, "\n"); } while ($key === false); do { $line = substr($decompressedData, 0, $key + 1); $decompressedData = substr_replace($decompressedData, '', 0, $key + 1); (yield $line); $key = strpos($decompressedData, "\n"); } while ($key !== false); } } else { throw new \Exception("не удалось открыть файл"); } }
/** * Open bz archive file * * @throws Mage_Exception * @param string $mode */ protected function _open($mode) { $this->_fileHandler = @bzopen($this->_filePath, $mode); if (false === $this->_fileHandler) { throw new Mage_Exception('Failed to open file ' . $this->_filePath); } }
/** * Static method to decompress data * * @param string $data * @return mixed */ public static function decompress($data) { // Decompress the file if (@file_exists($data)) { $bz = bzopen($data, 'r'); $uncompressed = ''; // Read the uncompressed data. while (!feof($bz)) { $uncompressed .= bzread($bz, 4096); } // Close the Bzip2 compressed file and write // the data to the uncompressed file. bzclose($bz); if (stripos($data, '.tbz2') !== false) { $newFile = str_replace('.tbz2', '.tar', $data); } else { if (stripos($data, '.tbz') !== false) { $newFile = str_replace('.tbz', '.tar', $data); } else { $newFile = str_replace('.bz2', '', $data); } } file_put_contents($newFile, $uncompressed); return $newFile; // Else, decompress the string } else { return bzdecompress($data); } }
function dumpHeaders($identifier, $multi_table = false) { if ($_POST["output"] == "bz2") { $this->filename = tempnam("", "bz2"); $this->fp = bzopen($this->filename, 'w'); header("Content-Type: application/x-bzip"); ob_start(array($this, '_bz2'), 1000000.0); } }
public function read($file) { $open = bzopen($file, 'r'); if (empty($open)) { throw new FileNotFoundException('Error', 'fileNotFound', $file); } $return = bzread($open, 8096); bzclose($open); return $return; }
function bzfile($file) { $bz = bzopen($file, "r"); $str = ""; while (!feof($bz)) { $str = $str . bzread($bz, 8192); bzclose($bz); } return $str; }
/** * @see File_Archive_Writer::newFile() * * Check that one single file is written in the BZip2 archive */ function newFile($filename, $stat = array(), $mime = "application/octet-stream") { if ($this->nbFiles > 1) { return PEAR::raiseError("A Bzip2 archive can only contain one single file." . "Use Tbz archive to be able to write several files"); } $this->nbFiles++; $this->tmpName = tempnam(File_Archive::getOption('tmpDirectory'), 'far'); $this->bzfile = bzopen($this->tmpName, 'w' . $this->compressionLevel); return true; }
/** * {@inheritdoc} * @throws \RuntimeException */ protected function _open($mode) { if (!extension_loaded('bz2')) { throw new \RuntimeException('PHP extension bz2 is required.'); } $this->_fileHandler = bzopen($this->_filePath, $mode); if (false === $this->_fileHandler) { throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Failed to open file %1', [$this->_filePath])); } }
public function __construct($file) { parent::__construct($file, FileDecoder::DECODER_BZIP2); if (!extension_loaded("bz2")) { throw new Exception(Log::err("Missing extension 'bz2' for bzip2 decoding")); } $this->tResource = bzopen($file, "r"); if ($this->tResource === false) { throw new Exception(Log::err("Cannot open file '{$file}' for reading")); } }
public function compress($fileName) { $this->tarHandler->compress($fileName); $bzh = bzopen($fileName . '.tar.bz2', 'wb'); $th = fopen($fileName . ".tar", 'rb'); while (!feof($th)) { $ustr = fread($th, 1048576); bzwrite($bzh, $ustr); } bzclose($bzh); fclose($th); }
function bunzip($infile, $outfile) { $string = null; $zp = bzopen($infile, "r"); while (!feof($zp)) { $string .= bzread($zp, 4096); } bzclose($zp); $fp = fopen($outfile, "w"); fwrite($fp, $string, strlen($string)); fclose($fp); }
function test_bzerror() { global $tmpfile; $f = fopen($tmpfile, "w"); fwrite($f, "this is a test"); fclose($f); $f = bzopen($tmpfile, "r"); bzread($f); $ret = bzerror($f); bzclose($f); unlink($tmpfile); VS($ret, array("errno" => -5, "errstr" => "DATA_ERROR_MAGIC")); }
public function stream_open($path, $mode, $options, &$opened_path) { if (!function_exists('bzopen')) { return false; } $resource = bzopen($path, $mode); if (!$resource) { return false; } $this->_path = $path; $this->_mode = $mode; $this->_resource = $resource; }
/** * {@inheritdoc} */ public function extract($file, $target, Format\FormatInterface $format) { $this->checkSupport($format); $basename = pathinfo($file, PATHINFO_FILENAME); if (false === $this->isValid($file)) { throw new Exception\IO\Input\FileCorruptedException($file, Exception\IO\Input\FileCorruptedException::SEVERITY_HIGH); } $source = bzopen($file, 'r'); $this->getFilesystem()->mkdir($target); $destination = fopen($target . DIRECTORY_SEPARATOR . $basename, 'w'); $bytes = stream_copy_to_stream($source, $destination); bzclose($source); fclose($destination); return $bytes > 0; }
function extractBzip2($src, $dest = false) { $bz = bzopen($src, "r"); $data = ''; while (!feof($bz)) { $data .= bzread($bz, 1024 * 1024); } bzclose($bz); if (empty($dest)) { return $data; } elseif (file_put_contents($dest, $data)) { return $dest; } return false; }
public function read($file = '', $length = 1024, $type = NULL) { if (!is_string($file) || empty($file)) { return Error::set('Error', 'stringParameter', '1.(file)'); } if (!is_numeric($length)) { return Error::set('Error', 'numericParameter', '2.(length)'); } $open = bzopen($file, 'r'); if (empty($open)) { return Error::set('Error', 'fileNotFound', $file); } $return = bzread($open, $length); bzclose($open); return $return; }
function x() { $uncheckedDir = opendir('.'); readdir($uncheckedDir); $uncheckedDir2 = bzopen('.'); bzclose($uncheckedDir2); $uncheckedDir3 = fopen('.', 'r+'); fclose($uncheckedDir3); readdir(opendir('uncheckedDir4')); readdir2(opendir('uncheckedDir5')); readdir(opendir2('uncheckedDir6')); $pspell_new = pspell_new('asdfasdf'); while ($f = pspell_suggest($pspell_new)) { print "{$f}\n"; } }
/** * @param InputInterface $input * @param OutputInterface $output * @return void * @throws Exception */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var RenaApp $app */ $app = RenaApp::getInstance(); // Setup the url and cache path $url = "https://www.fuzzwork.co.uk/dump/"; $cache = __DIR__ . "/../../cache/update"; // Create the cache dir if it doesn't exist if (!file_exists($cache)) { mkdir($cache); } // Fetch the md5 $md5file = "mysql-latest.tar.bz2.md5"; $md5 = explode(" ", $app->cURL->getData($url . $md5file, 0))[0]; $lastSeenMD5 = $app->Storage->get("ccpdataMD5"); if ($lastSeenMD5 !== $md5 || !$input->getOption("force") === false) { $output->writeln("Updating to latest CCP data dump"); $dbFiles = array("dgmAttributeCategories", "dgmAttributeTypes", "dgmEffects", "dgmTypeAttributes", "dgmTypeEffects", "invFlags", "invGroups", "invTypes", "mapDenormalize", "mapRegions", "mapSolarSystems", "mapConstellations"); $type = ".sql.bz2"; foreach ($dbFiles as $file) { $output->writeln("Updating {$file}"); $dataURL = $url . "latest/" . $file . $type; try { file_put_contents("{$cache}/{$file}{$type}", $app->cURL->getData($dataURL, 0)); $sqlData = bzopen("{$cache}/{$file}{$type}", "r"); // Open the BZip data // Read the BZip data and append it to data $data = ""; while (!feof($sqlData)) { $data .= bzread($sqlData, 4096); } } catch (\Exception $e) { throw new \Exception($e->getMessage()); } // Since rena uses tokuDB, we'll replace InnoDB with TokuDB here, just because we can.. $data = str_replace("ENGINE=InnoDB", "ENGINE=TokuDB", $data); $dataParts = explode(";\n", $data); foreach ($dataParts as $qry) { $query = $qry . ";"; $app->Db->execute($query); } // Remove the stored BZip file from the drive - no need to store it.. unlink("{$cache}/{$file}.sql.bz2"); } $app->Storage->set("ccpdataMD5", $md5); } }
function bzip2($in, $out) { if (!file_exists($in) || !is_readable($in)) { return false; } if (!file_exists($out) && !is_writeable(dirname($out)) || file_exists($out) && !is_writable($out)) { return false; } $in_file = fopen($in, "r"); $out_file = bzopen($out, "w"); while (!feof($in_file)) { $buffer = fgets($in_file, 4096); bzwrite($out_file, $buffer, 4096); } fclose($in_file); bzclose($out_file); return true; }
/** * Unpack file by BZIP2 compressor. * * @param string $source * @param string $destination * @return string */ public function unpack($source, $destination) { $data = ''; $bzPointer = bzopen($source, 'r'); if (empty($bzPointer)) { throw new Exception('Can\'t open BZ archive : ' . $source); } while (!feof($bzPointer)) { $data .= bzread($bzPointer, 131072); } bzclose($bzPointer); if (is_dir($destination)) { $file = $this->getFilename($source); $destination = $destination . $file; } echo $destination; $this->_writeFile($destination, $data); return $destination; }
static function load_bz($file_name) { global $wgOfflineWikiPath; $path = "{$wgOfflineWikiPath}/{$file_name}"; if (strlen($file_name) < 1) { return null; } #strange that bzopen doesn't choke on dir. $bz = bzopen($path, "r"); if (!$bz) { return null; } $out = ""; while ($bz && !feof($bz)) { $out .= bzread($bz, 8192); } bzclose($bz); return $out; }
/** * Décompression du contenu. Si path est donné les fichiers sont décompressés dans le répertoire en question, * sinon, ils sont retournés "en mémoire". * * @param filename, path where uncompress * @return buffer uncompressed or boolean for save success if path given */ public function uncompress($filename, $path = null) { if (!($bz = bzopen($filename, "r"))) { return null; } $decompressed_file = ''; while (!feof($bz)) { $decompressed_file .= bzread($bz, 4096); } bzclose($bz); if ($path) { if ($fp = fopen($path, 'w')) { fwrite($fp, $decompressed_file, strlen($decompressed_file)); fclose($fp); return true; } return false; } return $decompressed_file; }
function getFile($full_filename) { $extension = pathinfo($full_filename, PATHINFO_EXTENSION); if ($extension == "bz2") { $bz = bzopen($full_filename, "r"); while (!feof($bz)) $data .= bzread($bz, 4096); bzclose($bz); } else { $bz = fopen($full_filename, "r"); while (!feof($bz)) $data .= fread($bz, 4096); fclose($bz); } return $data; }
function bzip2_compress($src_file, $dest_file = null) { if ($dest_file == null) { $dest_file = $src_file; } $content = file_get_contents($src_file); if (empty($content)) { return 1; } $bz = bzopen($dest_file, "w"); if (!$bz) { return 2; } $ret = bzwrite($bz, $content); if (!$ret) { bzclose($bz); return 3; } bzclose($bz); return 0; }
/** * */ public function createCCPDB() { $ccpDataURL = "https://www.fuzzwork.co.uk/dump/sqlite-latest.sqlite.bz2"; $ccpDataMD5URL = "https://www.fuzzwork.co.uk/dump/sqlite-latest.sqlite.bz2.md5"; $dbLocation = BASEDIR . "/config/database/"; $md5 = explode(" ", $this->curl->getData($ccpDataMD5URL))[0]; $lastSeenMd5 = $this->storage->get("ccpDataMd5"); // If the last seen md5, isn't equal the current seen md5, we'll update! if ($lastSeenMd5 !== $md5) { try { $this->log->notice("Updating CCP SQLite Database"); $this->log->notice("Downloading bz2 file, and writing it to {$dbLocation}ccpData.sqlite.bz2"); $downloadedData = $this->curl->getLargeData($ccpDataURL, "{$dbLocation}ccpData.sqlite.bz2"); if ($downloadedData == false) { $this->log->warn("Error: File not downloaded successfully!"); die; } $this->log->notice("Opening bz2 file"); $sqliteData = bzopen("{$dbLocation}ccpData.sqlite.bz2", "r"); $this->log->notice("Reading from bz2 file"); $data = ""; while (!feof($sqliteData)) { $data .= bzread($sqliteData, 4096); } $this->log->notice("Writing bz2 file contents into .sqlite file"); file_put_contents("{$dbLocation}ccpData.sqlite", $data); $this->log->notice("Deleting bz2 file"); unlink("{$dbLocation}ccpData.sqlite.bz2"); $this->log->notice("Creating mapCelestials view"); $this->ccpDB->execute("CREATE VIEW mapAllCelestials AS SELECT itemID, itemName, typeName, mapDenormalize.typeID, solarSystemName, mapDenormalize.solarSystemID, mapDenormalize.constellationID, mapDenormalize.regionID, mapRegions.regionName, orbitID, mapDenormalize.x, mapDenormalize.y, mapDenormalize.z FROM mapDenormalize JOIN invTypes ON (mapDenormalize.typeID = invTypes.typeID) JOIN mapSolarSystems ON (mapSolarSystems.solarSystemID = mapDenormalize.solarSystemID) JOIN mapRegions ON (mapDenormalize.regionID = mapRegions.regionID) JOIN mapConstellations ON (mapDenormalize.constellationID = mapConstellations.constellationID)"); $this->log->notice("CCP Database updated!"); $this->storage->set("ccpDataMd5", $md5); } catch (\Exception $e) { $this->log->warn("Error updating the CCPDatabase. Bot can't run"); die; } } }
function download($url, $file) { $tmp = $file . '.bz2'; $fp = fopen($tmp, "w"); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_FILE, $fp); curl_setopt($curl, CURLOPT_HEADER, 0); $result = curl_exec($curl); curl_close($curl); fclose($fp); if ($result == false) { return false; } $bz = bzopen($tmp, "r"); $fp = fopen($file, "w"); while (!feof($bz)) { fwrite($fp, bzread($bz, 4096)); } bzclose($bz); fclose($fp); return $result; }
public static function read($f3, $md5, $filename) { $path_temp = PFH_MD5::get_tmp_file_path($f3, $md5, $filename); // 移除多餘的快取資料 PFH_Archive_cache::clean($f3, $path_temp); if (is_file($path_temp) === FALSE) { $path = PFH_MD5::get_file_path($f3, $md5, $filename); $tmp_dir = PFH_MD5::get_tmp_dir_path($f3, $md5); //$tmp_file = PFH_MD5::get_tmp_file_path($f3, $md5); if (is_file($path)) { // 沒有壓縮的情況 return $path; } //if (is_file($path)) { if (is_file($path . ".zip")) { $archive = new ZipArchive(); //echo $path; $res = $archive->open($path . ".zip"); //$zip->extractTo($tmp_file); $archive->extractTo($tmp_dir); $archive->close(); } else { if (is_file($path . ".bz2")) { $in_file = bzopen($path . ".bz2", "r"); $out_file = fopen($path_temp, "w"); while ($buffer = bzread($in_file, 4096)) { fwrite($out_file, $buffer, 4096); } bzclose($in_file); fclose($out_file); } } } // 加入快取資料 PFH_Archive_cache::add($f3, $path_temp); return $path_temp; }
private function restore_backup_db($working_dir, $working_dir_localpath, &$import_table_prefix) { do_action('updraftplus_restore_db_pre'); # This is now a legacy option (at least on the front end), so we should not see it much $this->prior_upload_path = get_option('upload_path'); // There is a file backup.db(.gz) inside the working directory # The 'off' check is for badly configured setups - http://wordpress.org/support/topic/plugin-wp-super-cache-warning-php-safe-mode-enabled-but-safe-mode-is-off if (@ini_get('safe_mode') && 'off' != strtolower(@ini_get('safe_mode'))) { echo "<p>" . __('Warning: PHP safe_mode is active on your server. Timeouts are much more likely. If these happen, then you will need to manually restore the file via phpMyAdmin or another method.', 'updraftplus') . "</p><br/>"; } $db_basename = 'backup.db.gz'; if (!empty($this->ud_foreign)) { $plugins = apply_filters('updraftplus_accept_archivename', array()); if (empty($plugins[$this->ud_foreign])) { return new WP_Error('unknown', sprintf(__('Backup created by unknown source (%s) - cannot be restored.', 'updraftplus'), $this->ud_foreign)); } if (!file_exists($working_dir_localpath . '/' . $db_basename) && file_exists($working_dir_localpath . '/backup.db')) { $db_basename = 'backup.db'; } elseif (!file_exists($working_dir_localpath . '/' . $db_basename) && file_exists($working_dir_localpath . '/backup.db.bz2')) { $db_basename = 'backup.db.bz2'; } if (!file_exists($working_dir_localpath . '/' . $db_basename)) { $separatedb = empty($plugins[$this->ud_foreign]['separatedb']) ? false : true; $filtered_db_name = apply_filters('updraftplus_foreign_dbfilename', false, $this->ud_foreign, $this->ud_backup_info, $working_dir_localpath, $separatedb); if (is_string($filtered_db_name)) { $db_basename = $filtered_db_name; } } } // wp_filesystem has no gzopen method, so we switch to using the local filesystem (which is harmless, since we are performing read-only operations) if (false === $db_basename || !is_readable($working_dir_localpath . '/' . $db_basename)) { return new WP_Error('dbopen_failed', __('Failed to find database file', 'updraftplus') . " ({$working_dir}/" . $db_basename . ")"); } global $wpdb, $updraftplus; $this->skin->feedback('restore_database'); $is_plain = substr($db_basename, -3, 3) == '.db'; $is_bz2 = substr($db_basename, -7, 7) == '.db.bz2'; // Read-only access: don't need to go through WP_Filesystem if ($is_plain) { $dbhandle = fopen($working_dir_localpath . '/' . $db_basename, 'r'); } elseif ($is_bz2) { if (!function_exists('bzopen')) { $updraftplus->log_e("Your web server's PHP installation has these functions disabled: %s.", 'bzopen'); $updraftplus->log_e('Your hosting company must enable these functions before %s can work.', __('restoration', 'updraftplus')); } $dbhandle = bzopen($working_dir_localpath . '/' . $db_basename, 'r'); } else { $dbhandle = gzopen($working_dir_localpath . '/' . $db_basename, 'r'); } if (!$dbhandle) { return new WP_Error('dbopen_failed', __('Failed to open database file', 'updraftplus')); } $this->line = 0; if (true == $this->use_wpdb) { $updraftplus->log_e('Database access: Direct MySQL access is not available, so we are falling back to wpdb (this will be considerably slower)'); } else { $updraftplus->log("Using direct MySQL access; value of use_mysqli is: " . ($this->use_mysqli ? '1' : '0')); if ($this->use_mysqli) { @mysqli_query($this->mysql_dbh, 'SET SESSION query_cache_type = OFF;'); } else { @mysql_query('SET SESSION query_cache_type = OFF;', $this->mysql_dbh); } } // Find the supported engines - in case the dump had something else (case seen: saved from MariaDB with engine Aria; imported into plain MySQL without) $supported_engines = $wpdb->get_results("SHOW ENGINES", OBJECT_K); $this->errors = 0; $this->statements_run = 0; $this->insert_statements_run = 0; $this->tables_created = 0; $sql_line = ""; $sql_type = -1; $this->start_time = microtime(true); $old_wpversion = ''; $this->old_siteurl = ''; $this->old_home = ''; $this->old_content = ''; $old_table_prefix = defined('UPDRAFTPLUS_OVERRIDE_IMPORT_PREFIX') && UPDRAFTPLUS_OVERRIDE_IMPORT_PREFIX ? UPDRAFTPLUS_OVERRIDE_IMPORT_PREFIX : ''; $old_siteinfo = array(); $gathering_siteinfo = true; $this->create_forbidden = false; $this->drop_forbidden = false; $this->last_error = ''; $random_table_name = 'updraft_tmp_' . rand(0, 9999999) . md5(microtime(true)); # The only purpose in funnelling queries directly here is to be able to get the error number if ($this->use_wpdb) { $req = $wpdb->query("CREATE TABLE {$random_table_name}"); if (!$req) { $this->last_error = $wpdb->last_error; } $this->last_error_no = false; } else { if ($this->use_mysqli) { $req = mysqli_query($this->mysql_dbh, "CREATE TABLE {$random_table_name}"); } else { $req = mysql_unbuffered_query("CREATE TABLE {$random_table_name}", $this->mysql_dbh); } if (!$req) { $this->last_error = $this->use_mysqli ? mysqli_error($this->mysql_dbh) : mysql_error($this->mysql_dbh); $this->last_error_no = $this->use_mysqli ? mysqli_errno($this->mysql_dbh) : mysql_errno($this->mysql_dbh); } } if (!$req && ($this->use_wpdb || 1142 === $this->last_error_no)) { $this->create_forbidden = true; # If we can't create, then there's no point dropping $this->drop_forbidden = true; echo '<strong>' . __('Warning:', 'updraftplus') . '</strong> '; $updraftplus->log_e('Your database user does not have permission to create tables. We will attempt to restore by simply emptying the tables; this should work as long as a) you are restoring from a WordPress version with the same database structure, and b) Your imported database does not contain any tables which are not already present on the importing site.', ' (' . $this->last_error . ')'); } else { if ($this->use_wpdb) { $req = $wpdb->query("DROP TABLE {$random_table_name}"); if (!$req) { $this->last_error = $wpdb->last_error; } $this->last_error_no = false; } else { if ($this->use_mysqli) { $req = mysqli_query($this->mysql_dbh, "DROP TABLE {$random_table_name}"); } else { $req = mysql_unbuffered_query("DROP TABLE {$random_table_name}", $this->mysql_dbh); } if (!$req) { $this->last_error = $this->use_mysqli ? mysqli_error($this->mysql_dbh) : mysql_error($this->mysql_dbh); $this->last_error_no = $this->use_mysqli ? mysqli_errno($this->mysql_dbh) : mysql_errno($this->mysql_dbh); } } if (!$req && ($this->use_wpdb || $this->last_error_no === 1142)) { $this->drop_forbidden = true; echo '<strong>' . __('Warning:', 'updraftplus') . '</strong> '; $updraftplus->log_e('Your database user does not have permission to drop tables. We will attempt to restore by simply emptying the tables; this should work as long as you are restoring from a WordPress version with the same database structure (%s)', ' (' . $this->last_error . ')'); } } $restoring_table = ''; $this->max_allowed_packet = $updraftplus->get_max_packet_size(); $updraftplus->log("Entering maintenance mode"); $this->maintenance_mode(true); // N.B. There is no such function as bzeof() - we have to detect that another way while ($is_plain && !feof($dbhandle) || !$is_plain && ($is_bz2 || !$is_bz2 && !gzeof($dbhandle))) { // Up to 1Mb if ($is_plain) { $buffer = rtrim(fgets($dbhandle, 1048576)); } elseif ($is_bz2) { if (!isset($bz2_buffer)) { $bz2_buffer = ''; } $buffer = ''; if (strlen($bz2_buffer) < 524288) { $bz2_buffer .= bzread($dbhandle, 1048576); } if (bzerrno($dbhandle) !== 0) { $updraftplus->log("bz2 error: " . bzerrstr($dbhandle) . " (code: " . bzerrno($bzhandle) . ")"); break; } if (false !== $bz2_buffer && '' !== $bz2_buffer) { if (false !== ($p = strpos($bz2_buffer, "\n"))) { $buffer .= substr($bz2_buffer, 0, $p + 1); $bz2_buffer = substr($bz2_buffer, $p + 1); } else { $buffer .= $bz2_buffer; $bz2_buffer = ''; } } else { break; } $buffer = rtrim($buffer); } else { $buffer = rtrim(gzgets($dbhandle, 1048576)); } // Discard comments if (empty($buffer) || substr($buffer, 0, 1) == '#' || preg_match('/^--(\\s|$)/', substr($buffer, 0, 3))) { if ('' == $this->old_siteurl && preg_match('/^\\# Backup of: (http(.*))$/', $buffer, $matches)) { $this->old_siteurl = untrailingslashit($matches[1]); $updraftplus->log_e('<strong>Backup of:</strong> %s', htmlspecialchars($this->old_siteurl)); do_action('updraftplus_restore_db_record_old_siteurl', $this->old_siteurl); $this->save_configuration_bundle(); } elseif (false === $this->created_by_version && preg_match('/^\\# Created by UpdraftPlus version ([\\d\\.]+)/', $buffer, $matches)) { $this->created_by_version = trim($matches[1]); echo '<strong>' . __('Backup created by:', 'updraftplus') . '</strong> ' . htmlspecialchars($this->created_by_version) . '<br>'; $updraftplus->log('Backup created by: ' . $this->created_by_version); } elseif ('' == $this->old_home && preg_match('/^\\# Home URL: (http(.*))$/', $buffer, $matches)) { $this->old_home = untrailingslashit($matches[1]); if ($this->old_siteurl && $this->old_home != $this->old_siteurl) { echo '<strong>' . __('Site home:', 'updraftplus') . '</strong> ' . htmlspecialchars($this->old_home) . '<br>'; $updraftplus->log('Site home: ' . $this->old_home); } do_action('updraftplus_restore_db_record_old_home', $this->old_home); } elseif ('' == $this->old_content && preg_match('/^\\# Content URL: (http(.*))$/', $buffer, $matches)) { $this->old_content = untrailingslashit($matches[1]); echo '<strong>' . __('Content URL:', 'updraftplus') . '</strong> ' . htmlspecialchars($this->old_content) . '<br>'; $updraftplus->log('Content URL: ' . $this->old_content); do_action('updraftplus_restore_db_record_old_content', $this->old_content); } elseif ('' == $old_table_prefix && (preg_match('/^\\# Table prefix: (\\S+)$/', $buffer, $matches) || preg_match('/^-- Table Prefix: (\\S+)$/i', $buffer, $matches))) { # We also support backwpup style: # -- Table Prefix: wp_ $old_table_prefix = $matches[1]; echo '<strong>' . __('Old table prefix:', 'updraftplus') . '</strong> ' . htmlspecialchars($old_table_prefix) . '<br>'; $updraftplus->log("Old table prefix: " . $old_table_prefix); } elseif ($gathering_siteinfo && preg_match('/^\\# Site info: (\\S+)$/', $buffer, $matches)) { if ('end' == $matches[1]) { $gathering_siteinfo = false; // Sanity checks if (isset($old_siteinfo['multisite']) && !$old_siteinfo['multisite'] && is_multisite()) { // Just need to check that you're crazy if (!defined('UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE') || UPDRAFTPLUS_EXPERIMENTAL_IMPORTINTOMULTISITE != true) { return new WP_Error('multisite_error', $this->strings['multisite_error']); } // Got the needed code? if (!class_exists('UpdraftPlusAddOn_MultiSite') || !class_exists('UpdraftPlus_Addons_Migrator')) { return new WP_Error('missing_addons', __('To import an ordinary WordPress site into a multisite installation requires both the multisite and migrator add-ons.', 'updraftplus')); } } } elseif (preg_match('/^([^=]+)=(.*)$/', $matches[1], $kvmatches)) { $key = $kvmatches[1]; $val = $kvmatches[2]; echo '<strong>' . __('Site information:', 'updraftplus') . '</strong>' . ' ' . htmlspecialchars($key) . ' = ' . htmlspecialchars($val) . '<br>'; $updraftplus->log("Site information: " . $key . "=" . $val); $old_siteinfo[$key] = $val; if ('multisite' == $key) { if ($val) { $this->ud_backup_is_multisite = 1; } else { $this->ud_backup_is_multisite = 0; } } } } continue; } // Detect INSERT commands early, so that we can split them if necessary if (preg_match('/^\\s*(insert into \\`?([^\\`]*)\\`?\\s+(values|\\())/i', $sql_line . $buffer, $matches)) { $sql_type = 3; $insert_prefix = $matches[1]; } # Deal with case where adding this line will take us over the MySQL max_allowed_packet limit - must split, if we can (if it looks like consecutive rows) # ALlow a 100-byte margin for error (including searching/replacing table prefix) if (3 == $sql_type && $sql_line && strlen($sql_line . $buffer) > $this->max_allowed_packet - 100 && preg_match('/,\\s*$/', $sql_line) && preg_match('/^\\s*\\(/', $buffer)) { // Remove the final comma; replace with semi-colon $sql_line = substr(rtrim($sql_line), 0, strlen($sql_line) - 1) . ';'; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } # Run the SQL command; then set up for the next one. $this->line++; echo __("Split line to avoid exceeding maximum packet size", 'updraftplus') . " (" . strlen($sql_line) . " + " . strlen($buffer) . " : " . $this->max_allowed_packet . ")<br>"; $updraftplus->log("Split line to avoid exceeding maximum packet size (" . strlen($sql_line) . " + " . strlen($buffer) . " : " . $this->max_allowed_packet . ")"); $do_exec = $this->sql_exec($sql_line, $sql_type, $import_table_prefix); if (is_wp_error($do_exec)) { return $do_exec; } # Reset, then carry on $sql_line = $insert_prefix . " "; } $sql_line .= $buffer; # Do we have a complete line yet? We used to just test the final character for ';' here (up to 1.8.12), but that was too unsophisticated if (3 == $sql_type && !preg_match('/\\)\\s*;$/', substr($sql_line, -3, 3)) || 3 != $sql_type && ';' != substr($sql_line, -1, 1)) { continue; } $this->line++; # We now have a complete line - process it if (3 == $sql_type && $sql_line && strlen($sql_line) > $this->max_allowed_packet) { $this->log_oversized_packet($sql_line); # Reset $sql_line = ''; $sql_type = -1; # If this is the very first SQL line of the options table, we need to bail; it's essential if (0 == $this->insert_statements_run && $restoring_table && $restoring_table == $import_table_prefix . 'options') { $updraftplus->log("Leaving maintenance mode"); $this->maintenance_mode(false); return new WP_Error('initial_db_error', sprintf(__('An error occurred on the first %s command - aborting run', 'updraftplus'), 'INSERT (options)')); } continue; } # The timed overhead of this is negligible if (preg_match('/^\\s*drop table if exists \\`?([^\\`]*)\\`?\\s*;/i', $sql_line, $matches)) { $sql_type = 1; if (!isset($printed_new_table_prefix)) { $import_table_prefix = $this->pre_sql_actions($import_table_prefix); if (false === $import_table_prefix || is_wp_error($import_table_prefix)) { return $import_table_prefix; } $printed_new_table_prefix = true; } $this->table_name = $matches[1]; // Legacy, less reliable - in case it was not caught before if ('' == $old_table_prefix && preg_match('/^([a-z0-9]+)_.*$/i', $this->table_name, $tmatches)) { $old_table_prefix = $tmatches[1] . '_'; echo '<strong>' . __('Old table prefix:', 'updraftplus') . '</strong> ' . htmlspecialchars($old_table_prefix) . '<br>'; $updraftplus->log("Old table prefix (detected from first table): {$old_table_prefix}"); } $this->new_table_name = $old_table_prefix ? $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $this->table_name) : $this->table_name; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } $this->tables_been_dropped[] = $this->new_table_name; } elseif (preg_match('/^\\s*create table \\`?([^\\`\\(]*)\\`?\\s*\\(/i', $sql_line, $matches)) { $sql_type = 2; $this->insert_statements_run = 0; $this->table_name = $matches[1]; // Legacy, less reliable - in case it was not caught before. We added it in here (CREATE) as well as in DROP because of SQL dumps which lack DROP statements. if ('' == $old_table_prefix && preg_match('/^([a-z0-9]+)_.*$/i', $this->table_name, $tmatches)) { $old_table_prefix = $tmatches[1] . '_'; echo '<strong>' . __('Old table prefix:', 'updraftplus') . '</strong> ' . htmlspecialchars($old_table_prefix) . '<br>'; $updraftplus->log("Old table prefix (detected from creating first table): {$old_table_prefix}"); } // MySQL 4.1 outputs TYPE=, but accepts ENGINE=; 5.1 onwards accept *only* ENGINE= $sql_line = $updraftplus->str_lreplace('TYPE=', 'ENGINE=', $sql_line); if (empty($printed_new_table_prefix)) { $import_table_prefix = $this->pre_sql_actions($import_table_prefix); if (false === $import_table_prefix || is_wp_error($import_table_prefix)) { return $import_table_prefix; } $printed_new_table_prefix = true; } $this->new_table_name = $old_table_prefix ? $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $this->table_name) : $this->table_name; // This CREATE TABLE command may be the de-facto mark for the end of processing a previous table (which is so if this is not the first table in the SQL dump) if ($restoring_table) { # Attempt to reconnect if the DB connection dropped (may not succeed, of course - but that will soon become evident) $updraftplus->check_db_connection($this->wpdb_obj); // After restoring the options table, we can set old_siteurl if on legacy (i.e. not already set) if ($restoring_table == $import_table_prefix . 'options') { if ('' == $this->old_siteurl || '' == $this->old_home || '' == $this->old_content) { global $updraftplus_addons_migrator; if (isset($updraftplus_addons_migrator->new_blogid)) { switch_to_blog($updraftplus_addons_migrator->new_blogid); } if ('' == $this->old_siteurl) { $this->old_siteurl = untrailingslashit($wpdb->get_row("SELECT option_value FROM {$wpdb->options} WHERE option_name='siteurl'")->option_value); do_action('updraftplus_restore_db_record_old_siteurl', $this->old_siteurl); } if ('' == $this->old_home) { $this->old_home = untrailingslashit($wpdb->get_row("SELECT option_value FROM {$wpdb->options} WHERE option_name='home'")->option_value); do_action('updraftplus_restore_db_record_old_home', $this->old_home); } if ('' == $this->old_content) { $this->old_content = $this->old_siteurl . '/wp-content'; do_action('updraftplus_restore_db_record_old_content', $this->old_content); } if (isset($updraftplus_addons_migrator->new_blogid)) { restore_current_blog(); } } } if ($restoring_table != $this->new_table_name) { $this->restored_table($restoring_table, $import_table_prefix, $old_table_prefix); } } $engine = "(?)"; $engine_change_message = ''; if (preg_match('/ENGINE=([^\\s;]+)/', $sql_line, $eng_match)) { $engine = $eng_match[1]; if (isset($supported_engines[$engine])) { #echo sprintf(__('Requested table engine (%s) is present.', 'updraftplus'), $engine); if ('myisam' == strtolower($engine)) { $sql_line = preg_replace('/PAGE_CHECKSUM=\\d\\s?/', '', $sql_line, 1); } } else { $engine_change_message = sprintf(__('Requested table engine (%s) is not present - changing to MyISAM.', 'updraftplus'), $engine) . "<br>"; $sql_line = $updraftplus->str_lreplace("ENGINE={$eng_match}", "ENGINE=MyISAM", $sql_line); // Remove (M)aria options if ('maria' == strtolower($engine) || 'aria' == strtolower($engine)) { $sql_line = preg_replace('/PAGE_CHECKSUM=\\d\\s?/', '', $sql_line, 1); $sql_line = preg_replace('/TRANSACTIONAL=\\d\\s?/', '', $sql_line, 1); } } } $this->table_name = $matches[1]; echo '<strong>' . sprintf(__('Restoring table (%s)', 'updraftplus'), $engine) . ":</strong> " . htmlspecialchars($this->table_name); $logline = "Restoring table ({$engine}): " . $this->table_name; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { echo ' - ' . __('will restore as:', 'updraftplus') . ' ' . htmlspecialchars($this->new_table_name); $logline .= " - will restore as: " . $this->new_table_name; $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } $updraftplus->log($logline); $restoring_table = $this->new_table_name; echo '<br>'; if ($engine_change_message) { echo $engine_change_message; } } elseif (preg_match('/^\\s*(insert into \\`?([^\\`]*)\\`?\\s+(values|\\())/i', $sql_line, $matches)) { $sql_type = 3; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } } elseif (preg_match('/^\\s*(\\/\\*\\!40000 )?(alter|lock) tables? \\`?([^\\`\\(]*)\\`?\\s+(write|disable|enable)/i', $sql_line, $matches)) { # Only binary mysqldump produces this pattern (LOCK TABLES `table` WRITE, ALTER TABLE `table` (DISABLE|ENABLE) KEYS) $sql_type = 4; if ('' != $old_table_prefix && $import_table_prefix != $old_table_prefix) { $sql_line = $updraftplus->str_replace_once($old_table_prefix, $import_table_prefix, $sql_line); } } elseif (preg_match('/^(un)?lock tables/i', $sql_line)) { # BackWPup produces these $sql_type = 5; } elseif (preg_match('/^(create|drop) database /i', $sql_line)) { # WPB2D produces these, as do some phpMyAdmin dumps $sql_type = 6; } elseif (preg_match('/^use /i', $sql_line)) { # WPB2D produces these, as do some phpMyAdmin dumps $sql_type = 7; } elseif (preg_match('#/\\*\\!40\\d+ SET NAMES (\\S+)#', $sql_line, $smatches)) { $sql_type = 8; $this->set_names = $smatches[1]; } else { # Prevent the previous value of $sql_type being retained for an unknown type $sql_type = 0; } // if (5 !== $sql_type) { if ($sql_type != 6 && $sql_type != 7) { $do_exec = $this->sql_exec($sql_line, $sql_type); if (is_wp_error($do_exec)) { return $do_exec; } } else { $updraftplus->log("Skipped SQL statement (unwanted type={$sql_type}): {$sql_line}"); } # Reset $sql_line = ''; $sql_type = -1; } $updraftplus->log("Leaving maintenance mode"); $this->maintenance_mode(false); if ($restoring_table) { $this->restored_table($restoring_table, $import_table_prefix, $old_table_prefix); } $time_taken = microtime(true) - $this->start_time; $updraftplus->log_e('Finished: lines processed: %d in %.2f seconds', $this->line, $time_taken); if ($is_plain) { fclose($dbhandle); } elseif ($is_bz2) { bzclose($dbhandle); } else { gzclose($dbhandle); } global $wp_filesystem; $wp_filesystem->delete($working_dir . '/' . $db_basename, false, 'f'); return true; }
/** * Initialize the package creator */ function init() { switch ($this->compress) { case 'zlib' : $this->tmp = gzopen($this->path, 'wb'); break; case 'bz2' : $this->tmp = bzopen($this->path, 'wb'); break; case 'none' : $this->tmp = fopen($this->path, 'wb'); break; default : throw new \PEAR2\Pyrus\Developer\Creator\Exception( 'unknown compression type ' . $this->compress); } }
function main($id, $mode) { global $cache, $db, $user, $template, $table_prefix, $request; global $phpbb_root_path, $phpbb_container, $phpbb_log; $this->db_tools = $phpbb_container->get('dbal.tools'); $user->add_lang('acp/database'); $this->tpl_name = 'acp_database'; $this->page_title = 'ACP_DATABASE'; $action = $request->variable('action', ''); $submit = isset($_POST['submit']) ? true : false; $template->assign_vars(array('MODE' => $mode)); switch ($mode) { case 'backup': $this->page_title = 'ACP_BACKUP'; switch ($action) { case 'download': $type = $request->variable('type', ''); $table = array_intersect($this->db_tools->sql_list_tables(), $request->variable('table', array(''))); $format = $request->variable('method', ''); $where = $request->variable('where', ''); if (!sizeof($table)) { trigger_error($user->lang['TABLE_SELECT_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING); } $store = $download = $structure = $schema_data = false; if ($where == 'store_and_download' || $where == 'store') { $store = true; } if ($where == 'store_and_download' || $where == 'download') { $download = true; } if ($type == 'full' || $type == 'structure') { $structure = true; } if ($type == 'full' || $type == 'data') { $schema_data = true; } @set_time_limit(1200); @set_time_limit(0); $time = time(); $filename = 'backup_' . $time . '_' . unique_id(); $extractor = $phpbb_container->get('dbal.extractor'); $extractor->init_extractor($format, $filename, $time, $download, $store); $extractor->write_start($table_prefix); foreach ($table as $table_name) { // Get the table structure if ($structure) { $extractor->write_table($table_name); } else { // We might wanna empty out all that junk :D switch ($db->get_sql_layer()) { case 'sqlite': case 'sqlite3': $extractor->flush('DELETE FROM ' . $table_name . ";\n"); break; case 'mssql': case 'mssql_odbc': case 'mssqlnative': $extractor->flush('TRUNCATE TABLE ' . $table_name . "GO\n"); break; case 'oracle': $extractor->flush('TRUNCATE TABLE ' . $table_name . "/\n"); break; default: $extractor->flush('TRUNCATE TABLE ' . $table_name . ";\n"); break; } } // Data if ($schema_data) { $extractor->write_data($table_name); } } $extractor->write_end(); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_BACKUP'); if ($download == true) { exit; } trigger_error($user->lang['BACKUP_SUCCESS'] . adm_back_link($this->u_action)); break; default: $tables = $this->db_tools->sql_list_tables(); asort($tables); foreach ($tables as $table_name) { if (strlen($table_prefix) === 0 || stripos($table_name, $table_prefix) === 0) { $template->assign_block_vars('tables', array('TABLE' => $table_name)); } } unset($tables); $template->assign_vars(array('U_ACTION' => $this->u_action . '&action=download')); $available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2'); foreach ($available_methods as $type => $module) { if (!@extension_loaded($module)) { continue; } $template->assign_block_vars('methods', array('TYPE' => $type)); } $template->assign_block_vars('methods', array('TYPE' => 'text')); break; } break; case 'restore': $this->page_title = 'ACP_RESTORE'; switch ($action) { case 'submit': $delete = $request->variable('delete', ''); $file = $request->variable('file', ''); $download = $request->variable('download', ''); if (!preg_match('#^backup_\\d{10,}_[a-z\\d]{16}\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches)) { trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); } $file_name = $phpbb_root_path . 'store/' . $matches[0]; if (!file_exists($file_name) || !is_readable($file_name)) { trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING); } if ($delete) { if (confirm_box(true)) { unlink($file_name); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_DELETE'); trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action)); } else { confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('delete' => $delete, 'file' => $file))); } } else { if ($download || confirm_box(true)) { if ($download) { $name = $matches[0]; switch ($matches[1]) { case 'sql': $mimetype = 'text/x-sql'; break; case 'sql.bz2': $mimetype = 'application/x-bzip2'; break; case 'sql.gz': $mimetype = 'application/x-gzip'; break; } header('Cache-Control: private, no-cache'); header("Content-Type: {$mimetype}; name=\"{$name}\""); header("Content-disposition: attachment; filename={$name}"); @set_time_limit(0); $fp = @fopen($file_name, 'rb'); if ($fp !== false) { while (!feof($fp)) { echo fread($fp, 8192); } fclose($fp); } flush(); exit; } switch ($matches[1]) { case 'sql': $fp = fopen($file_name, 'rb'); $read = 'fread'; $seek = 'fseek'; $eof = 'feof'; $close = 'fclose'; $fgetd = 'fgetd'; break; case 'sql.bz2': $fp = bzopen($file_name, 'r'); $read = 'bzread'; $seek = ''; $eof = 'feof'; $close = 'bzclose'; $fgetd = 'fgetd_seekless'; break; case 'sql.gz': $fp = gzopen($file_name, 'rb'); $read = 'gzread'; $seek = 'gzseek'; $eof = 'gzeof'; $close = 'gzclose'; $fgetd = 'fgetd'; break; } switch ($db->get_sql_layer()) { case 'mysql': case 'mysql4': case 'mysqli': case 'sqlite': case 'sqlite3': while (($sql = $fgetd($fp, ";\n", $read, $seek, $eof)) !== false) { $db->sql_query($sql); } break; case 'postgres': $delim = ";\n"; while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false) { $query = trim($sql); if (substr($query, 0, 13) == 'CREATE DOMAIN') { list(, , $domain) = explode(' ', $query); $sql = "SELECT domain_name\n\t\t\t\t\t\t\t\t\t\t\t\tFROM information_schema.domains\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE domain_name = '{$domain}';"; $result = $db->sql_query($sql); if (!$db->sql_fetchrow($result)) { $db->sql_query($query); } $db->sql_freeresult($result); } else { $db->sql_query($query); } if (substr($query, 0, 4) == 'COPY') { while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\\.') { if ($sub === false) { trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING); } pg_put_line($db->get_db_connect_id(), $sub . "\n"); } pg_put_line($db->get_db_connect_id(), "\\.\n"); pg_end_copy($db->get_db_connect_id()); } } break; case 'oracle': while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false) { $db->sql_query($sql); } break; case 'mssql': case 'mssql_odbc': case 'mssqlnative': while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false) { $db->sql_query($sql); } break; } $close($fp); // Purge the cache due to updated data $cache->purge(); $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_RESTORE'); trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action)); break; } else { if (!$download) { confirm_box(false, $user->lang['RESTORE_SELECTED_BACKUP'], build_hidden_fields(array('file' => $file))); } } } default: $methods = array('sql'); $available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2'); foreach ($available_methods as $type => $module) { if (!@extension_loaded($module)) { continue; } $methods[] = $type; } $dir = $phpbb_root_path . 'store/'; $dh = @opendir($dir); $backup_files = array(); if ($dh) { while (($file = readdir($dh)) !== false) { if (preg_match('#^backup_(\\d{10,})_[a-z\\d]{16}\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches)) { if (in_array($matches[2], $methods)) { $backup_files[(int) $matches[1]] = $file; } } } closedir($dh); } if (!empty($backup_files)) { krsort($backup_files); foreach ($backup_files as $name => $file) { $template->assign_block_vars('files', array('FILE' => $file, 'NAME' => $user->format_date($name, 'd-m-Y H:i:s', true), 'SUPPORTED' => true)); } } $template->assign_vars(array('U_ACTION' => $this->u_action . '&action=submit')); break; } break; } }