123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- <?php
- class ElggDiskFilestore extends \ElggFilestore {
-
- private $dir_root;
-
- const BUCKET_SIZE = 5000;
-
- private $CONFIG;
-
- public function __construct($directory_root = "") {
- global $CONFIG;
- $this->CONFIG = $CONFIG;
- if ($directory_root) {
- $this->dir_root = $directory_root;
- } else {
- $this->dir_root = $this->CONFIG->dataroot;
- }
- }
-
- public function open(\ElggFile $file, $mode) {
- $fullname = $this->getFilenameOnFilestore($file);
-
- $ls = strrpos($fullname, "/");
- if ($ls === false) {
- $ls = 0;
- }
- $path = substr($fullname, 0, $ls);
- if (($mode != 'write') && (!file_exists($fullname))) {
- return false;
- }
-
- if ($mode == 'write' || $mode == 'append') {
- try {
- $this->makeDirectoryRoot($path);
- } catch (Exception $e) {
- _elgg_services()->logger->warn("Couldn't create directory: $path");
- return false;
- }
- }
- switch ($mode) {
- case "read" :
- $mode = "rb";
- break;
- case "write" :
- $mode = "w+b";
- break;
- case "append" :
- $mode = "a+b";
- break;
- default:
- $msg = "Unrecognized file mode '" . $mode . "'";
- throw new \InvalidParameterException($msg);
- }
- return fopen($fullname, $mode);
- }
-
- public function write($f, $data) {
- return fwrite($f, $data);
- }
-
- public function read($f, $length, $offset = 0) {
- if ($offset) {
- $this->seek($f, $offset);
- }
- return fread($f, $length);
- }
-
- public function close($f) {
- return fclose($f);
- }
-
- public function delete(\ElggFile $file) {
- $filename = $this->getFilenameOnFilestore($file);
- if (file_exists($filename)) {
- return unlink($filename);
- } else {
- return true;
- }
- }
-
- public function seek($f, $position) {
- return fseek($f, $position);
- }
-
- public function tell($f) {
- return ftell($f);
- }
-
- public function eof($f) {
- return feof($f);
- }
-
- public function getFileSize(\ElggFile $file) {
- return filesize($this->getFilenameOnFilestore($file));
- }
-
- public function getFilenameOnFilestore(\ElggFile $file) {
- $owner_guid = $file->getOwnerGuid();
- if (!$owner_guid) {
- $owner_guid = _elgg_services()->session->getLoggedInUserGuid();
- }
- if (!$owner_guid) {
- $msg = "File " . $file->getFilename() . " (file guid:" . $file->guid . ") is missing an owner!";
- throw new \InvalidParameterException($msg);
- }
- $filename = $file->getFilename();
- if (!$filename) {
- return '';
- }
- $dir = new \Elgg\EntityDirLocator($owner_guid);
- return $this->dir_root . $dir . $file->getFilename();
- }
-
- public function grabFile(\ElggFile $file) {
- return file_get_contents($file->getFilenameOnFilestore());
- }
-
- public function exists(\ElggFile $file) {
- if (!$file->getFilename()) {
- return false;
- }
- return file_exists($this->getFilenameOnFilestore($file));
- }
-
- public function getSize($prefix, $container_guid) {
- if ($container_guid) {
- $dir = new \Elgg\EntityDirLocator($container_guid);
- return get_dir_size($this->dir_root . $dir . $prefix);
- } else {
- return false;
- }
- }
-
-
- protected function make_directory_root($dirroot) {
- elgg_deprecated_notice('\ElggDiskFilestore::make_directory_root() is deprecated by ::makeDirectoryRoot()', 1.8);
- return $this->makeDirectoryRoot($dirroot);
- }
-
-
- protected function makeDirectoryRoot($dirroot) {
- if (!file_exists($dirroot)) {
- if (!@mkdir($dirroot, 0700, true)) {
- throw new \IOException("Could not make " . $dirroot);
- }
- }
- return true;
- }
-
- public function getParameters() {
- return array("dir_root" => $this->dir_root);
- }
-
- public function setParameters(array $parameters) {
- if (isset($parameters['dir_root'])) {
- $this->dir_root = $parameters['dir_root'];
- return true;
- }
- return false;
- }
-
-
-
-
- protected function make_file_matrix($identifier) {
- elgg_deprecated_notice('\ElggDiskFilestore::make_file_matrix() is deprecated by \Elgg\EntityDirLocator', 1.8);
- return $this->makeFileMatrix($identifier);
- }
-
-
-
- protected function user_file_matrix($guid) {
- elgg_deprecated_notice('\ElggDiskFilestore::user_file_matrix() is deprecated by \Elgg\EntityDirLocator', 1.8);
- return $this->makeFileMatrix($guid);
- }
-
-
-
- private function mb_str_split($string, $charset = 'UTF8') {
- elgg_deprecated_notice('\ElggDiskFilestore::mb_str_split() is deprecated.', 1.8);
- if (is_callable('mb_substr')) {
- $length = mb_strlen($string);
- $array = array();
- while ($length) {
- $array[] = mb_substr($string, 0, 1, $charset);
- $string = mb_substr($string, 1, $length, $charset);
- $length = mb_strlen($string);
- }
- return $array;
- } else {
- return str_split($string);
- }
- }
-
-
- protected function makeFileMatrix($guid) {
- elgg_deprecated_notice('\ElggDiskFilestore::makeFileMatrix() is deprecated by \Elgg\EntityDirLocator', 1.9);
- $entity = get_entity($guid);
- if (!$entity instanceof \ElggEntity) {
- return false;
- }
- $dir = new \Elgg\EntityDirLocator($guid);
- return $dir->getPath();
- }
- }
|