Elgg  Version 1.9
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) {
92  $container_guid = $this->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 
133  public function detectMimeType($file = null, $default = null) {
134  if (!$file) {
135  if (isset($this) && $this->filename) {
136  $file = $this->filename;
137  } else {
138  return false;
139  }
140  }
141 
142  $mime = $default;
143 
144  // for PHP5 folks.
145  if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
146  $resource = finfo_open(FILEINFO_MIME_TYPE);
147  if ($resource) {
148  $mime = finfo_file($resource, $file);
149  }
150  }
151 
152  // for everyone else.
153  if (!$mime && function_exists('mime_content_type')) {
154  $mime = mime_content_type($file);
155  }
156 
157  $params = array(
158  'filename' => $file,
159  'original_filename' => $file->originalfilename, // @see file upload action
160  'default' => $default,
161  );
162  return elgg_trigger_plugin_hook('mime_type', 'file', $params, $mime);
163  }
164 
172  public function setDescription($description) {
173  $this->description = $description;
174  }
175 
185  public function open($mode) {
186  if (!$this->getFilename()) {
187  throw new IOException("You must specify a name before opening a file.");
188  }
189 
190  // See if file has already been saved
191  // seek on datastore, parameters and name?
192 
193  // Sanity check
194  if (
195  ($mode != "read") &&
196  ($mode != "write") &&
197  ($mode != "append")
198  ) {
199  $msg = "Unrecognized file mode '" . $mode . "'";
200  throw new InvalidParameterException($msg);
201  }
202 
203  // Get the filestore
204  $fs = $this->getFilestore();
205 
206  // Ensure that we save the file details to object store
207  //$this->save();
208 
209  // Open the file handle
210  $this->handle = $fs->open($this, $mode);
211 
212  return $this->handle;
213  }
214 
222  public function write($data) {
223  $fs = $this->getFilestore();
224 
225  return $fs->write($this->handle, $data);
226  }
227 
236  public function read($length, $offset = 0) {
237  $fs = $this->getFilestore();
238 
239  return $fs->read($this->handle, $length, $offset);
240  }
241 
247  public function grabFile() {
248  $fs = $this->getFilestore();
249  return $fs->grabFile($this);
250  }
251 
257  public function close() {
258  $fs = $this->getFilestore();
259 
260  if ($fs->close($this->handle)) {
261  $this->handle = null;
262 
263  return true;
264  }
265 
266  return false;
267  }
268 
274  public function delete() {
275  $fs = $this->getFilestore();
276 
277  $result = $fs->delete($this);
278 
279  if ($this->getGUID() && $result) {
281  }
282 
283  return $result;
284  }
285 
293  public function seek($position) {
294  $fs = $this->getFilestore();
295 
296  // @todo add seek() to ElggFilestore
297  return $fs->seek($this->handle, $position);
298  }
299 
305  public function tell() {
306  $fs = $this->getFilestore();
307 
308  return $fs->tell($this->handle);
309  }
310 
317  public function getSize() {
318  return $this->filestore->getFileSize($this);
319  }
320 
327  public function size() {
328  elgg_deprecated_notice("Use ElggFile::getSize() instead of ElggFile::size()", 1.9);
329  return $this->getSize();
330  }
331 
337  public function eof() {
338  $fs = $this->getFilestore();
339 
340  return $fs->eof($this->handle);
341  }
342 
348  public function exists() {
349  $fs = $this->getFilestore();
350 
351  return $fs->exists($this);
352  }
353 
361  public function setFilestore(ElggFilestore $filestore) {
362  $this->filestore = $filestore;
363  }
364 
374  protected function getFilestore() {
375  // Short circuit if already set.
376  if ($this->filestore) {
377  return $this->filestore;
378  }
379 
380  // ask for entity specific filestore
381  // saved as filestore::className in metadata.
382  // need to get all filestore::* metadata because the rest are "parameters" that
383  // get passed to filestore::setParameters()
384  if ($this->guid) {
385  $options = array(
386  'guid' => $this->guid,
387  'where' => array("n.string LIKE 'filestore::%'"),
388  );
389 
390  $mds = elgg_get_metadata($options);
391 
392  $parameters = array();
393  foreach ($mds as $md) {
394  list( , $name) = explode("::", $md->name);
395  if ($name == 'filestore') {
396  $filestore = $md->value;
397  }
398  $parameters[$name] = $md->value;
399  }
400  }
401 
402  // need to check if filestore is set because this entity is loaded in save()
403  // before the filestore metadata is saved.
404  if (isset($filestore)) {
405  if (!class_exists($filestore)) {
406  $msg = "Unable to load filestore class " . $filestore . " for file " . $this->guid;
407  throw new ClassNotFoundException($msg);
408  }
409 
410  $this->filestore = new $filestore();
411  $this->filestore->setParameters($parameters);
412  // @todo explain why $parameters will always be set here (PhpStorm complains)
413  }
414 
415  // this means the entity hasn't been saved so fallback to default
416  if (!$this->filestore) {
417  $this->filestore = get_default_filestore();
418  }
419 
420  return $this->filestore;
421  }
422 
433  public function save() {
434  if (!parent::save()) {
435  return false;
436  }
437 
438  // Save datastore metadata
439  $params = $this->filestore->getParameters();
440  foreach ($params as $k => $v) {
441  $this->setMetadata("filestore::$k", $v);
442  }
443 
444  // Now make a note of the filestore class
445  $this->setMetadata("filestore::filestore", get_class($this->filestore));
446 
447  return true;
448  }
449 }
setDescription($description)
Set the optional file description.
Definition: ElggFile.php:172
getSize()
Return the size of the file in bytes.
Definition: ElggFile.php:317
__construct($row=null)
Loads an ElggFile entity.
Definition: ElggFile.php:45
$mode
Configure site maintenance mode.
write($data)
Write data.
Definition: ElggFile.php:222
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:1183
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
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:257
seek($position)
Seek a position in the file.
Definition: ElggFile.php:293
setMetadata($name, $value, $value_type= '', $multiple=false, $owner_guid=0, $access_id=null)
Set metadata on this entity.
Definition: ElggEntity.php:386
$data
Definition: opendd.php:13
eof()
Return a boolean value whether the file handle is at the end of the file.
Definition: ElggFile.php:337
$comment description
Definition: save.php:49
if($screenshots) $description
Definition: full.php:173
exists()
Returns if the file exists.
Definition: ElggFile.php:348
getGUID()
Returns the guid.
$default
Definition: checkbox.php:36
$guid
Removes an admin notice.
save()
Save the file.
Definition: ElggFile.php:433
$position
Definition: move.php:10
get_default_filestore()
Return the default filestore.
Definition: filestore.php:467
$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:247
getFilestore()
Return a filestore suitable for saving this file.
Definition: ElggFile.php:374
initializeAttributes()
Set subtype to &#39;file&#39;.
Definition: ElggFile.php:34
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:1075
detectMimeType($file=null, $default=null)
Detects mime types based on filename or actual file.
Definition: ElggFile.php:133
read($length, $offset=0)
Read data.
Definition: ElggFile.php:236
tell()
Return the current position of the file.
Definition: ElggFile.php:305
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
getMimeType()
Get the mime type of the file.
Definition: ElggFile.php:104
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 river item elgg form comment save
Definition: components.php:242
setFilestore(ElggFilestore $filestore)
Set a filestore.
Definition: ElggFile.php:361
elgg_get_metadata(array $options=array())
Returns metadata.
Definition: metadata.php:282
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:327
open($mode)
Open the file with the given mode.
Definition: ElggFile.php:185
Elgg Object.
Definition: ElggObject.php:22