Elgg  Version master
File.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\FileService;
4 
7 
13 class File {
14 
15  const INLINE = 'inline';
16  const ATTACHMENT = 'attachment';
17 
21  private $file;
22 
26  private $expires;
27 
31  private $disposition;
32 
36  private $use_cookie = true;
37 
44  public function setFile(\ElggFile $file) {
45  $this->file = $file;
46  }
47 
53  public function getFile(): ?\ElggFile {
54  return $this->file;
55  }
56 
64  public function setExpires(string $expires = '+2 hours'): void {
65  $this->expires = strtotime($expires);
66  }
67 
76  public function setDisposition(string $disposition = self::ATTACHMENT): void {
77  if (!in_array($disposition, [self::ATTACHMENT, self::INLINE])) {
78  throw new DomainException("Disposition {$disposition} is not supported in " . __CLASS__);
79  }
80 
81  $this->disposition = $disposition;
82  }
83 
91  public function bindSession(bool $use_cookie = true): void {
92  $this->use_cookie = $use_cookie;
93  }
94 
100  public function getURL(): ?string {
101 
102  if (!$this->file->exists()) {
103  elgg_log('Unable to resolve resource URL for a file that does not exist on filestore');
104  return null;
105  }
106 
107  $relative_path = '';
108  $root_prefix = _elgg_services()->config->dataroot;
109  $path = $this->file->getFilenameOnFilestore();
110  if (str_starts_with($path, $root_prefix)) {
111  $relative_path = substr($path, strlen($root_prefix));
112  }
113 
114  if (!$relative_path) {
115  elgg_log('Unable to resolve relative path of the file on the filestore');
116  return null;
117  }
118 
119  if (preg_match('~[^a-zA-Z0-9_\./ ]~', $relative_path)) {
120  // Filenames may contain special characters that result in malformatted URLs
121  // and/or HMAC mismatches. We want to avoid that by encoding the path.
122  $relative_path = ':' . Base64Url::encode($relative_path);
123  }
124 
125  $data = [
126  'expires' => $this->expires ?? 0,
127  'last_updated' => filemtime($this->file->getFilenameOnFilestore()),
128  'disposition' => $this->disposition === self::INLINE ? 'i' : 'a',
129  'path' => $relative_path,
130  ];
131 
132  if ($this->use_cookie) {
133  $data['cookie'] = _elgg_services()->session->getID();
134  if (empty($data['cookie'])) {
135  return null;
136  }
137 
138  $data['use_cookie'] = 1;
139  } else {
140  $data['use_cookie'] = 0;
141  }
142 
143  ksort($data);
144  $mac = _elgg_services()->hmac->getHmac($data)->getToken();
145 
146  $url_segments = [
147  'serve-file',
148  "e{$data['expires']}",
149  "l{$data['last_updated']}",
150  "d{$data['disposition']}",
151  "c{$data['use_cookie']}",
152  $mac,
153  $relative_path,
154  ];
155 
156  return elgg_normalize_url(implode('/', $url_segments));
157  }
158 }
setDisposition(string $disposition=self::ATTACHMENT)
Sets content disposition.
Definition: File.php:76
bindSession(bool $use_cookie=true)
Bind URL to current user session.
Definition: File.php:91
setExpires(string $expires= '+2 hours')
Sets URL expiration.
Definition: File.php:64
File service.
Definition: File.php:13
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
Exception thrown if a value does not adhere to a defined valid data domain.
$path
Definition: details.php:70
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
getFile()
Returns file object.
Definition: File.php:53
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
setFile(\ElggFile $file)
Set file object.
Definition: File.php:44
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
elgg_normalize_url(string $url)
Definition: output.php:163
getURL()
Returns publicly accessible URL.
Definition: File.php:100
static encode($bytes)
Encode base 64 URL.
Definition: Base64Url.php:18