00001 <?php
00012 class ElggDiskFilestore extends ElggFilestore {
00016 private $dir_root;
00017
00022 const BUCKET_SIZE = 5000;
00023
00029 public function __construct($directory_root = "") {
00030 global $CONFIG;
00031
00032 if ($directory_root) {
00033 $this->dir_root = $directory_root;
00034 } else {
00035 $this->dir_root = $CONFIG->dataroot;
00036 }
00037 }
00038
00053 public function open(ElggFile $file, $mode) {
00054 $fullname = $this->getFilenameOnFilestore($file);
00055
00056
00057 $ls = strrpos($fullname, "/");
00058 if ($ls === false) {
00059 $ls = 0;
00060 }
00061
00062 $path = substr($fullname, 0, $ls);
00063 $name = substr($fullname, $ls);
00064
00065
00066 if (($mode != 'write') && (!file_exists($fullname))) {
00067 return false;
00068 }
00069
00070
00071 if ($mode == 'write' || $mode == 'append') {
00072 try {
00073 $this->makeDirectoryRoot($path);
00074 } catch (Exception $e) {
00075 elgg_log("Couldn't create directory: $path", 'WARNING');
00076 return false;
00077 }
00078 }
00079
00080 switch ($mode) {
00081 case "read" :
00082 $mode = "rb";
00083 break;
00084 case "write" :
00085 $mode = "w+b";
00086 break;
00087 case "append" :
00088 $mode = "a+b";
00089 break;
00090 default:
00091 $msg = "Unrecognized file mode '" . $mode . "'";
00092 throw new InvalidParameterException($msg);
00093 }
00094
00095 return fopen($fullname, $mode);
00096
00097 }
00098
00107 public function write($f, $data) {
00108 return fwrite($f, $data);
00109 }
00110
00120 public function read($f, $length, $offset = 0) {
00121 if ($offset) {
00122 $this->seek($f, $offset);
00123 }
00124
00125 return fread($f, $length);
00126 }
00127
00135 public function close($f) {
00136 return fclose($f);
00137 }
00138
00146 public function delete(ElggFile $file) {
00147 $filename = $this->getFilenameOnFilestore($file);
00148 if (file_exists($filename)) {
00149 return unlink($filename);
00150 } else {
00151 return true;
00152 }
00153 }
00154
00163 public function seek($f, $position) {
00164 return fseek($f, $position);
00165 }
00166
00174 public function tell($f) {
00175 return ftell($f);
00176 }
00177
00185 public function eof($f) {
00186 return feof($f);
00187 }
00188
00196 public function getFileSize(ElggFile $file) {
00197 return filesize($this->getFilenameOnFilestore($file));
00198 }
00199
00210 public function getFilenameOnFilestore(ElggFile $file) {
00211 $owner_guid = $file->getOwnerGuid();
00212 if (!$owner_guid) {
00213 $owner_guid = elgg_get_logged_in_user_guid();
00214 }
00215
00216 if (!$owner_guid) {
00217 $msg = "File " . $file->getFilename() . " (file guid:" . $file->guid . ") is missing an owner!";
00218 throw new InvalidParameterException($msg);
00219 }
00220
00221 $dir = new Elgg_EntityDirLocator($owner_guid);
00222
00223 return $this->dir_root . $dir . $file->getFilename();
00224 }
00225
00233 public function grabFile(ElggFile $file) {
00234 return file_get_contents($file->getFilenameOnFilestore());
00235 }
00236
00244 public function exists(ElggFile $file) {
00245 if (!$file->getFilename()) {
00246 return false;
00247 }
00248 return file_exists($this->getFilenameOnFilestore($file));
00249 }
00250
00259 public function getSize($prefix, $container_guid) {
00260 if ($container_guid) {
00261 $dir = new Elgg_EntityDirLocator($container_guid);
00262 return get_dir_size($this->dir_root . $dir . $prefix);
00263 } else {
00264 return false;
00265 }
00266 }
00267
00268
00278 protected function make_directory_root($dirroot) {
00279 elgg_deprecated_notice('ElggDiskFilestore::make_directory_root() is deprecated by ::makeDirectoryRoot()', 1.8);
00280
00281 return $this->makeDirectoryRoot($dirroot);
00282 }
00283
00284
00293 protected function makeDirectoryRoot($dirroot) {
00294 if (!file_exists($dirroot)) {
00295 if (!@mkdir($dirroot, 0700, true)) {
00296 throw new IOException("Could not make " . $dirroot);
00297 }
00298 }
00299
00300 return true;
00301 }
00302
00309 public function getParameters() {
00310 return array("dir_root" => $this->dir_root);
00311 }
00312
00320 public function setParameters(array $parameters) {
00321 if (isset($parameters['dir_root'])) {
00322 $this->dir_root = $parameters['dir_root'];
00323 return true;
00324 }
00325
00326 return false;
00327 }
00328
00329
00330
00335
00344 protected function make_file_matrix($identifier) {
00345 elgg_deprecated_notice('ElggDiskFilestore::make_file_matrix() is deprecated by Elgg_EntityDirLocator', 1.8);
00346
00347 return $this->makeFileMatrix($identifier);
00348 }
00349
00350
00351
00363 protected function user_file_matrix($guid) {
00364 elgg_deprecated_notice('ElggDiskFilestore::user_file_matrix() is deprecated by Elgg_EntityDirLocator', 1.8);
00365
00366 return $this->makeFileMatrix($guid);
00367 }
00368
00369
00370
00383 private function mb_str_split($string, $charset = 'UTF8') {
00384 elgg_deprecated_notice('ElggDiskFilestore::mb_str_split() is deprecated.', 1.8);
00385
00386 if (is_callable('mb_substr')) {
00387 $length = mb_strlen($string);
00388 $array = array();
00389
00390 while ($length) {
00391 $array[] = mb_substr($string, 0, 1, $charset);
00392 $string = mb_substr($string, 1, $length, $charset);
00393
00394 $length = mb_strlen($string);
00395 }
00396
00397 return $array;
00398 } else {
00399 return str_split($string);
00400 }
00401 }
00402
00403
00412 protected function makeFileMatrix($guid) {
00413 elgg_deprecated_notice('ElggDiskFilestore::makeFileMatrix() is deprecated by Elgg_EntityDirLocator', 1.9);
00414 $entity = get_entity($guid);
00415
00416 if (!$entity instanceof ElggEntity) {
00417 return false;
00418 }
00419
00420 $dir = new Elgg_EntityDirLocator($guid);
00421 return $dir->getPath();
00422 }
00423 }