ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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  #[\Override]
58  public function canBeDelivered(ilWACPath $ilWACPath): bool
59  {
60  return false;
61  }
62 
74  #[\Override]
75  public static function _getCommands(): array
76  {
77  return [
78  [
79  "permission" => Permissions::READ->value,
80  "cmd" => Capabilities::DOWNLOAD->value,
81  "lang_var" => "download",
82  // "default" => true, // we decide the best matching capability later in ListGUI
83  ],
84  [
85  "permission" => Permissions::VISIBLE->value,
86  "cmd" => Capabilities::INFO_PAGE->value,
87  "lang_var" => "info",
88  ],
89  [
90  "permission" => Permissions::VISIBLE->value,
91  "cmd" => Capabilities::FORCED_INFO_PAGE->value,
92  "lang_var" => "info",
93  ],
94  [
95  "permission" => Permissions::WRITE->value,
96  "cmd" => Capabilities::UNZIP->value,
97  "lang_var" => "unzip",
98  ],
99  [
100  "permission" => Permissions::EDIT_CONTENT->value,
101  "cmd" => Capabilities::EDIT_EXTERNAL->value,
102  "lang_var" => "open_external_editor",
103  ],
104  [
105  "permission" => Permissions::VIEW_CONTENT->value,
106  "cmd" => Capabilities::VIEW_EXTERNAL->value,
107  "lang_var" => "open_external_viewer",
108  ],
109  [
110  "permission" => Permissions::WRITE->value,
111  "cmd" => Capabilities::MANAGE_VERSIONS->value,
112  "lang_var" => "versions",
113  ],
114  [
115  "permission" => Permissions::WRITE->value,
116  "cmd" => Capabilities::EDIT_SETTINGS->value,
117  "lang_var" => "settings"
118  ]
119  ];
120  }
121 
126  #[\Override]
127  public function _checkAccess(string $cmd, string $permission, int $ref_id, int $obj_id, ?int $user_id = null): bool
128  {
129  global $DIC;
130  $ilUser = $DIC['ilUser'];
131  $lng = $DIC['lng'];
132  $rbacsystem = $DIC['rbacsystem'];
133  $ilAccess = $DIC['ilAccess'];
134  if (is_null($user_id)) {
135  $user_id = $ilUser->getId();
136  }
137 
138  switch ($cmd) {
139  case "view":
140  if (!self::_lookupOnline($obj_id)
141  && !$rbacsystem->checkAccessOfUser($user_id, 'write', $ref_id)
142  ) {
143  $ilAccess->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
144 
145  return false;
146  }
147  break;
148  // for permission query feature
149  case Capabilities::INFO_PAGE->value:
150  if (!self::_lookupOnline($obj_id)) {
151  $ilAccess->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
152  } else {
153  $ilAccess->addInfoItem(ilAccessInfo::IL_STATUS_MESSAGE, $lng->txt("online"));
154  }
155  break;
156  }
157  switch ($permission) {
158  case "read":
159  case "visible":
160  if (!self::_lookupOnline($obj_id)
161  && (!$rbacsystem->checkAccessOfUser($user_id, 'write', $ref_id))
162  ) {
163  $ilAccess->addInfoItem(ilAccessInfo::IL_NO_OBJECT_ACCESS, $lng->txt("offline"));
164 
165  return false;
166  }
167  break;
168  }
169 
170  return true;
171  }
172 
176  #[\Override]
177  public static function _checkGoto(string $a_target): bool
178  {
179  global $DIC;
180  $ilAccess = $DIC['ilAccess'];
181 
182  $t_arr = explode("_", $a_target);
183 
184  // personal workspace context: do not force normal login
185  if (isset($t_arr[2]) && $t_arr[2] === "wsp") {
186  return ilSharedResourceGUI::hasAccess($t_arr[1]);
187  }
188  if ($t_arr[0] !== "file") {
189  return false;
190  }
191  if (((int) $t_arr[1]) <= 0) {
192  return false;
193  }
194  if ($ilAccess->checkAccess("visible", "", $t_arr[1])) {
195  return true;
196  }
197  return (bool) $ilAccess->checkAccess("read", "", $t_arr[1]);
198  }
199 
200  public static function _shouldDownloadDirectly(int $obj_id): bool
201  {
202  global $DIC;
203 
204  $result = $DIC->database()->fetchAssoc(
205  $DIC->database()->queryF(
206  "SELECT on_click_mode FROM file_data WHERE file_id = %s;",
207  ['integer'],
208  [$obj_id]
209  )
210  );
211 
212  if (empty($result)) {
213  return false;
214  }
215 
216  return (((int) $result['on_click_mode']) === ilObjFile::CLICK_MODE_DOWNLOAD);
217  }
218 
222  public static function _lookupFileSize(int $a_id, bool $by_reference = true): int
223  {
224  try {
225  $info_repo = new ilObjFileInfoRepository();
226  $info = $by_reference ? $info_repo->getByRefId($a_id) : $info_repo->getByObjectId($a_id);
227 
228  return (int) $info->getFileSize()->inBytes();
229  } catch (Throwable) {
230  return 0;
231  }
232  }
233 
241  public static function _getFileExtension(string $a_file_name): string
242  {
243  if (preg_match('/\.([a-z0-9]+)\z/i', $a_file_name, $matches) == 1) {
244  return strtolower($matches[1]);
245  }
246  return '';
247  }
248 
257  public static function _isFileHidden(string $a_file_name): bool
258  {
259  return str_starts_with($a_file_name, '.') || str_ends_with($a_file_name, '~')
260  || str_starts_with($a_file_name, '~$')
261  || $a_file_name === 'Thumbs.db';
262  }
263 
284  public static function _appendNumberOfCopyToFilename($a_file_name, $nth_copy = null): string
285  {
286  global $DIC;
287  $lng = $DIC['lng'];
288 
289  $filenameWithoutExtension = $a_file_name;
290 
291  $extension = null;
292 
293  // create a regular expression from the language text copy_n_of_suffix, so that
294  // we can match it against $filenameWithoutExtension, and retrieve the number of the copy.
295  // for example, if copy_n_of_suffix is 'Copy (%1s)', this creates the regular
296  // expression '/ Copy \\([0-9]+)\\)$/'.
297  $nthCopyRegex = preg_replace(
298  '/([\^$.\[\]|()?*+{}])/',
299  '\\\\${1}',
300  ' '
301  . $lng->txt('copy_n_of_suffix')
302  );
303  $nthCopyRegex = '/' . preg_replace('/%1\\\\\$s/', '(\d+)', (string) $nthCopyRegex) . '$/';
304 
305  // Get the filename without any previously added number of copy.
306  // Determine the number of copy, if it has not been specified.
307  if (preg_match($nthCopyRegex, (string) $filenameWithoutExtension, $matches)) {
308  // this is going to be at least the third copy of the filename
309  $filenameWithoutCopy = substr((string) $filenameWithoutExtension, 0, -strlen($matches[0]));
310  if ($nth_copy == null) {
311  $nth_copy = $matches[1] + 1;
312  }
313  } elseif (str_ends_with((string) $filenameWithoutExtension, ' ' . $lng->txt('copy_of_suffix'))) {
314  // this is going to be the second copy of the filename
315  $filenameWithoutCopy = substr(
316  (string) $filenameWithoutExtension,
317  0,
318  -strlen(
319  ' '
320  . $lng->txt('copy_of_suffix')
321  )
322  );
323  if ($nth_copy == null) {
324  $nth_copy = 2;
325  }
326  } else {
327  // this is going to be the first copy of the filename
328  $filenameWithoutCopy = $filenameWithoutExtension;
329  if ($nth_copy == null) {
330  $nth_copy = 1;
331  }
332  }
333 
334  // Construct the new filename
335  if ($nth_copy > 1) {
336  // this is at least the second copy of the filename, append " - Copy ($nth_copy)"
337  return $filenameWithoutCopy . sprintf(
338  ' '
339  . $lng->txt('copy_n_of_suffix'),
340  $nth_copy
341  )
342  . $extension;
343  }
344 
345  // this is the first copy of the filename, append " - Copy"
346  return $filenameWithoutCopy . ' ' . $lng->txt('copy_of_suffix') . $extension;
347  }
348 
352  public static function _getPermanentDownloadLink(int $ref_id): string
353  {
354  return ilLink::_getStaticLink($ref_id, "file", true, "download");
355  }
356 
361  public static function _preloadData(array $obj_ids, array $ref_ids): void
362  {
364  $info->preloadData($obj_ids);
365  }
366 
367  public static function _lookupOnline(int $a_obj_id): bool
368  {
369  // TODO: remove this call to LocalDIC, since this is an internal class, see https://mantis.ilias.de/view.php?id=45558
370  return LocalDIC::dic()['properties.aggregator']->getFor($a_obj_id, 'file')->getPropertyIsOnline()->getIsOnline();
371  }
372 }
static array $preload_list_gui_data
static _getAllReferences(int $id)
get all reference ids for object ID
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.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ref_id
Definition: ltiauth.php:65
const CLICK_MODE_DOWNLOAD
static _preloadData(array $obj_ids, array $ref_ids)
global $DIC
Definition: shib_login.php:26
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.
global $lng
Definition: privfeed.php:31
static _getPermanentDownloadLink(int $ref_id)
Gets the permanent download link for the file.
static _lookupOnline(int $a_obj_id)
$info
Definition: entry_point.php:21
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