ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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 
16 {
17  // BEGIN WebDAV cache inline file extensions
24  protected static $_inlineFileExtensionsArray;
25  // END WebDAV cache inline file extensions
26 
27 
40  function _getCommands()
41  {
42  $commands = array();
43  $commands[] = array("permission" => "read", "cmd" => "sendfile", "lang_var" => "download","default" => true);
44  $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "edit_content");
45  $commands[] = array("permission" => "write", "cmd" => "versions", "lang_var" => "versions");
46 
47  return $commands;
48  }
49 
53  function _checkGoto($a_target)
54  {
55  global $ilAccess;
56 
57  $t_arr = explode("_", $a_target);
58 
59  // personal workspace context: do not force normal login
60  if(isset($t_arr[2]) && $t_arr[2] == "wsp")
61  {
62  include_once "Services/PersonalWorkspace/classes/class.ilSharedResourceGUI.php";
63  return ilSharedResourceGUI::hasAccess($t_arr[1]);
64  }
65 
66  if ($t_arr[0] != "file" || ((int) $t_arr[1]) <= 0)
67  {
68  return false;
69  }
70 
71  if ($ilAccess->checkAccess("visible", "", $t_arr[1]))
72  {
73  return true;
74  }
75  return false;
76  }
77 
82  function _lookupFileData($a_id)
83  {
84  global $ilDB;
85 
86  $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
87  $r = $ilDB->query($q);
88  $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
89 
90  return $row;
91  }
92 
96  function _lookupVersion($a_id)
97  {
98  global $ilDB;
99 
100  $q = "SELECT version FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
101  $r = $ilDB->query($q);
102  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
103 
104  return ilUtil::stripSlashes($row->version);
105  }
106 
111  public static function _lookupFileSize($a_id)
112  {
113  global $ilDB;
114 
115  $q = "SELECT file_size FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
116  $r = $ilDB->query($q);
117  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
118 
119  $size = $row->file_size;
120 
121  return $size;
122  }
123 
130  public static function _lookupFileSizeFromFilesystem($a_id)
131  {
132  global $ilDB;
133 
134  $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
135  $r = $ilDB->query($q);
136  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
137 
138  require_once('Modules/File/classes/class.ilFSStorageFile.php');
139  $fss = new ilFSStorageFile($a_id);
140  $file = $fss->getAbsolutePath().'/'.$row->file_name;
141 
142  if (@!is_file($file))
143  {
144  $version_subdir = "/".sprintf("%03d", ilObjFileAccess::_lookupVersion($a_id));
145  $file = $fss->getAbsolutePath().'/'.$version_subdir.'/'.$row->file_name;
146  }
147 
148  if (is_file($file))
149  {
150  $size = filesize($file);
151  }
152  else
153  {
154  $size = 0;
155  }
156 
157  return $size;
158  }
159 
160 
164  function _lookupSuffix($a_id)
165  {
166  include_once('Modules/File/classes/class.ilFSStorageFile.php');
167 
168  global $ilDB;
169 
170  // BEGIN WebDAV: Filename suffix is determined by file title
171  $q = "SELECT * FROM object_data WHERE obj_id = ".$ilDB->quote($a_id ,'integer');
172  $r = $ilDB->query($q);
173  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
174  require_once 'Modules/File/classes/class.ilObjFile.php';
175  return self::_getFileExtension($row->title);
176  // END WebDAV: Filename suffix is determined by file title
177  }
178 
184  function _lookupDiskUsage($a_id)
185  {
186  include_once('Modules/File/classes/class.ilFSStorageFile.php');
187  $fileStorage = new ilFSStorageFile($a_id);
188  $dir = $fileStorage->getAbsolutePath();
189  return ilUtil::dirsize($dir);
190  }
191 
192  // BEGIN WebDAV: Get file extension, determine if file is inline, guess file type.
196  public static function _isFileInline($a_file_name)
197  {
198  if (self::$_inlineFileExtensionsArray === null) // the === makes a huge difference, if the array is empty
199  {
200  require_once 'Services/Administration/classes/class.ilSetting.php';
201  $settings = new ilSetting('file_access');
202  self::$_inlineFileExtensionsArray = preg_split('/ /', $settings->get('inline_file_extensions'), -1, PREG_SPLIT_NO_EMPTY);
203  }
204  $extension = self::_getFileExtension($a_file_name);
205  return in_array($extension, self::$_inlineFileExtensionsArray);
206  }
219  public static function _getFileExtension($a_file_name)
220  {
221  if (preg_match('/\.([a-z0-9]+)\z/i',$a_file_name,$matches) == 1)
222  {
223  return strtolower($matches[1]);
224  }
225  else
226  {
227  return '';
228  }
229  }
230 
240  public static function _isFileHidden($a_file_name)
241  {
242  return substr($a_file_name,0,1) == '.' ||
243  substr($a_file_name,-1,1) == '~' ||
244  substr($a_file_name,0,2) == '~$' ||
245  $a_file_name == 'Thumbs.db';
246  }
247  // END WebDAV: Get file extension, determine if file is inline, guess file type.
248 
276  public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null)
277  {
278  global $lng;
279 
280  // Get the extension and the filename without the extension
281  $extension = ilObjFileAccess::_getFileExtension($a_file_name);
282  if (strlen($extension) > 0)
283  {
284  $extension = '.'.$extension;
285  $filenameWithoutExtension= substr($a_file_name, 0, -strlen($extension));
286  }
287  else
288  {
289  $filenameWithoutExtension= $a_file_name;
290  }
291 
292  // create a regular expression from the language text copy_n_of_suffix, so that
293  // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
294  // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
295  // expression '/ Copy \\([0-9]+)\\)$/'.
296  $nthCopyRegex = preg_replace('/([\^$.\[\]|()?*+{}])/','\\\\${1}', ' '.$lng->txt('copy_n_of_suffix'));
297  $nthCopyRegex = '/'.preg_replace('/%1\\\\\$s/', '([0-9]+)', $nthCopyRegex).'$/';
298 
299  // Get the filename without any previously added number of copy.
300  // Determine the number of copy, if it has not been specified.
301  if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches))
302  {
303  // this is going to be at least the third copy of the filename
304  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0]));
305  if ($nth_copy == null)
306  {
307  $nth_copy = $matches[1]+1;
308  }
309  }
310  else if (substr($filenameWithoutExtension,-strlen(' '.$lng->txt('copy_of_suffix'))) == ' '.$lng->txt('copy_of_suffix'))
311  {
312  // this is going to be the second copy of the filename
313  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' '.$lng->txt('copy_of_suffix')));
314  if ($nth_copy == null)
315  {
316  $nth_copy = 2;
317  }
318  }
319  else
320  {
321  // this is going to be the first copy of the filename
322  $filenameWithoutCopy = $filenameWithoutExtension;
323  if ($nth_copy == null)
324  {
325  $nth_copy = 1;
326  }
327  }
328 
329 
330  // Construct the new filename
331  if ($nth_copy > 1)
332  {
333  // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
334  $newFilename = $filenameWithoutCopy.sprintf(' '.$lng->txt('copy_n_of_suffix'), $nth_copy).$extension;
335  }
336  else
337  {
338  // this is the first copy of the filename, append " - Copy"
339  $newFilename = $filenameWithoutCopy.' '.$lng->txt('copy_of_suffix').$extension;
340  }
341 
342  return $newFilename;
343  }
344 }
345 
346 ?>