Elgg  Version 1.11
ElggDiskFilestore.php
Go to the documentation of this file.
1 <?php
15  private $dir_root;
16 
21  const BUCKET_SIZE = 5000;
22 
28  private $CONFIG;
29 
35  public function __construct($directory_root = "") {
36  global $CONFIG;
37  $this->CONFIG = $CONFIG;
38 
39  if ($directory_root) {
40  $this->dir_root = $directory_root;
41  } else {
42  $this->dir_root = $this->CONFIG->dataroot;
43  }
44  }
45 
60  public function open(\ElggFile $file, $mode) {
61  $fullname = $this->getFilenameOnFilestore($file);
62 
63  // Split into path and name
64  $ls = strrpos($fullname, "/");
65  if ($ls === false) {
66  $ls = 0;
67  }
68 
69  $path = substr($fullname, 0, $ls);
70 
71  if (($mode != 'write') && (!file_exists($fullname))) {
72  return false;
73  }
74 
75  // Try to create the dir for valid write modes
76  if ($mode == 'write' || $mode == 'append') {
77  try {
78  $this->makeDirectoryRoot($path);
79  } catch (Exception $e) {
80  _elgg_services()->logger->warn("Couldn't create directory: $path");
81  return false;
82  }
83  }
84 
85  switch ($mode) {
86  case "read" :
87  $mode = "rb";
88  break;
89  case "write" :
90  $mode = "w+b";
91  break;
92  case "append" :
93  $mode = "a+b";
94  break;
95  default:
96  $msg = "Unrecognized file mode '" . $mode . "'";
97  throw new \InvalidParameterException($msg);
98  }
99 
100  return fopen($fullname, $mode);
101 
102  }
103 
112  public function write($f, $data) {
113  return fwrite($f, $data);
114  }
115 
125  public function read($f, $length, $offset = 0) {
126  if ($offset) {
127  $this->seek($f, $offset);
128  }
129 
130  return fread($f, $length);
131  }
132 
140  public function close($f) {
141  return fclose($f);
142  }
143 
151  public function delete(\ElggFile $file) {
152  $filename = $this->getFilenameOnFilestore($file);
153  if (file_exists($filename)) {
154  return unlink($filename);
155  } else {
156  return true;
157  }
158  }
159 
168  public function seek($f, $position) {
169  return fseek($f, $position);
170  }
171 
179  public function tell($f) {
180  return ftell($f);
181  }
182 
190  public function eof($f) {
191  return feof($f);
192  }
193 
201  public function getFileSize(\ElggFile $file) {
202  return filesize($this->getFilenameOnFilestore($file));
203  }
204 
215  public function getFilenameOnFilestore(\ElggFile $file) {
216  $owner_guid = $file->getOwnerGuid();
217  if (!$owner_guid) {
218  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
219  }
220 
221  if (!$owner_guid) {
222  $msg = "File " . $file->getFilename() . " (file guid:" . $file->guid . ") is missing an owner!";
223  throw new \InvalidParameterException($msg);
224  }
225 
226  $filename = $file->getFilename();
227  if (!$filename) {
228  return '';
229  }
230 
231  $dir = new \Elgg\EntityDirLocator($owner_guid);
232 
233  return $this->dir_root . $dir . $file->getFilename();
234  }
235 
243  public function grabFile(\ElggFile $file) {
244  return file_get_contents($file->getFilenameOnFilestore());
245  }
246 
254  public function exists(\ElggFile $file) {
255  if (!$file->getFilename()) {
256  return false;
257  }
258  return file_exists($this->getFilenameOnFilestore($file));
259  }
260 
269  public function getSize($prefix, $container_guid) {
270  if ($container_guid) {
271  $dir = new \Elgg\EntityDirLocator($container_guid);
272  return get_dir_size($this->dir_root . $dir . $prefix);
273  } else {
274  return false;
275  }
276  }
277 
278  // @codingStandardsIgnoreStart
288  protected function make_directory_root($dirroot) {
289  elgg_deprecated_notice('\ElggDiskFilestore::make_directory_root() is deprecated by ::makeDirectoryRoot()', 1.8);
290 
291  return $this->makeDirectoryRoot($dirroot);
292  }
293  // @codingStandardsIgnoreEnd
294 
303  protected function makeDirectoryRoot($dirroot) {
304  if (!file_exists($dirroot)) {
305  if (!@mkdir($dirroot, 0700, true)) {
306  throw new \IOException("Could not make " . $dirroot);
307  }
308  }
309 
310  return true;
311  }
312 
319  public function getParameters() {
320  return array("dir_root" => $this->dir_root);
321  }
322 
330  public function setParameters(array $parameters) {
331  if (isset($parameters['dir_root'])) {
332  $this->dir_root = $parameters['dir_root'];
333  return true;
334  }
335 
336  return false;
337  }
338 
339 
340 
345  // @codingStandardsIgnoreStart
354  protected function make_file_matrix($identifier) {
355  elgg_deprecated_notice('\ElggDiskFilestore::make_file_matrix() is deprecated by \Elgg\EntityDirLocator', 1.8);
356 
357  return $this->makeFileMatrix($identifier);
358  }
359  // @codingStandardsIgnoreEnd
360 
361  // @codingStandardsIgnoreStart
373  protected function user_file_matrix($guid) {
374  elgg_deprecated_notice('\ElggDiskFilestore::user_file_matrix() is deprecated by \Elgg\EntityDirLocator', 1.8);
375 
376  return $this->makeFileMatrix($guid);
377  }
378  // @codingStandardsIgnoreEnd
379 
380  // @codingStandardsIgnoreStart
393  private function mb_str_split($string, $charset = 'UTF8') {
394  elgg_deprecated_notice('\ElggDiskFilestore::mb_str_split() is deprecated.', 1.8);
395 
396  if (is_callable('mb_substr')) {
397  $length = mb_strlen($string);
398  $array = array();
399 
400  while ($length) {
401  $array[] = mb_substr($string, 0, 1, $charset);
402  $string = mb_substr($string, 1, $length, $charset);
403 
404  $length = mb_strlen($string);
405  }
406 
407  return $array;
408  } else {
409  return str_split($string);
410  }
411  }
412  // @codingStandardsIgnoreEnd
413 
422  protected function makeFileMatrix($guid) {
423  elgg_deprecated_notice('\ElggDiskFilestore::makeFileMatrix() is deprecated by \Elgg\EntityDirLocator', 1.9);
425 
426  if (!$entity instanceof \ElggEntity) {
427  return false;
428  }
429 
430  $dir = new \Elgg\EntityDirLocator($guid);
431  return $dir->getPath();
432  }
433 }
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.
$mode
Configure site maintenance mode.
make_directory_root($dirroot)
Create a directory $dirroot.
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
user_file_matrix($guid)
Construct a filename matrix.
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
get_dir_size($dir, $total_size=0)
Get the size of the specified directory.
Definition: filestore.php:18
if(!$count) $offset
Definition: pagination.php:25
make_file_matrix($identifier)
Deprecated methods.
$guid
Removes an admin notice.
__construct($directory_root="")
Construct a disk filestore using the given directory root.
makeFileMatrix($guid)
Construct a file path matrix for an entity.
$position
Definition: move.php:10
$string
getFilenameOnFilestore()
Return the filename of this file as it is/will be stored on the filestore, which may be different to ...
Definition: ElggFile.php:78
makeDirectoryRoot($dirroot)
Create a directory $dirroot.
$owner_guid
write($f, $data)
Write data to a file.
getFilename()
Return the filename.
Definition: ElggFile.php:68
_elgg_services()
Definition: autoloader.php:14
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg global
Pointer to the global context.
Definition: elgglib.js:12
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.
exists(\ElggFile $file)
Tests if an file exists.
$filename
Definition: crop.php:23
tell($f)
Return the current location of the internal pointer.
seek($f, $position)
Seek to the specified position.
$container_guid
$entity
Definition: delete.php:10
getParameters()
Returns a list of attributes to save to the database when saving the object using this file store...
$path
Definition: invalid.php:17
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382