Elgg  Version 1.12
ElggFile.php
Go to the documentation of this file.
1 <?php
2 
22 class ElggFile extends \ElggObject {
24  private $filestore;
25 
27  private $handle;
28 
34  protected function initializeAttributes() {
35  parent::initializeAttributes();
36 
37  $this->attributes['subtype'] = "file";
38  }
39 
45  public function __construct($row = null) {
46  parent::__construct($row);
47 
48  // Set default filestore
49  $this->filestore = $this->getFilestore();
50  }
51 
59  public function setFilename($name) {
60  $this->filename = $name;
61  }
62 
68  public function getFilename() {
69  return $this->filename;
70  }
71 
78  public function getFilenameOnFilestore() {
79  return $this->filestore->getFilenameOnFilestore($this);
80  }
81 
90  public function getFilestoreSize($prefix = '', $container_guid = 0) {
91  if (!$container_guid) {
93  }
94  $fs = $this->getFilestore();
95  // @todo add getSize() to \ElggFilestore
96  return $fs->getSize($prefix, $container_guid);
97  }
98 
104  public function getMimeType() {
105  if ($this->mimetype) {
106  return $this->mimetype;
107  }
108 
109  // @todo Guess mimetype if not here
110  }
111 
119  public function setMimeType($mimetype) {
120  return $this->mimetype = $mimetype;
121  }
122 
135  public function detectMimeType($file = null, $default = null) {
136  $class = __CLASS__;
137  if (!$file && isset($this) && $this instanceof $class) {
138  $file = $this->getFilenameOnFilestore();
139  }
140 
141  if (!is_readable($file)) {
142  return false;
143  }
144 
145  $mime = $default;
146 
147  // for PHP5 folks.
148  if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
149  $resource = finfo_open(FILEINFO_MIME_TYPE);
150  if ($resource) {
151  $mime = finfo_file($resource, $file);
152  }
153  }
154 
155  // for everyone else.
156  if (!$mime && function_exists('mime_content_type')) {
157  $mime = mime_content_type($file);
158  }
159 
160  $original_filename = isset($this) && $this instanceof $class ? $this->originalfilename : basename($file);
161  $params = array(
162  'filename' => $file,
163  'original_filename' => $original_filename, // @see file upload action
164  'default' => $default,
165  );
166  return _elgg_services()->hooks->trigger('mime_type', 'file', $params, $mime);
167  }
168 
176  public function setDescription($description) {
177  $this->description = $description;
178  }
179 
189  public function open($mode) {
190  if (!$this->getFilename()) {
191  throw new \IOException("You must specify a name before opening a file.");
192  }
193 
194  // See if file has already been saved
195  // seek on datastore, parameters and name?
196 
197  // Sanity check
198  if (
199  ($mode != "read") &&
200  ($mode != "write") &&
201  ($mode != "append")
202  ) {
203  $msg = "Unrecognized file mode '" . $mode . "'";
204  throw new \InvalidParameterException($msg);
205  }
206 
207  // Get the filestore
208  $fs = $this->getFilestore();
209 
210  // Ensure that we save the file details to object store
211  //$this->save();
212 
213  // Open the file handle
214  $this->handle = $fs->open($this, $mode);
215 
216  return $this->handle;
217  }
218 
226  public function write($data) {
227  $fs = $this->getFilestore();
228 
229  return $fs->write($this->handle, $data);
230  }
231 
240  public function read($length, $offset = 0) {
241  $fs = $this->getFilestore();
242 
243  return $fs->read($this->handle, $length, $offset);
244  }
245 
251  public function grabFile() {
252  $fs = $this->getFilestore();
253  return $fs->grabFile($this);
254  }
255 
261  public function close() {
262  $fs = $this->getFilestore();
263 
264  if ($fs->close($this->handle)) {
265  $this->handle = null;
266 
267  return true;
268  }
269 
270  return false;
271  }
272 
278  public function delete() {
279  $fs = $this->getFilestore();
280 
281  $result = $fs->delete($this);
282 
283  if ($this->getGUID() && $result) {
285  }
286 
287  return $result;
288  }
289 
297  public function seek($position) {
298  $fs = $this->getFilestore();
299 
300  // @todo add seek() to \ElggFilestore
301  return $fs->seek($this->handle, $position);
302  }
303 
309  public function tell() {
310  $fs = $this->getFilestore();
311 
312  return $fs->tell($this->handle);
313  }
314 
321  public function getSize() {
322  return $this->filestore->getFileSize($this);
323  }
324 
331  public function size() {
332  elgg_deprecated_notice("Use \ElggFile::getSize() instead of \ElggFile::size()", 1.9);
333  return $this->getSize();
334  }
335 
341  public function eof() {
342  $fs = $this->getFilestore();
343 
344  return $fs->eof($this->handle);
345  }
346 
352  public function exists() {
353  $fs = $this->getFilestore();
354 
355  return $fs->exists($this);
356  }
357 
365  public function setFilestore(\ElggFilestore $filestore) {
366  $this->filestore = $filestore;
367  }
368 
378  protected function getFilestore() {
379  // Short circuit if already set.
380  if ($this->filestore) {
381  return $this->filestore;
382  }
383 
384  // ask for entity specific filestore
385  // saved as filestore::className in metadata.
386  // need to get all filestore::* metadata because the rest are "parameters" that
387  // get passed to filestore::setParameters()
388  if ($this->guid) {
389  $options = array(
390  'guid' => $this->guid,
391  'where' => array("n.string LIKE 'filestore::%'"),
392  );
393 
394  $mds = elgg_get_metadata($options);
395 
396  $parameters = array();
397  foreach ($mds as $md) {
398  list( , $name) = explode("::", $md->name);
399  if ($name == 'filestore') {
400  $filestore = $md->value;
401  }
402  $parameters[$name] = $md->value;
403  }
404  }
405 
406  // need to check if filestore is set because this entity is loaded in save()
407  // before the filestore metadata is saved.
408  if (isset($filestore)) {
409  if (!class_exists($filestore)) {
410  $msg = "Unable to load filestore class " . $filestore . " for file " . $this->guid;
411  throw new \ClassNotFoundException($msg);
412  }
413 
414  $this->filestore = new $filestore();
415  $this->filestore->setParameters($parameters);
416  // @todo explain why $parameters will always be set here (PhpStorm complains)
417  }
418 
419  // this means the entity hasn't been saved so fallback to default
420  if (!$this->filestore) {
421  $this->filestore = get_default_filestore();
422  }
423 
424  return $this->filestore;
425  }
426 
437  public function save() {
438  if (!parent::save()) {
439  return false;
440  }
441 
442  // Save datastore metadata
443  $params = $this->filestore->getParameters();
444  foreach ($params as $k => $v) {
445  $this->setMetadata("filestore::$k", $v);
446  }
447 
448  // Now make a note of the filestore class
449  $this->setMetadata("filestore::filestore", get_class($this->filestore));
450 
451  return true;
452  }
453 
459  public function __sleep() {
460  return array_diff(array_keys(get_object_vars($this)), array(
461  // Don't persist filestore, which contains CONFIG
462  // https://github.com/Elgg/Elgg/issues/9081#issuecomment-152859856
463  'filestore',
464 
465  // a resource
466  'handle',
467  ));
468  }
469 
476  public function __wakeup() {
477  $this->getFilestore();
478  }
479 }
setDescription($description)
Set the optional file description.
Definition: ElggFile.php:176
getSize()
Return the size of the file in bytes.
Definition: ElggFile.php:321
__construct($row=null)
Loads an entity.
Definition: ElggFile.php:45
$mode
Configure site maintenance mode.
write($data)
Write data.
Definition: ElggFile.php:226
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
elgg module widget elgg state draggable elgg widget handle
Definition: admin.php:1214
getFilestoreSize($prefix= '', $container_guid=0)
Return the size of the filestore associated with this file.
Definition: ElggFile.php:90
close()
Close the file and commit changes.
Definition: ElggFile.php:261
seek($position)
Seek a position in the file.
Definition: ElggFile.php:297
setMetadata($name, $value, $value_type= '', $multiple=false, $owner_guid=0, $access_id=null)
Set metadata on this entity.
Definition: ElggEntity.php:389
$data
Definition: opendd.php:13
eof()
Return a boolean value whether the file handle is at the end of the file.
Definition: ElggFile.php:341
if($screenshots) $description
Definition: full.php:173
exists()
Returns if the file exists.
Definition: ElggFile.php:352
if(!$count) $offset
Definition: pagination.php:26
getGUID()
Returns the guid.
$default
Definition: checkbox.php:34
$guid
Removes an admin notice.
__wakeup()
Reestablish filestore property.
Definition: ElggFile.php:476
if(isset($vars['id'])) $class
Definition: ajax_loader.php:19
save()
Save the file.
Definition: ElggFile.php:437
$position
Definition: move.php:10
setFilestore(\ElggFilestore $filestore)
Set a filestore.
Definition: ElggFile.php:365
get_default_filestore()
Return the default filestore.
Definition: filestore.php:432
$params
Definition: login.php:72
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
$options
Definition: index.php:14
grabFile()
Gets the full contents of this file.
Definition: ElggFile.php:251
getFilestore()
Return a filestore suitable for saving this file.
Definition: ElggFile.php:378
initializeAttributes()
Set subtype to &#39;file&#39;.
Definition: ElggFile.php:34
if(!$site) if(!($site instanceof ElggSite)) $site description
setMimeType($mimetype)
Set the mime type of the file.
Definition: ElggFile.php:119
getFilename()
Return the filename.
Definition: ElggFile.php:68
elgg menu widget elgg menu item delete
Definition: admin.php:1106
detectMimeType($file=null, $default=null)
Detects mime types based on filename or actual file.
Definition: ElggFile.php:135
read($length, $offset=0)
Read data.
Definition: ElggFile.php:240
_elgg_services()
Definition: autoloader.php:14
tell()
Return the current position of the file.
Definition: ElggFile.php:309
getMimeType()
Get the mime type of the file.
Definition: ElggFile.php:104
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1031
elgg river item elgg form comment save
Definition: components.php:246
elgg_get_metadata(array $options=array())
Returns metadata.
Definition: metadata.php:143
setFilename($name)
Set the filename of this file.
Definition: ElggFile.php:59
$filename
Definition: crop.php:23
$row
size()
Return the size of the file in bytes.
Definition: ElggFile.php:331
$container_guid
open($mode)
Open the file with the given mode.
Definition: ElggFile.php:189
__sleep()
Get property names to serialize.
Definition: ElggFile.php:459