aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/filestore.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/lib/filestore.php')
-rw-r--r--engine/lib/filestore.php554
1 files changed, 118 insertions, 436 deletions
diff --git a/engine/lib/filestore.php b/engine/lib/filestore.php
index 74a8b0315..a3c7ba439 100644
--- a/engine/lib/filestore.php
+++ b/engine/lib/filestore.php
@@ -1,36 +1,30 @@
<?php
/**
* Elgg filestore.
- * This file contains classes, interfaces and functions for saving and retrieving data to various file
- * stores.
+ * This file contains classes, interfaces and functions for
+ * saving and retrieving data to various file stores.
*
- * @package Elgg
- * @subpackage API
- * @author Curverider Ltd
- * @link http://elgg.org/
+ * @package Elgg.Core
+ * @subpackage DataModel.FileStorage
*/
-include_once("objects.php");
-
-require_once dirname(dirname(__FILE__)).'/classes/ElggFilestore.php';
-require_once dirname(dirname(__FILE__)).'/classes/ElggDiskFilestore.php';
-require_once dirname(dirname(__FILE__)).'/classes/ElggFile.php';
-
/**
* Get the size of the specified directory.
*
- * @param string $dir The full path of the directory
+ * @param string $dir The full path of the directory
+ * @param int $totalsize Add to current dir size
+ *
* @return int The size of the directory.
*/
-function get_dir_size($dir, $totalsize = 0){
+function get_dir_size($dir, $totalsize = 0) {
$handle = @opendir($dir);
- while ($file = @readdir ($handle)){
+ while ($file = @readdir($handle)) {
if (eregi("^\.{1,2}$", $file)) {
continue;
}
- if(is_dir($dir . $file)) {
+ if (is_dir($dir . $file)) {
$totalsize = get_dir_size($dir . $file . "/", $totalsize);
- } else{
+ } else {
$totalsize += filesize($dir . $file);
}
}
@@ -44,6 +38,7 @@ function get_dir_size($dir, $totalsize = 0){
* (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) {
@@ -59,16 +54,24 @@ function get_uploaded_file($input_name) {
* (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.
+ * @param int $maxwidth The maximum width of the resized image
+ * @param int $maxheight The maximum height of the resized image
+ * @param bool $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.
+ * @param bool $upscale Resize images smaller than $maxwidth x $maxheight?
+ *
* @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) {
+function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight,
+$square = false, $upscale = 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);
+ return get_resized_image_from_existing_file($_FILES[$input_name]['tmp_name'], $maxwidth,
+ $maxheight, $square, 0, 0, 0, 0, $upscale);
}
+
return false;
}
@@ -77,21 +80,24 @@ function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight
* (Returns false if the file was not an image)
*
* @param string $input_name The name of the file on the disk
- * @param int $maxwidth The desired width of the resized image
- * @param int $maxheight The desired height of the resized image
- * @param true|false $square If set to true, takes the smallest of maxwidth and
- * maxheight and use it to set the dimensions on the new image. If no
- * crop parameters are set, the largest square that fits in the image
- * centered will be used for the resize. If square, the crop must be a
- * square region.
- * @param int $x1 x coordinate for top, left corner
- * @param int $y1 y coordinate for top, left corner
- * @param int $x2 x coordinate for bottom, right corner
- * @param int $y2 y coordinate for bottom, right corner
- * @param bool $upscale Resize images smaller than $maxwidth x $maxheight?
+ * @param int $maxwidth The desired width of the resized image
+ * @param int $maxheight The desired height of the resized image
+ * @param bool $square If set to true, takes the smallest of maxwidth and
+ * maxheight and use it to set the dimensions on the new image.
+ * If no crop parameters are set, the largest square that fits
+ * in the image centered will be used for the resize. If square,
+ * the crop must be a square region.
+ * @param int $x1 x coordinate for top, left corner
+ * @param int $y1 y coordinate for top, left corner
+ * @param int $x2 x coordinate for bottom, right corner
+ * @param int $y2 y coordinate for bottom, right corner
+ * @param bool $upscale Resize images smaller than $maxwidth x $maxheight?
+ *
* @return false|mixed The contents of the resized image, or false on failure
*/
-function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) {
+function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE,
+$x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) {
+
// Get the size information from the image
$imgsizearray = getimagesize($input_name);
if ($imgsizearray == FALSE) {
@@ -143,6 +149,12 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight
return FALSE;
}
+ // color transparencies white (default is black)
+ imagefilledrectangle(
+ $new_image, 0, 0, $params['newwidth'], $params['newheight'],
+ imagecolorallocate($new_image, 255, 255, 255)
+ );
+
$rtn_code = imagecopyresampled( $new_image,
$original_image,
0,
@@ -171,9 +183,10 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight
/**
* Calculate the parameters for resizing an image
*
- * @param int $width Width of the original image
- * @param int $height Height of the original image
+ * @param int $width Width of the original image
+ * @param int $height Height of the original image
* @param array $options See $defaults for the options
+ *
* @return array or FALSE
* @since 1.7.2
*/
@@ -182,7 +195,7 @@ function get_image_resize_parameters($width, $height, $options) {
$defaults = array(
'maxwidth' => 100,
'maxheight' => 100,
-
+
'square' => FALSE,
'upscale' => FALSE,
@@ -241,7 +254,7 @@ function get_image_resize_parameters($width, $height, $options) {
} else {
// non-square new image
$new_width = $maxwidth;
- $new_height = $maxwidth;
+ $new_height = $maxheight;
// maintain aspect ratio of original image/crop
if (($selection_height / (float)$new_height) > ($selection_width / (float)$new_width)) {
@@ -260,21 +273,17 @@ function get_image_resize_parameters($width, $height, $options) {
}
}
- // check for upscaling
- if (!$upscale && ($height < $new_height || $width < $new_width)) {
- // determine if we can scale it down at all
- // (ie, if only one dimension is too small)
- // if not, just use original size.
- if ($height < $new_height && $width < $new_width) {
- $ratio = 1;
- } elseif ($height < $new_height) {
- $ratio = $new_width / $width;
- } elseif ($width < $new_width) {
- $ratio = $new_height / $height;
+ if (!$upscale && ($selection_height < $new_height || $selection_width < $new_width)) {
+ // we cannot upscale and selected area is too small so we decrease size of returned image
+ if ($square) {
+ $new_height = $selection_height;
+ $new_width = $selection_width;
+ } else {
+ if ($selection_height < $new_height && $selection_width < $new_width) {
+ $new_height = $selection_height;
+ $new_width = $selection_width;
+ }
}
-
- $selection_height = $height;
- $selection_width = $width;
}
$params = array(
@@ -289,12 +298,16 @@ function get_image_resize_parameters($width, $height, $options) {
return $params;
}
-// putting these here for now
+/**
+ * Delete an ElggFile file
+ *
+ * @param int $guid ElggFile GUID
+ *
+ * @return bool
+ */
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;
@@ -328,6 +341,7 @@ function file_delete($guid) {
* Returns an overall file type from the mimetype
*
* @param string $mimetype The MIME type
+ *
* @return string The overall type
*/
function file_get_general_file_type($mimetype) {
@@ -341,397 +355,35 @@ function file_get_general_file_type($mimetype) {
break;
}
- if (substr_count($mimetype,'text/')) {
+ if (substr_count($mimetype, 'text/')) {
return "document";
}
- if (substr_count($mimetype,'audio/')) {
+ if (substr_count($mimetype, 'audio/')) {
return "audio";
}
- if (substr_count($mimetype,'image/')) {
+ if (substr_count($mimetype, 'image/')) {
return "image";
}
- if (substr_count($mimetype,'video/')) {
+ if (substr_count($mimetype, 'video/')) {
return "video";
}
- if (substr_count($mimetype,'opendocument')) {
+ if (substr_count($mimetype, 'opendocument')) {
return "document";
}
return "general";
}
-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('container_guid'=>$container_guid));
- 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','image/jpg','image/jpe','image/pjpeg','image/x-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 = file_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))) {
- $folder_field_name = 'elgg_'.$plugin.'_folders';
- $folders = $container->$folder_field_name;
- if ($folders) {
- if (is_array($folders)) {
- if (!in_array($folder,$folders)) {
- $folders[] = $folder;
- $container->$folder_field_name = $folders;
- }
- } else {
- if ($folders != $folder) {
- $container->$folder_field_name = array($folders,$folder);
- }
- }
- } else {
- $container->$folder_field_name = $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);
-}
-
-/**
- * Manage a file download.
- *
- * @param unknown_type $plugin
- * @param unknown_type $file_guid If not specified then file_guid will be found in input.
- */
-function file_manage_download($plugin, $file_guid = "") {
- // Get the guid
- $file_guid = (int)$file_guid;
-
- if (!$file_guid) {
- $file_guid = (int)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"));
- }
-}
-
/**
- * Manage the download of a file icon.
+ * Delete a directory and all its contents
*
- * @param unknown_type $plugin
- * @param unknown_type $file_guid The guid, if not specified this is obtained from the input.
- */
-function file_manage_icon_download($plugin, $file_guid = "") {
- // Get the guid
- $file_guid = (int)$file_guid;
-
- if (!$file_guid) {
- $file_guid = (int)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)) {
- $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']);
- }
- }
-}
-
-/**
- * Recursively delete a directory
+ * @param string $directory Directory to delete
*
- * @param str $directory
+ * @return bool
*/
function delete_directory($directory) {
// sanity check: must be a directory
@@ -765,7 +417,11 @@ function delete_directory($directory) {
/**
* Removes all user files
*
- * @param ElggUser $user
+ * @warning This only deletes the physical files and not their entities.
+ * This will result in FileExceptions being thrown. Don't use this function.
+ *
+ * @param ElggUser $user And ElggUser
+ *
* @return void
*/
function clear_user_files($user) {
@@ -795,6 +451,10 @@ function get_default_filestore() {
/**
* Set the default filestore for the system.
+ *
+ * @param ElggFilestore $filestore An ElggFilestore object.
+ *
+ * @return true
*/
function set_default_filestore(ElggFilestore $filestore) {
global $DEFAULT_FILE_STORE;
@@ -805,7 +465,11 @@ function set_default_filestore(ElggFilestore $filestore) {
}
/**
- * Run once and only once.
+ * Register entity type objects, subtype file as
+ * ElggFile.
+ *
+ * @return void
+ * @access private
*/
function filestore_run_once() {
// Register a class
@@ -814,25 +478,43 @@ function filestore_run_once() {
/**
* Initialise the file modules.
- * Listens to system boot and registers any appropriate file types and classes
+ * Listens to system init and configures the default filestore
+ *
+ * @return void
+ * @access private
*/
function filestore_init() {
global $CONFIG;
// Now register a default filestore
- set_default_filestore(new ElggDiskFilestore($CONFIG->dataroot));
-
+ if (isset($CONFIG->dataroot)) {
+ set_default_filestore(new ElggDiskFilestore($CONFIG->dataroot));
+ }
+
// Now run this stuff, but only once
run_function_once("filestore_run_once");
}
-// Register a startup event
-register_elgg_event_handler('init', 'system', 'filestore_init', 100);
-
-// Unit testing
-register_plugin_hook('unit_test', 'system', 'filestore_test');
+/**
+ * Unit tests for files
+ *
+ * @param string $hook unit_test
+ * @param string $type system
+ * @param mixed $value Array of tests
+ * @param mixed $params Params
+ *
+ * @return array
+ * @access private
+ */
function filestore_test($hook, $type, $value, $params) {
global $CONFIG;
$value[] = "{$CONFIG->path}engine/tests/objects/filestore.php";
return $value;
}
+
+
+// Register a startup event
+elgg_register_event_handler('init', 'system', 'filestore_init', 100);
+
+// Unit testing
+elgg_register_plugin_hook_handler('unit_test', 'system', 'filestore_test');