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('STATUSNET') && !defined('LACONICA')) {
37 define('MAX_ORIGINAL', 480);
42 * We use jCrop plugin for jQuery to crop the image after upload.
46 * @author Evan Prodromou <evan@status.net>
47 * @author Zach Copley <zach@status.net>
48 * @author Sarven Capadisli <csarven@status.net>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://status.net/
52 class AvatarsettingsAction extends SettingsAction
55 var $imagefile = null;
61 * @return string Title of the page
65 // TRANS: Title for avatar upload page.
70 * Instructions for use
72 * @return instructions for use
74 function getInstructions()
76 // TRANS: Instruction for avatar upload page.
77 // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
78 return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'),
79 ImageFile::maxFileSize());
83 * Content area of the page
85 * Shows a form for uploading an avatar.
90 function showContent()
92 if ($this->mode == 'crop') {
93 $this->showCropForm();
95 $this->showUploadForm();
99 function showUploadForm()
101 $user = common_current_user();
103 $profile = $user->getProfile();
106 common_log_db_error($user, 'SELECT', __FILE__);
107 // TRANS: Error message displayed when referring to a user without a profile.
108 $this->serverError(_('User has no profile.'));
111 $this->elementStart('form', array('enctype' => 'multipart/form-data',
113 'id' => 'form_settings_avatar',
114 'class' => 'form_settings',
116 common_local_url('avatarsettings')));
117 $this->elementStart('fieldset');
118 // TRANS: Avatar upload page form legend.
119 $this->element('legend', null, _('Avatar settings'));
120 $this->hidden('token', common_session_token());
122 if (Event::handle('StartAvatarFormData', array($this))) {
123 $this->elementStart('ul', 'form_data');
125 $original = Avatar::getUploaded($profile);
127 $this->elementStart('li', array('id' => 'avatar_original',
128 'class' => 'avatar_view'));
129 // TRANS: Header on avatar upload page for thumbnail of originally uploaded avatar (h2).
130 $this->element('h2', null, _("Original"));
131 $this->elementStart('div', array('id'=>'avatar_original_view'));
132 $this->element('img', array('src' => $original->displayUrl(),
133 'width' => $original->width,
134 'height' => $original->height,
135 'alt' => $user->nickname));
136 $this->elementEnd('div');
137 $this->elementEnd('li');
138 } catch (NoAvatarException $e) {
139 // No original avatar found!
143 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
144 $this->elementStart('li', array('id' => 'avatar_preview',
145 'class' => 'avatar_view'));
146 // TRANS: Header on avatar upload page for thumbnail of to be used rendition of uploaded avatar (h2).
147 $this->element('h2', null, _("Preview"));
148 $this->elementStart('div', array('id'=>'avatar_preview_view'));
149 $this->element('img', array('src' => $avatar->displayUrl(),
150 'width' => AVATAR_PROFILE_SIZE,
151 'height' => AVATAR_PROFILE_SIZE,
152 'alt' => $user->nickname));
153 $this->elementEnd('div');
154 if (!empty($avatar->filename)) {
155 // TRANS: Button on avatar upload page to delete current avatar.
156 $this->submit('delete', _m('BUTTON','Delete'));
158 $this->elementEnd('li');
159 } catch (NoAvatarException $e) {
160 // No previously uploaded avatar to preview.
163 $this->elementStart('li', array ('id' => 'settings_attach'));
164 $this->element('input', array('name' => 'MAX_FILE_SIZE',
166 'id' => 'MAX_FILE_SIZE',
167 'value' => ImageFile::maxFileSizeInt()));
168 $this->element('input', array('name' => 'avatarfile',
170 'id' => 'avatarfile'));
171 $this->elementEnd('li');
172 $this->elementEnd('ul');
174 $this->elementStart('ul', 'form_actions');
175 $this->elementStart('li');
176 // TRANS: Button on avatar upload page to upload an avatar.
177 $this->submit('upload', _m('BUTTON','Upload'));
178 $this->elementEnd('li');
179 $this->elementEnd('ul');
181 Event::handle('EndAvatarFormData', array($this));
183 $this->elementEnd('fieldset');
184 $this->elementEnd('form');
187 function showCropForm()
189 $user = common_current_user();
191 $profile = $user->getProfile();
194 common_log_db_error($user, 'SELECT', __FILE__);
195 // TRANS: Error message displayed when referring to a user without a profile.
196 $this->serverError(_('User has no profile.'));
199 $this->elementStart('form', array('method' => 'post',
200 'id' => 'form_settings_avatar',
201 'class' => 'form_settings',
203 common_local_url('avatarsettings')));
204 $this->elementStart('fieldset');
205 // TRANS: Avatar upload page crop form legend.
206 $this->element('legend', null, _('Avatar settings'));
207 $this->hidden('token', common_session_token());
209 $this->elementStart('ul', 'form_data');
211 $this->elementStart('li',
212 array('id' => 'avatar_original',
213 'class' => 'avatar_view'));
214 // TRANS: Header on avatar upload crop form for thumbnail of originally uploaded avatar (h2).
215 $this->element('h2', null, _('Original'));
216 $this->elementStart('div', array('id'=>'avatar_original_view'));
217 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
218 'width' => $this->filedata['width'],
219 'height' => $this->filedata['height'],
220 'alt' => $user->nickname));
221 $this->elementEnd('div');
222 $this->elementEnd('li');
224 $this->elementStart('li',
225 array('id' => 'avatar_preview',
226 'class' => 'avatar_view'));
227 // TRANS: Header on avatar upload crop form for thumbnail of to be used rendition of uploaded avatar (h2).
228 $this->element('h2', null, _('Preview'));
229 $this->elementStart('div', array('id'=>'avatar_preview_view'));
230 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
231 'width' => AVATAR_PROFILE_SIZE,
232 'height' => AVATAR_PROFILE_SIZE,
233 'alt' => $user->nickname));
234 $this->elementEnd('div');
236 foreach (array('avatar_crop_x', 'avatar_crop_y',
237 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
238 $this->element('input', array('name' => $crop_info,
240 'id' => $crop_info));
243 // TRANS: Button on avatar upload crop form to confirm a selected crop as avatar.
244 $this->submit('crop', _m('BUTTON','Crop'));
246 $this->elementEnd('li');
247 $this->elementEnd('ul');
248 $this->elementEnd('fieldset');
249 $this->elementEnd('form');
255 * We mux on the button name to figure out what the user actually wanted.
259 function handlePost()
261 // Workaround for PHP returning empty $_POST and $_FILES when POST
262 // length > post_max_size in php.ini
266 && ($_SERVER['CONTENT_LENGTH'] > 0)
268 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
269 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
270 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
271 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
272 intval($_SERVER['CONTENT_LENGTH']));
273 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
279 $token = $this->trimmed('token');
280 if (!$token || $token != common_session_token()) {
281 // TRANS: Client error displayed when the session token does not match or is not given.
282 $this->showForm(_('There was a problem with your session token. '.
283 'Try again, please.'));
287 if (Event::handle('StartAvatarSaveForm', array($this))) {
288 if ($this->arg('upload')) {
289 $this->uploadAvatar();
290 } else if ($this->arg('crop')) {
292 } else if ($this->arg('delete')) {
293 $this->deleteAvatar();
295 // TRANS: Unexpected validation error on avatar upload form.
296 $this->showForm(_('Unexpected form submission.'));
298 Event::handle('EndAvatarSaveForm', array($this));
303 * Handle an image upload
305 * Does all the magic for handling an image upload, and crops the
310 function uploadAvatar()
313 $imagefile = ImageFile::fromUpload('avatarfile');
314 } catch (Exception $e) {
315 $this->showForm($e->getMessage());
318 if ($imagefile === null) {
319 // TRANS: Validation error on avatar upload form when no file was uploaded.
320 $this->showForm(_('No file uploaded.'));
324 $cur = common_current_user();
325 $type = $imagefile->preferredType();
326 $filename = Avatar::filename($cur->id,
327 image_type_to_extension($type),
329 'tmp'.common_timestamp());
331 $filepath = Avatar::path($filename);
332 $imagefile = $imagefile->copyTo($filepath);
334 $filedata = array('filename' => $filename,
335 'filepath' => $filepath,
336 'width' => $imagefile->width,
337 'height' => $imagefile->height,
340 $_SESSION['FILEDATA'] = $filedata;
342 $this->filedata = $filedata;
344 $this->mode = 'crop';
346 // TRANS: Avatar upload form instruction after uploading a file.
347 $this->showForm(_('Pick a square area of the image to be your avatar.'),
352 * Handle the results of jcrop.
356 public function cropAvatar()
358 $filedata = $_SESSION['FILEDATA'];
361 // TRANS: Server error displayed if an avatar upload went wrong somehow server side.
362 $this->serverError(_('Lost our file data.'));
365 $file_d = ($filedata['width'] > $filedata['height'])
366 ? $filedata['height'] : $filedata['width'];
368 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
369 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
370 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
371 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
372 $size = intval(min($dest_w, $dest_h, MAX_ORIGINAL));
374 $user = common_current_user();
375 $profile = $user->getProfile();
377 $imagefile = new ImageFile($user->id, $filedata['filepath']);
378 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
380 if ($profile->setOriginal($filename)) {
381 @unlink($filedata['filepath']);
382 unset($_SESSION['FILEDATA']);
383 $this->mode = 'upload';
384 // TRANS: Success message for having updated a user avatar.
385 $this->showForm(_('Avatar updated.'), true);
387 // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason.
388 $this->showForm(_('Failed updating avatar.'));
393 * Get rid of the current avatar.
397 function deleteAvatar()
399 $user = common_current_user();
400 $profile = $user->getProfile();
402 Avatar::deleteFromProfile($profile);
404 // TRANS: Success message for deleting a user avatar.
405 $this->showForm(_('Avatar deleted.'), true);
409 * Add the jCrop stylesheet
414 function showStylesheets()
416 parent::showStylesheets();
417 $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
421 * Add the jCrop scripts
425 function showScripts()
427 parent::showScripts();
429 if ($this->mode == 'crop') {
430 $this->script('extlib/jquery-jcrop/jcrop.js');
431 $this->script('jcrop.go.js');
434 $this->autofocus('avatarfile');