ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjFileAccess.php
Go to the documentation of this file.
1 <?php
2 
21 
30 {
36  protected static array $inline_file_extensions = [];
37 
38  protected static array $preload_list_gui_data = [];
39 
40 
41  protected function checkAccessToObjectId(int $obj_id): bool
42  {
43  global $DIC;
44  $ilAccess = $DIC['ilAccess'];
48  foreach (ilObject::_getAllReferences($obj_id) as $ref_id) {
49  if ($ilAccess->checkAccess('read', '', $ref_id)) {
50  return true;
51  }
52  }
53 
54  return false;
55  }
56 
57  public function canBeDelivered(ilWACPath $ilWACPath): bool
58  {
59  switch ($ilWACPath->getSecurePathId()) {
60  case 'previews':
61  preg_match('/\/previews\/[\d\/]{0,}\/preview_([\d]{0,})\//uU', $ilWACPath->getPath(), $matches);
62  $obj_id = (int) $matches[1];
63  break;
64  default:
65  $obj_id = -1;
66  break;
67  }
68 
69  return $this->checkAccessToObjectId($obj_id);
70  }
71 
72 
73 
85  public static function _getCommands(): array
86  {
87  $commands = array();
88  $commands[] = array(
89  "permission" => "read",
90  "cmd" => "sendfile",
91  "lang_var" => "download",
92  "default" => true,
93  );
94  $commands[] = array(
95  "permission" => "write",
97  "lang_var" => "unzip",
98  );
99  $commands[] = array(
100  "permission" => "write",
101  "cmd" => "versions",
102  "lang_var" => "versions",
103  );
104  $commands[] = array(
105  "permission" => "write",
106  "cmd" => "edit",
107  "lang_var" => "settings",
108  );
109 
110  return $commands;
111  }
112 
116  public static function _checkGoto(string $a_target): bool
117  {
118  global $DIC;
119  $access = $DIC['ilAccess'];
120 
121  $target_parts = explode("_", $a_target);
122 
123  // personal workspace context: do not force normal login
124  if (isset($target_parts[2]) && $target_parts[2] === "wsp") {
125  return ilSharedResourceGUI::hasAccess($target_parts[1]);
126  }
127 
128  $type = $target_parts[0]??null;
129  $ref_id = (int) ($target_parts[1] ?? 0);
130  if ($type !== "file" || $ref_id <= 0) {
131  return false;
132  }
133  return $access->checkAccess("visible", "", $ref_id)
134  || $access->checkAccess("read", "", $ref_id);
135  }
136 
141  public static function _lookupFileSize(int $a_id, bool $by_reference = true): int
142  {
143  try {
144  $info_repo = new ilObjFileInfoRepository();
145  if ($by_reference) {
146  $info = $info_repo->getByRefId($a_id);
147  } else {
148  $info = $info_repo->getByObjectId($a_id);
149  }
150 
151  return (int) $info->getFileSize()->inBytes();
152  } catch (Throwable $t) {
153  return 0;
154  }
155  }
156 
157 
165  public static function _getFileExtension(string $a_file_name): string
166  {
167  if (preg_match('/\.([a-z0-9]+)\z/i', $a_file_name, $matches) == 1) {
168  return strtolower($matches[1]);
169  } else {
170  return '';
171  }
172  }
173 
182  public static function _isFileHidden(string $a_file_name): bool
183  {
184  return substr($a_file_name, 0, 1) == '.' || substr($a_file_name, -1, 1) == '~'
185  || substr($a_file_name, 0, 2) == '~$'
186  || $a_file_name == 'Thumbs.db';
187  }
188 
209  public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null, $a_handle_extension = false): string
210  {
211  global $DIC;
212  $lng = $DIC['lng'];
213 
214  $filenameWithoutExtension = $a_file_name;
215 
216  $extension = null;
217  if ($a_handle_extension) {
218  // Get the extension and the filename without the extension
219  $extension = ilObjFileAccess::_getFileExtension($a_file_name);
220  if (strlen($extension) > 0) {
221  $extension = '.' . $extension;
222  $filenameWithoutExtension = substr($a_file_name, 0, -strlen($extension));
223  }
224  }
225 
226  // create a regular expression from the language text copy_n_of_suffix, so that
227  // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
228  // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
229  // expression '/ Copy \\([0-9]+)\\)$/'.
230  $nthCopyRegex = preg_replace('/([\^$.\[\]|()?*+{}])/', '\\\\${1}', ' '
231  . $lng->txt('copy_n_of_suffix'));
232  $nthCopyRegex = '/' . preg_replace('/%1\\\\\$s/', '([0-9]+)', $nthCopyRegex) . '$/';
233 
234  // Get the filename without any previously added number of copy.
235  // Determine the number of copy, if it has not been specified.
236  if (preg_match($nthCopyRegex, $filenameWithoutExtension, $matches)) {
237  // this is going to be at least the third copy of the filename
238  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen($matches[0]));
239  if ($nth_copy == null) {
240  $nth_copy = $matches[1] + 1;
241  }
242  } else {
243  if (substr($filenameWithoutExtension, -strlen(' ' . $lng->txt('copy_of_suffix')))
244  == ' ' . $lng->txt('copy_of_suffix')
245  ) {
246  // this is going to be the second copy of the filename
247  $filenameWithoutCopy = substr($filenameWithoutExtension, 0, -strlen(' '
248  . $lng->txt('copy_of_suffix')));
249  if ($nth_copy == null) {
250  $nth_copy = 2;
251  }
252  } else {
253  // this is going to be the first copy of the filename
254  $filenameWithoutCopy = $filenameWithoutExtension;
255  if ($nth_copy == null) {
256  $nth_copy = 1;
257  }
258  }
259  }
260 
261  // Construct the new filename
262  if ($nth_copy > 1) {
263  // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
264  $newFilename = $filenameWithoutCopy . sprintf(' '
265  . $lng->txt('copy_n_of_suffix'), $nth_copy)
266  . $extension;
267  } else {
268  // this is the first copy of the filename, append " - Copy"
269  $newFilename = $filenameWithoutCopy . ' ' . $lng->txt('copy_of_suffix') . $extension;
270  }
271 
272  return $newFilename;
273  }
274 
278  public static function _getPermanentDownloadLink(int $ref_id): string
279  {
280  return ilLink::_getStaticLink($ref_id, "file", true, "_download");
281  }
282 
287  public static function _preloadData(array $obj_ids, array $ref_ids): void
288  {
289  $info = new ilObjFileInfoRepository();
290  $info->preloadData($obj_ids);
291  }
292 
293 }
$type
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.
$lng
static array $preload_list_gui_data
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Access class for file objects.
static _isFileHidden(string $a_file_name)
Returns true, if a file with the specified name, is usually hidden from the user. ...
static _getCommands()
get commands this method returns an array of all possible commands/permission combinations example: $...
static _lookupFileSize(int $a_id, bool $by_reference=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
static _preloadData(array $obj_ids, array $ref_ids)
static hasAccess(int $a_node_id, bool $a_is_portfolio=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static array $inline_file_extensions
Contains an array of extensions separated by space.
static _getPermanentDownloadLink(int $ref_id)
Gets the permanent download link for the file.
static _getFileExtension(string $a_file_name)
Gets the file extension of the specified file name.
canBeDelivered(ilWACPath $ilWACPath)
static _checkGoto(string $a_target)
check whether goto script will succeed