3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: 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 Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2008-2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('GNUSOCIAL')) { exit(1); }
36 * We use jCrop plugin for jQuery to crop the image after upload.
40 * @author Evan Prodromou <evan@status.net>
41 * @author Zach Copley <zach@status.net>
42 * @author Sarven Capadisli <csarven@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class AvatarsettingsAction extends SettingsAction
49 var $imagefile = null;
55 * @return string Title of the page
59 // TRANS: Title for avatar upload page.
64 * Instructions for use
66 * @return instructions for use
68 function getInstructions()
70 // TRANS: Instruction for avatar upload page.
71 // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
72 return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'),
73 ImageFile::maxFileSize());
77 * Content area of the page
79 * Shows a form for uploading an avatar.
84 function showContent()
86 if ($this->mode == 'crop') {
87 $this->showCropForm();
89 $this->showUploadForm();
93 function showUploadForm()
95 $user = common_current_user();
97 $profile = $user->getProfile();
100 common_log_db_error($user, 'SELECT', __FILE__);
101 // TRANS: Error message displayed when referring to a user without a profile.
102 $this->serverError(_('User has no profile.'));
105 $this->elementStart('form', array('enctype' => 'multipart/form-data',
107 'id' => 'form_settings_avatar',
108 'class' => 'form_settings',
110 common_local_url('avatarsettings')));
111 $this->elementStart('fieldset');
112 // TRANS: Avatar upload page form legend.
113 $this->element('legend', null, _('Avatar settings'));
114 $this->hidden('token', common_session_token());
116 if (Event::handle('StartAvatarFormData', array($this))) {
117 $this->elementStart('ul', 'form_data');
119 $original = Avatar::getUploaded($profile);
121 $this->elementStart('li', array('id' => 'avatar_original',
122 'class' => 'avatar_view'));
123 // TRANS: Header on avatar upload page for thumbnail of originally uploaded avatar (h2).
124 $this->element('h2', null, _("Original"));
125 $this->elementStart('div', array('id'=>'avatar_original_view'));
126 $this->element('img', array('src' => $original->displayUrl(),
127 'width' => $original->width,
128 'height' => $original->height,
129 'alt' => $user->nickname));
130 $this->elementEnd('div');
131 $this->elementEnd('li');
132 } catch (NoAvatarException $e) {
133 // No original avatar found!
137 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
138 $this->elementStart('li', array('id' => 'avatar_preview',
139 'class' => 'avatar_view'));
140 // TRANS: Header on avatar upload page for thumbnail of to be used rendition of uploaded avatar (h2).
141 $this->element('h2', null, _("Preview"));
142 $this->elementStart('div', array('id'=>'avatar_preview_view'));
143 $this->element('img', array('src' => $avatar->displayUrl(),
144 'width' => AVATAR_PROFILE_SIZE,
145 'height' => AVATAR_PROFILE_SIZE,
146 'alt' => $user->nickname));
147 $this->elementEnd('div');
148 if (!empty($avatar->filename)) {
149 // TRANS: Button on avatar upload page to delete current avatar.
150 $this->submit('delete', _m('BUTTON','Delete'));
152 $this->elementEnd('li');
153 } catch (NoAvatarException $e) {
154 // No previously uploaded avatar to preview.
157 $this->elementStart('li', array ('id' => 'settings_attach'));
158 $this->element('input', array('name' => 'MAX_FILE_SIZE',
160 'id' => 'MAX_FILE_SIZE',
161 'value' => ImageFile::maxFileSizeInt()));
162 $this->element('input', array('name' => 'avatarfile',
164 'id' => 'avatarfile'));
165 $this->elementEnd('li');
166 $this->elementEnd('ul');
168 $this->elementStart('ul', 'form_actions');
169 $this->elementStart('li');
170 // TRANS: Button on avatar upload page to upload an avatar.
171 $this->submit('upload', _m('BUTTON','Upload'));
172 $this->elementEnd('li');
173 $this->elementEnd('ul');
175 Event::handle('EndAvatarFormData', array($this));
177 $this->elementEnd('fieldset');
178 $this->elementEnd('form');
181 function showCropForm()
183 $user = common_current_user();
185 $profile = $user->getProfile();
188 common_log_db_error($user, 'SELECT', __FILE__);
189 // TRANS: Error message displayed when referring to a user without a profile.
190 $this->serverError(_('User has no profile.'));
193 $this->elementStart('form', array('method' => 'post',
194 'id' => 'form_settings_avatar',
195 'class' => 'form_settings',
197 common_local_url('avatarsettings')));
198 $this->elementStart('fieldset');
199 // TRANS: Avatar upload page crop form legend.
200 $this->element('legend', null, _('Avatar settings'));
201 $this->hidden('token', common_session_token());
203 $this->elementStart('ul', 'form_data');
205 $this->elementStart('li',
206 array('id' => 'avatar_original',
207 'class' => 'avatar_view'));
208 // TRANS: Header on avatar upload crop form for thumbnail of originally uploaded avatar (h2).
209 $this->element('h2', null, _('Original'));
210 $this->elementStart('div', array('id'=>'avatar_original_view'));
211 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
212 'width' => $this->filedata['width'],
213 'height' => $this->filedata['height'],
214 'alt' => $user->nickname));
215 $this->elementEnd('div');
216 $this->elementEnd('li');
218 $this->elementStart('li',
219 array('id' => 'avatar_preview',
220 'class' => 'avatar_view'));
221 // TRANS: Header on avatar upload crop form for thumbnail of to be used rendition of uploaded avatar (h2).
222 $this->element('h2', null, _('Preview'));
223 $this->elementStart('div', array('id'=>'avatar_preview_view'));
224 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
225 'width' => AVATAR_PROFILE_SIZE,
226 'height' => AVATAR_PROFILE_SIZE,
227 'alt' => $user->nickname));
228 $this->elementEnd('div');
230 foreach (array('avatar_crop_x', 'avatar_crop_y',
231 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
232 $this->element('input', array('name' => $crop_info,
234 'id' => $crop_info));
237 // TRANS: Button on avatar upload crop form to confirm a selected crop as avatar.
238 $this->submit('crop', _m('BUTTON','Crop'));
240 $this->elementEnd('li');
241 $this->elementEnd('ul');
242 $this->elementEnd('fieldset');
243 $this->elementEnd('form');
249 * We mux on the button name to figure out what the user actually wanted.
253 function handlePost()
255 // Workaround for PHP returning empty $_POST and $_FILES when POST
256 // length > post_max_size in php.ini
260 && ($_SERVER['CONTENT_LENGTH'] > 0)
262 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
263 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
264 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
265 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
266 intval($_SERVER['CONTENT_LENGTH']));
267 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
273 $token = $this->trimmed('token');
274 if (!$token || $token != common_session_token()) {
275 // TRANS: Client error displayed when the session token does not match or is not given.
276 $this->showForm(_('There was a problem with your session token. '.
277 'Try again, please.'));
281 if (Event::handle('StartAvatarSaveForm', array($this))) {
282 if ($this->arg('upload')) {
283 $this->uploadAvatar();
284 } else if ($this->arg('crop')) {
286 } else if ($this->arg('delete')) {
287 $this->deleteAvatar();
289 // TRANS: Unexpected validation error on avatar upload form.
290 $this->showForm(_('Unexpected form submission.'));
292 Event::handle('EndAvatarSaveForm', array($this));
297 * Handle an image upload
299 * Does all the magic for handling an image upload, and crops the
304 function uploadAvatar()
307 $imagefile = ImageFile::fromUpload('avatarfile');
308 } catch (Exception $e) {
309 $this->showForm($e->getMessage());
312 if ($imagefile === null) {
313 // TRANS: Validation error on avatar upload form when no file was uploaded.
314 $this->showForm(_('No file uploaded.'));
318 $cur = common_current_user();
319 $type = $imagefile->preferredType();
320 $filename = Avatar::filename($cur->id,
321 image_type_to_extension($type),
323 'tmp'.common_timestamp());
325 $filepath = Avatar::path($filename);
326 $imagefile = $imagefile->copyTo($filepath);
328 $filedata = array('filename' => $filename,
329 'filepath' => $filepath,
330 'width' => $imagefile->width,
331 'height' => $imagefile->height,
334 $_SESSION['FILEDATA'] = $filedata;
336 $this->filedata = $filedata;
338 $this->mode = 'crop';
340 // TRANS: Avatar upload form instruction after uploading a file.
341 $this->showForm(_('Pick a square area of the image to be your avatar.'),
346 * Handle the results of jcrop.
350 public function cropAvatar()
352 $filedata = $_SESSION['FILEDATA'];
355 // TRANS: Server error displayed if an avatar upload went wrong somehow server side.
356 $this->serverError(_('Lost our file data.'));
359 $file_d = ($filedata['width'] > $filedata['height'])
360 ? $filedata['height'] : $filedata['width'];
362 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
363 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
364 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
365 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
366 $size = intval(min($dest_w, $dest_h, common_config('avatar', 'maxsize')));
368 $box = array('width' => $size, 'height' => $size,
369 'x' => $dest_x, 'y' => $dest_y,
370 'w' => $dest_w, 'h' => $dest_h);
372 $user = common_current_user();
373 $profile = $user->getProfile();
375 $imagefile = new ImageFile(null, $filedata['filepath']);
376 $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()),
377 $size, common_timestamp());
379 $imagefile->resizeTo(Avatar::path($filename), $box);
380 } catch (UseFileAsThumbnailException $e) {
381 common_debug('Using uploaded avatar directly without resizing, copying it to: '.$filename);
382 if (!copy($filedata['filepath'], Avatar::path($filename))) {
383 common_debug('Tried to copy image file '.$filedata['filepath'].' to destination '.Avatar::path($filename));
384 throw new ServerException('Could not copy file to destination.');
388 if ($profile->setOriginal($filename)) {
389 @unlink($filedata['filepath']);
390 unset($_SESSION['FILEDATA']);
391 $this->mode = 'upload';
392 // TRANS: Success message for having updated a user avatar.
393 $this->showForm(_('Avatar updated.'), true);
395 // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason.
396 $this->showForm(_('Failed updating avatar.'));
401 * Get rid of the current avatar.
405 function deleteAvatar()
407 $user = common_current_user();
408 $profile = $user->getProfile();
410 Avatar::deleteFromProfile($profile);
412 // TRANS: Success message for deleting a user avatar.
413 $this->showForm(_('Avatar deleted.'), true);
417 * Add the jCrop stylesheet
422 function showStylesheets()
424 parent::showStylesheets();
425 $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
429 * Add the jCrop scripts
433 function showScripts()
435 parent::showScripts();
437 if ($this->mode == 'crop') {
438 $this->script('extlib/jquery-jcrop/jcrop.js');
439 $this->script('jcrop.go.js');
442 $this->autofocus('avatarfile');