Elgg  Version 3.0
filestore.php
Go to the documentation of this file.
1 <?php
11 
20 function get_dir_size($dir, $total_size = 0) {
21  if (!is_dir($dir)) {
22  return $total_size;
23  }
24 
25  $handle = opendir($dir);
26  while (($file = readdir($handle)) !== false) {
27  if (in_array($file, ['.', '..'])) {
28  continue;
29  }
30  if (is_dir($dir . $file)) {
31  $total_size = get_dir_size($dir . $file . "/", $total_size);
32  } else {
33  $total_size += filesize($dir . $file);
34  }
35  }
36  closedir($handle);
37 
38  return($total_size);
39 }
40 
66 function elgg_save_resized_image($source, $destination = null, array $params = []) {
67  return _elgg_services()->imageService->resize($source, $destination, $params);
68 }
69 
77 function delete_directory($directory) {
78 
79  if (!file_exists($directory)) {
80  return true;
81  }
82 
83  if (!is_dir($directory)) {
84  return false;
85  }
86 
87  // sanity check: must be a directory
88  if (!$handle = opendir($directory)) {
89  return false;
90  }
91 
92  // loop through all files
93  while (($file = readdir($handle)) !== false) {
94  if (in_array($file, ['.', '..'])) {
95  continue;
96  }
97 
98  $path = "$directory/$file";
99  if (is_dir($path)) {
100  // recurse down through directory
101  if (!delete_directory($path)) {
102  return false;
103  }
104  } else {
105  // delete file
106  unlink($path);
107  }
108  }
109 
110  // remove empty directory
111  closedir($handle);
112  return rmdir($directory);
113 }
114 
123 function elgg_get_file_simple_type($mime_type) {
124  $params = ['mime_type' => $mime_type];
125  return elgg_trigger_plugin_hook('simple_type', 'file', $params, 'general');
126 }
127 
135 
136  // Fix MIME type detection for Microsoft zipped formats
137  elgg_register_plugin_hook_handler('mime_type', 'file', '_elgg_filestore_detect_mimetype');
138 
139  // Parse category of file from MIME type
140  elgg_register_plugin_hook_handler('simple_type', 'file', '_elgg_filestore_parse_simpletype');
141 
142  // Unit testing
143  elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_filestore_test');
144 
145  // Touch entity icons if entity access id has changed
146  elgg_register_event_handler('update:after', 'object', '_elgg_filestore_touch_icons');
147  elgg_register_event_handler('update:after', 'group', '_elgg_filestore_touch_icons');
148 
149  // Move entity icons if entity owner has changed
150  elgg_register_event_handler('update:after', 'object', '_elgg_filestore_move_icons');
151  elgg_register_event_handler('update:after', 'group', '_elgg_filestore_move_icons');
152 }
153 
165 function _elgg_filestore_detect_mimetype($hook, $type, $mime_type, $params) {
166 
167  $original_filename = elgg_extract('original_filename', $params);
168  $ext = pathinfo($original_filename, PATHINFO_EXTENSION);
169 
170  return (new \Elgg\Filesystem\MimeTypeDetector())->fixDetectionErrors($mime_type, $ext);
171 }
172 
184 function _elgg_filestore_parse_simpletype($hook, $type, $simple_type, $params) {
185 
186  $mime_type = elgg_extract('mime_type', $params);
187 
188  switch ($mime_type) {
189  case "application/msword":
190  case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
191  case "application/pdf":
192  return "document";
193 
194  case "application/ogg":
195  return "audio";
196  }
197 
198  if (preg_match('~^(audio|image|video)/~', $mime_type, $m)) {
199  return $m[1];
200  }
201  if (0 === strpos($mime_type, 'text/') || false !== strpos($mime_type, 'opendocument')) {
202  return "document";
203  }
204 
205  // unrecognized MIME
206  return $simple_type;
207 }
208 
220 function _elgg_filestore_test($hook, $type, $value) {
222  return $value;
223 }
224 
235 function elgg_get_download_url(\ElggFile $file, $use_cookie = true, $expires = '+2 hours') {
236  return $file->getDownloadURL($use_cookie, $expires);
237 }
238 
250 function elgg_get_inline_url(\ElggFile $file, $use_cookie = false, $expires = '') {
251  return $file->getInlineURL($use_cookie, $expires);
252 }
253 
267  return elgg_normalize_url(elgg_generate_url('serve-icon', [
268  'guid' => $entity->guid,
269  'size' => $size,
270  ]));
271 }
272 
282  $response = _elgg_services()->iconService->handleServeIconRequest();
283  $response->send();
284  exit;
285 }
286 
297  $original_attributes = $entity->getOriginalAttributes();
298  if (!array_key_exists('access_id', $original_attributes)) {
299  return;
300  }
301  if ($entity instanceof \ElggFile) {
302  // we touch the file to invalidate any previously generated download URLs
303  $entity->setModifiedTime();
304  }
305  $sizes = array_keys(elgg_get_icon_sizes($entity->getType(), $entity->getSubtype()));
306  foreach ($sizes as $size) {
307  $icon = $entity->getIcon($size);
308  if ($icon->exists()) {
309  $icon->setModifiedTime();
310  }
311  }
312 }
313 
331 
332  $original_attributes = $entity->getOriginalAttributes();
333  if (empty($original_attributes['owner_guid'])) {
334  return;
335  }
336 
337  $previous_owner_guid = $original_attributes['owner_guid'];
338  $new_owner_guid = $entity->owner_guid;
339 
340  $sizes = elgg_get_icon_sizes($entity->getType(), $entity->getSubtype());
341 
342  foreach ($sizes as $size => $opts) {
343  $new_icon = $entity->getIcon($size);
344  if ($new_icon->owner_guid == $entity->guid) {
345  // we do not need to update icons that are owned by the entity itself
346  continue;
347  }
348 
349  if ($new_icon->owner_guid != $new_owner_guid) {
350  // a plugin implements some custom logic
351  continue;
352  }
353 
354  $old_icon = new \ElggIcon();
355  $old_icon->owner_guid = $previous_owner_guid;
356  $old_icon->setFilename($new_icon->getFilename());
357  if (!$old_icon->exists()) {
358  // there is no icon to move
359  continue;
360  }
361 
362  if ($new_icon->exists()) {
363  // there is already a new icon
364  // just removing the old one
365  $old_icon->delete();
366  elgg_log("Entity $entity->guid has been transferred to a new owner but an icon was "
367  . "left behind under {$old_icon->getFilenameOnFilestore()}. "
368  . "Old icon has been deleted", 'NOTICE');
369  continue;
370  }
371 
372  $old_icon->transfer($new_icon->owner_guid, $new_icon->getFilename());
373  elgg_log("Entity $entity->guid has been transferred to a new owner. "
374  . "Icon was moved from {$old_icon->getFilenameOnFilestore()} to {$new_icon->getFilenameOnFilestore()}.", 'NOTICE');
375  }
376 }
377 
385  return _elgg_services()->uploads->getFiles($input_name);
386 }
387 
396 function elgg_get_uploaded_file($input_name, $check_for_validity = true) {
397  return _elgg_services()->uploads->getFile($input_name, $check_for_validity);
398 }
399 
406 function elgg_get_temp_file() {
407  return new ElggTempFile();
408 }
409 
413 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
414  $events->registerHandler('init', 'system', '_elgg_filestore_init', 100);
415 };
_elgg_filestore_serve_icon_handler()
Handler for /serve-icon resources /serve-icon/<entity_guid>/<size>
Definition: filestore.php:281
if(!array_key_exists($filename, $text_files)) $file
$source
$params
Saves global plugin settings.
Definition: save.php:13
This class represents a physical file (by default in the system temp directory).
elgg_normalize_url($url)
Definition: output.php:186
elgg_get_icon_sizes($entity_type=null, $entity_subtype=null, $type= 'icon')
Returns a configuration array of icon sizes.
elgg_get_download_url(\ElggFile $file, $use_cookie=true, $expires= '+2 hours')
Returns file&#39;s download URL.
Definition: filestore.php:235
$size
Definition: remove.php:24
elgg_get_embed_url(\ElggEntity $entity, $size)
Returns a URL suitable for embedding entity&#39;s icon in a text editor.
Definition: filestore.php:266
$path
Definition: details.php:89
get_dir_size($dir, $total_size=0)
Get the size of the specified directory.
Definition: filestore.php:20
Events service.
_elgg_filestore_parse_simpletype($hook, $type, $simple_type, $params)
Parse a file category of file from a MIME type.
Definition: filestore.php:184
$type
Definition: delete.php:21
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
elgg_get_uploaded_files($input_name)
Returns an array of uploaded file objects regardless of upload status/errors.
Definition: filestore.php:384
if(!$owner) $icon
Definition: default.php:16
elgg_get_inline_url(\ElggFile $file, $use_cookie=false, $expires= '')
Returns file&#39;s URL for inline display Suitable for displaying cacheable resources, such as user avatars.
Definition: filestore.php:250
elgg_get_uploaded_file($input_name, $check_for_validity=true)
Returns a single valid uploaded file object.
Definition: filestore.php:396
Configuration exception.
Base class for events and hooks.
if(!$entity instanceof ElggEntity) $input_name
Definition: default.php:14
$entity
Definition: reset.php:8
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
elgg_get_temp_file()
Returns a ElggTempFile which can handle writing/reading of data to a temporary file location...
Definition: filestore.php:406
elgg_generate_url($name, array $parameters=[])
Generate a URL for named route.
Definition: pagehandler.php:58
getDownloadURL($use_cookie=true, $expires= '+2 hours')
Returns file&#39;s download URL.
Definition: ElggFile.php:498
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
_elgg_filestore_move_icons($event, $type, $entity)
Listen to entity ownership changes and update icon ownership by moving icons to their new owner&#39;s dir...
Definition: filestore.php:330
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
$value
Definition: debugging.php:7
getInlineURL($use_cookie=false, $expires= '')
Returns file&#39;s URL for inline display Suitable for displaying cacheable resources, such as user avatars.
Definition: ElggFile.php:525
_elgg_filestore_detect_mimetype($hook, $type, $mime_type, $params)
Fix MIME type detection for Microsoft zipped formats.
Definition: filestore.php:165
elgg_save_resized_image($source, $destination=null, array $params=[])
Crops and resizes an image.
Definition: filestore.php:66
class
Definition: placeholder.php:21
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
delete_directory($directory)
Delete a directory and all its contents.
Definition: filestore.php:77
_elgg_filestore_init()
Register file-related handlers on "init, system" event.
Definition: filestore.php:134
_elgg_filestore_test($hook, $type, $value)
Unit tests for files.
Definition: filestore.php:220
elgg_get_file_simple_type($mime_type)
Returns the category of a file from its MIME type.
Definition: filestore.php:123
_elgg_filestore_touch_icons($event, $type, $entity)
Reset icon URLs if access_id has changed.
Definition: filestore.php:296