ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjFileAccess.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once("classes/class.ilObjectAccess.php");
25 
35 {
36  // BEGIN WebDAV cache inline file extensions
43  protected static $_inlineFileExtensionsArray;
44  // END WebDAV cache inline file extensions
45 
46 
59  function _getCommands()
60  {
61  $commands = array();
62  $commands[] = array("permission" => "read", "cmd" => "sendfile", "lang_var" => "download","default" => true);
63  $commands[] = array("permission" => "write", "cmd" => "edit", "lang_var" => "edit");
64  $commands[] = array("permission" => "write", "cmd" => "versions", "lang_var" => "versions");
65 
66  return $commands;
67  }
68 
72  function _checkGoto($a_target)
73  {
74  global $ilAccess;
75 
76  $t_arr = explode("_", $a_target);
77 
78  if ($t_arr[0] != "file" || ((int) $t_arr[1]) <= 0)
79  {
80  return false;
81  }
82 
83  if ($ilAccess->checkAccess("visible", "", $t_arr[1]))
84  {
85  return true;
86  }
87  return false;
88  }
89 
94  function _lookupFileData($a_id)
95  {
96  global $ilDB;
97 
98  $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
99  $r = $ilDB->query($q);
100  $row = $r->fetchRow(DB_FETCHMODE_ASSOC);
101 
102  return $row;
103  }
104 
108  function _lookupVersion($a_id)
109  {
110  global $ilDB;
111 
112  $q = "SELECT version FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
113  $r = $ilDB->query($q);
114  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
115 
116  return ilUtil::stripSlashes($row->version);
117  }
118 
123  public static function _lookupFileSize($a_id)
124  {
125  global $ilDB;
126 
127  $q = "SELECT file_size FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
128  $r = $ilDB->query($q);
129  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
130 
131  $size = $row->file_size;
132 
133  return $size;
134  }
135 
142  public static function _lookupFileSizeFromFilesystem($a_id)
143  {
144  global $ilDB;
145 
146  $q = "SELECT * FROM file_data WHERE file_id = ".$ilDB->quote($a_id ,'integer');
147  $r = $ilDB->query($q);
148  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
149 
150  require_once('Modules/File/classes/class.ilFSStorageFile.php');
151  $fss = new ilFSStorageFile($a_id);
152  $file = $fss->getAbsolutePath().'/'.$row->file_name;
153 
154  if (@!is_file($file))
155  {
156  $version_subdir = "/".sprintf("%03d", ilObjFileAccess::_lookupVersion($a_id));
157  $file = $fss->getAbsolutePath().'/'.$version_subdir.'/'.$row->file_name;
158  }
159 
160  if (is_file($file))
161  {
162  $size = filesize($file);
163  }
164  else
165  {
166  $size = 0;
167  }
168 
169  return $size;
170  }
171 
172 
176  function _lookupSuffix($a_id)
177  {
178  include_once('Modules/File/classes/class.ilFSStorageFile.php');
179 
180  global $ilDB;
181 
182  // BEGIN WebDAV: Filename suffix is determined by file title
183  $q = "SELECT * FROM object_data WHERE obj_id = ".$ilDB->quote($a_id ,'integer');
184  $r = $ilDB->query($q);
185  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
186  require_once 'Modules/File/classes/class.ilObjFile.php';
187  return self::_getFileExtension($row->title);
188  // END WebDAV: Filename suffix is determined by file title
189  }
190 
196  function _lookupDiskUsage($a_id)
197  {
198  include_once('Modules/File/classes/class.ilFSStorageFile.php');
199  $fileStorage = new ilFSStorageFile($a_id);
200  $dir = $fileStorage->getAbsolutePath();
201  return ilUtil::dirsize($dir);
202  }
203 
204  // BEGIN WebDAV: Get file extension, determine if file is inline, guess file type.
208  public static function _isFileInline($a_file_name)
209  {
210  if (self::$_inlineFileExtensionsArray === null) // the === makes a huge difference, if the array is empty
211  {
212  require_once 'Services/Administration/classes/class.ilSetting.php';
213  $settings = new ilSetting('file_access');
214  self::$_inlineFileExtensionsArray = preg_split('/ /', $settings->get('inline_file_extensions'), -1, PREG_SPLIT_NO_EMPTY);
215  }
216  $extension = self::_getFileExtension($a_file_name);
217  return in_array($extension, self::$_inlineFileExtensionsArray);
218  }
231  public static function _getFileExtension($a_file_name)
232  {
233  if (preg_match('/\.([a-z0-9]+)\z/i',$a_file_name,$matches) == 1)
234  {
235  return strtolower($matches[1]);
236  }
237  else
238  {
239  return '';
240  }
241  }
242 
252  public static function _isFileHidden($a_file_name)
253  {
254  return substr($a_file_name,0,1) == '.' ||
255  substr($a_file_name,-1,1) == '~' ||
256  substr($a_file_name,0,2) == '~$' ||
257  $a_file_name == 'Thumbs.db';
258  }
259  // END WebDAV: Get file extension, determine if file is inline, guess file type.
260 
288  public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null)
289  {
290  global $lng;
291 
292  // Get the extension and the filename without the extension
293  $extension = ilObjFileAccess::_getFileExtension($a_file_name);
294  if (strlen($extension) > 0)
295  {
296  $extension = '.'.$extension;
297  $filenameWithoutExtension= substr($a_file_name, 0, -strlen($extension));
298  }
299  else
300  {
301  $filenameWithoutExtension= $a_file_name;
302  }
303 
304  // create a regular expression from the language text copy_n_of_suffix, so that
305  // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
306  // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
307  // expression '/ Copy \\([0-9]+)\\)$/'.
308  $nthCopyRegex = preg_replace('/([\^$.\[\]|()?*+{}])/','\\\\${1}', ' '.$lng->txt('copy_n_of_suffix'));
309  $nthCopyRegex = '/'.preg_replace('/%1\\\\\$s/', '([0-9]+)', $nthCopyRegex).'$/';
310 
311  // Get the filename without any previously added number of copy.
312  // Determine the number of copy, if it has not been specified.
313  if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches))
314  {
315  // this is going to be at least the third copy of the filename
316  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0]));
317  if ($nth_copy == null)
318  {
319  $nth_copy = $matches[1]+1;
320  }
321  }
322  else if (substr($filenameWithoutExtension,-strlen(' '.$lng->txt('copy_of_suffix'))) == ' '.$lng->txt('copy_of_suffix'))
323  {
324  // this is going to be the second copy of the filename
325  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' '.$lng->txt('copy_of_suffix')));
326  if ($nth_copy == null)
327  {
328  $nth_copy = 2;
329  }
330  }
331  else
332  {
333  // this is going to be the first copy of the filename
334  $filenameWithoutCopy = $filenameWithoutExtension;
335  if ($nth_copy == null)
336  {
337  $nth_copy = 1;
338  }
339  }
340 
341 
342  // Construct the new filename
343  if ($nth_copy > 1)
344  {
345  // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
346  $newFilename = $filenameWithoutCopy.sprintf(' '.$lng->txt('copy_n_of_suffix'), $nth_copy).$extension;
347  }
348  else
349  {
350  // this is the first copy of the filename, append " - Copy"
351  $newFilename = $filenameWithoutCopy.' '.$lng->txt('copy_of_suffix').$extension;
352  }
353 
354  return $newFilename;
355  }
356 }
357 
358 ?>