mirror of
https://github.com/mainwp/mainwp-child.git
synced 2025-09-05 09:19:53 +08:00
This commit is contained in:
parent
6d684ade43
commit
5801f7b735
4 changed files with 190 additions and 28 deletions
|
@ -46,6 +46,20 @@ class MainWPBackup
|
|||
$pid = trailingslashit($backupdir) . 'backup-' . $pid . '.pid';
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Verify if another backup is running, if so, return an error
|
||||
$files = glob($backupdir . '*.pid');
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if (basename($file) == basename($pid)) continue;
|
||||
|
||||
if ((time() - filemtime($file)) < 160)
|
||||
{
|
||||
MainWPHelper::error('Another backup process is running, try again later');
|
||||
}
|
||||
}
|
||||
|
||||
$timestamp = time();
|
||||
if ($filePrefix != '') $filePrefix .= '-';
|
||||
|
||||
|
|
|
@ -681,14 +681,16 @@ class MainWPChild
|
|||
|
||||
if (isset($_GET['mainwptest']))
|
||||
{
|
||||
// error_reporting(E_ALL);
|
||||
// ini_set('display_errors', TRUE);
|
||||
// ini_set('display_startup_errors', TRUE);
|
||||
// echo '<pre>';
|
||||
// $start = microtime(true);
|
||||
//
|
||||
// $stop = microtime(true);
|
||||
// die(($stop - $start) . 's</pre>');
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', TRUE);
|
||||
ini_set('display_startup_errors', TRUE);
|
||||
echo '<pre>';
|
||||
$start = microtime(true);
|
||||
|
||||
print_r(base64_encode(gzdeflate("test1234")));
|
||||
|
||||
$stop = microtime(true);
|
||||
die(($stop - $start) . 's</pre>');
|
||||
}
|
||||
|
||||
//Register does not require auth, so we register here..
|
||||
|
@ -1777,16 +1779,6 @@ class MainWPChild
|
|||
}
|
||||
}
|
||||
|
||||
//Verify if another backup is running, if so, return an error
|
||||
$files = glob($backupdir . '*.pid');
|
||||
foreach ($files as $file)
|
||||
{
|
||||
if ((time() - filemtime($file)) < 160)
|
||||
{
|
||||
MainWPHelper::error('Another backup process is running, try again later');
|
||||
}
|
||||
}
|
||||
|
||||
$fileName = (isset($_POST['fileUID']) ? $_POST['fileUID'] : '');
|
||||
if ($_POST['type'] == 'full')
|
||||
{
|
||||
|
|
|
@ -15,6 +15,11 @@ class TarArchiver
|
|||
protected $archiveSize;
|
||||
protected $lastRun = 0;
|
||||
|
||||
protected $debug;
|
||||
|
||||
protected $chunk = ''; //1024 * 1024 * 4
|
||||
protected $chunkSize = 4194304; //1024 * 1024 * 4
|
||||
|
||||
/** @var $backup MainWPBackup */
|
||||
protected $backup;
|
||||
|
||||
|
@ -29,6 +34,8 @@ class TarArchiver
|
|||
|
||||
public function __construct($backup, $type = 'tar', $pidFile = false)
|
||||
{
|
||||
$this->debug = false;
|
||||
|
||||
$this->pidFile = $pidFile;
|
||||
$this->backup = $backup;
|
||||
|
||||
|
@ -132,7 +139,7 @@ class TarArchiver
|
|||
if ($append && @file_exists($filepath)) //todo: use wpFS
|
||||
{
|
||||
$this->mode = self::APPEND;
|
||||
$this->read($filepath);
|
||||
$this->prepareAppend($filepath);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -286,9 +293,22 @@ class TarArchiver
|
|||
|
||||
private function addData($data)
|
||||
{
|
||||
if ($this->debug)
|
||||
{
|
||||
$this->chunk .= $data;
|
||||
|
||||
if (strlen($this->chunk) > $this->chunkSize)
|
||||
{
|
||||
$this->writeChunk();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->type == 'tar.gz')
|
||||
{
|
||||
if (@fwrite($this->archive, $data, strlen($data)) === false)
|
||||
//if (@fwrite($this->archive, $data, strlen($data)) === false)
|
||||
if (@gzwrite($this->archive, $data, strlen($data)) === false)
|
||||
{
|
||||
throw new Exception('Could not write to archive');
|
||||
}
|
||||
|
@ -311,6 +331,47 @@ class TarArchiver
|
|||
}
|
||||
}
|
||||
|
||||
private function writeChunk()
|
||||
{
|
||||
$len = strlen($this->chunk);
|
||||
if ($len == 0) return;
|
||||
|
||||
// if ($this->cnt++ > 3)
|
||||
// {
|
||||
// $this->log('error?');
|
||||
// $this->cnt = 0;
|
||||
// throw new Exception('error!');
|
||||
// }
|
||||
|
||||
if ($this->type == 'tar.gz')
|
||||
{
|
||||
$this->log('writing & flushing ' . $len);;
|
||||
$this->chunk = gzencode($this->chunk);
|
||||
if (@fwrite($this->archive, $this->chunk, strlen($this->chunk)) === false)
|
||||
{
|
||||
throw new Exception('Could not write to archive');
|
||||
}
|
||||
@fflush($this->archive);
|
||||
}
|
||||
else if ($this->type == 'tar.bz2')
|
||||
{
|
||||
if (@bzwrite($this->archive, $this->chunk, strlen($len)) === false)
|
||||
{
|
||||
throw new Exception('Could not write to archive');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (@fwrite($this->archive, $len, strlen($len)) === false)
|
||||
{
|
||||
throw new Exception('Could not write to archive');
|
||||
}
|
||||
@fflush($this->archive);
|
||||
}
|
||||
|
||||
$this->chunk = '';
|
||||
}
|
||||
|
||||
private function addEmptyDir($path, $entryName)
|
||||
{
|
||||
$stat = @stat($path);
|
||||
|
@ -423,6 +484,8 @@ class TarArchiver
|
|||
// if ($this->cnt > 250) throw new Exception('Some error..' . $this->archivePath);
|
||||
// }
|
||||
|
||||
$this->updatePidFile();
|
||||
|
||||
$rslt = false;
|
||||
if ($this->mode == self::APPEND)
|
||||
{
|
||||
|
@ -433,8 +496,6 @@ class TarArchiver
|
|||
}
|
||||
}
|
||||
|
||||
$this->updatePidFile();
|
||||
|
||||
if (time() - $this->lastRun > 60)
|
||||
{
|
||||
@set_time_limit(20 * 60 * 60); /*20 minutes*/
|
||||
|
@ -583,7 +644,6 @@ class TarArchiver
|
|||
}
|
||||
}
|
||||
|
||||
//todo: add ceck to append!!!!
|
||||
$prefix = "";
|
||||
if (strlen($entryName) > 99)
|
||||
{
|
||||
|
@ -857,9 +917,23 @@ class TarArchiver
|
|||
function create($filepath)
|
||||
{
|
||||
$this->log('Creating ' . $filepath);
|
||||
if ($this->debug)
|
||||
{
|
||||
if ($this->type == 'tar.bz2')
|
||||
{
|
||||
$this->archive = @bzopen($filepath, 'w');
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->archive = @fopen($filepath, 'wb+');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->type == 'tar.gz')
|
||||
{
|
||||
$this->archive = @fopen('compress.zlib://' . $filepath, 'ab');
|
||||
//$this->archive = @fopen('compress.zlib://' . $filepath, 'ab');
|
||||
$this->archive = @gzopen($filepath, 'w');
|
||||
}
|
||||
else if ($this->type == 'tar.bz2')
|
||||
{
|
||||
|
@ -874,9 +948,23 @@ class TarArchiver
|
|||
function append($filepath)
|
||||
{
|
||||
$this->log('Appending to ' . $filepath);
|
||||
if ($this->debug)
|
||||
{
|
||||
if ($this->type == 'tar.bz2')
|
||||
{
|
||||
$this->archive = @bzopen($filepath, 'a');
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->archive = @fopen($filepath, 'ab+');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->type == 'tar.gz')
|
||||
{
|
||||
$this->archive = @fopen('compress.zlib://' . $filepath, 'ab');
|
||||
//$this->archive = @fopen('compress.zlib://' . $filepath, 'ab');
|
||||
$this->archive = @gzopen($filepath, 'a');
|
||||
}
|
||||
else if ($this->type == 'tar.bz2')
|
||||
{
|
||||
|
@ -893,6 +981,59 @@ class TarArchiver
|
|||
return !empty($this->archive);
|
||||
}
|
||||
|
||||
function prepareAppend($filepath)
|
||||
{
|
||||
if ($this->debug)
|
||||
{
|
||||
if (substr($filepath, -6) == 'tar.gz')
|
||||
{
|
||||
$text = chr(31) . chr(139) . chr(8) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0); //magic header!!
|
||||
|
||||
//Check if valid, if not, crop to valid!
|
||||
$fh = @fopen($filepath, 'rb');
|
||||
$read = '';
|
||||
$lastCorrect = 0;
|
||||
try
|
||||
{
|
||||
while (!feof($fh))
|
||||
{
|
||||
$read .= fread($fh, 1000);
|
||||
while (($pos = strpos($read, $text, 2)) !== false)
|
||||
{
|
||||
for ($i = 0; $i < 10; $i++)
|
||||
{
|
||||
echo ord($read[$i]) . "\n";
|
||||
}
|
||||
|
||||
if (!$this->isValidBlock(substr($read, 10, $pos - 10)))
|
||||
{
|
||||
throw new Exception('invalid!');
|
||||
}
|
||||
|
||||
$lastCorrect += $pos;
|
||||
$read = substr($read, $pos);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->isValidBlock(substr($read, 10))) throw new Exception('invalid!');
|
||||
|
||||
@fclose($fh);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
@fclose($fh);
|
||||
//reopen & truncate
|
||||
$fh = @fopen($filepath, 'ab+');
|
||||
@fseek($fh, $lastCorrect);
|
||||
@ftruncate($fh, $lastCorrect);
|
||||
@fclose($fh);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->read($filepath);
|
||||
}
|
||||
|
||||
function read($filepath)
|
||||
{
|
||||
$this->log('Reading ' . $filepath);
|
||||
|
@ -901,7 +1042,8 @@ class TarArchiver
|
|||
if (substr($filepath, -6) == 'tar.gz')
|
||||
{
|
||||
$this->type = 'tar.gz';
|
||||
$this->archive = @fopen('compress.zlib://' . $filepath, 'rb');
|
||||
// $this->archive = @fopen('compress.zlib://' . $filepath, 'rb');
|
||||
$this->archive = @gzopen($filepath, 'r');
|
||||
}
|
||||
else if (substr($filepath, -7) == 'tar.bz2')
|
||||
{
|
||||
|
@ -924,6 +1066,9 @@ class TarArchiver
|
|||
|
||||
function close($closeLog = true)
|
||||
{
|
||||
//Write chunk if it's not empty..
|
||||
$this->writeChunk();
|
||||
|
||||
$this->log('Closing archive');
|
||||
|
||||
if ($closeLog && $this->logHandle)
|
||||
|
@ -935,7 +1080,8 @@ class TarArchiver
|
|||
{
|
||||
if ($this->type == 'tar.gz')
|
||||
{
|
||||
@fclose($this->archive);
|
||||
//@fclose($this->archive);
|
||||
@gzclose($this->archive);
|
||||
}
|
||||
else if ($this->type == 'tar.bz2')
|
||||
{
|
||||
|
@ -1214,6 +1360,16 @@ class TarArchiver
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
function isValidBlock($block)
|
||||
{
|
||||
$test = @gzinflate($block);
|
||||
if ($test === false) return false;
|
||||
$crc = crc32($test);
|
||||
$crcFound = substr($block, strlen($block) - 8, 4);
|
||||
$crcFound = (ord($crcFound[3]) << 24) + (ord($crcFound[2]) << 16) + (ord($crcFound[1]) << 8) + (ord($crcFound[0]));
|
||||
return $crcFound == $crc;
|
||||
}
|
||||
}
|
||||
if (class_exists('SplHeap'))
|
||||
{
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
Description: Child Plugin for MainWP. The plugin is used so the installed blog can be securely managed remotely by your network. Plugin documentation and options can be found here http://docs.mainwp.com
|
||||
Author: MainWP
|
||||
Author URI: http://mainwp.com
|
||||
Version: 2.0.3
|
||||
Version: 2.0.4-beta
|
||||
*/
|
||||
header('X-Frame-Options: ALLOWALL');
|
||||
//header('X-Frame-Options: GOFORIT');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue