PluginProbe
ManageWP Worker / 4.9.29
ManageWP Worker v4.9.29
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / Action / DownloadFile.php

DownloadFile.php in ManageWP Worker 4.9.29, at src/MWP/Action/DownloadFile.php

91 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * This file is part of the ManageWP Worker plugin.
4 *
5 * (c) ManageWP LLC <contact@managewp.com>
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_DownloadFile extends MWP_Action_Abstract
12 {
13 const DOWNLOAD_FAILED = 12;
14
15 public function execute(array $params)
16 {
17 $requestedFiles = $params['files'];
18
19 if (count($params['files']) > 1 || is_dir($requestedFiles[0])) {
20 $requestedFile = $this->archiveFiles($params['files']);
21 } else {
22 $requestedFile = $requestedFiles[0];
23 }
24
25 $fp = fopen($requestedFile, "r");
26 if (!$fp) {
27 return array('message' => self::DOWNLOAD_FAILED);
28 }
29
30 $result = new MWP_FileManager_Model_DownloadFilesResult();
31 $file = new MWP_FileManager_Model_Files();
32 $file->setPathname($requestedFile);
33 $file->setStream(MWP_Stream_Stream::factory($fp));
34 $result->addFile($file);
35
36 return $result;
37 }
38
39 private function archiveFiles($files)
40 {
41 $filePath = WP_CONTENT_DIR."/mwp-download/";
42 if (!file($filePath)) {
43 mkdir($filePath);
44 $indexPHP = fopen($filePath."index.php", 'w+');
45 fwrite($indexPHP, "<?php \n\n // Silence is golden. \n");
46 fclose($indexPHP);
47 }
48
49 $randomString = mwp_generate_uuid4();
50
51 $zipName = $filePath.$randomString.".zip";
52 if (!class_exists('ZipArchive')) {
53 $escapedFiles = array();
54 foreach ($files as $file) {
55 $escapedFiles[] = escapeshellarg($file);
56 }
57
58 exec('zip -r ' . $zipName . ' ' . join(' ', $escapedFiles), $output, $exitCode);
59 return $zipName;
60 }
61
62 /** @handled class */
63 $zip = new ZipArchive();
64
65 /** @handled static */
66 $zip->open($zipName, ZipArchive::CREATE);
67
68 foreach ($files as $filePath) {
69 if (!is_dir($filePath)) {
70 $zip->addFile($filePath);
71 continue;
72 }
73
74 $filesFromDir = $this->getFilesRecursive($filePath);
75 foreach ($filesFromDir as $file) {
76 if (is_dir($file)) {
77 continue;
78 }
79 $zip->addFile($file->getRealPath(), $file->getPath()."/".$file->getFilename());
80 }
81 }
82 $zip->close();
83 return $zipName;
84 }
85
86 private function getFilesRecursive($path)
87 {
88 return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);
89 }
90 }
91