ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
5 include_once("./Services/Object/classes/class.ilObjectAccess.php");
6 
17 {
18  // BEGIN WebDAV cache inline file extensions
25  protected static $_inlineFileExtensionsArray;
26  // END WebDAV cache inline file extensions
27 
28  protected static $preload_list_gui_data; // [array]
29 
30 
43  function _getCommands()
44  {
45  $commands = array();
46  $commands[] = array("permission" => "read", "cmd" => "sendfile", "lang_var" => "download","default" => true);
47  $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "edit_content");
48  $commands[] = array("permission" => "write", "cmd" => "versions", "lang_var" => "versions");
49 
50  return $commands;
51  }
52 
56  function _checkGoto($a_target)
57  {
58  global $ilAccess;
59 
60  $t_arr = explode("_", $a_target);
61 
62  // personal workspace context: do not force normal login
63  if(isset($t_arr[2]) && $t_arr[2] == "wsp")
64  {
65  include_once "Services/PersonalWorkspace/classes/class.ilSharedResourceGUI.php";
66  return ilSharedResourceGUI::hasAccess($t_arr[1]);
67  }
68 
69  if ($t_arr[0] != "file" || ((int) $t_arr[1]) <= 0)
70  {
71  return false;
72  }
73 
74  if ($ilAccess->checkAccess("visible", "", $t_arr[1]))
75  {
76  return true;
77  }
78  return false;
79  }
80 
85  function _lookupFileData($a_id)
86  {
87  global $ilDB;
88 
89  $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
90  $r = $ilDB->query($q);
91  $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
92 
93  return $row;
94  }
95 
99  function _lookupVersion($a_id)
100  {
101  global $ilDB;
102 
103  $q = "SELECT version FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
104  $r = $ilDB->query($q);
105  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
106 
107  return ilUtil::stripSlashes($row->version);
108  }
109 
114  public static function _lookupFileSize($a_id)
115  {
116  global $ilDB;
117 
118  $q = "SELECT file_size FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
119  $r = $ilDB->query($q);
120  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
121 
122  $size = $row->file_size;
123 
124  return $size;
125  }
126 
133  public static function _lookupFileSizeFromFilesystem($a_id)
134  {
135  global $ilDB;
136 
137  $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
138  $r = $ilDB->query($q);
139  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
140 
141  require_once('Modules/File/classes/class.ilFSStorageFile.php');
142  $fss = new ilFSStorageFile($a_id);
143  $file = $fss->getAbsolutePath().'/'.$row->file_name;
144 
145  if (@!is_file($file))
146  {
147  $version_subdir = "/".sprintf("%03d", ilObjFileAccess::_lookupVersion($a_id));
148  $file = $fss->getAbsolutePath().'/'.$version_subdir.'/'.$row->file_name;
149  }
150 
151  if (is_file($file))
152  {
153  $size = filesize($file);
154  }
155  else
156  {
157  $size = 0;
158  }
159 
160  return $size;
161  }
162 
163 
167  function _lookupSuffix($a_id)
168  {
169  include_once('Modules/File/classes/class.ilFSStorageFile.php');
170 
171  global $ilDB;
172 
173  // BEGIN WebDAV: Filename suffix is determined by file title
174  $q = "SELECT * FROM object_data WHERE obj_id = ".$ilDB->quote($a_id ,'integer');
175  $r = $ilDB->query($q);
176  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
177  require_once 'Modules/File/classes/class.ilObjFile.php';
178  return self::_getFileExtension($row->title);
179  // END WebDAV: Filename suffix is determined by file title
180  }
181 
187  function _lookupDiskUsage($a_id)
188  {
189  include_once('Modules/File/classes/class.ilFSStorageFile.php');
190  $fileStorage = new ilFSStorageFile($a_id);
191  $dir = $fileStorage->getAbsolutePath();
192  return ilUtil::dirsize($dir);
193  }
194 
195  // BEGIN WebDAV: Get file extension, determine if file is inline, guess file type.
199  public static function _isFileInline($a_file_name)
200  {
201  if (self::$_inlineFileExtensionsArray === null) // the === makes a huge difference, if the array is empty
202  {
203  require_once 'Services/Administration/classes/class.ilSetting.php';
204  $settings = new ilSetting('file_access');
205  self::$_inlineFileExtensionsArray = preg_split('/ /', $settings->get('inline_file_extensions'), -1, PREG_SPLIT_NO_EMPTY);
206  }
207  $extension = self::_getFileExtension($a_file_name);
208  return in_array($extension, self::$_inlineFileExtensionsArray);
209  }
222  public static function _getFileExtension($a_file_name)
223  {
224  if (preg_match('/\.([a-z0-9]+)\z/i',$a_file_name,$matches) == 1)
225  {
226  return strtolower($matches[1]);
227  }
228  else
229  {
230  return '';
231  }
232  }
233 
243  public static function _isFileHidden($a_file_name)
244  {
245  return substr($a_file_name,0,1) == '.' ||
246  substr($a_file_name,-1,1) == '~' ||
247  substr($a_file_name,0,2) == '~$' ||
248  $a_file_name == 'Thumbs.db';
249  }
250  // END WebDAV: Get file extension, determine if file is inline, guess file type.
251 
279  public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null, $a_handle_extension = false)
280  {
281  global $lng;
282 
283  $filenameWithoutExtension= $a_file_name;
284 
285  $extension = null;
286  if ($a_handle_extension)
287  {
288  // Get the extension and the filename without the extension
289  $extension = ilObjFileAccess::_getFileExtension($a_file_name);
290  if (strlen($extension) > 0)
291  {
292  $extension = '.'.$extension;
293  $filenameWithoutExtension= substr($a_file_name, 0, -strlen($extension));
294  }
295  }
296 
297  // create a regular expression from the language text copy_n_of_suffix, so that
298  // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
299  // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
300  // expression '/ Copy \\([0-9]+)\\)$/'.
301  $nthCopyRegex = preg_replace('/([\^$.\[\]|()?*+{}])/','\\\\${1}', ' '.$lng->txt('copy_n_of_suffix'));
302  $nthCopyRegex = '/'.preg_replace('/%1\\\\\$s/', '([0-9]+)', $nthCopyRegex).'$/';
303 
304  // Get the filename without any previously added number of copy.
305  // Determine the number of copy, if it has not been specified.
306  if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches))
307  {
308  // this is going to be at least the third copy of the filename
309  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0]));
310  if ($nth_copy == null)
311  {
312  $nth_copy = $matches[1]+1;
313  }
314  }
315  else if (substr($filenameWithoutExtension,-strlen(' '.$lng->txt('copy_of_suffix'))) == ' '.$lng->txt('copy_of_suffix'))
316  {
317  // this is going to be the second copy of the filename
318  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' '.$lng->txt('copy_of_suffix')));
319  if ($nth_copy == null)
320  {
321  $nth_copy = 2;
322  }
323  }
324  else
325  {
326  // this is going to be the first copy of the filename
327  $filenameWithoutCopy = $filenameWithoutExtension;
328  if ($nth_copy == null)
329  {
330  $nth_copy = 1;
331  }
332  }
333 
334 
335  // Construct the new filename
336  if ($nth_copy > 1)
337  {
338  // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
339  $newFilename = $filenameWithoutCopy.sprintf(' '.$lng->txt('copy_n_of_suffix'), $nth_copy).$extension;
340  }
341  else
342  {
343  // this is the first copy of the filename, append " - Copy"
344  $newFilename = $filenameWithoutCopy.' '.$lng->txt('copy_of_suffix').$extension;
345  }
346 
347  return $newFilename;
348  }
349 
353  public static function _getPermanentDownloadLink($ref_id)
354  {
355  include_once("./Services/Link/classes/class.ilLink.php");
356  return ilLink::_getStaticLink($ref_id, "file", true, "_download");
357  }
358 
359  public function _preloadData($a_obj_ids, $a_ref_ids)
360  {
361  global $ilDB;
362 
363  self::$preload_list_gui_data = array();
364 
365  $set = $ilDB->query("SELECT obj_id,max(hdate) latest".
366  " FROM history".
367  " WHERE obj_type = ".$ilDB->quote("file", "text").
368  " AND ".$ilDB->in("obj_id", $a_obj_ids, "", "integer").
369  " GROUP BY obj_id");
370  while($row = $ilDB->fetchAssoc($set))
371  {
372  self::$preload_list_gui_data[$row["obj_id"]]["date"] = $row["latest"];
373  }
374 
375  $set = $ilDB->query("SELECT file_size,version,file_id".
376  " FROM file_data".
377  " WHERE ".$ilDB->in("file_id", $a_obj_ids, "", "integer"));
378  while($row = $ilDB->fetchAssoc($set))
379  {
380  self::$preload_list_gui_data[$row["file_id"]]["size"] = $row["file_size"];
381  self::$preload_list_gui_data[$row["file_id"]]["version"] = $row["version"];
382  }
383  }
384 
385  public static function getListGUIData($a_obj_id)
386  {
387  if(isset(self::$preload_list_gui_data[$a_obj_id]))
388  {
389  return self::$preload_list_gui_data[$a_obj_id];
390  }
391  }
392 }
393 
394 ?>
_lookupFileData($a_id)
looks up the file_data for the file object with the specified object id as an associative array...
_checkGoto($a_target)
check whether goto script will succeed
static $_inlineFileExtensionsArray
Contains an array of extensions separated by space.
_getCommands()
get commands
ILIAS Setting Class.
print $file
$size
Definition: RandomTest.php:79
static _getPermanentDownloadLink($ref_id)
Gets the permanent download link for the file.
static _isFileInline($a_file_name)
Returns true, if the specified file shall be displayed inline in the browser.
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.
_lookupSuffix($a_id)
lookup suffix
static _isFileHidden($a_file_name)
Returns true, if a file with the specified name, is usually hidden from the user. ...
static _getFileExtension($a_file_name)
Gets the file extension of the specified file name.
Access class for file objects.
_lookupVersion($a_id)
lookup version
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
static _lookupFileSize($a_id)
Quickly looks up the file size from the database and returns the number of bytes. ...
const DB_FETCHMODE_ASSOC
Definition: class.ilDB.php:10
static hasAccess($a_node_id, $a_is_portfolio=false)
static _lookupFileSizeFromFilesystem($a_id)
Looks up the file size by retrieving it from the filesystem.
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.
$ref_id
Definition: sahs_server.php:39
Class ilObjectAccess.
global $lng
Definition: privfeed.php:40
global $ilDB
_lookupDiskUsage($a_id)
Returns the number of bytes used on the harddisk by the file object with the specified object id...
_preloadData($a_obj_ids, $a_ref_ids)
static getListGUIData($a_obj_id)
$r