Elgg  Version 4.x
ElggDiskFilestore.php
Go to the documentation of this file.
1 <?php
2 
6 
14 
18  protected $dir_root;
19 
24  const BUCKET_SIZE = 5000;
25 
31  public function __construct($directory_root = '') {
32  if (!$directory_root) {
33  $directory_root = _elgg_services()->config->dataroot;
34  }
35 
36  $this->dir_root = Paths::sanitize($directory_root);
37  }
38 
52  public function open(\ElggFile $file, $mode) {
53  $fullname = $this->getFilenameOnFilestore($file);
54 
55  // Split into path and name
56  $ls = strrpos($fullname, '/');
57  if ($ls === false) {
58  $ls = 0;
59  }
60 
61  $path = substr($fullname, 0, $ls);
62 
63  if (($mode === 'read') && (!file_exists($fullname))) {
64  return false;
65  }
66 
67  // Try to create the dir for valid write modes
68  if ($mode == 'write' || $mode == 'append') {
69  try {
70  $this->makeDirectoryRoot($path);
71  } catch (Exception $e) {
72  _elgg_services()->logger->warning("Couldn't create directory: {$path}");
73  return false;
74  }
75  }
76 
77  switch ($mode) {
78  case 'read' :
79  $mode = 'rb';
80  break;
81  case 'write' :
82  $mode = 'w+b';
83  break;
84  case 'append' :
85  $mode = 'a+b';
86  break;
87  default:
88  throw new InvalidParameterException("Unrecognized file mode '{$mode}'");
89  }
90 
91  return fopen($fullname, $mode);
92  }
93 
102  public function write($f, $data) {
103  return fwrite($f, $data);
104  }
105 
115  public function read($f, $length, $offset = 0) {
116  if ($offset) {
117  $this->seek($f, $offset);
118  }
119 
120  return fread($f, $length);
121  }
122 
130  public function close($f) {
131  return fclose($f);
132  }
133 
142  public function delete(\ElggFile $file, $follow_symlinks = true) {
143  $filename = $this->getFilenameOnFilestore($file);
144  if (file_exists($filename) || is_link($filename)) {
145  if ($follow_symlinks && is_link($filename) && file_exists($filename)) {
146  $target = readlink($filename);
147  file_exists($target) && unlink($target);
148  }
149  return unlink($filename);
150  }
151 
152  return true;
153  }
154 
163  public function seek($f, $position) {
164  return fseek($f, $position);
165  }
166 
174  public function tell($f) {
175  return ftell($f);
176  }
177 
185  public function eof($f) {
186  return feof($f);
187  }
188 
196  public function getFileSize(\ElggFile $file) {
197  return filesize($this->getFilenameOnFilestore($file));
198  }
199 
210  public function getFilenameOnFilestore(\ElggFile $file) {
211  $owner_guid = $file->getOwnerGuid();
212  if (!$owner_guid) {
213  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
214  }
215 
216  if (!$owner_guid) {
217  throw new InvalidParameterException("File {$file->getFilename()} (file guid: {$file->guid}) is missing an owner!");
218  }
219 
220  $filename = $file->getFilename();
221  if (!$filename) {
222  return '';
223  }
224 
225  $dir = new \Elgg\EntityDirLocator($owner_guid);
226 
227  return Paths::sanitize($this->dir_root . $dir . $file->getFilename(), false);
228  }
229 
237  public function grabFile(\ElggFile $file) {
238  return file_get_contents($file->getFilenameOnFilestore());
239  }
240 
248  public function exists(\ElggFile $file) {
249  if (!$file->getFilename()) {
250  return false;
251  }
252 
253  try {
254  $real_filename = $this->getFilenameOnFilestore($file);
255  } catch (InvalidParameterException $e) {
256  // something wrong with the filename
257  return false;
258  }
259 
260  return file_exists($real_filename);
261  }
262 
271  public function getSize($prefix, $container_guid) {
272  if ($container_guid) {
273  $dir = new \Elgg\EntityDirLocator($container_guid);
274 
275  return get_dir_size(Paths::sanitize($this->dir_root . $dir . $prefix));
276  }
277 
278  return false;
279  }
280 
289  protected function makeDirectoryRoot($dirroot) {
290  if (!file_exists($dirroot)) {
291  if (!@mkdir($dirroot, 0755, true)) {
292  throw new IOException("Could not make {$dirroot}");
293  }
294  }
295 
296  return true;
297  }
298 
305  public function getParameters() {
306  return [
307  'dir_root' => $this->dir_root,
308  ];
309  }
310 
318  public function setParameters(array $parameters) {
319  if (isset($parameters['dir_root'])) {
320  $this->dir_root = Paths::sanitize($parameters['dir_root']);
321  return true;
322  }
323 
324  return false;
325  }
326 }
eof($f)
Tests for end of file on a file pointer.
close($f)
Close a file pointer.
const BUCKET_SIZE
Number of entries per matrix dir.
if(!$comment instanceof\ElggComment||!$comment->canEdit()) $target
Definition: edit.php:13
$mode
Configure site maintenance mode.
An IO Exception, throw when an IO Exception occurs.
Definition: IOException.php:12
getFilenameOnFilestore(\ElggFile $file)
Get the filename as saved on disk for an object.
read($f, $length, $offset=0)
Read data from a file.
if(!$item instanceof ElggEntity) $length
Definition: excerpt.php:16
open(\ElggFile $file, $mode)
Open a file for reading, writing, or both.
A filestore that uses disk as storage.
getFileSize(\ElggFile $file)
Returns the file size of an file.
get_dir_size($dir, $total_size=0)
Get the size of the specified directory.
Definition: filestore.php:17
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
if(!$count) $offset
Definition: pagination.php:26
__construct($directory_root= '')
Construct a disk filestore using the given directory root.
$position
Definition: move.php:7
getFilenameOnFilestore()
Return the filename of this file as it is/will be stored on the filestore, which may be different to ...
Definition: ElggFile.php:111
$path
Definition: details.php:68
makeDirectoryRoot($dirroot)
Create a directory $dirroot.
$owner_guid
write($f, $data)
Write data to a file.
getFilename()
Return the filename.
Definition: ElggFile.php:96
getSize($prefix, $container_guid)
Returns the size of all data stored under a directory in the disk store.
grabFile(\ElggFile $file)
Returns the contents of the file.
setParameters(array $parameters)
Sets parameters that should be saved to database.
This class defines the interface for all elgg data repositories.
$filename
exists(\ElggFile $file)
Tests if an file exists.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
tell($f)
Return the current location of the internal pointer.
seek($f, $position)
Seek to the specified position.
$container_guid
getParameters()
Returns a list of attributes to save to the database when saving the object using this file store...