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($args)
41 parent::prepare($args);
42 $this->user = common_current_user();
46 function handle($args)
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.');
66 $this->elementStart('form', array('enctype' => 'multipart/form-data',
68 'action' => common_local_url('photoupload')));
69 $this->elementStart('ul', 'form_data');
70 $this->elementStart('li');
71 $this->element('input', array('name' => 'photofile',
73 'id' => 'photofile'));
74 $this->elementEnd('li');
75 //$this->element('br');
76 $this->elementStart('li');
77 $this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
78 $this->elementEnd('li');
79 $this->elementStart('li');
80 $this->textarea('photo_description', _("Description"), null, _("A description of the photo. (Optional)"));
81 $this->elementEnd('li');
82 $this->elementStart('li');
83 $this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
84 $this->elementEnd('li');
85 $this->elementEnd('ul');
86 $this->submit('upload', _('Upload'));
87 $this->elementEnd('form');
91 $this->element('h3', array(), _("Create a new album"));
92 $this->elementStart('form', array('method' => 'post',
93 'action' =>common_local_url('photoupload')));
94 $this->elementStart('ul', 'form_data');
95 $this->elementStart('li');
96 $this->input('album_name', _("Title"), null, _("The title of the album."));
97 $this->elementEnd('li');
98 $this->elementStart('li');
99 $this->textarea('album_description', _("Description"), null, _("A description of the album. (Optional)"));
100 $this->elementEnd('li');
101 $this->elementEnd('ul');
102 $this->submit('create', _('Create'));
103 $this->elementEnd('form');
108 function handlePost()
111 common_log(LOG_INFO, 'handlPost()!');
112 // Workaround for PHP returning empty $_POST and $_FILES when POST
113 // length > post_max_size in php.ini
117 && ($_SERVER['CONTENT_LENGTH'] > 0)
119 $msg = _('The server was unable to handle that much POST ' .
120 'data (%s bytes) due to its current configuration.');
122 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
128 /* $token = $this->trimmed('token');
129 if (!$token || $token != common_session_token()) {
130 $this->showForm(_('There was a problem with your session token. '.
131 'Try again, please.'));
135 if ($this->arg('upload')) {
136 $this->uploadPhoto();
138 if ($this->arg('create')) {
139 $this->createAlbum();
143 function showForm($msg, $success=false)
146 $this->success = $success;
148 // $this->showPage();
153 $cur = common_current_user();
154 $album = new GNUsocialPhotoAlbum();
155 $album->user_id = $cur->id;
157 $albumlist = array();
158 if (!$album->find()) {
159 GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
161 while ($album->fetch()) {
162 $albumlist[$album->album_id] = $album->album_name;
167 function uploadPhoto()
169 $cur = common_current_user();
174 $imagefile = ImageFile::fromUpload('photofile');
175 } catch (Exception $e) {
176 $this->showForm($e->getMessage());
179 if ($imagefile === null) {
180 $this->showForm(_('No file uploaded.'));
184 $title = $this->trimmed('phototitle');
185 $photo_description = $this->trimmed('photo_description');
187 common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
189 $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
190 move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
191 photo_make_thumbnail($filename);
192 $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
193 $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
194 $profile_id = $cur->id;
196 $album = GNUsocialPhotoAlbum::staticGet('album_id', $this->trimmed('album'));
197 if ($album->profile_id != $profile_id) {
198 $this->showForm(_('Error: This is not your album!'));
201 GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
204 function createAlbum()
206 $cur = common_current_user();
211 $album_name = $this->trimmed('album_name');
212 $album_description = $this->trimmed('album_description');
214 GNUsocialPhotoAlbum::newAlbum($cur->id, $album_name, $album_description);