ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilObjFileDAV Class Reference

Class ilObjFileDAV. More...

+ Inheritance diagram for ilObjFileDAV:
+ Collaboration diagram for ilObjFileDAV:

Public Member Functions

 __construct (ilObjFile $a_obj, ilWebDAVRepositoryHelper $repo_helper, ilWebDAVObjDAVHelper $dav_helper)
 ilObjFileDAV represents the WebDAV-Interface to an ILIAS-Object More...
 
 put ($data)
 Replaces the contents of the file. More...
 
 get ()
 Returns the data. More...
 
 getName ()
 Returns title of file object. More...
 
 getContentType ()
 Returns the mime-type for a file. More...
 
 getETag ()
 Returns the ETag for a file. More...
 
 getSize ()
 Returns the size of the node, in bytes. More...
 
 setName ($a_name)
 
 handleFileUpload ($a_data)
 Handle uploaded file. More...
 
- Public Member Functions inherited from ilObjectDAV
 __construct (ilObject $a_obj, ilWebDAVRepositoryHelper $repo_helper, ilWebDAVObjDAVHelper $dav_helper)
 Constructor for DAV Object. More...
 
 getRefId ()
 Returns the ref id of this object. More...
 
 getObjectId ()
 Returns the object id of this object. More...
 
 getLastModified ()
 Returns the last modification time as a unix timestamp. More...
 
 delete ()
 Deletes the current node. More...
 
 setName ($a_name)
 Renames the node. More...
 
 getName ()
 SabreDAV interface function Returns the name of the node.This is used to generate the url.
Returns
string
. More...
 
 getObject ()
 Returns ILIAS Object. More...
 
 getLastModified ()
 Returns the last modification time as a unix timestamp. More...
 
 delete ()
 Deletes the current node. More...
 
 setName ($name)
 Renames the node. More...
 
 delete ()
 Deleted the current node. More...
 
 getName ()
 Returns the name of the node. More...
 
 setName ($name)
 Renames the node. More...
 
 getLastModified ()
 Returns the last modification time, as a unix timestamp. More...
 
 put ($data)
 Replaces the contents of the file. More...
 
 get ()
 Returns the data. More...
 
 getContentType ()
 Returns the mime-type for a file. More...
 
 getETag ()
 Returns the ETag for a file. More...
 
 getSize ()
 Returns the size of the node, in bytes. More...
 

Protected Member Functions

 fileUploadWithStream ($a_data, string $file_dest_path)
 Write given data (as Resource) to the given file. More...
 
 fileUploadWithString (string $a_data, string $file_dest_path)
 Write given data (as string) to the given file. More...
 
 getPathToDirectory ()
 
 getPathToFile ()
 This method is called in 2 use cases: More...
 
 checkForVirus (string $file_dest_path)
 
 setObjValuesForNewFileVersion ()
 Set object values for a new file version. More...
 
 createHistoryAndNotificationForObjUpdate ()
 Create history entry and a news notification for file object update. More...
 

Protected Attributes

 $obj
 
- Protected Attributes inherited from ilObjectDAV
 $ref_id
 
 $obj
 
 $repo_helper
 
 $dav_helper
 

Detailed Description

Class ilObjFileDAV.

Implementation for ILIAS File Objects represented as WebDAV File Objects

Author
Raphael Heer rapha.nosp@m.el.h.nosp@m.eer@h.nosp@m.slu..nosp@m.ch $Id$

\DAV\IFile

Definition at line 19 of file class.ilObjFileDAV.php.

Constructor & Destructor Documentation

◆ __construct()

ilObjFileDAV::__construct ( ilObjFile  $a_obj,
ilWebDAVRepositoryHelper  $repo_helper,
ilWebDAVObjDAVHelper  $dav_helper 
)

ilObjFileDAV represents the WebDAV-Interface to an ILIAS-Object

So an ILIAS is needed in the constructor. Otherwise this object would be useless.

Parameters
ilObjFile$a_obj
ilWebDAVRepositoryHelper$repo_helper
ilWebDAVObjDAVHelper$dav_helper

Definition at line 38 of file class.ilObjFileDAV.php.

39 {
40 parent::__construct($a_obj, $repo_helper, $dav_helper);
41 }

References ilObjectDAV\$dav_helper, and ilObjectDAV\$repo_helper.

