Elgg  Version 1.9
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 = "") {
30 
31  if ($directory_root) {
32  $this->dir_root = $directory_root;
33  } else {
34  $this->dir_root = $CONFIG->dataroot;
35  }
36  }
37 
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 != 'write') && (!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_log("Couldn't create directory: $path", 'WARNING');
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  $msg = "Unrecognized file mode '" . $mode . "'";
89  throw new InvalidParameterException($msg);
90  }
91 
92  return fopen($fullname, $mode);
93 
94  }
95 
104  public function write($f, $data) {
105  return fwrite($f, $data);
106  }
107 
117  public function read($f, $length, $offset = 0) {
118  if ($offset) {
119  $this->seek($f, $offset);
120  }
121 
122  return fread($f, $length);
123  }
124 
132  public function close($f) {
133  return fclose($f);
134  }
135 
143  public function delete(ElggFile $file) {
144  $filename = $this->getFilenameOnFilestore($file);
145  if (file_exists($filename)) {
146  return unlink($filename);
147  } else {
148  return true;
149  }
150  }
151 
160  public function seek($f, $position) {
161  return fseek($f, $position);
162  }
163 
171  public function tell($f) {
172  return ftell($f);
173  }
174 
182  public function eof($f) {
183  return feof($f);
184  }
185 
193  public function getFileSize(ElggFile $file) {
194  return filesize($this->getFilenameOnFilestore($file));
195  }
196 
207  public function getFilenameOnFilestore(ElggFile $file) {
208  $owner_guid = $file->getOwnerGuid();
209  if (!$owner_guid) {
211  }
212 
213  if (!$owner_guid) {
214  $msg = "File " . $file->getFilename() . " (file guid:" . $file->guid . ") is missing an owner!";
215  throw new InvalidParameterException($msg);
216  }
217 
218  $filename = $file->getFilename();
219  if (!$filename) {
220  return '';
221  }
222 
224 
225  return $this->dir_root . $dir . $file->getFilename();
226  }
227 
235  public function grabFile(ElggFile $file) {
236  return file_get_contents($file->getFilenameOnFilestore());
237  }
238 
246  public function exists(ElggFile $file) {
247  if (!$file->getFilename()) {
248  return false;
249  }
250  return file_exists($this->getFilenameOnFilestore($file));
251  }
252 
261  public function getSize($prefix, $container_guid) {
262  if ($container_guid) {
263  $dir = new Elgg_EntityDirLocator($container_guid);
264  return get_dir_size($this->dir_root . $dir . $prefix);
265  } else {
266  return false;
267  }
268  }
269 
270  // @codingStandardsIgnoreStart
280  protected function make_directory_root($dirroot) {
281  elgg_deprecated_notice('ElggDiskFilestore::make_directory_root() is deprecated by ::makeDirectoryRoot()', 1.8);
282 
283  return $this->makeDirectoryRoot($dirroot);
284  }
285  // @codingStandardsIgnoreEnd
286 
295  protected function makeDirectoryRoot($dirroot) {
296  if (!file_exists($dirroot)) {
297  if (!@mkdir($dirroot, 0700, true)) {
298  throw new IOException("Could not make " . $dirroot);
299  }
300  }
301 
302  return true;
303  }
304 
311  public function getParameters() {
312  return array("dir_root" => $this->dir_root);
313  }
314 
322  public function setParameters(array $parameters) {
323  if (isset($parameters['dir_root'])) {
324  $this->dir_root = $parameters['dir_root'];
325  return true;
326  }
327 
328  return false;
329  }
330 
331 
332 
337  // @codingStandardsIgnoreStart
346  protected function make_file_matrix($identifier) {
347  elgg_deprecated_notice('ElggDiskFilestore::make_file_matrix() is deprecated by Elgg_EntityDirLocator', 1.8);
348 
349  return $this->makeFileMatrix($identifier);
350  }
351  // @codingStandardsIgnoreEnd
352 
353  // @codingStandardsIgnoreStart
365  protected function user_file_matrix($guid) {
366  elgg_deprecated_notice('ElggDiskFilestore::user_file_matrix() is deprecated by Elgg_EntityDirLocator', 1.8);
367 
368  return $this->makeFileMatrix($guid);
369  }
370  // @codingStandardsIgnoreEnd
371 
372  // @codingStandardsIgnoreStart
385  private function mb_str_split($string, $charset = 'UTF8') {
386  elgg_deprecated_notice('ElggDiskFilestore::mb_str_split() is deprecated.', 1.8);
387 
388  if (is_callable('mb_substr')) {
389  $length = mb_strlen($string);
390  $array = array();
391 
392  while ($length) {
393  $array[] = mb_substr($string, 0, 1, $charset);
394  $string = mb_substr($string, 1, $length, $charset);
395 
396  $length = mb_strlen($string);
397  }
398 
399  return $array;
400  } else {
401  return str_split($string);
402  }
403  }
404  // @codingStandardsIgnoreEnd
405 
414  protected function makeFileMatrix($guid) {
415  elgg_deprecated_notice('ElggDiskFilestore::makeFileMatrix() is deprecated by Elgg_EntityDirLocator', 1.9);
417 
418  if (!$entity instanceof ElggEntity) {
419  return false;
420  }
421 
422  $dir = new Elgg_EntityDirLocator($guid);
423  return $dir->getPath();
424  }
425 }
eof($f)
Tests for end of file on a file pointer.
close($f)
Close a file pointer.
get_dir_size($dir, $totalsize=0)
Get the size of the specified directory.
Definition: filestore.php:18
const BUCKET_SIZE
Number of entries per matrix dir.
$mode
Configure site maintenance mode.
make_directory_root($dirroot)
Create a directory $dirroot.
read($f, $length, $offset=0)
Read data from a file.
$e
Definition: metadata.php:12
user_file_matrix($guid)
Construct a filename matrix.
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
$data
Definition: opendd.php:13
make_file_matrix($identifier)
Deprecated methods.
$guid
Removes an admin notice.
__construct($directory_root="")
Construct a disk filestore using the given directory root.
open(ElggFile $file, $mode)
Open a file for reading, writing, or both.
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.
getFilenameOnFilestore(ElggFile $file)
Get the filename as saved on disk for an ElggFile object.
$owner_guid
write($f, $data)
Write data to a file.
exists(ElggFile $file)
Tests if an ElggFile file exists.
getFilename()
Return the filename.
Definition: ElggFile.php:68
global $CONFIG
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
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.
setParameters(array $parameters)
Sets parameters that should be saved to database.
getFileSize(ElggFile $file)
Returns the file size of an ElggFile file.
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1083
$filename
Definition: crop.php:23
tell($f)
Return the current location of the internal pointer.
seek($f, $position)
Seek to the specified position.
$entity
Definition: delete.php:10
getParameters()
Returns a list of attributes to save to the database when saving the ElggFile object using this file ...
grabFile(ElggFile $file)
Returns the contents of the ElggFile file.
$path
Definition: invalid.php:17
elgg_get_logged_in_user_guid()
Return the current logged in user by guid.
Definition: sessions.php:42
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604