diff options
Diffstat (limited to 'engine')
-rw-r--r-- | engine/lib/filestore.php | 687 |
1 files changed, 571 insertions, 116 deletions
diff --git a/engine/lib/filestore.php b/engine/lib/filestore.php index 3832a0fec..e33730021 100644 --- a/engine/lib/filestore.php +++ b/engine/lib/filestore.php @@ -113,13 +113,13 @@ * Set the parameters from the associative array produced by $this->getParameters(). */ abstract public function setParameters(array $parameters); -
- /**
- * Get the contents of the whole file.
- *
- * @param mixed $file The file handle.
- * @return mixed The file contents.
- */
+ + /** + * Get the contents of the whole file. + * + * @param mixed $file The file handle. + * @return mixed The file contents. + */ abstract public function grabFile(ElggFile $file); /** @@ -127,7 +127,7 @@ * * @param ElggFile $file */ - abstract public function exists(ElggFile $file);
+ abstract public function exists(ElggFile $file); } @@ -182,8 +182,8 @@ case "append" : $mode = "a+b"; break; default: throw new InvalidParameterException(sprintf(elgg_echo('InvalidParameterException:UnrecognisedFileMode'), $mode)); } -
- return fopen($fullname, $mode);
+ + return fopen($fullname, $mode); } @@ -206,12 +206,12 @@ } public function delete(ElggFile $file) - {
- $filename = $this->getFilenameOnFilestore($file);
+ { + $filename = $this->getFilenameOnFilestore($file); if (file_exists($filename)) { - return unlink($filename);
- } else {
- return true;
+ return unlink($filename); + } else { + return true; } } @@ -245,17 +245,25 @@ return $this->dir_root . $this->make_file_matrix($owner->username) . $file->getFilename(); } -
- public function grabFile(ElggFile $file) {
-
- return file_get_contents($file->getFilenameOnFilestore());
-
+ + public function grabFile(ElggFile $file) { + + return file_get_contents($file->getFilenameOnFilestore()); + } public function exists(ElggFile $file) { return file_exists($this->getFilenameOnFilestore($file)); - }
+ } + + public function getSize($prefix,$container_guid) { + if ($container_guid && ($container=get_entity($container_guid)) && ($username = $container->username)) { + return get_dir_size($this->dir_root.$this->make_file_matrix($username).$prefix); + } else { + return false; + } + } /** * Make the directory root. @@ -263,7 +271,7 @@ * @param string $dirroot */ protected function make_directory_root($dirroot) - {
+ { if (!file_exists($dirroot)) if (!@mkdir($dirroot, 0700, true)) throw new IOException(sprintf(elgg_echo('IOException:CouldNotMake'), $dirroot)); @@ -321,8 +329,8 @@ if ($len>$this->matrix_depth) $len = $this->matrix_depth; - for ($n = 0; $n < $len; $n++) {
- $matrix .= $filename[$n] . "/";
+ for ($n = 0; $n < $len; $n++) { + $matrix .= $filename[$n] . "/"; } return $matrix.$name."/"; @@ -403,6 +411,18 @@ */ public function getFilenameOnFilestore() { return $this->filestore->getFilenameOnFilestore($this); } + /* + * Return the size of the filestore associated with this file + * + */ + public function getFilestoreSize($prefix='',$container_guid=0) { + if (!$container_guid) { + $container_guid = $this->container_guid; + } + $fs = $this->getFilestore(); + return $fs->getSize($prefix,$container_guid); + } + /** * Get the mime type of the file. */ @@ -485,18 +505,18 @@ return $fs->read($this->handle, $length, $offset); } -
- /**
- * Gets the full contents of this file.
- *
- * @return mixed The file contents.
- */
- public function grabFile() {
-
- $fs = $this->getFilestore();
- return $fs->grabFile($this);
-
- }
+ + /** + * Gets the full contents of this file. + * + * @return mixed The file contents. + */ + public function grabFile() { + + $fs = $this->getFilestore(); + return $fs->grabFile($this); + + } /** * Close the file and commit changes @@ -521,8 +541,8 @@ public function delete() { $fs = $this->getFilestore(); - if ($fs->delete($this)) {
- return parent::delete();
+ if ($fs->delete($this)) { + return parent::delete(); } } @@ -599,7 +619,7 @@ // If filestore meta set then retrieve filestore TODO: Better way of doing this? $metas = get_metadata_for_entity($this->guid); - $parameters = array();
+ $parameters = array(); if (is_array($metas)) foreach ($metas as $meta) { @@ -651,37 +671,59 @@ } } -
- /**
- * Get the contents of an uploaded file.
- * (Returns false if there was an issue.)
- *
- * @param string $input_name The name of the file input field on the submission form
- * @return mixed|false The contents of the file, or false on failure.
- */
- function get_uploaded_file($input_name) {
-
- // If the file exists ...
- if (isset($_FILES[$input_name]) && $_FILES[$input_name]['error'] == 0) {
- return file_get_contents($_FILES[$input_name]['tmp_name']);
- }
- return false;
-
- }
-
- /**
- * Gets the jpeg contents of the resized version of an uploaded image
- * (Returns false if the uploaded file was not an image)
- *
- * @param string $input_name The name of the file input field on the submission form
- * @param int $maxwidth The maximum width of the resized image
- * @param int $maxheight The maximum height of the resized image
- * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
- * @return false|mixed The contents of the resized image, or false on failure
- */
- function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight, $square = false) {
- // If our file exists ...
- if (isset($_FILES[$input_name]) && $_FILES[$input_name]['error'] == 0) {
+ + /** + * Get the size of the specified directory. + * + * @param string $dir The full path of the directory + * @return int The size of the directory. + */ + function get_dir_size($dir,$totalsize = 0){ + $handle = @opendir($dir); + while ($file = @readdir ($handle)){ + if (eregi("^\.{1,2}$",$file)) + continue; + if(is_dir($dir.$file)){ + $totalsize = get_dir_size($dir.$file."/",$totalsize); + } else{ + $totalsize += filesize($dir.$file); + } + } + @closedir($handle); + + return($totalsize); + } + + /** + * Get the contents of an uploaded file. + * (Returns false if there was an issue.) + * + * @param string $input_name The name of the file input field on the submission form + * @return mixed|false The contents of the file, or false on failure. + */ + function get_uploaded_file($input_name) { + + // If the file exists ... + if (isset($_FILES[$input_name]) && $_FILES[$input_name]['error'] == 0) { + return file_get_contents($_FILES[$input_name]['tmp_name']); + } + return false; + + } + + /** + * Gets the jpeg contents of the resized version of an uploaded image + * (Returns false if the uploaded file was not an image) + * + * @param string $input_name The name of the file input field on the submission form + * @param int $maxwidth The maximum width of the resized image + * @param int $maxheight The maximum height of the resized image + * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped. + * @return false|mixed The contents of the resized image, or false on failure + */ + function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight, $square = false) { + // If our file exists ... + if (isset($_FILES[$input_name]) && $_FILES[$input_name]['error'] == 0) { return get_resized_image_from_existing_file($_FILES[$input_name]['tmp_name'], $maxwidth, $maxheight, $square); @@ -696,7 +738,7 @@ * * @param string $input_name The name of the file input field on the submission form * @param int $maxwidth The maximum width of the resized image - * @param int $maxheight The maximum height of the resized image
+ * @param int $maxheight The maximum height of the resized image * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped. * @return false|mixed The contents of the resized image, or false on failure */ @@ -710,19 +752,19 @@ $height = $imgsizearray[1]; $newwidth = $width; $newheight = $height; -
- // Square the image dimensions if we're wanting a square image
- if ($square) {
- if ($width < $height) {
- $height = $width;
- } else {
- $width = $height;
- }
-
- $newwidth = $width;
- $newheight = $height;
-
- }
+ + // Square the image dimensions if we're wanting a square image + if ($square) { + if ($width < $height) { + $height = $width; + } else { + $width = $height; + } + + $newwidth = $width; + $newheight = $height; + + } if ($width > $maxwidth) { $newheight = floor($height * ($maxwidth / $width)); @@ -743,39 +785,39 @@ if (array_key_exists($imgsizearray['mime'],$accepted_formats)) { $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']]; - $newimage = imagecreatetruecolor($newwidth,$newheight);
-
+ $newimage = imagecreatetruecolor($newwidth,$newheight); + if (is_callable($function) && $oldimage = $function($input_name)) { -
- // Crop the image if we need a square
- if ($square) {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $widthoffset = floor(($imgsizearray[0] - $width) / 2);
- $heightoffset = floor(($imgsizearray[1] - $height) / 2);
- } else {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = ($x2 - $x1);
- $height = $width;
- }
- } else {
- if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
- $widthoffset = 0;
- $heightoffset = 0;
- } else {
- $widthoffset = $x1;
- $heightoffset = $y1;
- $width = ($x2 - $x1);
- $height = ($y2 - $y1);
- }
+ + // Crop the image if we need a square + if ($square) { + if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) { + $widthoffset = floor(($imgsizearray[0] - $width) / 2); + $heightoffset = floor(($imgsizearray[1] - $height) / 2); + } else { + $widthoffset = $x1; + $heightoffset = $y1; + $width = ($x2 - $x1); + $height = $width; + } + } else { + if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) { + $widthoffset = 0; + $heightoffset = 0; + } else { + $widthoffset = $x1; + $heightoffset = $y1; + $width = ($x2 - $x1); + $height = ($y2 - $y1); + } }//else { - // Resize and return the image contents!
- if ($square) {
- $newheight = $maxheight;
- $newwidth = $maxwidth;
+ // Resize and return the image contents! + if ($square) { + $newheight = $maxheight; + $newwidth = $maxwidth; } - imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height);
- //}
+ imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height); + //} // imagecopyresized($newimage, $oldimage, 0,0,0,0,$newwidth,$newheight,$width,$height); ob_start(); @@ -790,9 +832,422 @@ } return false; - }
+ } + // putting these here for now + + function file_delete($guid) { + if ($file = get_entity($guid)) { + + if ($file->canEdit()) { + + $container = get_entity($file->container_guid); + + $thumbnail = $file->thumbnail; + $smallthumb = $file->smallthumb; + $largethumb = $file->largethumb; + if ($thumbnail) { + + $delfile = new ElggFile(); + $delfile->owner_guid = $file->owner_guid; + $delfile->setFilename($thumbnail); + $delfile->delete(); + + } + if ($smallthumb) { + + $delfile = new ElggFile(); + $delfile->owner_guid = $file->owner_guid; + $delfile->setFilename($smallthumb); + $delfile->delete(); + + } + if ($largethumb) { + + $delfile = new ElggFile(); + $delfile->owner_guid = $file->owner_guid; + $delfile->setFilename($largethumb); + $delfile->delete(); + + } + + return $file->delete(); + } + } + + return false; + } + + function file_handle_upload($prefix,$subtype,$plugin) { + $desc = get_input("description"); + $tags = get_input("tags"); + $tags = explode(",", $tags); + $folder = get_input("folder_text"); + if (!$folder) { + $folder = get_input("folder_select"); + } + $access_id = (int) get_input("access_id"); + $container_guid = (int) get_input('container_guid', 0); + if (!$container_guid) + $container_guid == get_loggedin_userid(); + + // Extract file from, save to default filestore (for now) + + // see if a plugin has set a quota for this user + $file_quota = trigger_plugin_hook("$plugin:quotacheck",'user',array('user'=>get_loggedin_user())); + if (!$file_quota) { + // no, see if there is a generic quota set + $file_quota = get_plugin_setting('quota', $plugin); + } + if ($file_quota) { + // convert to megabytes + $file_quota = $file_quota*1000*1024; + } + + // handle uploaded files + $number_of_files = get_input('number_of_files',0); + $quota_exceeded = false; + $bad_mime_type = false; + + for ($i = 0; $i < $number_of_files; $i++) { + + $title = get_input("title_".$i); + $uploaded = $_FILES["upload_".$i]; + if (!$uploaded || !$uploaded['name']) { + // no such file, so skip it + continue; + } + if ($plugin == "photo") { + // do a mime type test + if (in_array($uploaded['type'],array('image/jpeg','image/gif','image/png'))) { + $file = new PhotoPluginFile(); + } else { + $bad_mime_type = true; + break; + } + + } else { + $file = new FilePluginFile(); + } + $dir_size = $file->getFilestoreSize($prefix,$container_guid); + $filestorename = strtolower(time().$uploaded['name']); + $file->setFilename($prefix.$filestorename); + $file->setMimeType($uploaded['type']); + + $file->originalfilename = $uploaded['name']; + + $file->subtype = $subtype; + + $file->access_id = $access_id; + + $uf = get_uploaded_file('upload_'.$i); + + if ($file_quota) { + $file_size = strlen($uf); + if (($dir_size + $file_size) > $file_quota) { + $quota_exceeded = true; + } + } + + if (!$quota_exceeded) { + // all clear, so try to save the data + + $file->open("write"); + $file->write($uf); + $file->close(); + + $file->title = $title; + $file->description = $desc; + if ($container_guid) + $file->container_guid = $container_guid; + + // Save tags + $file->tags = $tags; + + $file->simpletype = get_general_file_type($uploaded['type']); + $file->folder = $folder; + + $result = $file->save(); + + if ($result) + { + + // Generate thumbnail (if image) + if (substr_count($file->getMimeType(),'image/')) + { + $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true); + $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true); + $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false); + if ($thumbnail) { + $thumb = new ElggFile(); + $thumb->setMimeType($uploaded['type']); + + $thumb->setFilename($prefix."thumb".$filestorename); + $thumb->open("write"); + $thumb->write($thumbnail); + $thumb->close(); + + $file->thumbnail = $prefix."thumb".$filestorename; + + $thumb->setFilename($prefix."smallthumb".$filestorename); + $thumb->open("write"); + $thumb->write($thumbsmall); + $thumb->close(); + $file->smallthumb = $prefix."smallthumb".$filestorename; + + $thumb->setFilename($prefix."largethumb".$filestorename); + $thumb->open("write"); + $thumb->write($thumblarge); + $thumb->close(); + $file->largethumb = $prefix."largethumb".$filestorename; + + } + } + + // add to this user's file folders + file_add_to_folders($folder,$container_guid,$plugin); + + add_to_river("river/object/$plugin/create",'create',$_SESSION['user']->guid,$file->guid); + } else { + break; + } + } else { + break; + } + } + + if ($quota_exceeded) { + echo elgg_echo("$plugin:quotaexceeded"); + } else if ($bad_mime_type) { + echo elgg_echo("$plugin:badmimetype"); + } else if ($result) { + if ($number_of_files > 1) { + echo elgg_echo("$plugin:saved_multi"); + } else { + echo elgg_echo("$plugin:saved"); + } + } else { + if ($number_of_files > 1) { + echo elgg_echo("$plugin:uploadfailed_multi"); + } else { + echo elgg_echo("$plugin:uploadfailed"); + } + } + } + + function file_add_to_folders($folder,$container_guid,$plugin) { + if ($container_guid && ($container = get_entity($container_guid))) { + if ($plugin == 'photo') { + $folders = $container->elgg_file_albums; + } else { + $folders = $container->elgg_file_folders; + } + if ($folders) { + if (is_array($folders)) { + if (!in_array($folder,$folders)) { + $folders[] = $folder; + if ($plugin == 'photo') { + $container->elgg_file_albums = $folders; + } else { + $container->elgg_file_folders = $folders; + } + } + } else { + if ($folders != $folder) { + if ($plugin == 'photo') { + $container->elgg_file_albums = array($folders,$folder); + } else { + $container->elgg_file_folders = array($folders,$folder); + } + } + } + } else { + + if ($plugin == 'photo') { + $container->elgg_file_albums = $folder; + } else { + $container->elgg_file_folders = $folder; + } + } + } + } + + function file_handle_save($forward,$plugin) { + // Get variables + $title = get_input("title"); + $desc = get_input("description"); + $tags = get_input("tags"); + $folder = get_input("folder_text"); + if (!$folder) { + $folder = get_input("folder_select"); + } + $access_id = (int) get_input("access_id"); + + $guid = (int) get_input('file_guid'); + + if (!$file = get_entity($guid)) { + register_error(elgg_echo("$plugin:uploadfailed")); + forward($forward . $_SESSION['user']->username); + exit; + } + + $result = false; + + $container_guid = $file->container_guid; + $container = get_entity($container_guid); + + if ($file->canEdit()) { + + $file->access_id = $access_id; + $file->title = $title; + $file->description = $desc; + $file->folder = $folder; + // add to this user's file folders + file_add_to_folders($folder,$container_guid,$plugin); + + // Save tags + $tags = explode(",", $tags); + $file->tags = $tags; + + $result = $file->save(); + } + + if ($result) + system_message(elgg_echo("$plugin:saved")); + else + register_error(elgg_echo("$plugin:uploadfailed")); + + forward($forward . $container->username); + } + + function file_manage_download($plugin) { + // Get the guid + $file_guid = get_input("file_guid"); + + // Get the file + $file = get_entity($file_guid); + + if ($file) + { + $mime = $file->getMimeType(); + if (!$mime) $mime = "application/octet-stream"; + + $filename = $file->originalfilename; + + header("Content-type: $mime"); + if (strpos($mime, "image/")!==false) + header("Content-Disposition: inline; filename=\"$filename\""); + else + header("Content-Disposition: attachment; filename=\"$filename\""); + + echo $file->grabFile(); + exit; + } + else + register_error(elgg_echo("$plugin:downloadfailed")); + } + + function file_manage_icon_download($plugin) { + // Get the guid + $file_guid = get_input("file_guid"); + + // Get the file + $file = get_entity($file_guid); + + if ($file) + { + $mime = $file->getMimeType(); + if (!$mime) $mime = "application/octet-stream"; + + $filename = $file->thumbnail; + + header("Content-type: $mime"); + if (strpos($mime, "image/")!==false) + header("Content-Disposition: inline; filename=\"$filename\""); + else + header("Content-Disposition: attachment; filename=\"$filename\""); + + + $readfile = new ElggFile(); + $readfile->owner_guid = $file->owner_guid; + $readfile->setFilename($filename); + + /* + if ($file->open("read")); + { + while (!$file->eof()) + { + echo $file->read(10240, $file->tell()); + } + } + */ + + $contents = $readfile->grabFile(); + if (empty($contents)) { + echo file_get_contents(dirname(dirname(__FILE__)) . "/graphics/icons/general.jpg" ); + } else { + echo $contents; + } + exit; + } + else + register_error(elgg_echo("$plugin:downloadfailed")); + } + + function file_display_thumbnail($file_guid,$size) { + // Get file entity + if ($file = get_entity($file_guid)) { + $subtype = $file->getSubtype(); + if (($subtype == "file") || ($subtype = "photo")) { + + $simpletype = $file->simpletype; + if ($simpletype == "image") { + + // Get file thumbnail + if ($size == "small") { + $thumbfile = $file->smallthumb; + } else { + $thumbfile = $file->largethumb; + } + + // Grab the file + if ($thumbfile && !empty($thumbfile)) { + $readfile = new ElggFile(); + $readfile->owner_guid = $file->owner_guid; + $readfile->setFilename($thumbfile); + $mime = $file->getMimeType(); + $contents = $readfile->grabFile(); + + header("Content-type: $mime"); + echo $contents; + exit; + + } + + } + + } + + } + } + + function file_set_page_owner($file) { + $page_owner = page_owner_entity(); + if ($page_owner === false || is_null($page_owner)) { + $container_guid = $file->container_guid; + if (!empty($container_guid)) + if ($page_owner = get_entity($container_guid)) { + set_page_owner($page_owner->guid); + } + if (empty($page_owner)) { + $page_owner = $_SESSION['user']; + set_page_owner($_SESSION['guid']); + } + } + } + + /// Variable holding the default datastore $DEFAULT_FILE_STORE = NULL; @@ -846,4 +1301,4 @@ // Register a startup event register_elgg_event_handler('init','system','filestore_init',100); -?>
\ No newline at end of file +?> |