FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Files.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\User;
4 
5 class Files extends \Rsi\Fred\Component{
6 
7  public $quota = 16; //!< Maximum total user files size (per user; in mega bytes).
8 
9  protected $_user = null;
10  protected $_root = null; //!< Root directory for the user files.
11 
12  /**
13  * Get full filename for a file.
14  * @param string $filename User filename, including optional directories.
15  * @return string Full filename (including root). False if root not set, no user ID, or incorrect filename.
16  */
17  protected function filename($filename){
18  return $this->_root && $this->_user->id && preg_match('/^(\\w+\\/)*\\w+(\\.\\w+)?$/',$filename)
19  ? $this->_root . $this->_user->id . '/' . $filename
20  : false;
21  }
22  /**
23  * List files.
24  * @param string|array $types Allowed types (regex or array with extensions).
25  * @return array Key = full name, value = info.
26  */
27  public function list($types = null){
28  $result = [];
29  $filters = [\Rsi\File::FIND_FILTER_SIZE . '>' => 0,\Rsi\File::FIND_FILTER_TIME . '>' => 0];
30  if($types) $filters[\Rsi\File::FIND_FILTER_NAME . '//'] = is_array($types) ? '/\\.(' . implode('|',$types) . ')$/i' : $types;
31  if($this->_root && $this->_user->id && ($files = \Rsi\File::find($root = $this->_root . $this->_user->id,$filters,true))){
32  $root = strlen($root) + 1;
33  foreach($files as $filename => $info) $result[str_replace(DIRECTORY_SEPARATOR,'/',substr($filename,$root))] = $info;
34  }
35  return $result;
36  }
37  /**
38  * Total size used (in bytes).
39  * @return int
40  */
41  public function total(){
42  $total = 0;
43  foreach($this->list() as $info) $total += $info['size'];
44  return $total;
45  }
46  /**
47  * Save a file.
48  * @param string $filename User filename, including optional directories.
49  * @param mixed $data Data to save.
50  * @return int Number of bytes written or false on error.
51  */
52  public function save($filename,$data){
53  if(($filename = $this->filename($filename)) && (($this->total() + (is_file($filename) ? 0 : strlen($data))) <= $this->quota * 1048576)){
54  \Rsi\File::mkdir(dirname($filename));
55  return file_put_contents($filename,$data);
56  }
57  return false;
58  }
59  /**
60  * Read a file.
61  * @param string $filename User filename, including optional directories.
62  * @return mixed Data. False on error or null if file does not exist.
63  */
64  public function fetch($filename){
65  return ($filename = $this->filename($filename)) ? \Rsi\File::read($filename,null) : false;
66  }
67  /**
68  * Delete a file.
69  * @param string $filename User filename, including optional directories.
70  * @return bool True on success.
71  */
72  public function delete($filename){
73  if(($filename = $this->filename($filename)) && \Rsi\File::unlink($filename)){
74  $root = strlen($this->_root);
75  while((strlen($filename = dirname($filename)) > $root) && !(new \FilesystemIterator($filename))->valid()) rmdir($filename);
76  return true;
77  }
78  return false;
79  }
80  /**
81  * Delete all files.
82  */
83  public function clear(){
84  foreach($this->list() as $filename => $info) $this->delete($filename);
85  }
86 
87 }
save($filename, $data)
Save a file.
Definition: Files.php:52
total()
Total size used (in bytes).
Definition: Files.php:41
list($types=null)
List files.
Definition: Files.php:27
$quota
Maximum total user files size (per user; in mega bytes).
Definition: Files.php:7
filename($filename)
Get full filename for a file.
Definition: Files.php:17
$_root
Root directory for the user files.
Definition: Files.php:10
fetch($filename)
Read a file.
Definition: Files.php:64
clear()
Delete all files.
Definition: Files.php:83