upload.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Multi-image uploader action
  4. *
  5. * @author Cash Costello
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
  7. */
  8. elgg_load_library('tidypics:upload');
  9. $img_river_view = elgg_get_plugin_setting('img_river_view', 'tidypics');
  10. $guid = (int) get_input('guid');
  11. $album = get_entity($guid);
  12. if (!$album) {
  13. register_error(elgg_echo('tidypics:baduploadform'));
  14. forward(REFERER);
  15. }
  16. // post limit exceeded
  17. if (count($_FILES) == 0) {
  18. trigger_error('Tidypics warning: user exceeded post limit on image upload', E_USER_WARNING);
  19. register_error(elgg_echo('tidypics:exceedpostlimit'));
  20. forward(REFERER);
  21. }
  22. // test to make sure at least 1 image was selected by user
  23. $num_images = 0;
  24. foreach($_FILES['images']['name'] as $name) {
  25. if (!empty($name)) {
  26. $num_images++;
  27. }
  28. }
  29. if ($num_images == 0) {
  30. // have user try again
  31. register_error(elgg_echo('tidypics:noimages'));
  32. forward(REFERER);
  33. }
  34. // create the image object for each upload
  35. $uploaded_images = array();
  36. $not_uploaded = array();
  37. $error_msgs = array();
  38. foreach ($_FILES['images']['name'] as $index => $value) {
  39. $data = array();
  40. foreach ($_FILES['images'] as $key => $values) {
  41. $data[$key] = $values[$index];
  42. }
  43. if (empty($data['name'])) {
  44. continue;
  45. }
  46. $name = htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8', false);
  47. $mime = tp_upload_get_mimetype($name);
  48. $image = new TidypicsImage();
  49. $image->title = $name;
  50. $image->container_guid = $album->getGUID();
  51. $image->setMimeType($mime);
  52. $image->access_id = $album->access_id;
  53. try {
  54. $result = $image->save($data);
  55. } catch (Exception $e) {
  56. $image->delete();
  57. $result = false;
  58. array_push($not_uploaded, $name);
  59. array_push($error_msgs, $e->getMessage());
  60. }
  61. if ($result) {
  62. array_push($uploaded_images, $image->getGUID());
  63. if ($img_river_view == "all") {
  64. elgg_create_river_item(array('view' => 'river/object/image/create',
  65. 'action_type' => 'create',
  66. 'subject_guid' => $image->getOwnerGUID(),
  67. 'object_guid' => $image->getGUID()));
  68. }
  69. }
  70. }
  71. if (count($uploaded_images)) {
  72. // Create a new batch object to contain these photos
  73. $batch = new TidypicsBatch();
  74. $batch->access_id = $album->access_id;
  75. $batch->container_guid = $album->getGUID();
  76. if ($batch->save()) {
  77. foreach ($uploaded_images as $uploaded_guid) {
  78. add_entity_relationship($uploaded_guid, "belongs_to_batch", $batch->getGUID());
  79. }
  80. }
  81. $album->prependImageList($uploaded_images);
  82. // "added images to album" river
  83. if ($img_river_view == "batch" && $album->new_album == false) {
  84. elgg_create_river_item(array('view' => 'river/object/tidypics_batch/create',
  85. 'action_type' => 'create',
  86. 'subject_guid' => $batch->getOwnerGUID(),
  87. 'object_guid' => $batch->getGUID()));
  88. } else if ($img_river_view == "1" && $album->new_album == false) {
  89. elgg_create_river_item(array('view' => 'river/object/tidypics_batch/create_single_image',
  90. 'action_type' => 'create',
  91. 'subject_guid' => $batch->getOwnerGUID(),
  92. 'object_guid' => $batch->getGUID()));
  93. }
  94. // "created album" river
  95. if ($album->new_album) {
  96. $album->new_album = false;
  97. $album->first_upload = true;
  98. $album_river_view = elgg_get_plugin_setting('album_river_view', 'tidypics');
  99. if ($album_river_view != "none") {
  100. elgg_create_river_item(array('view' => 'river/object/album/create',
  101. 'action_type' => 'create',
  102. 'subject_guid' => $album->getOwnerGUID(),
  103. 'object_guid' => $album->getGUID()));
  104. }
  105. // "created album" notifications
  106. // we throw the notification manually here so users are not told about the new album until
  107. // there are at least a few photos in it
  108. if ($album->shouldNotify()) {
  109. elgg_trigger_event('album_first', 'album', $album);
  110. $album->last_notified = time();
  111. }
  112. } else {
  113. // "added image to album" notifications
  114. if ($album->first_upload) {
  115. $album->first_upload = false;
  116. }
  117. if ($album->shouldNotify()) {
  118. elgg_trigger_event('album_more', 'album', $album);
  119. $album->last_notified = time();
  120. }
  121. }
  122. }
  123. if (count($not_uploaded) > 0) {
  124. if (count($uploaded_images) > 0) {
  125. $error = sprintf(elgg_echo("tidypics:partialuploadfailure"), count($not_uploaded), count($not_uploaded) + count($uploaded_images)) . '<br />';
  126. } else {
  127. $error = elgg_echo("tidypics:completeuploadfailure") . '<br />';
  128. }
  129. $num_failures = count($not_uploaded);
  130. for ($i = 0; $i < $num_failures; $i++) {
  131. $error .= "{$not_uploaded[$i]}: {$error_msgs[$i]} <br />";
  132. }
  133. register_error($error);
  134. if (count($uploaded_images) == 0) {
  135. //upload failed, so forward to previous page
  136. forward(REFERER);
  137. } else {
  138. // some images did upload so we fall through
  139. }
  140. } else {
  141. system_message(elgg_echo('tidypics:upl_success'));
  142. }
  143. forward("photos/edit/$batch->guid");