From 4766f36a4d74924f21ff329c4318ce4e069ffa04 Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 3 Mar 2010 17:53:05 +0000 Subject: Pulled in the interface changes. git-svn-id: http://code.elgg.org/elgg/trunk@5257 36083f99-b078-4883-b0ff-0f9b5a30f544 --- mod/file/actions/upload.php | 171 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 mod/file/actions/upload.php (limited to 'mod/file/actions/upload.php') diff --git a/mod/file/actions/upload.php b/mod/file/actions/upload.php new file mode 100644 index 000000000..e515c73e0 --- /dev/null +++ b/mod/file/actions/upload.php @@ -0,0 +1,171 @@ + 0) { + $new_file = false; + } + + if ($new_file) { + // must have a file if a new file upload + if (empty($_FILES['upload']['name'])) { + // cache information in session + $_SESSION['uploadtitle'] = $title; + $_SESSION['uploaddesc'] = $desc; + $_SESSION['uploadtags'] = $tags; + $_SESSION['uploadaccessid'] = $access_id; + + register_error(elgg_echo('file:nofile')); + forward($_SERVER['HTTP_REFERER']); + } + + $file = new FilePluginFile(); + $file->subtype = "file"; + + // if no title on new upload, grab filename + if (empty($title)) { + $title = $_FILES['upload']['name']; + } + + } else { + // load original file object + $file = get_entity($guid); + if (!$file) { + register_error(elgg_echo('file:cannotload')); + forward($_SERVER['HTTP_REFERER']); + } + + // user must be able to edit file + if (!$file->canEdit()) { + register_error(elgg_echo('file:noaccess')); + forward($_SERVER['HTTP_REFERER']); + } + } + + $file->title = $title; + $file->description = $desc; + $file->access_id = $access_id; + $file->container_guid = $container_guid; + + $tags = explode(",", $tags); + $file->tags = $tags; + + // we have a file upload, so process it + if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) { + + $prefix = "file/"; + + // if previous file, delete it + if ($new_file == false) { + $filename = $file->getFilenameOnFilestore(); + if (file_exists($filename)) { + unlink($filename); + } + + // use same filename on the disk - ensures thumbnails are overwritten + $filestorename = $file->getFilename(); + $filestorename = substr($filestorename, strlen($prefix)); + } else { + $filestorename = strtolower(time().$_FILES['upload']['name']); + } + + $file->setFilename($prefix.$filestorename); + $file->setMimeType($_FILES['upload']['type']); + $file->originalfilename = $_FILES['upload']['name']; + $file->simpletype = get_general_file_type($_FILES['upload']['type']); + + $file->open("write"); + $file->write(get_uploaded_file('upload')); + $file->close(); + + $guid = $file->save(); + + // if image, we need to create thumbnails (this should be moved into a function) + if ($guid && $file->simpletype == "image") { + $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true); + if ($thumbnail) { + $thumb = new ElggFile(); + $thumb->setMimeType($_FILES['upload']['type']); + + $thumb->setFilename($prefix."thumb".$filestorename); + $thumb->open("write"); + $thumb->write($thumbnail); + $thumb->close(); + + $file->thumbnail = $prefix."thumb".$filestorename; + unset($thumbnail); + } + + $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true); + if ($thumbsmall) { + $thumb->setFilename($prefix."smallthumb".$filestorename); + $thumb->open("write"); + $thumb->write($thumbsmall); + $thumb->close(); + $file->smallthumb = $prefix."smallthumb".$filestorename; + unset($thumbsmall); + } + + $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false); + if ($thumblarge) { + $thumb->setFilename($prefix."largethumb".$filestorename); + $thumb->open("write"); + $thumb->write($thumblarge); + $thumb->close(); + $file->largethumb = $prefix."largethumb".$filestorename; + unset($thumblarge); + } + } + } + + // make sure session cache is cleared + unset($_SESSION['uploadtitle']); + unset($_SESSION['uploaddesc']); + unset($_SESSION['uploadtags']); + unset($_SESSION['uploadaccessid']); + + // handle results differently for new files and file updates + if ($new_file) { + if ($guid) { + system_message(elgg_echo("file:saved")); + add_to_river('river/object/file/create', 'create', get_loggedin_userid(), $file->guid); + } else { + // failed to save file object - nothing we can do about this + register_error(elgg_echo("file:uploadfailed")); + } + + $container_user = get_entity($container_guid); + forward($CONFIG->wwwroot . "pg/file/" . $container_user->username); + + } else { + if ($guid) { + system_message(elgg_echo("file:saved")); + } else { + register_error(elgg_echo("file:uploadfailed")); + } + + forward($file->getURL()); + } -- cgit v1.2.3