Member Function Documentation

◆ checkForVirus()

ilObjFileDAV::checkForVirus ( string  $file_dest_path)
protected

Definition at line 290 of file class.ilObjFileDAV.php.

291 {
292 $vrs = ilUtil::virusHandling($file_dest_path, '', true);
293 // If vrs[0] == false -> virus found
294 if ($vrs[0] == false) {
295 ilLoggerFactory::getLogger('WebDAV')->error(get_class($this) . ' ' . $this->obj->getTitle() . " -> virus found on '$file_dest_path'!");
296 unlink($file_dest_path);
297 $this->obj->delete();
298 throw new Exception\Forbidden('Virus found!');
299 }
300 }
static getLogger($a_component_id)
Get component logger.
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible

References ilLoggerFactory\getLogger(), and ilUtil\virusHandling().

Referenced by handleFileUpload().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createHistoryAndNotificationForObjUpdate()

ilObjFileDAV::createHistoryAndNotificationForObjUpdate ( )
protected

Create history entry and a news notification for file object update.

Definition at line 319 of file class.ilObjFileDAV.php.

320 {
321 // Add history entry and notification for new file version (stolen from ilObjFile->addFileVersion)
322 ilHistory::_createEntry($this->obj->getId(), "new_version", $this->obj->getTitle() . "," . $this->obj->getVersion() . "," . $this->obj->getMaxVersion());
323 $this->obj->addNewsNotification("file_updated");
324 }
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.

References ilHistory\_createEntry().

Referenced by handleFileUpload().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fileUploadWithStream()

ilObjFileDAV::fileUploadWithStream (   $a_data,
string  $file_dest_path 
)
protected

Write given data (as Resource) to the given file.

Parameters
Resource$a_data
string$file_dest_path
Exceptions
Exception
Returns
number

Definition at line 225 of file class.ilObjFileDAV.php.

226 {
227 try {
228 $written_length = 0;
229 $write_stream = fopen($file_dest_path, 'w');
230 while (!feof($a_data)) {
231 if (false === ($written = fwrite($write_stream, fread($a_data, 4096)))) {
232 fclose($write_stream);
233 throw new Exception\Forbidden('Forbidden to write file');
234 }
235 $written_length += $written;
236 }
237 } catch (Exception $e) {
238 ilLoggerFactory::getLogger('WebDAV')->error("Error on uploading {$this->obj->getTitle()} to path $file_dest_path with message: " . $e->getMessage());
239 throw new Exception();
240 } finally {
241 fclose($write_stream);
242 }
243
244 return $written_length;
245 }
Main Exception class.
Definition: Exception.php:18

References ilLoggerFactory\getLogger().

Referenced by handleFileUpload().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fileUploadWithString()

ilObjFileDAV::fileUploadWithString ( string  $a_data,
string  $file_dest_path 
)
protected

Write given data (as string) to the given file.

Parameters
string$a_data
string$file_dest_path
Exceptions
Forbidden
Returns
number $written_length

Definition at line 255 of file class.ilObjFileDAV.php.

256 {
257 $write_stream = fopen($file_dest_path, 'w');
258 $written_length = fwrite($write_stream, $a_data);
259 fclose($write_stream);
260 if ($written_length === false && strlen($a_data) > 0) {
261 throw new Exception\Forbidden('Forbidden to write file');
262 }
263 return $written_length;
264 }

Referenced by handleFileUpload().

+ Here is the caller graph for this function:

◆ get()

ilObjFileDAV::get ( )

Returns the data.

This method may either return a string or a readable stream resource

Returns
mixed
Exceptions
Forbidden

Implements Sabre\DAV\IFile.

Definition at line 83 of file class.ilObjFileDAV.php.

84 {
85 if ($this->repo_helper->checkAccess("read", $this->obj->getRefId())) {
86 $file = $this->getPathToFile();
87 if (file_exists($file)) {
88 return fopen($file, 'r');
89 } else {
90 throw new Exception\NotFound("File not found");
91 }
92 }
93
94 throw new Exception\Forbidden("Permission denied. No read access for this file");
95 }
getPathToFile()
This method is called in 2 use cases:

References getPathToFile().

+ Here is the call graph for this function:

◆ getContentType()

ilObjFileDAV::getContentType ( )

