TYPO3 CMS  TYPO3_7-6
Download.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
24 class Download implements \SplObserver
25 {
29  protected $filePointer = false;
30 
34  protected $targetFilePath = '';
35 
39  protected $targetDirectory = '';
40 
44  protected $targetFilename = '';
45 
53  public function __construct($directory, $filename = '')
54  {
55  $this->setDirectory($directory);
56  $this->setFilename($filename);
57  }
58 
68  public function update(\SplSubject $request)
69  {
70  $event = $request->getLastEvent();
71  switch ($event['name']) {
72  case 'receivedHeaders':
73  if ($this->targetFilename === '') {
74  $this->determineFilename($request, $event['data']);
75  }
76  $this->openFile();
77  break;
78  case 'receivedBodyPart':
79  case 'receivedEncodedBodyPart':
80  fwrite($this->filePointer, $event['data']);
81  break;
82  case 'receivedBody':
83  $this->closeFile();
84  break;
85  default:
86  // Do nothing
87  }
88  }
89 
97  public function setDirectory($directory)
98  {
99  if (!is_dir($directory)) {
100  throw new \InvalidArgumentException($directory . ' is not a directory', 1312223779);
101  }
102  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isAllowedAbsPath($directory)) {
103  throw new \InvalidArgumentException($directory . ' is not within the PATH_site' . ' OR within the lockRootPath', 1328734617);
104  }
105  $this->targetDirectory = ($directory = rtrim($directory, DIRECTORY_SEPARATOR));
106  }
107 
116  public function setFilename($filename = '')
117  {
118  $this->targetFilename = basename($filename);
119  }
120 
129  protected function determineFilename(\HTTP_Request2 $request, \HTTP_Request2_Response $response)
130  {
131  $matches = [];
132  $disposition = $response->getHeader('content-disposition');
133  if ($disposition !== null && 0 === strpos($disposition, 'attachment') && 1 === preg_match('/filename="([^"]+)"/', $disposition, $matches)) {
134  $filename = basename($matches[1]);
135  } else {
136  $filename = basename($request->getUrl()->getPath());
137  }
138  $this->setFilename($filename);
139  }
140 
151  protected function openFile()
152  {
153  if ($this->targetFilename === '') {
154  throw new \UnexpectedValueException('The file name must not be empty', 1321113658);
155  }
156  $this->targetFilePath = $this->targetDirectory . DIRECTORY_SEPARATOR . $this->targetFilename;
157  $this->filePointer = @fopen($this->targetFilePath, 'wb');
158  if ($this->filePointer === false) {
159  throw new \TYPO3\CMS\Core\Exception('Cannot open target file ' . $this->targetFilePath, 1320833203);
160  }
161  }
162 
168  protected function closeFile()
169  {
170  fclose($this->filePointer);
171  $this->filePointer = false;
173  }
174 }
__construct($directory, $filename='')
Definition: Download.php:53
static fixPermissions($path, $recursive=false)
update(\SplSubject $request)
Definition: Download.php:68
determineFilename(\HTTP_Request2 $request, \HTTP_Request2_Response $response)
Definition: Download.php:129