| @@ -2,15 +2,13 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace WPIDE\App\Controllers\FileManager; |
| 4 | 4 | |
| 5 | 5 | use WPIDE\App\Config\Config; |
| 6 | -use WPIDE\App\Helpers\ImageStateData; | |
| 7 | 6 | use WPIDE\App\Kernel\Request; |
| 8 | 7 | use WPIDE\App\Kernel\Response; |
| 9 | 8 | use WPIDE\App\Kernel\StreamedResponse; |
| 10 | 9 | use WPIDE\App\Services\Archiver\ArchiverInterface; |
| 11 | 10 | use WPIDE\App\Services\Auth\AuthInterface; |
| 12 | -use WPIDE\App\Services\Session\SessionStorageInterface as Session; | |
| 13 | 11 | use WPIDE\App\Services\Storage\Filesystem; |
| 14 | 12 | use WPIDE\App\Services\Tmpfs\TmpfsInterface; |
| 15 | 13 | use Symfony\Component\HttpFoundation\HeaderUtils; |
| 16 | 14 | use Symfony\Component\Mime\MimeTypes; |
| @@ -30,13 +28,8 @@ | ||
| 30 | 28 | */ |
| 31 | 29 | protected $user; |
| 32 | 30 | |
| 33 | 31 | /** |
| 34 | - * @var Session | |
| 35 | - */ | |
| 36 | - protected $session; | |
| 37 | - | |
| 38 | - /** | |
| 39 | 32 | * @var Config |
| 40 | 33 | */ |
| 41 | 34 | protected $config; |
| 42 | 35 | |
| @@ -45,16 +38,19 @@ | ||
| 45 | 38 | */ |
| 46 | 39 | protected $storage; |
| 47 | 40 | |
| 48 | 41 | /** |
| 42 | + * @var | |
| 43 | + */ | |
| 44 | + protected $separator; | |
| 45 | + | |
| 46 | + /** | |
| 49 | 47 | * @param Config $config |
| 50 | - * @param Session $session | |
| 51 | 48 | * @param AuthInterface $auth |
| 52 | 49 | * @param Filesystem $storage |
| 53 | 50 | */ |
| 54 | - public function __construct(Config $config, Session $session, AuthInterface $auth, Filesystem $storage) | |
| 51 | + public function __construct(Config $config, AuthInterface $auth, Filesystem $storage) | |
| 55 | 52 | { |
| 56 | - $this->session = $session; | |
| 57 | 53 | $this->config = $config; |
| 58 | 54 | $this->auth = $auth; |
| 59 | 55 | |
| 60 | 56 | $this->user = $this->auth->user() ?: $this->auth->getGuest(); |
| @@ -60,8 +56,10 @@ | ||
| 60 | 56 | $this->user = $this->auth->user() ?: $this->auth->getGuest(); |
| 61 | 57 | |
| 62 | 58 | $this->storage = $storage; |
| 63 | 59 | $this->storage->setPathPrefix($this->user->getHomeDir()); |
| 60 | + | |
| 61 | + $this->separator = $this->storage->getSeparator(); | |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | 64 | /** |
| 67 | 65 | * @param Request $request |
| @@ -83,11 +81,34 @@ | ||
| 83 | 81 | } catch (\Exception $e) { |
| 84 | 82 | return $response->redirect('/'); |
| 85 | 83 | } |
| 86 | 84 | |
| 87 | - $this->downloadFile($file, $request, $streamedResponse); | |
| 85 | + return $this->downloadFile($file, $request, $response, $streamedResponse); | |
| 88 | 86 | } |
| 89 | 87 | |
| 88 | + public function readContent(Request $request, Response $response) | |
| 89 | + { | |
| 90 | + $encodedPath = $request->input('path'); | |
| 91 | + $path = is_string($encodedPath) ? base64_decode($encodedPath, true) : false; | |
| 92 | + | |
| 93 | + if ($path === false || !$this->storage->fileExists($path)) { | |
| 94 | + return $response->json(__('Requested file does not exist', 'wpide'), 404); | |
| 95 | + } | |
| 96 | + | |
| 97 | + try { | |
| 98 | + $file = $this->storage->read($path); | |
| 99 | + } catch (\Exception $error) { | |
| 100 | + return $response->json(__('Cannot read file, please check file permissions!', 'wpide'), 422); | |
| 101 | + } | |
| 102 | + | |
| 103 | + $content = $file['contents']; | |
| 104 | + if (!is_string($content) || strlen($content) !== (int) $file['filesize'] || preg_match('//u', $content) !== 1) { | |
| 105 | + return $response->json(__('Cannot read file, please check file permissions!', 'wpide'), 422); | |
| 106 | + } | |
| 107 | + | |
| 108 | + return $response->json($content); | |
| 109 | + } | |
| 110 | + | |
| 90 | 111 | /** |
| 91 | 112 | * @param $file array |
| 92 | 113 | * @param Request $request |
| 93 | 114 | * @param StreamedResponse $streamedResponse |
| @@ -92,19 +113,39 @@ | ||
| 92 | 113 | * @param Request $request |
| 93 | 114 | * @param StreamedResponse $streamedResponse |
| 94 | 115 | * @return void |
| 95 | 116 | */ |
| 96 | - public function downloadFile(array $file, Request $request, StreamedResponse $streamedResponse) | |
| 117 | + public function downloadFile(array $file, Request $request, Response $response, StreamedResponse $streamedResponse) | |
| 97 | 118 | { |
| 98 | 119 | |
| 120 | + if($file['stream'] === false) { | |
| 121 | + return $response->json('Cannot read file, please check file permissions!', 422); | |
| 122 | + } | |
| 123 | + | |
| 124 | + // Output produced before this response would be prepended to the file. With | |
| 125 | + // Content-Length set below, clients can then lose the same number of bytes | |
| 126 | + // from the end of the file. | |
| 127 | + if (headers_sent()) { | |
| 128 | + fclose($file['stream']); | |
| 129 | + return $response->json('Cannot safely stream file!', 500); | |
| 130 | + } | |
| 131 | + | |
| 132 | + while (ob_get_level() > 0) { | |
| 133 | + if (!@ob_end_clean()) { | |
| 134 | + fclose($file['stream']); | |
| 135 | + return $response->json('Cannot safely stream file!', 500); | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 99 | 139 | $streamedResponse->setCallback(function () use ($file) { |
| 100 | 140 | // @codeCoverageIgnoreStart |
| 101 | - set_time_limit(0); | |
| 141 | + @set_time_limit(0); | |
| 142 | + | |
| 102 | 143 | if ($file['stream']) { |
| 103 | 144 | while (! feof($file['stream'])) { |
| 104 | 145 | echo fread($file['stream'], 1024 * 8); |
| 105 | - ob_flush(); | |
| 106 | - flush(); | |
| 146 | + @ob_flush(); | |
| 147 | + @flush(); | |
| 107 | 148 | } |
| 108 | 149 | fclose($file['stream']); |
| 109 | 150 | } |
| 110 | 151 | // @codeCoverageIgnoreEnd |
| @@ -152,11 +193,8 @@ | ||
| 152 | 193 | ); |
| 153 | 194 | } |
| 154 | 195 | // @codeCoverageIgnoreEnd |
| 155 | 196 | |
| 156 | - // close session, so we can continue streaming, note: dev is single-threaded | |
| 157 | - $this->session->save(); | |
| 158 | - | |
| 159 | 197 | $streamedResponse->send(); |
| 160 | 198 | } |
| 161 | 199 | |
| 162 | 200 | /** |
| @@ -180,11 +218,8 @@ | ||
| 180 | 218 | $uniqid = $request->input('uniqid'); |
| 181 | 219 | $archiver->setArchive($this->storage, $uniqid); |
| 182 | 220 | } |
| 183 | 221 | |
| 184 | - // close session | |
| 185 | - $this->session->save(); | |
| 186 | - | |
| 187 | 222 | foreach ($items as $item) { |
| 188 | 223 | if ($item->type == 'dir') { |
| 189 | 224 | if($recursive) { |
| 190 | 225 | $archiver->addDirectoryFromStorage($item->path, $destination); |
| @@ -219,18 +254,20 @@ | ||
| 219 | 254 | { |
| 220 | 255 | |
| 221 | 256 | |
| 222 | 257 | $uniqid = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('uniqid')); |
| 258 | + $name = $request->input('name', $this->config->get('file.default_archive_name')); | |
| 259 | + | |
| 223 | 260 | $file = $tmpfs->readStream($uniqid); |
| 224 | 261 | |
| 225 | 262 | $streamedResponse->setCallback(function () use ($file, $tmpfs, $uniqid) { |
| 226 | 263 | // @codeCoverageIgnoreStart |
| 227 | - set_time_limit(0); | |
| 264 | + @set_time_limit(0); | |
| 228 | 265 | if ($file['stream']) { |
| 229 | 266 | while (! feof($file['stream'])) { |
| 230 | 267 | echo fread($file['stream'], 1024 * 8); |
| 231 | - ob_flush(); | |
| 232 | - flush(); | |
| 268 | + @ob_flush(); | |
| 269 | + @flush(); | |
| 233 | 270 | } |
| 234 | 271 | fclose($file['stream']); |
| 235 | 272 | } |
| 236 | 273 | $tmpfs->remove($uniqid); |
| @@ -240,10 +277,10 @@ | ||
| 240 | 277 | $streamedResponse->headers->set( |
| 241 | 278 | 'Content-Disposition', |
| 242 | 279 | HeaderUtils::makeDisposition( |
| 243 | 280 | HeaderUtils::DISPOSITION_ATTACHMENT, |
| 244 | - $this->config->get('file.default_archive_name'), | |
| 245 | - 'archive.zip' | |
| 281 | + $name, | |
| 282 | + $this->config->get('file.default_archive_name') | |
| 246 | 283 | ) |
| 247 | 284 | ); |
| 248 | 285 | $streamedResponse->headers->set( |
| 249 | 286 | 'Content-Type', |
| @@ -258,10 +295,8 @@ | ||
| 258 | 295 | 'Content-Length', |
| 259 | 296 | $file['filesize'] |
| 260 | 297 | ); |
| 261 | 298 | } |
| 262 | - // close session, so we can continue streaming, note: dev is single-threaded | |
| 263 | - $this->session->save(); | |
| 264 | 299 | |
| 265 | 300 | $streamedResponse->send(); |
| 266 | 301 | } |
| 267 | 302 | |