Returns the mime-type for a file.

If null is returned, we'll assume application/octet-stream

Returns
string|null

Implements Sabre\DAV\IFile.

Definition at line 114 of file class.ilObjFileDAV.php.

115 {
116 return $this->obj->guessFileType();
117 }

◆ getETag()

ilObjFileDAV::getETag ( )

Returns the ETag for a file.

An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.

Return null if the ETag can not effectively be determined.

The ETag must be surrounded by double-quotes, so something like this would make a valid ETag:

return '"someetag"';

Returns
string|null

Implements Sabre\DAV\IFile.

Definition at line 133 of file class.ilObjFileDAV.php.

134 {
135 if (file_exists($path = $this->getPathToFile())) {
136 return '"' . sha1(
137 fileinode($path) .
138 filesize($path) .
139 filemtime($path)
140 ) . '"';
141 }
142
143 return null;
144 }
$path
Definition: aliased.php:25

References $path, and getPathToFile().

Referenced by put().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getName()

ilObjFileDAV::getName ( )

Returns title of file object.

If it has a forbidden file extension -> ".sec" will be added

Returns
string

Reimplemented from ilObjectDAV.

Definition at line 102 of file class.ilObjFileDAV.php.

103 {
104 return ilFileUtils::getValidFilename($this->obj->getTitle());
105 }
static getValidFilename($a_filename)
Get valid filename.

References ilFileUtils\getValidFilename().

Referenced by handleFileUpload().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPathToDirectory()

ilObjFileDAV::getPathToDirectory ( )
protected

Definition at line 266 of file class.ilObjFileDAV.php.

267 {
268 return $this->obj->getDirectory($this->obj->getVersion());
269 }

Referenced by getPathToFile(), and handleFileUpload().

+ Here is the caller graph for this function:

◆ getPathToFile()

ilObjFileDAV::getPathToFile ( )
protected

This method is called in 2 use cases:

Use case 1: Get the path to an already existing file to download it -> read operation Use case 2: Get the path to save a new file into or overwrite an existing one -> write operation

Exceptions
ilFileUtilsException
Returns
string

Definition at line 280 of file class.ilObjFileDAV.php.

281 {
282 // ilObjFile delivers the filename like it was on the upload. But if the file-extension is forbidden, the file
283 // will be safed as .sec-file. In this case ->getFileName returns the wrong file name
284 $path = $this->getPathToDirectory() . "/" . $this->obj->getFileName();
285
286 // For the case of forbidden file-extensions, ::getValidFilename($path) returns the path with the .sec extension
288 }

References $path, getPathToDirectory(), and ilFileUtils\getValidFilename().

Referenced by get(), getETag(), getSize(), and handleFileUpload().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getSize()

ilObjFileDAV::getSize ( )

Returns the size of the node, in bytes.

Returns
int

Implements Sabre\DAV\IFile.

Definition at line 151 of file class.ilObjFileDAV.php.

152 {
153 if (file_exists($this->getPathToFile())) {
154 return $this->obj->getFileSize();
155 }
156 return 0;
157 }

References getPathToFile().

+ Here is the call graph for this function:

◆ handleFileUpload()

ilObjFileDAV::handleFileUpload (   $a_data)

Handle uploaded file.

Either it is a new file upload to a directory or it is an upload to replace an existing file.

Given data can be a resource or data (given from the sabreDAV library)

Parameters
string  |  resource$a_data
Exceptions
BadRequest
Forbidden

Definition at line 182 of file class.ilObjFileDAV.php.

