ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjFileDAV.php
Go to the documentation of this file.
1<?php
2use Sabre\DAV\Exception;
3use Sabre\DAV\Exception\Forbidden;
7
20{
23 protected $request;
24
31
43 {
44 global $DIC;
45 $settings = new ilSetting('webdav');
46 $this->resource_manager = $DIC->resourceStorage()->manage();
47 $this->resource_consumer = $DIC->resourceStorage()->consume();
48 $this->versioning_enabled = (bool) $settings->get('webdav_versioning_enabled', true);
50 }
51
73 public function put($data)
74 {
75 if ($this->repo_helper->checkAccess('write', $this->getRefId())) {
76 if ($this->versioning_enabled === true ||
77 $this->obj->getVersion() === '1' && $this->getSize() === 0) {
78 // Stolen from ilObjFile->addFileVersion
79 return $this->handleFileUpload($data, 'new_version');
80 } else {
81 return $this->handleFileUpload($data, 'replace');
82 }
83 }
84 throw new Exception\Forbidden("Permission denied. No write access for this file");
85 }
86
95 public function get()
96 {
97 if ($this->repo_helper->checkAccess("read", $this->obj->getRefId())) {
102 if ($this->getSize() === 0) {
103 return "";
104 }
105
106 if (($r_id = $this->obj->getResourceId()) &&
107 ($identification = $this->resource_manager->find($r_id))) {
108 return $this->resource_consumer->stream($identification)->getStream()->getContents();
109 }
110
111 /*
112 * @todo: This is legacy and should be removed with ILIAS 8
113 */
114 if (file_exists($file = $this->getPathToFile())) {
115 return fopen($file, 'r');
116 }
117
118 throw new Exception\NotFound("File not found");
119 }
120
121 throw new Exception\Forbidden("Permission denied. No read access for this file");
122 }
123
129 public function getName()
130 {
131 return ilFileUtils::getValidFilename($this->obj->getTitle());
132 }
133
141 public function getContentType()
142 {
143 return $this->obj->getFileType();
144 }
145
160 public function getETag()
161 {
162 if (file_exists($path = $this->getPathToFile())) {
163 return '"' . sha1(
164 fileinode($path) .
165 filesize($path) .
166 filemtime($path)
167 ) . '"';
168 }
169
170 if ($this->getSize() > 0) {
171 return '"' . sha1(
172 $this->getSize() .
173 $this->getName() .
174 $this->obj->getCreateDate()
175 ) . '"';
176 }
177
178 return null;
179 }
180
186 public function getSize() : int
187 {
188 return $this->obj->getFileSize();
189 }
190
195 public function setName($a_name)
196 {
197 if ($this->dav_helper->isValidFileNameWithValidFileExtension($a_name)) {
198 parent::setName($a_name);
199 } else {
200 throw new Exception\Forbidden("Invalid file extension");
201 }
202 }
203
213 public function handleFileUpload($a_data, $a_file_action)
214 {
221 if ($a_file_action != 'create' && file_exists($this->getPathToFile())) {
222 global $DIC;
224 $DIC->fileSystem()->storage(),
225 $DIC->database(),
226 rtrim(CLIENT_DATA_DIR, "/") . '/ilFile/migration_log.csv'
227 );
228 $migration->migrate(new ilFileObjectToStorageDirectory($this->obj->getId(), $this->obj->getDirectory()));
229 }
230
231 $size = (int) $this->request->getHeader('Content-Length')[0];
232
233 if ($size === 0 && $this->request->hasHeader('X-Expected-Entity-Length')) {
234 $size = $this->request->getHeader('X-Expected-Entity-Length')[0];
235 }
236
238 throw new Exception\Forbidden('File is too big');
239 }
240
241 $stream = Streams::ofResource($a_data);
242
243 if ($a_file_action === 'replace') {
244 $this->obj->replaceWithStream($stream, $this->obj->getTitle());
245 } else {
246 $this->obj->appendStream($stream, $this->obj->getTitle());
247 }
248
249 $stream->close();
250
251 // TODO filename is "input" and metadata etc.
252
253 if ($this->obj->update()) {
254 $this->createHistoryAndNotificationForObjUpdate($a_file_action);
255 ilPreview::createPreview($this->obj, true);
256 }
257
258 return $this->getETag();
259 }
260
261
262 protected function getPathToDirectory()
263 {
264 return $this->obj->getDirectory($this->obj->getVersion());
265 }
266
275 protected function getPathToFile()
276 {
277 // ilObjFile delivers the filename like it was on the upload. But if the file-extension is forbidden, the file
278 // will be safed as .sec-file. In this case ->getFileName returns the wrong file name
279 $path = $this->getPathToDirectory() . "/" . $this->obj->getFileName();
280
281 // For the case of forbidden file-extensions, ::getValidFilename($path) returns the path with the .sec extension
282 return ilFileUtils::getValidFilename($path);
283 }
284
285 protected function checkForVirus(string $file_dest_path)
286 {
287 $vrs = ilUtil::virusHandling($file_dest_path, '', true);
288 // If vrs[0] == false -> virus found
289 if ($vrs[0] == false) {
290 ilLoggerFactory::getLogger('WebDAV')->error(get_class($this) . ' ' . $this->obj->getTitle() . " -> virus found on '$file_dest_path'!");
291 throw new Exception\Forbidden('Virus found!');
292 }
293 }
294
300 protected function createHistoryAndNotificationForObjUpdate($a_action)
301 {
302 // Add history entry and notification for new file version (stolen from ilObjFile->addFileVersion)
303 switch ($a_action) {
304 case "new_version":
305 case "replace":
306 ilHistory::_createEntry($this->obj->getId(), $a_action, $this->obj->getTitle() . "," . $this->obj->getVersion() . "," . $this->obj->getMaxVersion());
307 break;
308 }
309
310 $this->obj->notifyUpdate($this->obj->getId());
311 }
312
316 protected function deleteObjOrVersion()
317 {
318 if ($this->obj->getVersion() > 1) {
319 $version_dir = $this->obj->getDirectory($this->obj->getVersion());
320 ilUtil::delDir($version_dir);
321 } else {
322 $this->obj->deleteVersions();
323 $this->obj->delete();
324 }
325 }
326}
$size
Definition: RandomTest.php:84
An exception for terminatinating execution or to throw for unit testing.
Class Streams Stream factory which enables the user to create streams without the knowledge of the co...
Definition: Streams.php:17
Class ilFileObjectToStorageDirectory.
static getValidFilename($a_filename)
Get valid filename.
static _createEntry( $a_obj_id, $a_action, $a_info_params="", $a_obj_type="", $a_user_comment="", $a_update_last=false)
Creates a new history entry for an object.
static getLogger($a_component_id)
Get component logger.
Class ilObjFileDAV.
getSize()
Returns the size of the node, in bytes.
getName()
Returns title of file object.
deleteObjOrVersion()
Delete an object if there is no other version in it otherwise delete version.
put($data)
Replaces the contents of the file.
__construct(ilObjFile $a_obj, ilWebDAVRepositoryHelper $repo_helper, ilWebDAVObjDAVHelper $dav_helper)
ilObjFileDAV represents the WebDAV-Interface to an ILIAS-Object
getContentType()
Returns the mime-type for a file.
handleFileUpload($a_data, $a_file_action)
Handle uploaded file.
checkForVirus(string $file_dest_path)
createHistoryAndNotificationForObjUpdate($a_action)
Create history entry and a news notification for file object update.
getETag()
Returns the ETag for a file.
getPathToFile()
This method only exists for legacy reasons.
Class ilObjFile.
Class ilObjectDAV.
static createPreview($a_obj, $a_force=false)
Creates the preview for the object with the specified id.
ILIAS Setting Class.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getUploadSizeLimitBytes()
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
Class ilWebDAVObjDAVHelper.
Class ilWebDAVRepositoryHelper.
const CLIENT_DATA_DIR
Definition: constants.php:44
global $DIC
Definition: goto.php:24
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$data
Definition: storeScorm.php:23