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;
52 function prepare(array $args=array())
54 $avatarpath = Avatar::path('');
56 if (!is_writable($avatarpath)) {
57 throw new Exception(_("The administrator of your site needs to
58 add write permissions on the avatar upload folder before
59 you're able to set one."));
62 parent::prepare($args);
69 * @return string Title of the page
73 // TRANS: Title for avatar upload page.
78 * Instructions for use
80 * @return instructions for use
82 function getInstructions()
84 // TRANS: Instruction for avatar upload page.
85 // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
86 return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'),
87 ImageFile::maxFileSize());
91 * Content area of the page
93 * Shows a form for uploading an avatar. Currently overrides FormAction's showContent
94 * since we haven't made classes out of AvatarCropForm and AvatarUploadForm.
98 function showContent()
100 if ($this->mode == 'crop') {
101 $this->showCropForm();
103 $this->showUploadForm();
107 function showUploadForm()
109 $this->elementStart('form', array('enctype' => 'multipart/form-data',
111 'id' => 'form_settings_avatar',
112 'class' => 'form_settings',
114 common_local_url('avatarsettings')));
115 $this->elementStart('fieldset');
116 // TRANS: Avatar upload page form legend.
117 $this->element('legend', null, _('Avatar settings'));
118 $this->hidden('token', common_session_token());
120 if (Event::handle('StartAvatarFormData', array($this))) {
121 $this->elementStart('ul', 'form_data');
123 $original = Avatar::getUploaded($this->scoped);
125 $this->elementStart('li', array('id' => 'avatar_original',
126 'class' => 'avatar_view'));
127 // TRANS: Header on avatar upload page for thumbnail of originally uploaded avatar (h2).
128 $this->element('h2', null, _("Original"));
129 $this->elementStart('div', array('id'=>'avatar_original_view'));
130 $this->element('img', array('src' => $original->displayUrl(),
131 'width' => $original->width,
132 'height' => $original->height,
133 'alt' => $this->scoped->getNickname()));
134 $this->elementEnd('div');
135 $this->elementEnd('li');
136 } catch (NoAvatarException $e) {
137 // No original avatar found!
141 $avatar = $this->scoped->getAvatar(AVATAR_PROFILE_SIZE);
142 $this->elementStart('li', array('id' => 'avatar_preview',
143 'class' => 'avatar_view'));
144 // TRANS: Header on avatar upload page for thumbnail of to be used rendition of uploaded avatar (h2).
145 $this->element('h2', null, _("Preview"));
146 $this->elementStart('div', array('id'=>'avatar_preview_view'));
147 $this->element('img', array('src' => $avatar->displayUrl(),
148 'width' => AVATAR_PROFILE_SIZE,
149 'height' => AVATAR_PROFILE_SIZE,
150 'alt' => $this->scoped->getNickname()));
151 $this->elementEnd('div');
152 if (!empty($avatar->filename)) {
153 // TRANS: Button on avatar upload page to delete current avatar.
154 $this->submit('delete', _m('BUTTON','Delete'));
156 $this->elementEnd('li');
157 } catch (NoAvatarException $e) {
158 // No previously uploaded avatar to preview.
161 $this->elementStart('li', array ('id' => 'settings_attach'));
162 $this->element('input', array('name' => 'MAX_FILE_SIZE',
164 'id' => 'MAX_FILE_SIZE',
165 'value' => ImageFile::maxFileSizeInt()));
166 $this->element('input', array('name' => 'avatarfile',
168 'id' => 'avatarfile'));
169 $this->elementEnd('li');
170 $this->elementEnd('ul');
172 $this->elementStart('ul', 'form_actions');
173 $this->elementStart('li');
174 // TRANS: Button on avatar upload page to upload an avatar.
175 $this->submit('upload', _m('BUTTON','Upload'));
176 $this->elementEnd('li');
177 $this->elementEnd('ul');
179 Event::handle('EndAvatarFormData', array($this));
181 $this->elementEnd('fieldset');
182 $this->elementEnd('form');
185 function showCropForm()
187 $this->elementStart('form', array('method' => 'post',
188 'id' => 'form_settings_avatar',
189 'class' => 'form_settings',
191 common_local_url('avatarsettings')));
192 $this->elementStart('fieldset');
193 // TRANS: Avatar upload page crop form legend.
194 $this->element('legend', null, _('Avatar settings'));
195 $this->hidden('token', common_session_token());
197 $this->elementStart('ul', 'form_data');
199 $this->elementStart('li',
200 array('id' => 'avatar_original',
201 'class' => 'avatar_view'));
202 // TRANS: Header on avatar upload crop form for thumbnail of originally uploaded avatar (h2).
203 $this->element('h2', null, _('Original'));
204 $this->elementStart('div', array('id'=>'avatar_original_view'));
205 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
206 'width' => $this->filedata['width'],
207 'height' => $this->filedata['height'],
208 'alt' => $this->scoped->getNickname()));
209 $this->elementEnd('div');
210 $this->elementEnd('li');
212 $this->elementStart('li',
213 array('id' => 'avatar_preview',
214 'class' => 'avatar_view'));
215 // TRANS: Header on avatar upload crop form for thumbnail of to be used rendition of uploaded avatar (h2).
216 $this->element('h2', null, _('Preview'));
217 $this->elementStart('div', array('id'=>'avatar_preview_view'));
218 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
219 'width' => AVATAR_PROFILE_SIZE,
220 'height' => AVATAR_PROFILE_SIZE,
221 'alt' => $this->scoped->getNickname()));
222 $this->elementEnd('div');
224 foreach (array('avatar_crop_x', 'avatar_crop_y',
225 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
226 $this->element('input', array('name' => $crop_info,
228 'id' => $crop_info));
231 // TRANS: Button on avatar upload crop form to confirm a selected crop as avatar.
232 $this->submit('crop', _m('BUTTON','Crop'));
234 $this->elementEnd('li');
235 $this->elementEnd('ul');
236 $this->elementEnd('fieldset');
237 $this->elementEnd('form');
240 protected function doPost()
242 if (Event::handle('StartAvatarSaveForm', array($this))) {
243 if ($this->trimmed('upload')) {
244 return $this->uploadAvatar();
245 } else if ($this->trimmed('crop')) {
246 return $this->cropAvatar();
247 } else if ($this->trimmed('delete')) {
248 return $this->deleteAvatar();
250 // TRANS: Unexpected validation error on avatar upload form.
251 throw new ClientException(_('Unexpected form submission.'));
253 Event::handle('EndAvatarSaveForm', array($this));
258 * Handle an image upload
260 * Does all the magic for handling an image upload, and crops the
265 function uploadAvatar()
267 // ImageFile throws exception if something goes wrong, which we'll
268 // pick up and show as an error message above the form.
269 $imagefile = ImageFile::fromUpload('avatarfile');
271 $type = $imagefile->preferredType();
272 $filename = Avatar::filename($this->scoped->getID(),
273 image_type_to_extension($type),
275 'tmp'.common_timestamp());
277 $filepath = Avatar::path($filename);
278 $imagefile = $imagefile->copyTo($filepath);
280 $filedata = array('filename' => $filename,
281 'filepath' => $filepath,
282 'width' => $imagefile->width,
283 'height' => $imagefile->height,
286 $_SESSION['FILEDATA'] = $filedata;
288 $this->filedata = $filedata;
290 $this->mode = 'crop';
292 // TRANS: Avatar upload form instruction after uploading a file.
293 return _('Pick a square area of the image to be your avatar.');
297 * Handle the results of jcrop.
301 public function cropAvatar()
303 $filedata = $_SESSION['FILEDATA'];
305 if (empty($filedata)) {
306 // TRANS: Server error displayed if an avatar upload went wrong somehow server side.
307 throw new ServerException(_('Lost our file data.'));
310 $file_d = min($filedata['width'], $filedata['height']);
312 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
313 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
314 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
315 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
316 $size = intval(min($dest_w, $dest_h, common_config('avatar', 'maxsize')));
318 $box = array('width' => $size, 'height' => $size,
319 'x' => $dest_x, 'y' => $dest_y,
320 'w' => $dest_w, 'h' => $dest_h);
322 $imagefile = new ImageFile(null, $filedata['filepath']);
323 $filename = Avatar::filename($this->scoped->getID(), image_type_to_extension($imagefile->preferredType()),
324 $size, common_timestamp());
326 $imagefile->resizeTo(Avatar::path($filename), $box);
327 } catch (UseFileAsThumbnailException $e) {
328 common_debug('Using uploaded avatar directly without resizing, copying it to: '.$filename);
329 if (!copy($filedata['filepath'], Avatar::path($filename))) {
330 common_debug('Tried to copy image file '.$filedata['filepath'].' to destination '.Avatar::path($filename));
331 throw new ServerException('Could not copy file to destination.');
335 if ($this->scoped->setOriginal($filename)) {
336 @unlink($filedata['filepath']);
337 unset($_SESSION['FILEDATA']);
338 $this->mode = 'upload';
339 // TRANS: Success message for having updated a user avatar.
340 return _('Avatar updated.');
343 // TRANS: Error displayed on the avatar upload page if the avatar could not be updated for an unknown reason.
344 throw new ServerException(_('Failed updating avatar.'));
348 * Get rid of the current avatar.
352 function deleteAvatar()
354 Avatar::deleteFromProfile($this->scoped);
356 // TRANS: Success message for deleting a user avatar.
357 return _('Avatar deleted.');
361 * Add the jCrop stylesheet
366 function showStylesheets()
368 parent::showStylesheets();
369 $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
373 * Add the jCrop scripts
377 function showScripts()
379 parent::showScripts();
381 if ($this->mode == 'crop') {
382 $this->script('extlib/jquery-jcrop/jcrop.js');
383 $this->script('jcrop.go.js');
386 $this->autofocus('avatarfile');