]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Cleaned up the upload script
authorCash Costello <cash.costello@gmail.com>
Sat, 1 May 2010 11:41:56 +0000 (11:41 +0000)
committerCash Costello <cash.costello@gmail.com>
Sat, 1 May 2010 11:41:56 +0000 (11:41 +0000)
actions/upload.php
contributions.txt
languages/en.php

index 0913ed9378b96e175f932a741de85ea265e04c49..b1eb4efe13848d3ae40fb314ee0d2c22064868e5 100644 (file)
 <?php
-       /**
-        * Elgg multi-image uploader action
-       * 
-       * This will upload up to 10 images at at time to an album
-        */
-
-       global $CONFIG;
-       include dirname(dirname(__FILE__)) . "/lib/resize.php";
-       include dirname(dirname(__FILE__)) . "/lib/exif.php";
-
-       // Make sure we're logged in 
-       gatekeeper();
-       
-       // Get common variables
-       $access_id = (int) get_input("access_id");
-       $container_guid = (int) get_input('container_guid', 0);
-       if (!$container_guid)
-               $container_guid == $_SESSION['user']->getGUID();
-
-       $album = get_entity($container_guid);
-
-       $maxfilesize = (float) get_plugin_setting('maxfilesize','tidypics'); 
-       if (!$maxfilesize)
-               $maxfilesize = 5; // default to 5 MB if not set 
-       $maxfilesize = 1024 * 1024 * $maxfilesize; // convert to bytes from MBs
-
-       $quota = get_plugin_setting('quota','tidypics');
-       $quota = 1024 * 1024 * $quota;
-       $image_repo_size_md = get_metadata_byname($album->container_guid, "image_repo_size");
-       $image_repo_size = (int)$image_repo_size_md->value;
-
-       $image_lib = get_plugin_setting('image_lib', 'tidypics');
-       if (!$image_lib)
-               $image_lib = "GD";
-
-       // post limit exceeded
-       if (count($_FILES) == 0) {
-               trigger_error('Tidypics warning: user exceeded post limit on image upload', E_USER_WARNING);
-               register_error(elgg_echo('tidypics:exceedpostlimit'));
-               forward(get_input('forward_url', $_SERVER['HTTP_REFERER']));
+/**
+ * Elgg multi-image uploader action
+ *
+ * This will upload up to 10 images at at time to an album
+ */
+
+include dirname(dirname(__FILE__)) . "/lib/resize.php";
+include dirname(dirname(__FILE__)) . "/lib/exif.php";
+
+// Get common variables
+$access_id = (int) get_input("access_id");
+$container_guid = (int) get_input('container_guid', 0);
+$album = get_entity($container_guid);
+if (!$album) {
+       register_error(elgg_echo('tidypics:baduploadform'));
+       forward($_SERVER['HTTP_REFERER']);
+}
+
+$maxfilesize = (float) get_plugin_setting('maxfilesize','tidypics'); 
+if (!$maxfilesize) {
+       $maxfilesize = 5; // default to 5 MB if not set
+}
+$maxfilesize = 1024 * 1024 * $maxfilesize; // convert to bytes from MBs
+
+$quota = get_plugin_setting('quota','tidypics');
+$quota = 1024 * 1024 * $quota;
+$image_repo_size_md = get_metadata_byname($album->container_guid, "image_repo_size");
+$image_repo_size = (int)$image_repo_size_md->value;
+
+$image_lib = get_plugin_setting('image_lib', 'tidypics');
+if (!$image_lib) {
+       $image_lib = "GD";
+}
+
+// post limit exceeded
+if (count($_FILES) == 0) {
+       trigger_error('Tidypics warning: user exceeded post limit on image upload', E_USER_WARNING);
+       register_error(elgg_echo('tidypics:exceedpostlimit'));
+       forward($_SERVER['HTTP_REFERER']);
+}
+
+// test to make sure at least 1 image was selected by user
+$num_images = 0;
+foreach($_FILES as $key => $sent_file) {
+       if (!empty($sent_file['name'])) {
+               $num_images++;
        }
+}
+if ($num_images == 0) {
+       // have user try again
+       register_error(elgg_echo('tidypics:noimages'));
+       forward($_SERVER['HTTP_REFERER']);
+}
 
-       // test to make sure at least 1 image was selected by user
-       $num_images = 0;
-       foreach($_FILES as $key => $sent_file) {
-               if (!empty($sent_file['name']))
-                       $num_images++;
-       }
-       if ($num_images == 0) {
-               // have user try again
-               register_error(elgg_echo('tidypics:noimages'));
-               forward(get_input('forward_url', $_SERVER['HTTP_REFERER']));
+$uploaded_images = array();
+$not_uploaded = array();
+$error_msgs = array();
+
+$img_river_view = get_plugin_setting('img_river_view', 'tidypics');
+
+$accepted_formats = array(
+               'image/jpeg',
+               'image/png',
+               'image/gif',
+               'image/pjpeg',
+               'image/x-png',
+);
+
+
+foreach($_FILES as $key => $sent_file) {
+
+       // skip empty entries
+       if (empty($sent_file['name'])) {
+               continue;
        }
 
-       $uploaded_images = array();
-       $not_uploaded = array();
-       $error_msgs = array();
-       
-       $img_river_view = get_plugin_setting('img_river_view', 'tidypics');
-       
-       $accepted_formats = array(
-                                                               'image/jpeg',
-                                                               'image/png',
-                                                               'image/gif',
-                                                               'image/pjpeg',
-                                                               'image/x-png',
-                                                               );
-
-
-       foreach($_FILES as $key => $sent_file) {
-               
-               // skip empty entries 
-               if (empty($sent_file['name']))
-                       continue;
-               
-               $name = $sent_file['name'];
-               $mime = $sent_file['type'];
+       $name = $sent_file['name'];
+       $mime = $sent_file['type'];
 
-               if ($sent_file['error']) {
-                       array_push($not_uploaded, $sent_file['name']);
-                       if ($sent_file['error'] == 1) {
-                               trigger_error('Tidypics warning: image exceed server php upload limit', E_USER_WARNING);
-                               array_push($error_msgs, elgg_echo('tidypics:image_mem'));
-                       }
-                       else {
-                               array_push($error_msgs, elgg_echo('tidypics:unk_error'));
-                       }
-                       continue;
-               }
-               
-               //make sure file is an image
-               if (!in_array($mime, $accepted_formats)) {
-                       array_push($not_uploaded, $sent_file['name']);
-                       array_push($error_msgs, elgg_echo('tidypics:not_image'));
-                       continue;
+       if ($sent_file['error']) {
+               array_push($not_uploaded, $sent_file['name']);
+               if ($sent_file['error'] == 1) {
+                       trigger_error('Tidypics warning: image exceeded server php upload limit', E_USER_WARNING);
+                       array_push($error_msgs, elgg_echo('tidypics:image_mem'));
+               } else {
+                       array_push($error_msgs, elgg_echo('tidypics:unk_error'));
                }
+               continue;
+       }
 
-               // check quota
-               if ($quota) {
-                       if ($image_repo_size + $sent_file['size'] > $quota) {
-                               array_push($not_uploaded, $sent_file['name']);
-                               array_push($error_msgs, elgg_echo('tidypics:exceed_quota'));
-                               continue;
-                       }
-               }
-               
-               // make sure file does not exceed memory limit
-               if ($sent_file['size'] > $maxfilesize) {
+       //make sure file is an image
+       if (!in_array($mime, $accepted_formats)) {
+               array_push($not_uploaded, $sent_file['name']);
+               array_push($error_msgs, elgg_echo('tidypics:not_image'));
+               continue;
+       }
+
+       // check quota
+       if ($quota) {
+               if ($image_repo_size + $sent_file['size'] > $quota) {
                        array_push($not_uploaded, $sent_file['name']);
-                       array_push($error_msgs, elgg_echo('tidypics:image_mem'));
+                       array_push($error_msgs, elgg_echo('tidypics:exceed_quota'));
                        continue;
                }
-               
-               // make sure the in memory image size does not exceed memory available - GD only
-               $imginfo = getimagesize($sent_file['tmp_name']);
-               $mem_avail = ini_get('memory_limit');
-               $mem_avail = rtrim($mem_avail, 'M');
-               $mem_avail = $mem_avail * 1024 * 1024;
-               if ($image_lib == 'GD') {
-                       $mem_required = ceil(5.35 * $imginfo[0] * $imginfo[1]);
-                       
-                       $mem_used = memory_get_usage();
-                               
-                       $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer
-                       if ($mem_required > $mem_avail) {
-                               array_push($not_uploaded, $sent_file['name']);
-                               array_push($error_msgs, elgg_echo('tidypics:image_pixels'));
-                               trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
-                               continue;
-                       }
-               } else if ($image_lib == 'ImageMagickPHP') {
-                       // haven't been able to determine a limit like there is for GD
-               }
+       }
 
-               //this will save to users folder in /image/ and organize by photo album
-               $prefix = "image/" . $container_guid . "/";
-               $file = new ElggFile();
-               $filestorename = strtolower(time().$name);
-               $file->setFilename($prefix.$filestorename);
-               $file->setMimeType($mime);
-               $file->originalfilename = $name;
-               $file->subtype="image";
-               $file->simpletype="image";
-               $file->access_id = $access_id;
-               if ($container_guid) {
-                       $file->container_guid = $container_guid;
-               }
-               $file->open("write");
-               $file->write(get_uploaded_file($key));
-               $file->close();
-               $result = $file->save();
+       // make sure file does not exceed memory limit
+       if ($sent_file['size'] > $maxfilesize) {
+               array_push($not_uploaded, $sent_file['name']);
+               array_push($error_msgs, elgg_echo('tidypics:image_mem'));
+               continue;
+       }
+
+       // make sure the in memory image size does not exceed memory available - GD only
+       $imginfo = getimagesize($sent_file['tmp_name']);
+       $mem_avail = ini_get('memory_limit');
+       $mem_avail = rtrim($mem_avail, 'M');
+       $mem_avail = $mem_avail * 1024 * 1024;
+       if ($image_lib == 'GD') {
+               $mem_required = ceil(5.35 * $imginfo[0] * $imginfo[1]);
 
-               if (!$result) {
+               $mem_used = memory_get_usage();
+
+               $mem_avail = $mem_avail - $mem_used - 2097152; // 2 MB buffer
+               if ($mem_required > $mem_avail) {
                        array_push($not_uploaded, $sent_file['name']);
-                       array_push($error_msgs, elgg_echo('tidypics:save_error'));
+                       array_push($error_msgs, elgg_echo('tidypics:image_pixels'));
+                       trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING);
                        continue;
                }
-               
-               //get and store the exif data
-               td_get_exif($file);
-               
-               
-               // resize photos to create thumbnails
-               if ($image_lib == 'ImageMagick') { // ImageMagick command line
-                       
-                       if (tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename) != true) {
-                               trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick command line', E_USER_WARNING);
-                       }
-                       
-               } else if ($image_lib == 'ImageMagickPHP') {  // imagick php extension 
-                       
-                       if (tp_create_imagick_thumbnails($file, $prefix, $filestorename) != true) {
-                               trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick PHP', E_USER_WARNING);
-                       }
-               
-               } else { 
-                       
-                       if (tp_create_gd_thumbnails($file, $prefix, $filestorename) != true) {
-                               trigger_error('Tidypics warning: failed to create thumbnails - GD', E_USER_WARNING);
-                       }
-                       
-               } // end of image library selector
-
-               //keep one file handy so we can add a notice to the river if single image option selected
-               if(!$file_for_river) {
-                       $file_for_river = $file;
-               }
+       } else if ($image_lib == 'ImageMagickPHP') {
+               // haven't been able to determine a limit like there is for GD
+       }
 
-               array_push($uploaded_images, $file->guid);
+       //this will save to users folder in /image/ and organize by photo album
+       $prefix = "image/" . $container_guid . "/";
+       $file = new ElggFile();
+       $filestorename = strtolower(time().$name);
+       $file->setFilename($prefix.$filestorename);
+       $file->setMimeType($mime);
+       $file->originalfilename = $name;
+       $file->subtype="image";
+       $file->simpletype="image";
+       $file->access_id = $access_id;
+       if ($container_guid) {
+               $file->container_guid = $container_guid;
+       }
+       $file->open("write");
+       $file->write(get_uploaded_file($key));
+       $file->close();
+       $result = $file->save();
+
+       if (!$result) {
+               array_push($not_uploaded, $sent_file['name']);
+               array_push($error_msgs, elgg_echo('tidypics:save_error'));
+               continue;
+       }
 
-               // update user/group size for checking quota
-               $image_repo_size += $sent_file['size'];
+       //get and store the exif data
+       td_get_exif($file);
 
-               // plugins can register to be told when a new image has been uploaded
-               trigger_elgg_event('upload', 'tp_image', $file);
-               
-               // successful upload so check if this is a new album and throw river event/notification if so
-               if ($album->new_album == TP_NEW_ALBUM) {
-                       $album->new_album = TP_OLD_ALBUM;
 
-                       // we throw the notification manually here so users are not told about the new album until there
-                       // is at least a few photos in it
-                       object_notifications('create', 'object', $album);
-                       
-                       if (function_exists('add_to_river'))
-                               add_to_river('river/object/album/create', 'create', $album->owner_guid, $album->guid);
-               }
-       
-               if ($img_river_view == "all") {
-                       add_to_river('river/object/image/create', 'create', $file->getObjectOwnerGUID(), $file->getGUID());
+       // resize photos to create thumbnails
+       if ($image_lib == 'ImageMagick') { // ImageMagick command line
+
+               if (tp_create_im_cmdline_thumbnails($file, $prefix, $filestorename) != true) {
+                       trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick command line', E_USER_WARNING);
                }
-               unset($file);  // may not be needed but there seems to be a memory leak
-
-       } //end of for loop
-                               
-       if (count($not_uploaded) > 0) {
-               if (count($uploaded_images) > 0)
-                       $error = sprintf(elgg_echo("tidypics:partialuploadfailure"), count($not_uploaded), count($not_uploaded) + count($uploaded_images))  . '<br />';
-               else
-                       $error = elgg_echo("tidypics:completeuploadfailure") . '<br />';
-
-               $num_failures = count($not_uploaded);
-               for ($i = 0; $i < $num_failures; $i++) {
-                       $error .= "{$not_uploaded[$i]}: {$error_msgs[$i]} <br />";
+
+       } else if ($image_lib == 'ImageMagickPHP') {  // imagick php extension
+
+               if (tp_create_imagick_thumbnails($file, $prefix, $filestorename) != true) {
+                       trigger_error('Tidypics warning: failed to create thumbnails - ImageMagick PHP', E_USER_WARNING);
                }
-               register_error($error);
-               
-               if (count($uploaded_images) == 0)
-                       forward(get_input('forward_url', $_SERVER['HTTP_REFERER'])); //upload failed, so forward to previous page
-               else {
-                       // some images did upload so we fall through
+
+       } else {
+
+               if (tp_create_gd_thumbnails($file, $prefix, $filestorename) != true) {
+                       trigger_error('Tidypics warning: failed to create thumbnails - GD', E_USER_WARNING);
                }
+
+       } // end of image library selector
+
+       //keep one file handy so we can add a notice to the river if single image option selected
+       if (!$file_for_river) {
+               $file_for_river = $file;
+       }
+
+       array_push($uploaded_images, $file->guid);
+
+       // update user/group size for checking quota
+       $image_repo_size += $sent_file['size'];
+
+       // plugins can register to be told when a new image has been uploaded
+       trigger_elgg_event('upload', 'tp_image', $file);
+
+       // successful upload so check if this is a new album and throw river event/notification if so
+       if ($album->new_album == TP_NEW_ALBUM) {
+               $album->new_album = TP_OLD_ALBUM;
+
+               // we throw the notification manually here so users are not told about the new album until there
+               // is at least a few photos in it
+               object_notifications('create', 'object', $album);
+
+               add_to_river('river/object/album/create', 'create', $album->owner_guid, $album->guid);
+       }
+
+       if ($img_river_view == "all") {
+               add_to_river('river/object/image/create', 'create', $file->getObjectOwnerGUID(), $file->getGUID());
+       }
+       unset($file);  // may not be needed but there seems to be a memory leak
+
+} //end of for loop
+
+if (count($not_uploaded) > 0) {
+       if (count($uploaded_images) > 0) {
+               $error = sprintf(elgg_echo("tidypics:partialuploadfailure"), count($not_uploaded), count($not_uploaded) + count($uploaded_images))  . '<br />';
        } else {
-                       system_message(elgg_echo('tidypics:upl_success'));
+               $error = elgg_echo("tidypics:completeuploadfailure") . '<br />';
        }
 
-       
-       if (count($uploaded_images) && $img_river_view == "1") {
-               if (function_exists('add_to_river')) {
-                       add_to_river('river/object/image/create', 'create', $file_for_river->getObjectOwnerGUID(), $file_for_river->getGUID());
-               }
+       $num_failures = count($not_uploaded);
+       for ($i = 0; $i < $num_failures; $i++) {
+               $error .= "{$not_uploaded[$i]}: {$error_msgs[$i]} <br />";
+       }
+       register_error($error);
+
+       if (count($uploaded_images) == 0) {
+               //upload failed, so forward to previous page
+               forward($_SERVER['HTTP_REFERER']);
+       } else {
+               // some images did upload so we fall through
        }
-       
-       // update image repo size
-       create_metadata($album->container_guid, "image_repo_size", $image_repo_size, 'integer', $album->container_guid);
-       
-       // plugins can register to be told when a Tidypics album has had images added
-       trigger_elgg_event('upload', 'tp_album', $album);
-       
-       
-       //forward to multi-image edit page
-       forward($CONFIG->wwwroot . 'mod/tidypics/pages/edit_multiple.php?files=' . implode('-', $uploaded_images)); 
-
-?>
+} else {
+       system_message(elgg_echo('tidypics:upl_success'));
+}
+
+
+if (count($uploaded_images) && $img_river_view == "1") {
+       add_to_river('river/object/image/create', 'create', $file_for_river->getObjectOwnerGUID(), $file_for_river->getGUID());
+}
+
+// update image repo size
+create_metadata($album->container_guid, "image_repo_size", $image_repo_size, 'integer', $album->container_guid);
+
+// plugins can register to be told when a Tidypics album has had images added
+trigger_elgg_event('upload', 'tp_album', $album);
+
+
+//forward to multi-image edit page
+forward($CONFIG->wwwroot . 'mod/tidypics/pages/edit_multiple.php?files=' . implode('-', $uploaded_images)); 
index fac5edc6f4ef817dd9622e2343013840d0c05061..2e09ca1cf904aadb9a6028fb7a05ff8766ccfb21 100644 (file)
@@ -7,7 +7,7 @@ Release Date: 05/01/2010
 ------------------------------------------------------------------------\r
 BEGIN VERSION 1.6.8 CHANGES\r
 ------------------------------------------------------------------------\r
- * New language strings: tidypics:nophotosingroup\r
+ * New language strings: tidypics:nophotosingroup, tidypics:baduploadform\r
  * Fixed bug in titles of albums RSS\r
  * Added a new css element: tidypics_line_break\r
  * Fixed layout of album covers due to long titles or other text\r
index 7211ed9be889632ff1b1c730e0c99358693f3cd3..30cef2db484796884e584ee725707273764dac71 100644 (file)
@@ -191,6 +191,7 @@ The photo can be viewed here: %s",
 
                //Error messages
 
+                       'tidypics:baduploadform' => "There was an error with the upload form",
                        'tidypics:partialuploadfailure' => "There were errors uploading some of the images (%s of %s images).",
                        'tidypics:completeuploadfailure' => "Upload of images failed.",
                        'tidypics:exceedpostlimit' => "Too many large images - try to upload fewer or smaller images.",