Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/MWP/Incremen...
File: HashComputer.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
class MWP_IncrementalBackup_HashComputer
[10] Fix | Delete
{
[11] Fix | Delete
[12] Fix | Delete
// 100kb default value
[13] Fix | Delete
private $maxChunkByteSize = 102400;
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* @param int $maxChunkByteSize
[17] Fix | Delete
*/
[18] Fix | Delete
public function setMaxChunkByteSize($maxChunkByteSize)
[19] Fix | Delete
{
[20] Fix | Delete
$this->maxChunkByteSize = $maxChunkByteSize;
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* @return int
[25] Fix | Delete
*/
[26] Fix | Delete
public function getMaxChunkByteSize()
[27] Fix | Delete
{
[28] Fix | Delete
return $this->maxChunkByteSize;
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Compute a full md5 file hash or compute a partial hash if $limit is present.
[33] Fix | Delete
* Returns null on failure.
[34] Fix | Delete
*
[35] Fix | Delete
* @param string $realPath
[36] Fix | Delete
* @param int $offset in bytes
[37] Fix | Delete
* @param int $limit in bytes
[38] Fix | Delete
* @param boolean $forcePartialHashing
[39] Fix | Delete
*
[40] Fix | Delete
* @return null|string
[41] Fix | Delete
*/
[42] Fix | Delete
public function computeMd5Hash($realPath, $offset = 0, $limit = 0, $forcePartialHashing = false)
[43] Fix | Delete
{
[44] Fix | Delete
if ($limit === 0 && $offset === 0 && !$forcePartialHashing) {
[45] Fix | Delete
// md5_file is always faster if we don't chunk the file
[46] Fix | Delete
$hash = md5_file($realPath);
[47] Fix | Delete
[48] Fix | Delete
return $hash !== false ? $hash : null;
[49] Fix | Delete
}
[50] Fix | Delete
[51] Fix | Delete
$ctx = hash_init('md5');
[52] Fix | Delete
if (!$ctx) {
[53] Fix | Delete
// Fail to initialize file hashing
[54] Fix | Delete
return null;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
// Calculate limit from file size and offset
[58] Fix | Delete
if ($limit === 0) {
[59] Fix | Delete
$limit = filesize($realPath) - $offset;
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
$fh = @fopen($realPath, "rb");
[63] Fix | Delete
if ($fh === false) {
[64] Fix | Delete
// Failed opening file, cleanup hash context
[65] Fix | Delete
hash_final($ctx);
[66] Fix | Delete
[67] Fix | Delete
return null;
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
fseek($fh, $offset);
[71] Fix | Delete
[72] Fix | Delete
while ($limit > 0) {
[73] Fix | Delete
// Limit chunk size to either our remaining chunk or max chunk size
[74] Fix | Delete
$chunkSize = $limit < $this->maxChunkByteSize ? $limit : $this->maxChunkByteSize;
[75] Fix | Delete
$limit -= $chunkSize;
[76] Fix | Delete
[77] Fix | Delete
$chunk = fread($fh, $chunkSize);
[78] Fix | Delete
hash_update($ctx, $chunk);
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
fclose($fh);
[82] Fix | Delete
[83] Fix | Delete
return hash_final($ctx);
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Run md5sum process and return file hash, or null in case of error
[88] Fix | Delete
*
[89] Fix | Delete
* @param $realPath
[90] Fix | Delete
*
[91] Fix | Delete
* @return string|null
[92] Fix | Delete
*/
[93] Fix | Delete
public function computeUnixMd5Sum($realPath)
[94] Fix | Delete
{
[95] Fix | Delete
try {
[96] Fix | Delete
$processBuilder = Symfony_Process_ProcessBuilder::create()
[97] Fix | Delete
->setPrefix('md5sum')
[98] Fix | Delete
->add($realPath);
[99] Fix | Delete
[100] Fix | Delete
if (!mwp_is_shell_available()) {
[101] Fix | Delete
throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, "Shell is not available");
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
$process = $processBuilder->getProcess();
[105] Fix | Delete
$process->run();
[106] Fix | Delete
[107] Fix | Delete
if (!$process->isSuccessful()) {
[108] Fix | Delete
throw new Symfony_Process_Exception_ProcessFailedException($process);
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
// Output is in the format of "md5hash filename"
[112] Fix | Delete
$output = trim($process->getOutput());
[113] Fix | Delete
$parts = explode(' ', $output);
[114] Fix | Delete
[115] Fix | Delete
// Return only the first part of the output
[116] Fix | Delete
return trim($parts[0]);
[117] Fix | Delete
} catch (Symfony_Process_Exception_ProcessFailedException $e) {
[118] Fix | Delete
mwp_logger()->error('MD5 command line sum failed', array(
[119] Fix | Delete
'process' => $e->getProcess(),
[120] Fix | Delete
));
[121] Fix | Delete
} catch (Exception $e) {
[122] Fix | Delete
mwp_logger()->error('MD5 command line sum failed', array(
[123] Fix | Delete
'exception' => $e,
[124] Fix | Delete
));
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
return null;
[128] Fix | Delete
}
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function