ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
5include_once("./Services/Object/classes/class.ilObjectAccess.php");
6require_once('./Services/WebAccessChecker/interfaces/interface.ilWACCheckingClass.php');
7
18
23 protected function checkAccessToObjectId($obj_id) {
24 global $ilAccess;
28 foreach (ilObject::_getAllReferences($obj_id) as $ref_id) {
29 if ($ilAccess->checkAccess('read', '', $ref_id)) {
30 return true;
31 }
32 }
33
34 return false;
35 }
36
37
42 public function canBeDelivered(ilWACPath $ilWACPath) {
43 switch ($ilWACPath->getSecurePathId()) {
44 case 'previews':
45 $re = '/\/previews\/[\d\/]{0,}\/preview_([\d]{0,})\//uU';
46 break;
47 }
48 preg_match($re, $ilWACPath->getPath(), $matches);
49
50 return $this->checkAccessToObjectId($matches[1]);
51 }
52
53
54
55 // BEGIN WebDAV cache inline file extensions
63 // END WebDAV cache inline file extensions
64
65 protected static $preload_list_gui_data; // [array]
66
67
80 function _getCommands()
81 {
82 $commands = array();
83 $commands[] = array("permission" => "read", "cmd" => "sendfile", "lang_var" => "download","default" => true);
84 $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "edit_content");
85 $commands[] = array("permission" => "write", "cmd" => "versions", "lang_var" => "versions");
86
87 return $commands;
88 }
89
93 function _checkGoto($a_target)
94 {
95 global $ilAccess;
96
97 $t_arr = explode("_", $a_target);
98
99 // personal workspace context: do not force normal login
100 if(isset($t_arr[2]) && $t_arr[2] == "wsp")
101 {
102 include_once "Services/PersonalWorkspace/classes/class.ilSharedResourceGUI.php";
103 return ilSharedResourceGUI::hasAccess($t_arr[1]);
104 }
105
106 if ($t_arr[0] != "file" || ((int) $t_arr[1]) <= 0)
107 {
108 return false;
109 }
110
111 if ($ilAccess->checkAccess("visible", "", $t_arr[1]) ||
112 $ilAccess->checkAccess("read", "", $t_arr[1]))
113 {
114 return true;
115 }
116 return false;
117 }
118
123 function _lookupFileData($a_id)
124 {
125 global $ilDB;
126
127 $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
128 $r = $ilDB->query($q);
129 $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
130
131 return $row;
132 }
133
137 function _lookupVersion($a_id)
138 {
139 global $ilDB;
140
141 $q = "SELECT version FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
142 $r = $ilDB->query($q);
143 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
144
145 return ilUtil::stripSlashes($row->version);
146 }
147
152 public static function _lookupFileSize($a_id)
153 {
154 global $ilDB;
155
156 $q = "SELECT file_size FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
157 $r = $ilDB->query($q);
158 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
159
160 $size = $row->file_size;
161
162 return $size;
163 }
164
171 public static function _lookupFileSizeFromFilesystem($a_id)
172 {
173 global $ilDB;
174
175 $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
176 $r = $ilDB->query($q);
177 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
178
179 require_once('Modules/File/classes/class.ilFSStorageFile.php');
180 $fss = new ilFSStorageFile($a_id);
181 $file = $fss->getAbsolutePath().'/'.$row->file_name;
182
183 if (@!is_file($file))
184 {
185 $version_subdir = "/".sprintf("%03d", ilObjFileAccess::_lookupVersion($a_id));
186 $file = $fss->getAbsolutePath().'/'.$version_subdir.'/'.$row->file_name;
187 }
188
189 if (is_file($file))
190 {
191 $size = filesize($file);
192 }
193 else
194 {
195 $size = 0;
196 }
197
198 return $size;
199 }
200
201
205 function _lookupSuffix($a_id)
206 {
207 include_once('Modules/File/classes/class.ilFSStorageFile.php');
208
209 global $ilDB;
210
211 // BEGIN WebDAV: Filename suffix is determined by file title
212 $q = "SELECT * FROM object_data WHERE obj_id = ".$ilDB->quote($a_id ,'integer');
213 $r = $ilDB->query($q);
214 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
215 require_once 'Modules/File/classes/class.ilObjFile.php';
216 return self::_getFileExtension($row->title);
217 // END WebDAV: Filename suffix is determined by file title
218 }
219
225 function _lookupDiskUsage($a_id)
226 {
227 include_once('Modules/File/classes/class.ilFSStorageFile.php');
228 $fileStorage = new ilFSStorageFile($a_id);
229 $dir = $fileStorage->getAbsolutePath();
230 return ilUtil::dirsize($dir);
231 }
232
233 // BEGIN WebDAV: Get file extension, determine if file is inline, guess file type.
237 public static function _isFileInline($a_file_name)
238 {
239 if (self::$_inlineFileExtensionsArray === null) // the === makes a huge difference, if the array is empty
240 {
241 require_once 'Services/Administration/classes/class.ilSetting.php';
242 $settings = new ilSetting('file_access');
243 self::$_inlineFileExtensionsArray = preg_split('/ /', $settings->get('inline_file_extensions'), -1, PREG_SPLIT_NO_EMPTY);
244 }
245 $extension = self::_getFileExtension($a_file_name);
246 return in_array($extension, self::$_inlineFileExtensionsArray);
247 }
260 public static function _getFileExtension($a_file_name)
261 {
262 if (preg_match('/\.([a-z0-9]+)\z/i',$a_file_name,$matches) == 1)
263 {
264 return strtolower($matches[1]);
265 }
266 else
267 {
268 return '';
269 }
270 }
271
281 public static function _isFileHidden($a_file_name)
282 {
283 return substr($a_file_name,0,1) == '.' ||
284 substr($a_file_name,-1,1) == '~' ||
285 substr($a_file_name,0,2) == '~$' ||
286 $a_file_name == 'Thumbs.db';
287 }
288 // END WebDAV: Get file extension, determine if file is inline, guess file type.
289
317 public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null, $a_handle_extension = false)
318 {
319 global $lng;
320
321 $filenameWithoutExtension= $a_file_name;
322
323 $extension = null;
324 if ($a_handle_extension)
325 {
326 // Get the extension and the filename without the extension
327 $extension = ilObjFileAccess::_getFileExtension($a_file_name);
328 if (strlen($extension) > 0)
329 {
330 $extension = '.'.$extension;
331 $filenameWithoutExtension= substr($a_file_name, 0, -strlen($extension));
332 }
333 }
334
335 // create a regular expression from the language text copy_n_of_suffix, so that
336 // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
337 // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
338 // expression '/ Copy \\‍([0-9]+)\\‍)$/'.
339 $nthCopyRegex = preg_replace('/([\^$.\[\]|()?*+{}])/','\\\\${1}', ' '.$lng->txt('copy_n_of_suffix'));
340 $nthCopyRegex = '/'.preg_replace('/%1\\\\\$s/', '([0-9]+)', $nthCopyRegex).'$/';
341
342 // Get the filename without any previously added number of copy.
343 // Determine the number of copy, if it has not been specified.
344 if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches))
345 {
346 // this is going to be at least the third copy of the filename
347 $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0]));
348 if ($nth_copy == null)
349 {
350 $nth_copy = $matches[1]+1;
351 }
352 }
353 else if (substr($filenameWithoutExtension,-strlen(' '.$lng->txt('copy_of_suffix'))) == ' '.$lng->txt('copy_of_suffix'))
354 {
355 // this is going to be the second copy of the filename
356 $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' '.$lng->txt('copy_of_suffix')));
357 if ($nth_copy == null)
358 {
359 $nth_copy = 2;
360 }
361 }
362 else
363 {
364 // this is going to be the first copy of the filename
365 $filenameWithoutCopy = $filenameWithoutExtension;
366 if ($nth_copy == null)
367 {
368 $nth_copy = 1;
369 }
370 }
371
372
373 // Construct the new filename
374 if ($nth_copy > 1)
375 {
376 // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
377 $newFilename = $filenameWithoutCopy.sprintf(' '.$lng->txt('copy_n_of_suffix'), $nth_copy).$extension;
378 }
379 else
380 {
381 // this is the first copy of the filename, append " - Copy"
382 $newFilename = $filenameWithoutCopy.' '.$lng->txt('copy_of_suffix').$extension;
383 }
384
385 return $newFilename;
386 }
387
391 public static function _getPermanentDownloadLink($ref_id)
392 {
393 include_once("./Services/Link/classes/class.ilLink.php");
394 return ilLink::_getStaticLink($ref_id, "file", true, "_download");
395 }
396
397 public function _preloadData($a_obj_ids, $a_ref_ids)
398 {
399 global $ilDB;
400
401 self::$preload_list_gui_data = array();
402
403 $set = $ilDB->query("SELECT obj_id,max(hdate) latest".
404 " FROM history".
405 " WHERE obj_type = ".$ilDB->quote("file", "text").
406 " AND ".$ilDB->in("obj_id", $a_obj_ids, "", "integer").
407 " GROUP BY obj_id");
408 while($row = $ilDB->fetchAssoc($set))
409 {
410 self::$preload_list_gui_data[$row["obj_id"]]["date"] = $row["latest"];
411 }
412
413 $set = $ilDB->query("SELECT file_size,version,file_id".
414 " FROM file_data".
415 " WHERE ".$ilDB->in("file_id", $a_obj_ids, "", "integer"));
416 while($row = $ilDB->fetchAssoc($set))
417 {
418 self::$preload_list_gui_data[$row["file_id"]]["size"] = $row["file_size"];
419 self::$preload_list_gui_data[$row["file_id"]]["version"] = $row["version"];
420 }
421 }
422
423 public static function getListGUIData($a_obj_id)
424 {
425 if(isset(self::$preload_list_gui_data[$a_obj_id]))
426 {
427 return self::$preload_list_gui_data[$a_obj_id];
428 }
429 }
430}
431
432?>
print $file
$size
Definition: RandomTest.php:79
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
Access class for file objects.
static _lookupFileSize($a_id)
Quickly looks up the file size from the database and returns the number of bytes.
_preloadData($a_obj_ids, $a_ref_ids)
Preload data.
static _lookupFileSizeFromFilesystem($a_id)
Looks up the file size by retrieving it from the filesystem.
_getCommands()
get commands
static _getFileExtension($a_file_name)
Gets the file extension of the specified file name.
_lookupVersion($a_id)
lookup version
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.
_lookupDiskUsage($a_id)
Returns the number of bytes used on the harddisk by the file object with the specified object id.
static _getPermanentDownloadLink($ref_id)
Gets the permanent download link for the file.
_checkGoto($a_target)
check whether goto script will succeed
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.
_lookupSuffix($a_id)
lookup suffix
Class ilObjectAccess.
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.
$r
Definition: example_031.php:79
Class ilWACCheckingClass.
global $lng
Definition: privfeed.php:40
$ref_id
Definition: sahs_server.php:39
global $ilDB