Elgg  Version 2.3
ElggDiskFilestore.php
Go to the documentation of this file.
1 <?php
15  private $dir_root;
16 
21  const BUCKET_SIZE = 5000;
22 
28  public function __construct($directory_root = "") {
29  if ($directory_root) {
30  $this->dir_root = $directory_root;
31  } else {
32  $this->dir_root = _elgg_services()->config->getDataPath();
33  }
34  }
35 
50  public function open(\ElggFile $file, $mode) {
51  $fullname = $this->getFilenameOnFilestore($file);
52 
53  // Split into path and name
54  $ls = strrpos($fullname, "/");
55  if ($ls === false) {
56  $ls = 0;
57  }
58 
59  $path = substr($fullname, 0, $ls);
60 
61  if (($mode === 'read') && (!file_exists($fullname))) {
62  return false;
63  }
64 
65  // Try to create the dir for valid write modes
66  if ($mode == 'write' || $mode == 'append') {
67  try {
68  $this->makeDirectoryRoot($path);
69  } catch (Exception $e) {
70  _elgg_services()->logger->warn("Couldn't create directory: $path");
71  return false;
72  }
73  }
74 
75  switch ($mode) {
76  case "read" :
77  $mode = "rb";
78  break;
79  case "write" :
80  $mode = "w+b";
81  break;
82  case "append" :
83  $mode = "a+b";
84  break;
85  default:
86  $msg = "Unrecognized file mode '" . $mode . "'";
87  throw new \InvalidParameterException($msg);
88  }
89 
90  return fopen($fullname, $mode);
91 
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  } else {
268  return false;
269  }
270  }
271 
280  protected function makeDirectoryRoot($dirroot) {
281  if (!file_exists($dirroot)) {
282  if (!@mkdir($dirroot, 0700, true)) {
283  throw new \IOException("Could not make " . $dirroot);
284  }
285  }
286 
287  return true;
288  }
289 
296  public function getParameters() {
297  return array("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 
316 
317 
330  protected function makeFileMatrix($guid) {
331  elgg_deprecated_notice('\ElggDiskFilestore::makeFileMatrix() is deprecated by \Elgg\EntityDirLocator', 1.9);
333 
334  if (!$entity instanceof \ElggEntity) {
335  return false;
336  }
337 
338  $dir = new \Elgg\EntityDirLocator($guid);
339  return $dir->getPath();
340  }
341 }
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.
$e
Definition: metadata.php:12
open(\ElggFile $file, $mode)
Open a file for reading, writing, or both.
getFileSize(\ElggFile $file)
Returns the file size of an file.
$data
Definition: opendd.php:13
$path
Definition: details.php:88
get_dir_size($dir, $total_size=0)
Get the size of the specified directory.
Definition: filestore.php:20
if(!$count) $offset
Definition: pagination.php:26
$guid
Removes an admin notice.
__construct($directory_root="")
Construct a disk filestore using the given directory root.
$length
Definition: excerpt.php:14
makeFileMatrix($guid)
Deprecated methods.
$position
Definition: move.php:10
getFilenameOnFilestore()
Return the filename of this file as it is/will be stored on the filestore, which may be different to ...
Definition: ElggFile.php:101
makeDirectoryRoot($dirroot)
Create a directory $dirroot.
$owner_guid
write($f, $data)
Write data to a file.
if(!($comment instanceof\ElggComment)||!$comment->canEdit()) $target
Definition: edit.php:17
getFilename()
Return the filename.
Definition: ElggFile.php:91
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1098
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.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
setParameters(array $parameters)
Sets parameters that should be saved to database.
$entity
Definition: delete.php:7
$filename
exists(\ElggFile $file)
Tests if an file exists.
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...
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204