ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjFileAccess.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4
6
7include_once("./Services/Object/classes/class.ilObjectAccess.php");
8require_once('./Services/WebAccessChecker/interfaces/interface.ilWACCheckingClass.php');
9
18{
19
24 protected function checkAccessToObjectId($obj_id)
25 {
26 global $DIC;
27 $ilAccess = $DIC['ilAccess'];
31 foreach (ilObject::_getAllReferences($obj_id) as $ref_id) {
32 if ($ilAccess->checkAccess('read', '', $ref_id)) {
33 return true;
34 }
35 }
36
37 return false;
38 }
39
44 public function canBeDelivered(ilWACPath $ilWACPath)
45 {
46 switch ($ilWACPath->getSecurePathId()) {
47 case 'previews':
48 $re = '/\/previews\/[\d\/]{0,}\/preview_([\d]{0,})\//uU';
49 break;
50 }
51 preg_match($re, $ilWACPath->getPath(), $matches);
52
53 return $this->checkAccessToObjectId($matches[1]);
54 }
55
56
57
58 // BEGIN WebDAV cache inline file extensions
59
67 // END WebDAV cache inline file extensions
68
69 protected static $preload_list_gui_data; // [array]
70
81 public static function _getCommands()
82 {
83 $commands = array();
84 $commands[] = array(
85 "permission" => "read",
86 "cmd" => "sendfile",
87 "lang_var" => "download",
88 "default" => true,
89 );
90 $commands[] = array(
91 "permission" => "write",
92 "cmd" => "versions",
93 "lang_var" => "versions",
94 );
95 $commands[] = array(
96 "permission" => "write",
97 "cmd" => "edit",
98 "lang_var" => "settings",
99 );
100
101 return $commands;
102 }
103
107 public static function _checkGoto($a_target)
108 {
109 global $DIC;
110 $ilAccess = $DIC['ilAccess'];
111
112 $t_arr = explode("_", $a_target);
113
114 // personal workspace context: do not force normal login
115 if (isset($t_arr[2]) && $t_arr[2] == "wsp") {
116 include_once "Services/PersonalWorkspace/classes/class.ilSharedResourceGUI.php";
117
118 return ilSharedResourceGUI::hasAccess($t_arr[1]);
119 }
120
121 if ($t_arr[0] != "file" || ((int) $t_arr[1]) <= 0) {
122 return false;
123 }
124
125 if ($ilAccess->checkAccess("visible", "", $t_arr[1])
126 || $ilAccess->checkAccess("read", "", $t_arr[1])
127 ) {
128 return true;
129 }
130
131 return false;
132 }
133
138 public static function _lookupFileData($a_id)
139 {
140 global $DIC;
141 $ilDB = $DIC['ilDB'];
142
143 $q = "SELECT * FROM file_data WHERE file_id = " . $ilDB->quote($a_id, 'integer');
144 $r = $ilDB->query($q);
145 $row = $r->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
146
147 return $row;
148 }
149
153 public static function _lookupVersion($a_id)
154 {
155 global $DIC;
156 $ilDB = $DIC['ilDB'];
157
158 $q = "SELECT version FROM file_data WHERE file_id = " . $ilDB->quote($a_id, 'integer');
159 $r = $ilDB->query($q);
160 $row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
161
162 $striped = ilUtil::stripSlashes($row->version);
163
164 return $striped > 0 ? $striped : 1;
165 }
166
172 public static function _lookupFileSize($a_id) // FSX mus use storage in future
173 {
174 global $DIC;
175 $ilDB = $DIC['ilDB'];
176
177 $q = "SELECT file_size FROM file_data WHERE file_id = " . $ilDB->quote($a_id, 'integer');
178 $r = $ilDB->query($q);
179 $row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
180
181 $size = $row->file_size;
182
183 return $size;
184 }
185
192 public static function _lookupFileSizeFromFilesystem($a_id)
193 {
194 global $DIC;
195 $ilDB = $DIC['ilDB'];
196
197 $q = "SELECT * FROM file_data WHERE file_id = " . $ilDB->quote($a_id, 'integer');
198 $r = $ilDB->query($q);
199 $row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
200
201 require_once('Modules/File/classes/class.ilFSStorageFile.php');
202 $fss = new ilFSStorageFile($a_id);
203 $file = $fss->getAbsolutePath() . '/' . $row->file_name;
204
205 if (@!is_file($file)) {
206 $version_subdir = "/" . sprintf("%03d", ilObjFileAccess::_lookupVersion($a_id));
207 $file = $fss->getAbsolutePath() . '/' . $version_subdir . '/' . $row->file_name;
208 }
209
210 if (is_file($file)) {
211 $size = filesize($file);
212 } else {
213 $size = 0;
214 }
215
216 return $size;
217 }
218
222 public static function _lookupSuffix($a_id)
223 {
224 include_once('Modules/File/classes/class.ilFSStorageFile.php');
225
226 global $DIC;
227 $ilDB = $DIC['ilDB'];
228
229 // BEGIN WebDAV: Filename suffix is determined by file title
230 $q = "SELECT * FROM object_data WHERE obj_id = " . $ilDB->quote($a_id, 'integer');
231 $r = $ilDB->query($q);
232 $row = $r->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
233 require_once 'Modules/File/classes/class.ilObjFile.php';
234
235 return self::_getFileExtension($row->title);
236 // END WebDAV: Filename suffix is determined by file title
237 }
238
244 public static function _lookupDiskUsage($a_id)
245 {
246 include_once('Modules/File/classes/class.ilFSStorageFile.php');
247 $fileStorage = new ilFSStorageFile($a_id);
248 $dir = $fileStorage->getAbsolutePath();
249
250 return ilUtil::dirsize($dir);
251 }
252
253 // BEGIN WebDAV: Get file extension, determine if file is inline, guess file type.
254
258 public static function _isFileInline($a_file_name)
259 {
260 if (self::$_inlineFileExtensionsArray
261 === null
262 ) { // the === makes a huge difference, if the array is empty
263 $settings = new ilSetting('file_access');
264 self::$_inlineFileExtensionsArray = preg_split('/ /', $settings->get('inline_file_extensions'), -1,
265 PREG_SPLIT_NO_EMPTY);
266 }
267 $extension = self::_getFileExtension($a_file_name);
268
269 return in_array($extension, self::$_inlineFileExtensionsArray);
270 }
271
272 public function isMigrated():bool
273 {
274
275 }
276
285 public static function _getFileExtension($a_file_name)
286 {
287 if (preg_match('/\.([a-z0-9]+)\z/i', $a_file_name, $matches) == 1) {
288 return strtolower($matches[1]);
289 } else {
290 return '';
291 }
292 }
293
302 public static function _isFileHidden($a_file_name)
303 {
304 return substr($a_file_name, 0, 1) == '.' || substr($a_file_name, -1, 1) == '~'
305 || substr($a_file_name, 0, 2) == '~$'
306 || $a_file_name == 'Thumbs.db';
307 }
308 // END WebDAV: Get file extension, determine if file is inline, guess file type.
309
330 public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null, $a_handle_extension = false)
331 {
332 global $DIC;
333 $lng = $DIC['lng'];
334
335 $filenameWithoutExtension = $a_file_name;
336
337 $extension = null;
338 if ($a_handle_extension) {
339 // Get the extension and the filename without the extension
340 $extension = ilObjFileAccess::_getFileExtension($a_file_name);
341 if (strlen($extension) > 0) {
342 $extension = '.' . $extension;
343 $filenameWithoutExtension = substr($a_file_name, 0, -strlen($extension));
344 }
345 }
346
347 // create a regular expression from the language text copy_n_of_suffix, so that
348 // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
349 // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
350 // expression '/ Copy \\‍([0-9]+)\\‍)$/'.
351 $nthCopyRegex = preg_replace('/([\^$.\[\]|()?*+{}])/', '\\\\${1}', ' '
352 . $lng->txt('copy_n_of_suffix'));
353 $nthCopyRegex = '/' . preg_replace('/%1\\\\\$s/', '([0-9]+)', $nthCopyRegex) . '$/';
354
355 // Get the filename without any previously added number of copy.
356 // Determine the number of copy, if it has not been specified.
357 if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches)) {
358 // this is going to be at least the third copy of the filename
359 $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0]));
360 if ($nth_copy == null) {
361 $nth_copy = $matches[1] + 1;
362 }
363 } else {
364 if (substr($filenameWithoutExtension, -strlen(' ' . $lng->txt('copy_of_suffix')))
365 == ' ' . $lng->txt('copy_of_suffix')
366 ) {
367 // this is going to be the second copy of the filename
368 $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' '
369 . $lng->txt('copy_of_suffix')));
370 if ($nth_copy == null) {
371 $nth_copy = 2;
372 }
373 } else {
374 // this is going to be the first copy of the filename
375 $filenameWithoutCopy = $filenameWithoutExtension;
376 if ($nth_copy == null) {
377 $nth_copy = 1;
378 }
379 }
380 }
381
382 // Construct the new filename
383 if ($nth_copy > 1) {
384 // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
385 $newFilename = $filenameWithoutCopy . sprintf(' '
386 . $lng->txt('copy_n_of_suffix'), $nth_copy)
387 . $extension;
388 } else {
389 // this is the first copy of the filename, append " - Copy"
390 $newFilename = $filenameWithoutCopy . ' ' . $lng->txt('copy_of_suffix') . $extension;
391 }
392
393 return $newFilename;
394 }
395
401 public static function _getPermanentDownloadLink($ref_id)
402 {
403 return ilLink::_getStaticLink($ref_id, "file", true, "_download");
404 }
405
410 public static function _preloadData($a_obj_ids, $a_ref_ids) : void
411 {
412 global $DIC;
413
414 $DIC->language()->loadLanguageModule('file');
419 self::$preload_list_gui_data = array();
420
421 $set = $DIC->database()->query("SELECT obj_id,max(hdate) latest" . " FROM history"
422 . " WHERE obj_type = " . $DIC->database()->quote("file", "text") . " AND "
423 . $DIC->database()->in("obj_id", $a_obj_ids, "", "integer") . " GROUP BY obj_id");
424 while ($row = $DIC->database()->fetchAssoc($set)) {
425 self::$preload_list_gui_data[$row["obj_id"]]["date"] = $row["latest"];
426 }
427
428 $set = $DIC->database()->query("SELECT file_size, version, file_id, page_count, rid" . " FROM file_data" . " WHERE "
429 . $DIC->database()->in("file_id", $a_obj_ids, "", "integer"));
430 while ($row = $DIC->database()->fetchAssoc($set)) {
431 self::$preload_list_gui_data[$row["file_id"]]["size"] = $row["file_size"];
432 self::$preload_list_gui_data[$row["file_id"]]["version"] = $row["version"];
433 self::$preload_list_gui_data[$row["file_id"]]["page_count"] = $row["page_count"];
434 self::$preload_list_gui_data[$row["file_id"]]["rid"] = $row["rid"];
435 }
436
437 $res = $DIC->database()->query("SELECT rid, file_id FROM file_data WHERE rid IS NOT NULL AND rid !='' AND " . $DIC->database()->in('file_id',
438 $a_obj_ids, false,
439 'integer'));
440 $rids = [];
441 while ($row = $DIC->database()->fetchObject($res)) {
442 $rids[$row->file_id] = $row->rid;
443 }
444 $DIC->resourceStorage()->preload($rids);
445
446 foreach ($rids as $file_id => $rid) {
447 if ($id = $DIC->resourceStorage()->manage()->find($rid)) {
448 $max = $DIC->resourceStorage()->manage()->getResource($id)->getCurrentRevision();
449 self::$preload_list_gui_data[$file_id]["version"] = $max->getVersionNumber();
450 self::$preload_list_gui_data[$file_id]["size"] = $max->getInformation()->getSize();
451 self::$preload_list_gui_data[$file_id]["date"] = $max->getInformation()->getCreationDate()->format(DATE_ATOM);
452 }
453 }
454
455
456 }
457
458
463 public static function getListGUIData($a_obj_id)
464 {
465 if (isset(self::$preload_list_gui_data[$a_obj_id])) {
466 return self::$preload_list_gui_data[$a_obj_id];
467 }
468 }
469}
$size
Definition: RandomTest.php:84
An exception for terminatinating execution or to throw for unit testing.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:19
Class ilFSStorageFile.
Access class for file objects.
static _lookupFileSize($a_id)
static _checkGoto($a_target)
check whether goto script will succeed
static _lookupFileSizeFromFilesystem($a_id)
Looks up the file size by retrieving it from the filesystem.
static _getCommands()
get commands this method returns an array of all possible commands/permission combinations example: $...
static _lookupVersion($a_id)
lookup version
static _getFileExtension($a_file_name)
Gets the file extension of the specified file name.
static _isFileHidden($a_file_name)
Returns true, if a file with the specified name, is usually hidden from the user.
canBeDelivered(ilWACPath $ilWACPath)
static _isFileInline($a_file_name)
Returns true, if the specified file shall be displayed inline in the browser.
static _lookupFileData($a_id)
looks up the file_data for the file object with the specified object id as an associative array.
static _getPermanentDownloadLink($ref_id)
Gets the permanent download link for the file.
static getListGUIData($a_obj_id)
static _appendNumberOfCopyToFilename($a_file_name, $nth_copy=null, $a_handle_extension=false)
Appends the text " - Copy" to a filename in the language of the current user.
static $_inlineFileExtensionsArray
Contains an array of extensions separated by space.
static _lookupSuffix($a_id)
lookup suffix
static _lookupDiskUsage($a_id)
Returns the number of bytes used on the harddisk by the file object with the specified object id.
Class ilObjectAccess.
static _preloadData($a_obj_ids, $a_ref_ids)
Preload data.
static _getAllReferences($a_id)
get all reference ids of object
ILIAS Setting Class.
static hasAccess($a_node_id, $a_is_portfolio=false)
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static dirsize($directory)
get size of a directory or a file.
Class ilWACPath.
global $DIC
Definition: goto.php:24
Class ilWACCheckingClass.
$lng
foreach($_POST as $key=> $value) $res
global $ilDB