3 * Laconica, 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@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @copyright 2008-2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!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@controlyourself.ca>
47 * @author Zach Copley <zach@controlyourself.ca>
48 * @author Sarven Capadisli <csarven@controlyourself.ca>
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50 * @link http://laconi.ca/
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 $this->elementStart('ul', 'form_data');
124 $this->elementStart('li', array('id' => 'avatar_original',
125 'class' => 'avatar_view'));
126 $this->element('h2', null, _("Original"));
127 $this->elementStart('div', array('id'=>'avatar_original_view'));
128 $this->element('img', array('src' => $original->url,
129 'width' => $original->width,
130 'height' => $original->height,
131 'alt' => $user->nickname));
132 $this->elementEnd('div');
133 $this->elementEnd('li');
136 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
139 $this->elementStart('li', array('id' => 'avatar_preview',
140 'class' => 'avatar_view'));
141 $this->element('h2', null, _("Preview"));
142 $this->elementStart('div', array('id'=>'avatar_preview_view'));
143 $this->element('img', array('src' => $original->url,
144 'width' => AVATAR_PROFILE_SIZE,
145 'height' => AVATAR_PROFILE_SIZE,
146 'alt' => $user->nickname));
147 $this->elementEnd('div');
148 $this->elementEnd('li');
151 $this->elementStart('li', array ('id' => 'settings_attach'));
152 $this->element('input', array('name' => 'avatarfile',
154 'id' => 'avatarfile'));
155 $this->element('input', array('name' => 'MAX_FILE_SIZE',
157 'id' => 'MAX_FILE_SIZE',
158 'value' => ImageFile::maxFileSizeInt()));
159 $this->elementEnd('li');
160 $this->elementEnd('ul');
162 $this->elementStart('ul', 'form_actions');
163 $this->elementStart('li');
164 $this->submit('upload', _('Upload'));
165 $this->elementEnd('li');
166 $this->elementEnd('ul');
168 $this->elementEnd('fieldset');
169 $this->elementEnd('form');
173 function showCropForm()
175 $user = common_current_user();
177 $profile = $user->getProfile();
180 common_log_db_error($user, 'SELECT', __FILE__);
181 $this->serverError(_('User without matching profile'));
185 $original = $profile->getOriginalAvatar();
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 $this->element('legend', null, _('Avatar settings'));
194 $this->hidden('token', common_session_token());
196 $this->elementStart('ul', 'form_data');
198 $this->elementStart('li',
199 array('id' => 'avatar_original',
200 'class' => 'avatar_view'));
201 $this->element('h2', null, _("Original"));
202 $this->elementStart('div', array('id'=>'avatar_original_view'));
203 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
204 'width' => $this->filedata['width'],
205 'height' => $this->filedata['height'],
206 'alt' => $user->nickname));
207 $this->elementEnd('div');
208 $this->elementEnd('li');
210 $this->elementStart('li',
211 array('id' => 'avatar_preview',
212 'class' => 'avatar_view'));
213 $this->element('h2', null, _("Preview"));
214 $this->elementStart('div', array('id'=>'avatar_preview_view'));
215 $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
216 'width' => AVATAR_PROFILE_SIZE,
217 'height' => AVATAR_PROFILE_SIZE,
218 'alt' => $user->nickname));
219 $this->elementEnd('div');
221 foreach (array('avatar_crop_x', 'avatar_crop_y',
222 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
223 $this->element('input', array('name' => $crop_info,
225 'id' => $crop_info));
227 $this->submit('crop', _('Crop'));
229 $this->elementEnd('li');
230 $this->elementEnd('ul');
231 $this->elementEnd('fieldset');
232 $this->elementEnd('form');
239 * We mux on the button name to figure out what the user actually wanted.
244 function handlePost()
248 $token = $this->trimmed('token');
249 if (!$token || $token != common_session_token()) {
250 $this->show_form(_('There was a problem with your session token. '.
251 'Try again, please.'));
255 if ($this->arg('upload')) {
256 $this->uploadAvatar();
257 } else if ($this->arg('crop')) {
260 $this->showForm(_('Unexpected form submission.'));
265 * Handle an image upload
267 * Does all the magic for handling an image upload, and crops the
273 function uploadAvatar()
276 $imagefile = ImageFile::fromUpload('avatarfile');
277 } catch (Exception $e) {
278 $this->showForm($e->getMessage());
282 $cur = common_current_user();
284 $filename = Avatar::filename($cur->id,
285 image_type_to_extension($imagefile->type),
287 'tmp'.common_timestamp());
289 $filepath = Avatar::path($filename);
291 move_uploaded_file($imagefile->filepath, $filepath);
293 $filedata = array('filename' => $filename,
294 'filepath' => $filepath,
295 'width' => $imagefile->width,
296 'height' => $imagefile->height,
297 'type' => $imagefile->type);
299 $_SESSION['FILEDATA'] = $filedata;
301 $this->filedata = $filedata;
303 $this->mode = 'crop';
305 $this->showForm(_('Pick a square area of the image to be your avatar'),
310 * Handle the results of jcrop.
315 function cropAvatar()
317 $filedata = $_SESSION['FILEDATA'];
320 $this->serverError(_('Lost our file data.'));
324 // If image is not being cropped assume pos & dimentions of original
325 $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
326 $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
327 $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
328 $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
329 $size = min($dest_w, $dest_h);
330 $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
332 $user = common_current_user();
333 $profile = $user->getProfile();
335 $imagefile = new ImageFile($user->id, $filedata['filepath']);
336 $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
338 if ($profile->setOriginal($filename)) {
339 @unlink($filedata['filepath']);
340 unset($_SESSION['FILEDATA']);
341 $this->mode = 'upload';
342 $this->showForm(_('Avatar updated.'), true);
344 $this->showForm(_('Failed updating avatar.'));
349 * Add the jCrop stylesheet
354 function showStylesheets()
356 parent::showStylesheets();
358 common_path('theme/base/css/jquery.Jcrop.css?version='.LACONICA_VERSION);
360 $this->element('link', array('rel' => 'stylesheet',
361 'type' => 'text/css',
362 'href' => $jcropStyle,
363 'media' => 'screen, projection, tv'));
367 * Add the jCrop scripts
372 function showScripts()
374 parent::showScripts();
376 if ($this->mode == 'crop') {
377 $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
378 $jcropGo = common_path('js/jcrop/jquery.Jcrop.go.js');
380 $this->element('script', array('type' => 'text/javascript',
381 'src' => $jcropPack));
382 $this->element('script', array('type' => 'text/javascript',