ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjFileAccess.php
Go to the documentation of this file.
1 <?php
2 
22 
31 {
37  protected static array $inline_file_extensions = [];
38 
39  protected static array $preload_list_gui_data = [];
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  return false;
60  }
61 
73  public static function _getCommands(): array
74  {
75  return [
76  [
77  "permission" => Permissions::READ->value,
78  "cmd" => Capabilities::DOWNLOAD->value,
79  "lang_var" => "download",
80  // "default" => true, // we decide the best matching capability later in ListGUI
81  ],
82  [
83  "permission" => Permissions::VISIBLE->value,
84  "cmd" => Capabilities::INFO_PAGE->value,
85  "lang_var" => "info",
86  ],
87  [
88  "permission" => Permissions::VISIBLE->value,
89  "cmd" => Capabilities::FORCED_INFO_PAGE->value,
90  "lang_var" => "info",
91  ],
92  [
93  "permission" => Permissions::WRITE->value,
94  "cmd" => Capabilities::UNZIP->value,
95  "lang_var" => "unzip",
96  ],
97  [
98  "permission" => Permissions::EDIT_CONTENT->value,
99  "cmd" => Capabilities::EDIT_EXTERNAL->value,
100  "lang_var" => "open_external_editor",
101  ],
102  [
103  "permission" => Permissions::VIEW_CONTENT->value,
104  "cmd" => Capabilities::VIEW_EXTERNAL->value,
105  "lang_var" => "open_external_viewer",
106  ],
107  [
108  "permission" => Permissions::WRITE->value,
109  "cmd" => Capabilities::MANAGE_VERSIONS->value,
110  "lang_var" => "versions",
111  ],
112  [
113  "permission" => Permissions::WRITE->value,
114  "cmd" => Capabilities::EDIT_SETTINGS->value,
115  "lang_var" => "settings"
116  ]
117  ];
118  }
119 
124  public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
125  {
126  global $DIC;
127  $ilUser = $DIC['ilUser'];
128  $lng = $DIC['lng'];
129  $rbacsystem = $DIC['rbacsystem'];
130  $ilAccess = $DIC['ilAccess'];
131  if (is_null($user_id)) {
132  $user_id = $ilUser->getId();
133  }
134 
135  switch ($cmd) {
136  case "view":
137  if (!self::_lookupOnline($obj_id)
138  && !$rbacsystem->checkAccessOfUser($user_id, 'write', $ref_id)
139  ) {
140  $ilAccess->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
141 
142  return false;
143  }
144  break;
145  // for permission query feature
146  case Capabilities::INFO_PAGE->value:
147  if (!self::_lookupOnline($obj_id)) {
148  $ilAccess->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
149  } else {
150  $ilAccess->addInfoItem(ilAccessInfo::IL_STATUS_MESSAGE, $lng->txt("online"));
151  }
152  break;
153  }
154  switch ($permission) {
155  case "read":
156  case "visible":
157  if (!self::_lookupOnline($obj_id)
158  && (!$rbacsystem->checkAccessOfUser($user_id, 'write', $ref_id))
159  ) {
160  $ilAccess->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
161 
162  return false;
163  }
164  break;
165  }
166 
167  return true;
168  }
169 
173  public static function _checkGoto(string $a_target): bool
174  {
175  global $DIC;
176  $ilAccess = $DIC['ilAccess'];
177 
178  $t_arr = explode("_", $a_target);
179 
180  // personal workspace context: do not force normal login
181  if (isset($t_arr[2]) && $t_arr[2] === "wsp") {
182  return ilSharedResourceGUI::hasAccess($t_arr[1]);
183  }
184  if ($t_arr[0] !== "file") {
185  return false;
186  }
187  if (((int) $t_arr[1]) <= 0) {
188  return false;
189  }
190  if ($ilAccess->checkAccess("visible", "", $t_arr[1])) {
191  return true;
192  }
193  return (bool) $ilAccess->checkAccess("read", "", $t_arr[1]);
194  }
195 
196  public static function _shouldDownloadDirectly(int $obj_id): bool
197  {
198  global $DIC;
199 
200  $result = $DIC->database()->fetchAssoc(
201  $DIC->database()->queryF(
202  "SELECT on_click_mode FROM file_data WHERE file_id = %s;",
203  ['integer'],
204  [$obj_id]
205  )
206  );
207 
208  if (empty($result)) {
209  return false;
210  }
211 
212  return (((int) $result['on_click_mode']) === ilObjFile::CLICK_MODE_DOWNLOAD);
213  }
214 
218  public static function _lookupFileSize(int $a_id, bool $by_reference = true): int
219  {
220  try {
221  $info_repo = new ilObjFileInfoRepository();
222  $info = $by_reference ? $info_repo->getByRefId($a_id) : $info_repo->getByObjectId($a_id);
223 
224  return (int) $info->getFileSize()->inBytes();
225  } catch (Throwable) {
226  return 0;
227  }
228  }
229 
237  public static function _getFileExtension(string $a_file_name): string
238  {
239  if (preg_match('/\.([a-z0-9]+)\z/i', $a_file_name, $matches) == 1) {
240  return strtolower($matches[1]);
241  }
242  return '';
243  }
244 
253  public static function _isFileHidden(string $a_file_name): bool
254  {
255  return str_starts_with($a_file_name, '.') || str_ends_with($a_file_name, '~')
256  || str_starts_with($a_file_name, '~$')
257  || $a_file_name === 'Thumbs.db';
258  }
259 
280  public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null): string
281  {
282  global $DIC;
283  $lng = $DIC['lng'];
284 
285  $filenameWithoutExtension = $a_file_name;
286 
287  $extension = null;
288 
289  // create a regular expression from the language text copy_n_of_suffix, so that
290  // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
291  // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
292  // expression '/ Copy \\([0-9]+)\\)$/'.
293  $nthCopyRegex = preg_replace(
294  '/([\^$.\[\]|()?*+{}])/',
295  '\\\\${1}',
296  ' '
297  . $lng->txt('copy_n_of_suffix')
298  );
299  $nthCopyRegex = '/' . preg_replace('/%1\\\\\$s/', '(\d+)', $nthCopyRegex) . '$/';
300 
301  // Get the filename without any previously added number of copy.
302  // Determine the number of copy, if it has not been specified.
303  if (preg_match($nthCopyRegex, (string) $filenameWithoutExtension, $matches)) {
304  // this is going to be at least the third copy of the filename
305  $filenameWithoutCopy = substr((string) $filenameWithoutExtension, 0, -strlen($matches[0]));
306  if ($nth_copy == null) {
307  $nth_copy = $matches[1] + 1;
308  }
309  } elseif (str_ends_with((string) $filenameWithoutExtension, ' ' . $lng->txt('copy_of_suffix'))) {
310  // this is going to be the second copy of the filename
311  $filenameWithoutCopy = substr(
312  (string) $filenameWithoutExtension,
313  0,
314  -strlen(
315  ' '
316  . $lng->txt('copy_of_suffix')
317  )
318  );
319  if ($nth_copy == null) {
320  $nth_copy = 2;
321  }
322  } else {
323  // this is going to be the first copy of the filename
324  $filenameWithoutCopy = $filenameWithoutExtension;
325  if ($nth_copy == null) {
326  $nth_copy = 1;
327  }
328  }
329 
330  // Construct the new filename
331  if ($nth_copy > 1) {
332  // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
333  return $filenameWithoutCopy . sprintf(
334  ' '
335  . $lng->txt('copy_n_of_suffix'),
336  $nth_copy
337  )
338  . $extension;
339  }
340 
341  // this is the first copy of the filename, append " - Copy"
342  return $filenameWithoutCopy . ' ' . $lng->txt('copy_of_suffix') . $extension;
343  }
344 
348  public static function _getPermanentDownloadLink(int $ref_id): string
349  {
350  return ilLink::_getStaticLink($ref_id, "file", true, "download");
351  }
352 
357  public static function _preloadData(array $obj_ids, array $ref_ids): void
358  {
359  $info = new ilObjFileInfoRepository();
360  $info->preloadData($obj_ids);
361  }
362 
363  public static function _lookupOnline(int $a_obj_id): bool
364  {
365  return ilObjectDIC::dic()['object_properties_agregator']->getFor($a_obj_id, 'file')->getPropertyIsOnline()->getIsOnline();
366  }
367 }
static array $preload_list_gui_data
static _getAllReferences(int $id)
get all reference ids for object ID
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)
_checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id=null)
checks whether a user may invoke a command or not (this method is called by ilAccessHandler::checkAcc...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _appendNumberOfCopyToFilename($a_file_name, $nth_copy=null)
Appends the text " - Copy" to a filename in the language of the current user.
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
const CLICK_MODE_DOWNLOAD
$lng
static _preloadData(array $obj_ids, array $ref_ids)
static hasAccess(int $a_node_id, bool $a_is_portfolio=false)
static _shouldDownloadDirectly(int $obj_id)
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 _lookupOnline(int $a_obj_id)
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