Elgg  Version 3.0
ElggDiskFilestore.php
Go to the documentation of this file.
1 <?php
2 
13 
17  protected $dir_root;
18 
23  const BUCKET_SIZE = 5000;
24 
30  public function __construct($directory_root = "") {
31  if ($directory_root) {
32  $this->dir_root = $directory_root;
33  } else {
34  $this->dir_root = _elgg_config()->dataroot;
35  }
36  }
37 
51  public function open(\ElggFile $file, $mode) {
52  $fullname = $this->getFilenameOnFilestore($file);
53 
54  // Split into path and name
55  $ls = strrpos($fullname, "/");
56  if ($ls === false) {
57  $ls = 0;
58  }
59 
60  $path = substr($fullname, 0, $ls);
61 
62  if (($mode === 'read') && (!file_exists($fullname))) {
63  return false;
64  }
65 
66  // Try to create the dir for valid write modes
67  if ($mode == 'write' || $mode == 'append') {
68  try {
69  $this->makeDirectoryRoot($path);
70  } catch (Exception $e) {
71  _elgg_services()->logger->warning("Couldn't create directory: $path");
72  return false;
73  }
74  }
75 
76  switch ($mode) {
77  case "read" :
78  $mode = "rb";
79  break;
80  case "write" :
81  $mode = "w+b";
82  break;
83  case "append" :
84  $mode = "a+b";
85  break;
86  default:
87  $msg = "Unrecognized file mode '" . $mode . "'";
88  throw new \InvalidParameterException($msg);
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 
141  public function delete(\ElggFile $file, $follow_symlinks = true) {
142  $filename = $this->getFilenameOnFilestore($file);
143  if (file_exists($filename) || is_link($filename)) {
144  if ($follow_symlinks && is_link($filename) && file_exists($filename)) {
145  $target = readlink($filename);
146  file_exists($target) && unlink($target);
147  }
148  return unlink($filename);
149  } else {
150  return true;
151  }
152  }
153 
162  public function seek($f, $position) {
163  return fseek($f, $position);
164  }
165 
173  public function tell($f) {
174  return ftell($f);
175  }
176 
184  public function eof($f) {
185  return feof($f);
186  }
187 
195  public function getFileSize(\ElggFile $file) {
196  return filesize($this->getFilenameOnFilestore($file));
197  }
198 
209  public function getFilenameOnFilestore(\ElggFile $file) {
210  $owner_guid = $file->getOwnerGuid();
211  if (!$owner_guid) {
212  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
213  }
214 
215  if (!$owner_guid) {
216  $msg = "File " . $file->getFilename() . " (file guid:" . $file->guid . ") is missing an owner!";
217  throw new \InvalidParameterException($msg);
218  }
219 
220  $filename = $file->getFilename();
221  if (!$filename) {
222  return '';
223  }
224 
225  $dir = new \Elgg\EntityDirLocator($owner_guid);
226 
227  return $this->dir_root . $dir . $file->getFilename();
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  return file_exists($this->getFilenameOnFilestore($file));
253  }
254 
263  public function getSize($prefix, $container_guid) {
264  if ($container_guid) {
265  $dir = new \Elgg\EntityDirLocator($container_guid);
266  return get_dir_size($this->dir_root . $dir . $prefix);
267  }
268 
269  return false;
270  }
271 
280  protected function makeDirectoryRoot($dirroot) {
281  if (!file_exists($dirroot)) {
282  if (!@mkdir($dirroot, 0755, true)) {
283  throw new \IOException("Could not make " . $dirroot);
284  }
285  }
286 
287  return true;
288  }
289 
296  public function getParameters() {
297  return ["dir_root" => $this->dir_root];
298  }
299 
307  public function setParameters(array $parameters) {
308  if (isset($parameters['dir_root'])) {
309  $this->dir_root = $parameters['dir_root'];
310  return true;
311  }
312 
313  return false;
314  }
315 }
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(!array_key_exists($filename, $text_files)) $file
$mode
Configure site maintenance mode.
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.
getFileSize(\ElggFile $file)
Returns the file size of an file.
$path
Definition: details.php:89
get_dir_size($dir, $total_size=0)
Get the size of the specified directory.
Definition: filestore.php:20
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:57
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:77
makeDirectoryRoot($dirroot)
Create a directory $dirroot.
$owner_guid
write($f, $data)
Write data to a file.
getFilename()
Return the filename.
Definition: ElggFile.php:67
if(!($comment instanceof\ElggComment)||!$comment->canEdit()) $target
Definition: edit.php:17
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.
_elgg_config()
Get the Elgg config service.
$filename
exists(\ElggFile $file)
Tests if an file exists.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
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...