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')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
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/
53 class AvatarsettingsAction extends AccountSettingsAction
56 var $imagefile = null;
62 * @return string Title of the page
71 * Instructions for use
73 * @return instructions for use
76 function getInstructions()
78 return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'), ImageFile::maxFileSize());
82 * Content area of the page
84 * Shows a form for uploading an avatar.
89 function showContent()
91 if ($this->mode == 'crop') {
92 $this->showCropForm();
94 $this->showUploadForm();
98 function showUploadForm()
100 $user = common_current_user();
102 $profile = $user->getProfile();
105 common_log_db_error($user, 'SELECT', __FILE__);
106 $this->serverError(_('User without matching profile.'));
110 $original = $profile->getOriginalAvatar();
112 $this->elementStart('form', array('enctype' => 'multipart/form-data',
114 'id' => 'form_settings_avatar',
115 'class' => 'form_settings',
117 common_local_url('avatarsettings')));
118 $this->elementStart('fieldset');
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 $this->elementStart('li', array('id' => 'avatar_original',
126 'class' => 'avatar_view'));
127 $this->element('h2', null, _("Original"));
128 $this->elementStart('div', array('id'=>'avatar_original_view'));
129 $this->element('img', array('src' => $original->url,
130 'width' => $original->width,
131 'height' => $original->height,
132 'alt' => $user->nickname));
133 $this->elementEnd('div');
134 $this->elementEnd('li');
137 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
140 $this->elementStart('li', array('id' => 'avatar_preview',
141 'class' => 'avatar_view'));
142 $this->element('h2', null, _("Preview"));
143 $this->elementStart('div', array('id'=>'avatar_preview_view'));
144 $this->element('img', array('src' => $original->url,
145 'width' => AVATAR_PROFILE_SIZE,
146 'height' => AVATAR_PROFILE_SIZE,
147 'alt' => $user->nickname));
148 $this->elementEnd('div');
149 $this->submit('delete', _('Delete'));
150 $this->elementEnd('li');
153 $this->elementStart('li', array ('id' => 'settings_attach'));
154 $this->element('input', array('name' => 'avatarfile',
156 'id' => 'avatarfile'));
157 $this->element('input', array('name' => 'MAX_FILE_SIZE',
159 'id' => 'MAX_FILE_SIZE',
160 'value' => ImageFile::maxFileSizeInt()));
161 $this->elementEnd('li');
162 $this->elementEnd('ul');
164 $this->elementStart('ul', 'form_actions');
165 $this->elementStart('li');
166 $this->submit('upload', _('Upload'));
167 $this->elementEnd('li');
168 $this->elementEnd('ul');
170 Event::handle('EndAvatarFormData', array($this));
172 $this->elementEnd('fieldset');
173 $this->elementEnd('form');
177 function showCropForm()
179 $user = common_current_user();
181 $profile = $user->getProfile();
184 common_log_db_error($user, 'SELECT', __FILE__);
185 $this->serverError(_('User without matching profile.'));
189 $original = $profile->getOriginalAvatar();
191 $this->elementStart('form', array('method' => 'post',
192 'id' => 'form_settings_avatar',
193 'class' => 'form_settings',
195 common_local_url('avatarsettings')));
196 $this->elementStart('fieldset');
197 $this->element('legend', null, _('Avatar settings'));
198 $this->hidden('token', common_session_token());
200 $this->elementStart('ul', 'form_data');
202 $this->elementStart('li',
203 array('id' => 'avatar_original',
204 'class' => 'avatar_view'));
205 $this->element('h2', null, _("Original"));
206 $this->elementStart('div', array('id'=>'avatar_original_view'));
207 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
208 'width' => $this->filedata['width'],
209 'height' => $this->filedata['height'],
210 'alt' => $user->nickname));
211 $this->elementEnd('div');
212 $this->elementEnd('li');
214 $this->elementStart('li',
215 array('id' => 'avatar_preview',
216 'class' => 'avatar_view'));
217 $this->element('h2', null, _("Preview"));
218 $this->elementStart('div', array('id'=>'avatar_preview_view'));
219 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
220 'width' => AVATAR_PROFILE_SIZE,
221 'height' => AVATAR_PROFILE_SIZE,
222 'alt' => $user->nickname));
223 $this->elementEnd('div');
225 foreach (array('avatar_crop_x', 'avatar_crop_y',
226 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
227 $this->element('input', array('name' => $crop_info,
229 'id' => $crop_info));
231 $this->submit('crop', _('Crop'));
233 $this->elementEnd('li');
234 $this->elementEnd('ul');
235 $this->elementEnd('fieldset');
236 $this->elementEnd('form');
243 * We mux on the button name to figure out what the user actually wanted.
248 function handlePost()
250 // Workaround for PHP returning empty $_POST and $_FILES when POST
251 // length > post_max_size in php.ini
255 && ($_SERVER['CONTENT_LENGTH'] > 0)
257 // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
258 // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
259 $msg = _m('The server was unable to handle that much POST data (%s byte) due to its current configuration.',
260 'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
261 intval($_SERVER['CONTENT_LENGTH']));
262 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
268 $token = $this->trimmed('token');
269 if (!$token || $token != common_session_token()) {
270 $this->showForm(_('There was a problem with your session token. '.
271 'Try again, please.'));
275 if (Event::handle('StartAvatarSaveForm', array($this))) {
276 if ($this->arg('upload')) {
277 $this->uploadAvatar();
278 } else if ($this->arg('crop')) {
280 } else if ($this->arg('delete')) {
281 $this->deleteAvatar();
283 $this->showForm(_('Unexpected form submission.'));
285 Event::handle('EndAvatarSaveForm', array($this));
290 * Handle an image upload
292 * Does all the magic for handling an image upload, and crops the
298 function uploadAvatar()
301 $imagefile = ImageFile::fromUpload('avatarfile');
302 } catch (Exception $e) {
303 $this->showForm($e->getMessage());
306 if ($imagefile === null) {
307 $this->showForm(_('No file uploaded.'));
311 $cur = common_current_user();
313 $filename = Avatar::filename($cur->id,
314 image_type_to_extension($imagefile->type),
316 'tmp'.common_timestamp());
318 $filepath = Avatar::path($filename);
320 move_uploaded_file($imagefile->filepath, $filepath);
322 $filedata = array('filename' => $filename,
323 'filepath' => $filepath,
324 'width' => $imagefile->width,
325 'height' => $imagefile->height,
326 'type' => $imagefile->type);
328 $_SESSION['FILEDATA'] = $filedata;
330 $this->filedata = $filedata;
332 $this->mode = 'crop';
334 $this->showForm(_('Pick a square area of the image to be your avatar'),
339 * Handle the results of jcrop.
344 function cropAvatar()
346 $filedata = $_SESSION['FILEDATA'];
349 $this->serverError(_('Lost our file data.'));
353 $file_d = ($filedata['width'] > $filedata['height'])
354 ? $filedata['height'] : $filedata['width'];
356 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
357 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
358 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
359 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
360 $size = min($dest_w, $dest_h, MAX_ORIGINAL);
362 $user = common_current_user();
363 $profile = $user->getProfile();
365 $imagefile = new ImageFile($user->id, $filedata['filepath']);
366 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
368 if ($profile->setOriginal($filename)) {
369 @unlink($filedata['filepath']);
370 unset($_SESSION['FILEDATA']);
371 $this->mode = 'upload';
372 $this->showForm(_('Avatar updated.'), true);
373 common_broadcast_profile($profile);
375 $this->showForm(_('Failed updating avatar.'));
380 * Get rid of the current avatar.
385 function deleteAvatar()
387 $user = common_current_user();
388 $profile = $user->getProfile();
390 $avatar = $profile->getOriginalAvatar();
391 if($avatar) $avatar->delete();
392 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
393 if($avatar) $avatar->delete();
394 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
395 if($avatar) $avatar->delete();
396 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
397 if($avatar) $avatar->delete();
399 $this->showForm(_('Avatar deleted.'), true);
403 * Add the jCrop stylesheet
408 function showStylesheets()
410 parent::showStylesheets();
411 $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
415 * Add the jCrop scripts
420 function showScripts()
422 parent::showScripts();
424 if ($this->mode == 'crop') {
425 $this->script('jcrop/jquery.Jcrop.min.js');
426 $this->script('jcrop/jquery.Jcrop.go.js');
429 $this->autofocus('avatarfile');