Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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 === 'read') && (!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, $follow_symlinks = true) {
153  if (file_exists($filename) || is_link($filename)) {
154  if ($follow_symlinks && is_link($filename) && file_exists($filename)) {
155  $target = readlink($filename);
156  file_exists($target) && unlink($target);
157  }
158  return unlink($filename);
159  } else {
160  return true;
161  }
162  }
163 
172  public function seek($f, $position) {
173  return fseek($f, $position);
174  }
175 
183  public function tell($f) {
184  return ftell($f);
185  }
186 
194  public function eof($f) {
195  return feof($f);
196  }
197 
205  public function getFileSize(\ElggFile $file) {
206  return filesize($this->getFilenameOnFilestore($file));
207  }
208 
220  $owner_guid = $file->getOwnerGuid();
221  if (!$owner_guid) {
222  $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
223  }
224 
225  if (!$owner_guid) {
226  $msg = "File " . $file->getFilename() . " (file guid:" . $file->guid . ") is missing an owner!";
227  throw new \InvalidParameterException($msg);
228  }
229 
230  $filename = $file->getFilename();
231  if (!$filename) {
232  return '';
233  }
234 
235  $dir = new \Elgg\EntityDirLocator($owner_guid);
236 
237  return $this->dir_root . $dir . $file->getFilename();
238  }
239 
247  public function grabFile(\ElggFile $file) {
248  return file_get_contents($file->getFilenameOnFilestore());
249  }
250 
258  public function exists(\ElggFile $file) {
259  if (!$file->getFilename()) {
260  return false;
261  }
262  return file_exists($this->getFilenameOnFilestore($file));
263  }
264 
273  public function getSize($prefix, $container_guid) {
274  if ($container_guid) {
275  $dir = new \Elgg\EntityDirLocator($container_guid);
276  return get_dir_size($this->dir_root . $dir . $prefix);
277  } else {
278  return false;
279  }
280  }
281 
290  protected function makeDirectoryRoot($dirroot) {
291  if (!file_exists($dirroot)) {
292  if (!@mkdir($dirroot, 0700, true)) {
293  throw new \IOException("Could not make " . $dirroot);
294  }
295  }
296 
297  return true;
298  }
299 
306  public function getParameters() {
307  return array("dir_root" => $this->dir_root);
308  }
309 
317  public function setParameters(array $parameters) {
318  if (isset($parameters['dir_root'])) {
319  $this->dir_root = $parameters['dir_root'];
320  return true;
321  }
322 
323  return false;
324  }
325 
326 
327 
340  protected function makeFileMatrix($guid) {
341  elgg_deprecated_notice('\ElggDiskFilestore::makeFileMatrix() is deprecated by \Elgg\EntityDirLocator', 1.9);
343 
344  if (!$entity instanceof \ElggEntity) {
345  return false;
346  }
347 
348  $dir = new \Elgg\EntityDirLocator($guid);
349  return $dir->getPath();
350  }
351 }
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:18
if(!$count) $offset
Definition: pagination.php:26
$guid
Removes an admin notice.
__construct($directory_root="")
Construct a disk filestore using the given directory root.
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:94
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:84
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1070
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