photoupload.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * GNU Social
  4. * Copyright (C) 2010, Free Software Foundation, Inc.
  5. *
  6. * PHP version 5
  7. *
  8. * LICENCE:
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Widget
  23. * @package GNU Social
  24. * @author Ian Denhardt <ian@zenhack.net>
  25. * @author Sean Corbett <sean@gnu.org>
  26. * @author Max Shinn <trombonechamp@gmail.com>
  27. * @copyright 2010 Free Software Foundation, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. class PhotouploadAction extends Action
  34. {
  35. var $user = null;
  36. function prepare($args)
  37. {
  38. parent::prepare($args);
  39. $this->user = common_current_user();
  40. return true;
  41. }
  42. function handle($args)
  43. {
  44. parent::handle($args);
  45. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  46. $this->handlePost();
  47. }
  48. $this->showPage();
  49. }
  50. function title()
  51. {
  52. return _m('Upload Photos');
  53. }
  54. function showContent()
  55. {
  56. //Upload a photo
  57. if(empty($this->user)) {
  58. $this->element('p', array(), 'You are not logged in.');
  59. } else {
  60. //showForm() data
  61. if(!empty($this->msg)) {
  62. $class = ($this->success) ? 'success' : 'error';
  63. $this->element('p', array('class' => $class), $this->msg);
  64. }
  65. $this->elementStart('form', array('enctype' => 'multipart/form-data',
  66. 'method' => 'post',
  67. 'action' => common_local_url('photoupload')));
  68. $this->elementStart('ul', 'form_data');
  69. $this->elementStart('li');
  70. $this->element('input', array('name' => 'photofile',
  71. 'type' => 'file',
  72. 'id' => 'photofile'));
  73. $this->elementEnd('li');
  74. //$this->element('br');
  75. $this->elementStart('li');
  76. $this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
  77. $this->elementEnd('li');
  78. $this->elementStart('li');
  79. $this->textarea('photo_description', _("Description"), null, _("A description of the photo. (Optional)"));
  80. $this->elementEnd('li');
  81. $this->elementStart('li');
  82. $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
  83. $this->elementEnd('li');
  84. $this->elementEnd('ul');
  85. $this->submit('upload', _('Upload'));
  86. $this->elementEnd('form');
  87. $this->element('br');
  88. //Create a new album
  89. $this->element('h3', array(), _("Create a new album"));
  90. $this->elementStart('form', array('method' => 'post',
  91. 'action' =>common_local_url('photoupload')));
  92. $this->elementStart('ul', 'form_data');
  93. $this->elementStart('li');
  94. $this->input('album_name', _("Title"), null, _("The title of the album."));
  95. $this->elementEnd('li');
  96. $this->elementStart('li');
  97. $this->textarea('album_description', _("Description"), null, _("A description of the album. (Optional)"));
  98. $this->elementEnd('li');
  99. $this->elementEnd('ul');
  100. $this->submit('create', _('Create'));
  101. $this->elementEnd('form');
  102. //Delete an album
  103. $this->element('h3', array(), _("Delete an album"));
  104. $this->elementStart('form', array('method' => 'post',
  105. 'action' =>common_local_url('photoupload')));
  106. $this->elementStart('ul', 'form_data');
  107. $this->elementStart('li');
  108. $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
  109. $this->elementEnd('li');
  110. $this->elementEnd('ul');
  111. $this->submit('deletealbum', _('Delete'));
  112. $this->elementEnd('form');
  113. }
  114. }
  115. function handlePost()
  116. {
  117. common_log(LOG_INFO, 'handlPost()!');
  118. // Workaround for PHP returning empty $_POST and $_FILES when POST
  119. // length > post_max_size in php.ini
  120. if (empty($_FILES)
  121. && empty($_POST)
  122. && ($_SERVER['CONTENT_LENGTH'] > 0)
  123. ) {
  124. $msg = _('The server was unable to handle that much POST ' .
  125. 'data (%s bytes) due to its current configuration.');
  126. $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
  127. return;
  128. }
  129. // CSRF protection
  130. /* $token = $this->trimmed('token');
  131. if (!$token || $token != common_session_token()) {
  132. $this->showForm(_('There was a problem with your session token. '.
  133. 'Try again, please.'));
  134. return;
  135. } */
  136. if ($this->arg('upload')) {
  137. $this->uploadPhoto();
  138. }
  139. if ($this->arg('create')) {
  140. $this->createAlbum();
  141. }
  142. if ($this->arg('deletealbum')) {
  143. $this->deleteAlbum();
  144. }
  145. }
  146. function showForm($msg, $success=false)
  147. {
  148. $this->msg = $msg;
  149. $this->success = $success;
  150. // $this->showPage();
  151. }
  152. function albumList()
  153. {
  154. $cur = common_current_user();
  155. $album = new GNUsocialPhotoAlbum();
  156. $album->user_id = $cur->id;
  157. $albumlist = array();
  158. if (!$album->find()) {
  159. GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
  160. }
  161. while ($album->fetch()) {
  162. $albumlist[$album->album_id] = $album->album_name;
  163. }
  164. return $albumlist;
  165. }
  166. function uploadPhoto()
  167. {
  168. $cur = common_current_user();
  169. if(empty($cur)) {
  170. return;
  171. }
  172. try {
  173. $imagefile = ImageFile::fromUpload('photofile');
  174. } catch (Exception $e) {
  175. $this->showForm($e->getMessage());
  176. return;
  177. }
  178. if ($imagefile === null) {
  179. $this->showForm(_('No file uploaded.'));
  180. return;
  181. }
  182. $title = $this->trimmed('phototitle');
  183. $photo_description = $this->trimmed('photo_description');
  184. common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
  185. $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
  186. move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
  187. photo_make_thumbnail($filename);
  188. $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
  189. $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
  190. $profile_id = $cur->id;
  191. $album = GNUsocialPhotoAlbum::getKV('album_id', $this->trimmed('album'));
  192. if ($album->profile_id != $profile_id) {
  193. $this->showForm(_('Error: This is not your album!'));
  194. return;
  195. }
  196. GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
  197. }
  198. function createAlbum()
  199. {
  200. $cur = common_current_user();
  201. if(empty($cur)) {
  202. return;
  203. }
  204. $album_name = $this->trimmed('album_name');
  205. $album_description = $this->trimmed('album_description');
  206. GNUsocialPhotoAlbum::newAlbum($cur->id, $album_name, $album_description);
  207. }
  208. function deleteAlbum()
  209. {
  210. $cur = common_current_user();
  211. if(empty($cur)) return;
  212. $album_id = $this->trimmed('album');
  213. $album = GNUsocialPhotoAlbum::getKV('album_id', $album_id);
  214. if (empty($album)) {
  215. $this->showForm(_('This album does not exist or has been deleted.'));
  216. return;
  217. }
  218. //Check if the album has any photos in it before deleting
  219. $photos = GNUsocialPhoto::getKV('album_id', $album_id);
  220. if(empty($photos)) {
  221. $album->delete();
  222. $this->showForm(_('Album deleted'), true);
  223. }
  224. else {
  225. $this->showForm(_('Cannot delete album when there are photos in it!'), false);
  226. }
  227. }
  228. }