ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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, $a_file_action)
 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 . More...
 
 getObject ()
 Returns ILIAS Object. More...
 

Protected Member Functions

 uploadFile ($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 ($a_action)
 Create history entry and a news notification for file object update. More...
 
 deleteObjOrVersion ()
 Delete an object if there is no other version in it otherwise delete version. More...
 

Protected Attributes

 $obj
 
 $versioning_enabled
 
- 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 18 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 44 of file class.ilObjFileDAV.php.

45 {
46 $settings = new ilSetting('file_access');
47 $this->versioning_enabled = (bool) $settings->get('webdav_versioning_enabled', true);
49 }
ILIAS Setting Class.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc

References ilObjectDAV\$dav_helper, ilObjectDAV\$repo_helper, and ILIAS\GlobalScreen\Provider\__construct().

+ Here is the call graph for this function:

Member Function Documentation

◆ checkForVirus()

ilObjFileDAV::checkForVirus ( string  $file_dest_path)
protected

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

274 {
275 $vrs = ilUtil::virusHandling($file_dest_path, '', true);
276 // If vrs[0] == false -> virus found
277 if ($vrs[0] == false) {
278 ilLoggerFactory::getLogger('WebDAV')->error(get_class($this) . ' ' . $this->obj->getTitle() . " -> virus found on '$file_dest_path'!");
279 $this->deleteObjOrVersion();
280 throw new Exception\Forbidden('Virus found!');
281 }
282 }
static getLogger($a_component_id)
Get component logger.
deleteObjOrVersion()
Delete an object if there is no other version in it otherwise delete version.
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible

References deleteObjOrVersion(), 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 (   $a_action)
protected

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

Parameters
$a_action

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

304 {
305 // Add history entry and notification for new file version (stolen from ilObjFile->addFileVersion)
306 switch ($a_action) {
307 case "new_version":
308 case "replace":
309 ilHistory::_createEntry($this->obj->getId(), $a_action, $this->obj->getTitle() . "," . $this->obj->getVersion() . "," . $this->obj->getMaxVersion());
310 break;
311 }
312
313 $this->obj->addNewsNotification("file_updated");
314 }
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:

◆ deleteObjOrVersion()

ilObjFileDAV::deleteObjOrVersion ( )
protected

Delete an object if there is no other version in it otherwise delete version.

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

320 {
321 if ($this->obj->getVersion() > 1) {
322 $version_dir = $this->obj->getDirectory($this->obj->getVersion());
323 ilUtil::delDir($version_dir);
324 } else {
325 $this->obj->deleteVersions();
326 $this->obj->delete();
327 }
328 }
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively

References ilUtil\delDir().

Referenced by checkForVirus(), and handleFileUpload().

+ Here is the call graph for this function:
+ 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

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

98 {
99 if ($this->repo_helper->checkAccess("read", $this->obj->getRefId())) {
100 $file = $this->getPathToFile();
101
102 if (file_exists($file)) {
103 return fopen($file, 'r');
104 } else {
105 throw new Exception\NotFound("File not found");
106 }
107 }
108
109 throw new Exception\Forbidden("Permission denied. No read access for this file");
110 }
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

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

130 {
131 return $this->obj->guessFileType();
132 }

◆ 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

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

149 {
150 if (file_exists($path = $this->getPathToFile())) {
151 return '"' . sha1(
152 fileinode($path) .
153 filesize($path) .
154 filemtime($path)
155 ) . '"';
156 }
157
158 return null;
159 }

References 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 117 of file class.ilObjFileDAV.php.

118 {
119 return ilFileUtils::getValidFilename($this->obj->getTitle());
120 }
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 249 of file class.ilObjFileDAV.php.

250 {
251 return $this->obj->getDirectory($this->obj->getVersion());
252 }

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 263 of file class.ilObjFileDAV.php.

264 {
265 // ilObjFile delivers the filename like it was on the upload. But if the file-extension is forbidden, the file
266 // will be safed as .sec-file. In this case ->getFileName returns the wrong file name
267 $path = $this->getPathToDirectory() . "/" . $this->obj->getFileName();
268
269 // For the case of forbidden file-extensions, ::getValidFilename($path) returns the path with the .sec extension
270 return ilFileUtils::getValidFilename($path);
271 }

References 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

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

167 {
168 if (file_exists($this->getPathToFile())) {
169 return $this->obj->getFileSize();
170 }
171 return 0;
172 }

References getPathToFile().

+ Here is the call graph for this function:

◆ handleFileUpload()

ilObjFileDAV::handleFileUpload (   $a_data,
  $a_file_action 
)

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
Forbidden

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

197 {
198 // Set name for uploaded file because due to the versioning, the title can change for different versions. This setter-call here
199 // ensures that the uploaded file is saved with the title of the object. This obj->setFileName() has to be called
200 // before $this->getPathToFile(). Otherwise, the file could be saved under the wrong filename.
201 if ($a_file_action === 'replace') {
202 $this->obj->deleteVersions();
203 $this->obj->clearDataDirectory();
204 }
205 $this->obj->setFileName($this->getName());
206 $file_dest_path = $this->getPathToFile();
207
208 // If dir does not exist yet -> create it
209 if (!file_exists($file_dest_path)) {
211 }
212
213 $written_length = $this->uploadFile($a_data, $file_dest_path);
214
215 if ($written_length > ilUtil::getUploadSizeLimitBytes()) {
216 $this->deleteObjOrVersion();
217 throw new Exception\Forbidden('File is too big');
218 }
219
220 // Security checks
221 $this->checkForVirus($file_dest_path);
222
223 // Set last meta data
224 $this->obj->setFileType(ilMimeTypeUtil::lookupMimeType($file_dest_path));
225 $this->obj->setFileSize($written_length);
226 if ($this->obj->update()) {
227 $this->createHistoryAndNotificationForObjUpdate($a_file_action);
228 ilPreview::createPreview($this->obj, true);
229 }
230 }
static lookupMimeType($path_to_file, $fallback=self::APPLICATION__OCTET_STREAM, $a_external=null)
getName()
Returns title of file object.
uploadFile($a_data, string $file_dest_path)
Write given data (as string) to the given file.
checkForVirus(string $file_dest_path)
createHistoryAndNotificationForObjUpdate($a_action)
Create history entry and a news notification for file object update.
static createPreview($a_obj, $a_force=false)
Creates the preview for the object with the specified id.
static getUploadSizeLimitBytes()
static makeDirParents($a_dir)
Create a new directory and all parent directories.

References checkForVirus(), createHistoryAndNotificationForObjUpdate(), ilPreview\createPreview(), deleteObjOrVersion(), getName(), getPathToDirectory(), getPathToFile(), ilUtil\getUploadSizeLimitBytes(), ilMimeTypeUtil\lookupMimeType(), ilUtil\makeDirParents(), and uploadFile().

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
Forbidden

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

73 {
74 if ($this->repo_helper->checkAccess('write', $this->getRefId())) {
76 if ($this->versioning_enabled === true) {
77 // Stolen from ilObjFile->addFileVersion
78 $this->handleFileUpload($data, 'new_version');
79 } else {
80 $this->handleFileUpload($data, 'replace');
81 }
82
83
84 return $this->getETag();
85 }
86 throw new Exception\Forbidden("Permission denied. No write access for this file");
87 }
setObjValuesForNewFileVersion()
Set object values for a new file version.
handleFileUpload($a_data, $a_file_action)
Handle uploaded file.
getETag()
Returns the ETag for a file.
$data
Definition: storeScorm.php:23

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 178 of file class.ilObjFileDAV.php.

179 {
180 if ($this->dav_helper->isValidFileNameWithValidFileExtension($a_name)) {
181 parent::setName($a_name);
182 } else {
183 throw new Exception\Forbidden("Invalid file extension");
184 }
185 }

◆ setObjValuesForNewFileVersion()

ilObjFileDAV::setObjValuesForNewFileVersion ( )
protected

Set object values for a new file version.

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

288 {
289 // This is necessary for windows explorer. Because windows explorer makes always 2 PUT requests. One with a 0 Byte
290 // file to test, if the user has write permissions and the second one to upload the original file.
291 if ($this->obj->getFileSize() > 0) {
292 // Stolen from ilObjFile->addFileVersion
293 $this->obj->setVersion($this->obj->getMaxVersion() + 1);
294 $this->obj->setMaxVersion($this->obj->getMaxVersion() + 1);
295 }
296 }

Referenced by put().

+ Here is the caller graph for this function:

◆ uploadFile()

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

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

Parameters
string | resource$a_data
string$file_dest_path
Exceptions
Exception

Forbidden

Returns
number $written_length

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

241 {
242 $written_length = file_put_contents($file_dest_path, $a_data);
243 if ($written_length === false && strlen($a_data) > 0) {
244 throw new Exception\Forbidden('Forbidden to write file');
245 }
246 return $written_length;
247 }

Referenced by handleFileUpload().

+ Here is the caller graph for this function:

Field Documentation

◆ $obj

ilObjFileDAV::$obj
protected

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

◆ $versioning_enabled

ilObjFileDAV::$versioning_enabled
protected

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


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