From 5cc8de829c0a7b86c8df27293f4825d0e340d592 Mon Sep 17 00:00:00 2001 From: Brett Profitt Date: Fri, 16 Mar 2012 22:51:12 -0700 Subject: Fixes #14. Fixed uploadify. --- actions/ajax_upload.php | 61 ---------- actions/ajax_upload_complete.php | 56 --------- actions/photos/image/ajax_upload.php | 57 +++++++++ actions/photos/image/ajax_upload_complete.php | 74 +++++++++++ actions/photos/image/upload.php | 2 + classes/TidypicsImage.php | 2 +- lib/tidypics.php | 2 +- lib/watermark.php | 8 +- pages/photos/image/upload.php | 8 +- start.php | 43 ++++--- views/default/forms/photos/ajax_upload.php | 66 +++++----- views/default/js/photos/uploading.php | 85 +++++++++++-- views/default/photos/css.php | 169 +++++++++++++------------- 13 files changed, 358 insertions(+), 275 deletions(-) delete mode 100644 actions/ajax_upload.php delete mode 100644 actions/ajax_upload_complete.php create mode 100644 actions/photos/image/ajax_upload.php create mode 100644 actions/photos/image/ajax_upload_complete.php diff --git a/actions/ajax_upload.php b/actions/ajax_upload.php deleted file mode 100644 index 1f5588197..000000000 --- a/actions/ajax_upload.php +++ /dev/null @@ -1,61 +0,0 @@ -container_guid = $album_guid; -$image->setMimeType($mime); -$image->simpletype = "image"; -$image->access_id = $album->access_id; -$image->title = substr($name, 0, strrpos($name, '.')); -$image->batch = get_input('batch'); -$result = $image->save(); - -$image->setOriginalFilename($name); -$image->saveImageFile($temp_file, $file_size); - -$image->extractExifData(); -$image->saveThumbnails($image_lib); - -$album->prependImageList(array($image->guid)); - - -if (get_plugin_setting('img_river_view', 'tidypics') === "all") { - add_to_river('river/object/image/create', 'create', $image->owner_guid, $image->guid); -} - -echo "success"; -exit; \ No newline at end of file diff --git a/actions/ajax_upload_complete.php b/actions/ajax_upload_complete.php deleted file mode 100644 index 2abca50dc..000000000 --- a/actions/ajax_upload_complete.php +++ /dev/null @@ -1,56 +0,0 @@ -new_album == TP_NEW_ALBUM) { - $new_album = true; -} else { - $new_album = false; -} - -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); -} - -$params = array( - 'type' => 'object', - 'subtype' => 'image', - 'metadata_names' => 'batch', - 'metadata_values' => get_input('batch'), -); -$images = elgg_get_entities_from_metadata($params); -if ($images) { - // Create a new batch object to contain these photos - $batch = new ElggObject(); - $batch->subtype = "tidypics_batch"; - $batch->access_id = ACCESS_PUBLIC; - $batch->container_guid = $album->guid; - if ($batch->save()) { - foreach ($images as $image) { - add_entity_relationship($image->guid, "belongs_to_batch", $batch->getGUID()); - } - if (get_plugin_setting('img_river_view', 'tidypics') == "batch" && $new_album == false) { - add_to_river('river/object/tidypics_batch/create', 'create', $batch->getObjectOwnerGUID(), $batch->getGUID()); - } - } -} - -// plugins can register to be told when a Tidypics album has had images added -trigger_elgg_event('upload', 'tp_album', $album); - -exit; \ No newline at end of file diff --git a/actions/photos/image/ajax_upload.php b/actions/photos/image/ajax_upload.php new file mode 100644 index 000000000..d6b083cf6 --- /dev/null +++ b/actions/photos/image/ajax_upload.php @@ -0,0 +1,57 @@ +container_guid = $album->getGUID(); +$image->setMimeType($mime); +$image->access_id = $album->access_id; +$image->batch = $batch; + +try { + $image->save($file); + $album->prependImageList(array($image->guid)); + + if (elgg_get_plugin_setting('img_river_view', 'tidypics') === "all") { + add_to_river('river/object/image/create', 'create', $image->getObjectOwnerGUID(), $image->getGUID()); + } + + echo elgg_echo('success'); +} catch (Exception $e) { + // remove the bits that were saved + $image->delete(); + echo $e->getMessage(); +} + +exit; \ No newline at end of file diff --git a/actions/photos/image/ajax_upload_complete.php b/actions/photos/image/ajax_upload_complete.php new file mode 100644 index 000000000..6d398b3aa --- /dev/null +++ b/actions/photos/image/ajax_upload_complete.php @@ -0,0 +1,74 @@ + 'object', + 'subtype' => 'image', + 'metadata_names' => 'batch', + 'metadata_values' => $batch, + 'limit' => 0 +); + +$images = elgg_get_entities_from_metadata($params); +if ($images) { + // Create a new batch object to contain these photos + $batch = new ElggObject(); + $batch->subtype = "tidypics_batch"; + $batch->access_id = ACCESS_PUBLIC; + $batch->container_guid = $album->guid; + + if ($batch->save()) { + foreach ($images as $image) { + add_entity_relationship($image->guid, "belongs_to_batch", $batch->getGUID()); + } + } +} else { + // @todo some sort of message to edit them manually. + exit; +} + +// "added images to album" river +if ($img_river_view == "batch" && $album->new_album == false) { + add_to_river('river/object/tidypics_batch/create', 'create', $batch->getObjectOwnerGUID(), $batch->getGUID()); +} + +// "created album" river +if ($album->new_album) { + $album->new_album = false; + $album->first_upload = true; + + add_to_river('river/object/album/create', 'create', $album->getOwnerGUID(), $album->getGUID()); + + // "created album" notifications + // we throw the notification manually here so users are not told about the new album until + // there are at least a few photos in it + if ($album->shouldNotify()) { + object_notifications('create', 'object', $album); + $album->last_notified = time(); + } +} else { + // "added image to album" notifications + if ($album->first_upload) { + $album->first_upload = false; + } + + if ($album->shouldNotify()) { + object_notifications('create', 'object', $album); + $album->last_notified = time(); + } +} + +echo json_encode(array('batch_guid' => $batch->getGUID())); +exit; \ No newline at end of file diff --git a/actions/photos/image/upload.php b/actions/photos/image/upload.php index 29df59b63..b917a1598 100644 --- a/actions/photos/image/upload.php +++ b/actions/photos/image/upload.php @@ -50,6 +50,8 @@ foreach ($_FILES['images']['name'] as $index => $value) { continue; } + $mime = tp_upload_get_mimetype($data['name']); + $image = new TidypicsImage(); $image->container_guid = $album->getGUID(); $image->setMimeType($mime); diff --git a/classes/TidypicsImage.php b/classes/TidypicsImage.php index a64743017..c0b5de723 100644 --- a/classes/TidypicsImage.php +++ b/classes/TidypicsImage.php @@ -302,7 +302,7 @@ class TidypicsImage extends ElggFile { } $file = new ElggFile(); - $file->owner_guid = $this->getObjectOwnerGUID(); + $file->owner_guid = $this->getOwnerGUID(); $file->setFilename($thumb); return $file->grabFile(); } diff --git a/lib/tidypics.php b/lib/tidypics.php index 16773f982..77a8787f6 100644 --- a/lib/tidypics.php +++ b/lib/tidypics.php @@ -196,7 +196,7 @@ function tidypics_list_photos(array $options = array()) { foreach ($entities as $entity) { $keys[] = $entity->guid; } - var_dump($options); + $entities = array_combine($keys, $entities); $sorted_entities = array(); diff --git a/lib/watermark.php b/lib/watermark.php index ce77c00e2..5b9a4abcd 100644 --- a/lib/watermark.php +++ b/lib/watermark.php @@ -48,7 +48,7 @@ function tp_get_watermark_filename($text, $owner) { function tp_gd_watermark($image) { global $CONFIG; - $watermark_text = get_plugin_setting('watermark_text', 'tidypics'); + $watermark_text = elgg_get_plugin_setting('watermark_text', 'tidypics'); if (!$watermark_text) { return; } @@ -58,7 +58,7 @@ function tp_gd_watermark($image) { return; } - $owner = get_loggedin_user(); + $owner = elgg_get_logged_in_user_guid(); $watermark_text = tp_process_watermark_text($watermark_text, $owner); @@ -91,13 +91,13 @@ function tp_gd_watermark($image) { */ function tp_imagick_watermark($filename) { - $watermark_text = get_plugin_setting('watermark_text', 'tidypics'); + $watermark_text = elgg_get_plugin_setting('watermark_text', 'tidypics'); if (!$watermark_text) { return false; } // plugins can do their own watermark and return false to prevent this function from running - if (trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false) { + if (elgg_trigger_plugin_hook('tp_watermark', 'imagick', $filename, true) === false) { return true; } diff --git a/pages/photos/image/upload.php b/pages/photos/image/upload.php index 0c327d53e..395651ca9 100644 --- a/pages/photos/image/upload.php +++ b/pages/photos/image/upload.php @@ -14,12 +14,6 @@ if (!$album_guid) { forward(); } -if (elgg_get_plugin_setting('uploader', 'tidypics')) { - $uploader = get_input('uploader', 'ajax'); -} else { - $uploader = 'basic'; -} - $album = get_entity($album_guid); if (!$album) { // @todo @@ -44,7 +38,7 @@ elgg_push_breadcrumb($owner->name, "photos/owner/$owner->username"); elgg_push_breadcrumb($album->getTitle(), $album->getURL()); elgg_push_breadcrumb(elgg_echo('album:addpix')); - +$uploader = get_input('uploader'); if ($uploader == 'basic') { $content = elgg_view('forms/photos/basic_upload', array('entity' => $album)); } else { diff --git a/start.php b/start.php index 6ef3af541..1129c7f2f 100644 --- a/start.php +++ b/start.php @@ -12,7 +12,6 @@ elgg_register_event_handler('init', 'system', 'tidypics_init'); * Tidypics plugin initialization */ function tidypics_init() { - // Include core libraries require dirname(__FILE__) . "/lib/tidypics.php"; @@ -78,14 +77,14 @@ function tidypics_init() { elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'tidypics_notify_message'); -/* + // ajax handler for uploads when use_only_cookies is set + elgg_register_plugin_hook_handler('forward', 'csrf', 'tidypics_ajax_session_handler'); + + /* // Register for notifications // slideshow plugin hook register_plugin_hook('tp_slideshow', 'album', 'tidypics_slideshow'); - - // ajax handler for uploads when use_only_cookies is set - register_plugin_hook('forward', 'system', 'tidypics_ajax_session_handler'); */ // Register actions $base_dir = elgg_get_plugins_path() . 'tidypics/actions/photos'; @@ -95,8 +94,8 @@ function tidypics_init() { elgg_register_action("photos/image/upload", "$base_dir/image/upload.php"); elgg_register_action("photos/image/save", "$base_dir/image/save.php"); elgg_register_action("photos/batch/edit", "$base_dir/batch/edit.php"); - //register_action("tidypics/ajax_upload", true, "$base_dir/ajax_upload.php"); - //register_action("tidypics/ajax_upload_complete", true, "$base_dir/ajax_upload_complete.php"); + elgg_register_action("photos/image/ajax_upload", "$base_dir/image/ajax_upload.php", 'logged_in'); + elgg_register_action("photos/image/ajax_upload_complete", "$base_dir/image/ajax_upload_complete.php", 'logged_in'); elgg_register_action("photos/image/tag", "$base_dir/image/tag.php"); elgg_register_action("photos/image/untag", "$base_dir/image/untag.php"); @@ -192,7 +191,14 @@ function tidypics_page_handler($page) { case "upload": // upload images to album set_input('guid', $page[1]); - set_input('uploader', elgg_extract(2, $page, 'basic')); + + if (elgg_get_plugin_setting('uploader', 'tidypics')) { + $default_uploader = 'ajax'; + } else { + $default_uploader = 'basic'; + } + + set_input('uploader', elgg_extract(2, $page, $default_uploader)); require "$base/image/upload.php"; break; @@ -454,15 +460,15 @@ function tp_mostrecentimages($max = 8, $pagination = true) { * @param string $returnvalue * @param array $params */ -function tidypics_ajax_session_handler($hook, $entity_type, $returnvalue, $params) { - global $CONFIG; +function tidypics_ajax_session_handler($hook, $entity_type, $value, $params) { + $www_root = elgg_get_config('wwwroot'); + $url = $params['current_url']; - $url = current_page_url(); - if ($url !== "{$CONFIG->wwwroot}action/tidypics/ajax_upload/") { + if ($url !== "{$www_root}action/photos/image/ajax_upload") { return; } - if (get_loggedin_userid() != 0) { + if (elgg_get_logged_in_user_guid() != 0) { return; } @@ -472,10 +478,15 @@ function tidypics_ajax_session_handler($hook, $entity_type, $returnvalue, $param $token = get_input('__elgg_token'); $ts = get_input('__elgg_ts'); $session_id = get_input('Elgg'); + $session_token = get_input('session_token'); $tidypics_token = get_input('tidypics_token'); $user_guid = get_input('user_guid'); - $user = get_user($user_guid); + $timeout = elgg_get_config('action_token_timeout'); + if (!$timeout) { + $timeout = 2; + } + if (!$user) { return; } @@ -493,12 +504,14 @@ function tidypics_ajax_session_handler($hook, $entity_type, $returnvalue, $param $generated_token = md5($session_id . get_site_secret() . $ts . $user->salt); if ($tidypics_token !== $generated_token) { + echo "bad tp token"; return; } // passed token test, so login and process action login($user); - include $CONFIG->actions['tidypics/ajax_upload']['file']; + $actions = elgg_get_config('actions'); + include $actions['photos/image/ajax_upload']['file']; exit; } diff --git a/views/default/forms/photos/ajax_upload.php b/views/default/forms/photos/ajax_upload.php index f0738994a..be25d628e 100644 --- a/views/default/forms/photos/ajax_upload.php +++ b/views/default/forms/photos/ajax_upload.php @@ -2,26 +2,22 @@ /** * Tidypics ajax upload form body * - * @uses $vars['album'] + * @uses $vars['entity'] */ -$album = $vars['album']; +$album = $vars['entity']; $ts = time(); -$token = generate_action_token($ts); $batch = time(); -$tidypics_token = md5(session_id() . get_site_secret() . $ts . get_loggedin_user()->salt); - +$tidypics_token = md5(session_id() . get_site_secret() . $ts . elgg_get_logged_in_user_entity()->salt); $basic_uploader_url = current_page_url() . '/basic'; -$upload_endpoint_url = "{$vars['url']}action/tidypics/ajax_upload/"; -$upload_complete_url = "{$vars['url']}action/tidypics/ajax_upload_complete/"; -$maxfilesize = (float) get_plugin_setting('maxfilesize','tidypics'); +$maxfilesize = (float) elgg_get_plugin_setting('maxfilesize', 'tidypics'); if (!$maxfilesize) { $maxfilesize = 5; } -$quota = get_plugin_setting('quota','tidypics'); +$quota = elgg_get_plugin_setting('quota', 'tidypics'); if ($quota) { $image_repo_size_md = get_metadata_byname($album->container_guid, "image_repo_size"); $image_repo_size = (int)$image_repo_size_md->value; @@ -40,30 +36,32 @@ if ($quota) { ?> -
- -

+

-
+
+ + + + + + +
+ + +
  • + + 2. + +
  • +
  • + + 3. + +
  • + diff --git a/views/default/js/photos/uploading.php b/views/default/js/photos/uploading.php index 59448dd46..cb79dc77c 100644 --- a/views/default/js/photos/uploading.php +++ b/views/default/js/photos/uploading.php @@ -1,27 +1,92 @@ +//