| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_Action_IncrementalBackup_HashFiles extends MWP_Action_IncrementalBackup_AbstractFiles |
| 12 |
{ |
| 13 |
// 100MB |
| 14 |
const UNIX_HASH_THRESHOLD = 104857600; |
| 15 |
|
| 16 |
// 100KB |
| 17 |
const MAX_CHUNK_SIZE = 102400; |
| 18 |
|
| 19 |
public function execute(array $params = array(), MWP_Worker_Request $request) |
| 20 |
{ |
| 21 |
$hashComputer = new MWP_IncrementalBackup_HashComputer(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Each file is structured like: |
| 25 |
* [ |
| 26 |
* "relativePath" => file path relative to ABSPATH, |
| 27 |
* "pathEncoded" => is path url encoded?, |
| 28 |
* "size" => file size sent for reference, |
| 29 |
* "offset" => number of bytes to offset hash start (integer, optional, default 0), |
| 30 |
* "limit" => number of bytes to hash (integer, optional, default 0), |
| 31 |
* "forcePartialHashing" => partially hashes file instead of md5_file always (boolean, optional, default false), |
| 32 |
* ] |
| 33 |
*/ |
| 34 |
$files = $params['files']; |
| 35 |
$result = array(); |
| 36 |
|
| 37 |
// Allow overriding max chunk byte size per request for doing partial hashes |
| 38 |
$chunkByteSize = isset($params['maxChunkByteSize']) ? $params['maxChunkByteSize'] : self::MAX_CHUNK_SIZE; |
| 39 |
$hashComputer->setMaxChunkByteSize($chunkByteSize); |
| 40 |
|
| 41 |
$unixHashThreshold = isset($params['unixMd5Threshold']) ? $params['unixMd5Threshold'] : self::UNIX_HASH_THRESHOLD; |
| 42 |
|
| 43 |
foreach ($files as $file) { |
| 44 |
$relativePath = $file['path']; |
| 45 |
$size = $file['size']; |
| 46 |
$offset = isset($file['offset']) ? $file['offset'] : 0; |
| 47 |
$limit = isset($file['limit']) ? $file['limit'] : 0; |
| 48 |
$forcePartial = isset($file['forcePartialHashing']) ? $file['forcePartialHashing'] : false; |
| 49 |
$decodedRelativePath = $file['pathEncoded'] ? $this->pathDecode($relativePath) : $relativePath; |
| 50 |
$realPath = $this->getRealPath($decodedRelativePath); |
| 51 |
|
| 52 |
// Run a unix command to generate md5 hash if file size exceeds threshold |
| 53 |
// Ignore partial requests for big files because of speed problems |
| 54 |
if ($size > $unixHashThreshold && $offset === 0 && $limit === 0) { |
| 55 |
$hash = $hashComputer->computeUnixMd5Sum($realPath); |
| 56 |
|
| 57 |
if ($hash !== null) { |
| 58 |
$result[] = array( |
| 59 |
'path' => $relativePath, |
| 60 |
'hash' => $hash, |
| 61 |
); |
| 62 |
|
| 63 |
continue; |
| 64 |
} |
| 65 |
// In case of a failed hashing fall back bellow to compute md5 hash from PHP |
| 66 |
} |
| 67 |
|
| 68 |
$hash = $hashComputer->computeMd5Hash($realPath, $offset, $limit, $forcePartial); |
| 69 |
$result[] = array( |
| 70 |
'path' => $relativePath, |
| 71 |
'hash' => $hash, |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
return $this->createResult(array( |
| 76 |
'files' => $result, |
| 77 |
)); |
| 78 |
} |
| 79 |
} |
| 80 |
|