4 * Copyright (C) 2010, Free Software Foundation, Inc.
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.
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.
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/>.
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
31 if (!defined('STATUSNET')) {
35 class PhotouploadAction extends Action
39 function prepare(array $args=array())
41 parent::prepare($args);
42 $this->user = common_current_user();
46 function handle(array $args=array())
48 parent::handle($args);
49 if($_SERVER['REQUEST_METHOD'] == 'POST') {
57 return _m('Upload Photos');
60 function showContent()
63 if(empty($this->user)) {
64 $this->element('p', array(), 'You are not logged in.');
67 if(!empty($this->msg)) {
68 $class = ($this->success) ? 'success' : 'error';
69 $this->element('p', array('class' => $class), $this->msg);
72 $this->elementStart('form', array('enctype' => 'multipart/form-data',
74 'action' => common_local_url('photoupload')));
75 $this->elementStart('ul', 'form_data');
76 $this->elementStart('li');
77 $this->element('input', array('name' => 'photofile',
79 'id' => 'photofile'));
80 $this->elementEnd('li');
81 //$this->element('br');
82 $this->elementStart('li');
83 $this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
84 $this->elementEnd('li');
85 $this->elementStart('li');
86 $this->textarea('photo_description', _("Description"), null, _("A description of the photo. (Optional)"));
87 $this->elementEnd('li');
88 $this->elementStart('li');
89 $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
90 $this->elementEnd('li');
91 $this->elementEnd('ul');
92 $this->submit('upload', _('Upload'));
93 $this->elementEnd('form');
97 $this->element('h3', array(), _("Create a new album"));
98 $this->elementStart('form', array('method' => 'post',
99 'action' =>common_local_url('photoupload')));
100 $this->elementStart('ul', 'form_data');
101 $this->elementStart('li');
102 $this->input('album_name', _("Title"), null, _("The title of the album."));
103 $this->elementEnd('li');
104 $this->elementStart('li');
105 $this->textarea('album_description', _("Description"), null, _("A description of the album. (Optional)"));
106 $this->elementEnd('li');
107 $this->elementEnd('ul');
108 $this->submit('create', _('Create'));
109 $this->elementEnd('form');
112 $this->element('h3', array(), _("Delete an album"));
113 $this->elementStart('form', array('method' => 'post',
114 'action' =>common_local_url('photoupload')));
115 $this->elementStart('ul', 'form_data');
116 $this->elementStart('li');
117 $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
118 $this->elementEnd('li');
119 $this->elementEnd('ul');
120 $this->submit('deletealbum', _('Delete'));
121 $this->elementEnd('form');
126 function handlePost()
129 common_log(LOG_INFO, 'handlPost()!');
130 // Workaround for PHP returning empty $_POST and $_FILES when POST
131 // length > post_max_size in php.ini
135 && ($_SERVER['CONTENT_LENGTH'] > 0)
137 $msg = _('The server was unable to handle that much POST ' .
138 'data (%s bytes) due to its current configuration.');
140 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
146 /* $token = $this->trimmed('token');
147 if (!$token || $token != common_session_token()) {
148 $this->showForm(_('There was a problem with your session token. '.
149 'Try again, please.'));
153 if ($this->arg('upload')) {
154 $this->uploadPhoto();
156 if ($this->arg('create')) {
157 $this->createAlbum();
159 if ($this->arg('deletealbum')) {
160 $this->deleteAlbum();
164 function showForm($msg, $success=false)
167 $this->success = $success;
169 // $this->showPage();
174 $cur = common_current_user();
175 $album = new GNUsocialPhotoAlbum();
176 $album->user_id = $cur->id;
178 $albumlist = array();
179 if (!$album->find()) {
180 GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
182 while ($album->fetch()) {
183 $albumlist[$album->album_id] = $album->album_name;
188 function uploadPhoto()
190 $cur = common_current_user();
195 $imagefile = ImageFile::fromUpload('photofile');
196 } catch (Exception $e) {
197 $this->showForm($e->getMessage());
200 if ($imagefile === null) {
201 $this->showForm(_('No file uploaded.'));
205 $title = $this->trimmed('phototitle');
206 $photo_description = $this->trimmed('photo_description');
208 common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
210 $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
211 move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
212 photo_make_thumbnail($filename);
213 $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
214 $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
215 $profile_id = $cur->id;
217 $album = GNUsocialPhotoAlbum::getKV('album_id', $this->trimmed('album'));
218 if ($album->profile_id != $profile_id) {
219 $this->showForm(_('Error: This is not your album!'));
222 GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
225 function createAlbum()
227 $cur = common_current_user();
232 $album_name = $this->trimmed('album_name');
233 $album_description = $this->trimmed('album_description');
235 GNUsocialPhotoAlbum::newAlbum($cur->id, $album_name, $album_description);
238 function deleteAlbum()
240 $cur = common_current_user();
241 if(empty($cur)) return;
243 $album_id = $this->trimmed('album');
244 $album = GNUsocialPhotoAlbum::getKV('album_id', $album_id);
246 $this->showForm(_('This album does not exist or has been deleted.'));
249 //Check if the album has any photos in it before deleting
250 $photos = GNUsocialPhoto::getKV('album_id', $album_id);
253 $this->showForm(_('Album deleted'), true);
256 $this->showForm(_('Cannot delete album when there are photos in it!'), false);