Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/MWP/Action
File: DownloadFile.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_Action_DownloadFile extends MWP_Action_Abstract
[10] Fix | Delete
{
[11] Fix | Delete
const DOWNLOAD_FAILED = 12;
[12] Fix | Delete
[13] Fix | Delete
public function execute(array $params)
[14] Fix | Delete
{
[15] Fix | Delete
$requestedFiles = $params['files'];
[16] Fix | Delete
[17] Fix | Delete
if (count($params['files']) > 1 || is_dir($requestedFiles[0])) {
[18] Fix | Delete
$requestedFile = $this->archiveFiles($params['files']);
[19] Fix | Delete
} else {
[20] Fix | Delete
$requestedFile = $requestedFiles[0];
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
$fp = fopen($requestedFile, "r");
[24] Fix | Delete
if (!$fp) {
[25] Fix | Delete
return array('message' => self::DOWNLOAD_FAILED);
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
$result = new MWP_FileManager_Model_DownloadFilesResult();
[29] Fix | Delete
$file = new MWP_FileManager_Model_Files();
[30] Fix | Delete
$file->setPathname($requestedFile);
[31] Fix | Delete
$file->setStream(MWP_Stream_Stream::factory($fp));
[32] Fix | Delete
$result->addFile($file);
[33] Fix | Delete
[34] Fix | Delete
return $result;
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
private function archiveFiles($files)
[38] Fix | Delete
{
[39] Fix | Delete
$filePath = WP_CONTENT_DIR."/mwp-download/";
[40] Fix | Delete
if (!file($filePath)) {
[41] Fix | Delete
mkdir($filePath);
[42] Fix | Delete
$indexPHP = fopen($filePath."index.php", 'w+');
[43] Fix | Delete
fwrite($indexPHP, "<?php \n\n // Silence is golden. \n");
[44] Fix | Delete
fclose($indexPHP);
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
$randomString = mwp_generate_uuid4();
[48] Fix | Delete
[49] Fix | Delete
$zipName = $filePath.$randomString.".zip";
[50] Fix | Delete
if (!class_exists('ZipArchive')) {
[51] Fix | Delete
$escapedFiles = array();
[52] Fix | Delete
foreach ($files as $file) {
[53] Fix | Delete
$escapedFiles[] = escapeshellarg($file);
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
exec('zip -r ' . $zipName . ' ' . join(' ', $escapedFiles), $output, $exitCode);
[57] Fix | Delete
return $zipName;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/** @handled class */
[61] Fix | Delete
$zip = new ZipArchive();
[62] Fix | Delete
[63] Fix | Delete
/** @handled static */
[64] Fix | Delete
$zip->open($zipName, ZipArchive::CREATE);
[65] Fix | Delete
[66] Fix | Delete
foreach ($files as $filePath) {
[67] Fix | Delete
if (!is_dir($filePath)) {
[68] Fix | Delete
$zip->addFile($filePath);
[69] Fix | Delete
continue;
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
$filesFromDir = $this->getFilesRecursive($filePath);
[73] Fix | Delete
foreach ($filesFromDir as $file) {
[74] Fix | Delete
if (is_dir($file)) {
[75] Fix | Delete
continue;
[76] Fix | Delete
}
[77] Fix | Delete
$zip->addFile($file->getRealPath(), $file->getPath()."/".$file->getFilename());
[78] Fix | Delete
}
[79] Fix | Delete
}
[80] Fix | Delete
$zip->close();
[81] Fix | Delete
return $zipName;
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
private function getFilesRecursive($path)
[85] Fix | Delete
{
[86] Fix | Delete
return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);
[87] Fix | Delete
}
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function