Elgg  Version 3.0
File.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\FileService;
4 
6 
12 class File {
13 
14  const INLINE = 'inline';
15  const ATTACHMENT = 'attachment';
16 
20  private $file;
21 
25  private $expires;
26 
30  private $disposition;
31 
35  private $use_cookie = true;
36 
43  public function setFile(\ElggFile $file) {
44  $this->file = $file;
45  }
46 
51  public function getFile() {
52  return $this->file;
53  }
54 
62  public function setExpires($expires = '+2 hours') {
63  $this->expires = strtotime($expires);
64  }
65 
72  public function setDisposition($disposition = self::ATTACHMENT) {
73  if (!in_array($disposition, [self::ATTACHMENT, self::INLINE])) {
74  throw new \InvalidArgumentException("Disposition $disposition is not supported in " . __CLASS__);
75  }
76  $this->disposition = $disposition;
77  }
78 
85  public function bindSession($use_cookie = true) {
86  $this->use_cookie = $use_cookie;
87  }
88 
93  public function getURL() {
94 
95  if (!$this->file->exists()) {
96  elgg_log("Unable to resolve resource URL for a file that does not exist on filestore");
97  return false;
98  }
99 
100  $relative_path = '';
101  $root_prefix = _elgg_config()->dataroot;
102  $path = $this->file->getFilenameOnFilestore();
103  if (substr($path, 0, strlen($root_prefix)) == $root_prefix) {
104  $relative_path = substr($path, strlen($root_prefix));
105  }
106 
107  if (!$relative_path) {
108  elgg_log("Unable to resolve relative path of the file on the filestore");
109  return false;
110  }
111 
112  if (preg_match('~[^a-zA-Z0-9_\./ ]~', $relative_path)) {
113  // Filenames may contain special characters that result in malformatted URLs
114  // and/or HMAC mismatches. We want to avoid that by encoding the path.
115  $relative_path = ':' . Base64Url::encode($relative_path);
116  }
117 
118  $data = [
119  'expires' => isset($this->expires) ? $this->expires : 0,
120  'last_updated' => filemtime($this->file->getFilenameOnFilestore()),
121  'disposition' => $this->disposition == self::INLINE ? 'i' : 'a',
122  'path' => $relative_path,
123  ];
124 
125  if ($this->use_cookie) {
126  $data['cookie'] = _elgg_services()->session->getId();
127  if (empty($data['cookie'])) {
128  return false;
129  }
130  $data['use_cookie'] = 1;
131  } else {
132  $data['use_cookie'] = 0;
133  }
134 
135  ksort($data);
136  $mac = _elgg_services()->hmac->getHmac($data)->getToken();
137 
138  $url_segments = [
139  'serve-file',
140  "e{$data['expires']}",
141  "l{$data['last_updated']}",
142  "d{$data['disposition']}",
143  "c{$data['use_cookie']}",
144  $mac,
145  $relative_path,
146  ];
147 
148  return elgg_normalize_url(implode('/', $url_segments));
149  }
150 }
bindSession($use_cookie=true)
Bind URL to current user session.
Definition: File.php:85
File service.
Definition: File.php:12
elgg_normalize_url($url)
Definition: output.php:186
$path
Definition: details.php:89
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
getFile()
Returns file object.
Definition: File.php:51
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:786
setFile(\ElggFile $file)
Set file object.
Definition: File.php:43
_elgg_config()
Get the Elgg config service.
setExpires($expires= '+2 hours')
Sets URL expiration.
Definition: File.php:62
setDisposition($disposition=self::ATTACHMENT)
Sets content disposition.
Definition: File.php:72
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
getURL()
Returns publicly accessible URL.
Definition: File.php:93
static encode($bytes)
Encode base 64 URL.
Definition: Base64Url.php:18