183 {
184 // Set name for uploaded file because due to the versioning, the title can change for different versions. This setter-call here
185 // ensures that the uploaded file is saved with the title of the object. This obj->setFileName() has to be called
186 // before $this->getPathToFile(). Otherwise, the file could be saved under the wrong filename.
187 $this->obj->setFileName($this->getName());
188 $file_dest_path = $this->getPathToFile();
189
190 // If dir does not exist yet -> create it
191 if (!file_exists($file_dest_path)) {
193 }
194
195 // File upload
196 if (is_resource($a_data)) {
197 $written_length = $this->fileUploadWithStream($a_data, $file_dest_path);
198 } elseif (is_string($a_data)) {
199 $written_length = $this->fileUploadWithString($a_data, $file_dest_path);
200 } else {
201 ilLoggerFactory::getLogger('WebDAV')->warning(get_class($this) . ' ' . $this->obj->getTitle() . " -> invalid upload data sent");
202 throw new Exception\BadRequest('Invalid put data sent');
203 }
204
205 // Security checks
206 $this->checkForVirus($file_dest_path);
207
208 // Set last meta data
209 include_once("./Services/Utilities/classes/class.ilMimeTypeUtil.php");
210 $this->obj->setFileType(ilMimeTypeUtil::lookupMimeType($file_dest_path));
211 $this->obj->setFileSize($written_length);
212 if ($this->obj->update() && $this->obj->getMaxVersion() > 1) {
214 }
215 }
static lookupMimeType($path_to_file, $fallback=self::APPLICATION__OCTET_STREAM, $a_external=null)
fileUploadWithString(string $a_data, string $file_dest_path)
Write given data (as string) to the given file.
getName()
Returns title of file object.
createHistoryAndNotificationForObjUpdate()
Create history entry and a news notification for file object update.
checkForVirus(string $file_dest_path)
fileUploadWithStream($a_data, string $file_dest_path)
Write given data (as Resource) to the given file.
static makeDirParents($a_dir)
Create a new directory and all parent directories.

References checkForVirus(), createHistoryAndNotificationForObjUpdate(), fileUploadWithStream(), fileUploadWithString(), ilLoggerFactory\getLogger(), getName(), getPathToDirectory(), getPathToFile(), ilMimeTypeUtil\lookupMimeType(), and ilUtil\makeDirParents().

Referenced by put().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ put()

ilObjFileDAV::put (   $data)

Replaces the contents of the file.

The data argument is a readable stream resource.

After a successful put operation, you may choose to return an ETag. The etag must always be surrounded by double-quotes. These quotes must appear in the actual string you're returning.

Clients may use the ETag from a PUT request to later on make sure that when they update the file, the contents haven't changed in the mean time.

If you don't plan to store the file byte-by-byte, and you return a different object on a subsequent GET you are strongly recommended to not return an ETag, and just return null.

Parameters
resource | string$data
Returns
string|null
Exceptions
BadRequest
Forbidden

Implements Sabre\DAV\IFile.

Definition at line 65 of file class.ilObjFileDAV.php.

66 {
67 if ($this->repo_helper->checkAccess('write', $this->getRefId())) {
69 $this->handleFileUpload($data);
70 return $this->getETag();
71 }
72 throw new Exception\Forbidden("Permission denied. No write access for this file");
73 }
handleFileUpload($a_data)
Handle uploaded file.
setObjValuesForNewFileVersion()
Set object values for a new file version.
getETag()
Returns the ETag for a file.
$data
Definition: bench.php:6

References $data, getETag(), handleFileUpload(), and setObjValuesForNewFileVersion().

+ Here is the call graph for this function:

◆ setName()

ilObjFileDAV::setName (   $a_name)
Parameters
string$a_name
Exceptions
Forbidden

Reimplemented from ilObjectDAV.

Definition at line 163 of file class.ilObjFileDAV.php.

164 {
165 if ($this->dav_helper->isValidFileNameWithValidFileExtension($a_name)) {
166 parent::setName($a_name);
167 } else {
168 throw new Exception\Forbidden("Invalid file extension");
169 }
170 }

◆ setObjValuesForNewFileVersion()

ilObjFileDAV::setObjValuesForNewFileVersion ( )
protected

Set object values for a new file version.

Definition at line 305 of file class.ilObjFileDAV.php.

306 {
307 // This is necessary for windows explorer. Because windows explorer makes always 2 PUT requests. One with a 0 Byte
308 // file to test, if the user has write permissions and the second one to upload the original file.
309 if ($this->obj->getFileSize() > 0) {
310 // Stolen from ilObjFile->addFileVersion
311 $this->obj->setVersion($this->obj->getMaxVersion() + 1);
312 $this->obj->setMaxVersion($this->obj->getMaxVersion() + 1);
313 }
314 }

Referenced by put().

+ Here is the caller graph for this function:

Field Documentation

◆ $obj

ilObjFileDAV::$obj
protected

Definition at line 26 of file class.ilObjFileDAV.php.


The documentation for this class was generated from the following file: