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';
40 * We use jQuery to crop the image after upload.
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @author Zach Copley <zach@controlyourself.ca>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://laconi.ca/
50 class AvatarsettingsAction extends AccountSettingsAction
55 * @return string Title of the page
64 * Instructions for use
66 * @return instructions for use
69 function getInstructions()
71 return _('Set your personal avatar.');
75 * Content area of the page
77 * Shows a form for uploading an avatar.
82 function showContent()
84 $user = common_current_user();
86 $profile = $user->getProfile();
89 common_log_db_error($user, 'SELECT', __FILE__);
90 $this->serverError(_('User without matching profile'));
94 $original = $profile->getOriginalAvatar();
96 $this->elementStart('form', array('enctype' => 'multipart/form-data',
100 common_local_url('avatarsettings')));
101 $this->hidden('token', common_session_token());
104 $this->elementStart('div',
105 array('id' => 'avatar_original',
106 'class' => 'avatar_view'));
107 $this->element('h3', null, _("Original:"));
108 $this->elementStart('div', array('id'=>'avatar_original_view'));
109 $this->element('img', array('src' => $original->url,
110 'class' => 'avatar original',
111 'width' => $original->width,
112 'height' => $original->height,
113 'alt' => $user->nickname));
114 $this->elementEnd('div');
115 $this->elementEnd('div');
118 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
121 $this->elementStart('div',
122 array('id' => 'avatar_preview',
123 'class' => 'avatar_view'));
124 $this->element('h3', null, _("Preview:"));
125 $this->elementStart('div', array('id'=>'avatar_preview_view'));
126 $this->element('img', array('src' => $original->url,//$avatar->url,
127 'class' => 'avatar profile',
128 'width' => AVATAR_PROFILE_SIZE,
129 'height' => AVATAR_PROFILE_SIZE,
130 'alt' => $user->nickname));
131 $this->elementEnd('div');
132 $this->elementEnd('div');
134 foreach (array('avatar_crop_x', 'avatar_crop_y',
135 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
136 $this->element('input', array('name' => $crop_info,
138 'id' => $crop_info));
140 $this->submit('crop', _('Crop'));
143 $this->element('input', array('name' => 'MAX_FILE_SIZE',
145 'id' => 'MAX_FILE_SIZE',
146 'value' => MAX_AVATAR_SIZE));
148 $this->elementStart('p');
150 $this->element('input', array('name' => 'avatarfile',
152 'id' => 'avatarfile'));
153 $this->elementEnd('p');
155 $this->submit('upload', _('Upload'));
156 $this->elementEnd('form');
163 * We mux on the button name to figure out what the user actually wanted.
168 function handlePost()
172 $token = $this->trimmed('token');
173 if (!$token || $token != common_session_token()) {
174 $this->show_form(_('There was a problem with your session token. '.
175 'Try again, please.'));
179 if ($this->arg('upload')) {
180 $this->uploadAvatar();
181 } else if ($this->arg('crop')) {
184 $this->showForm(_('Unexpected form submission.'));
189 * Handle an image upload
191 * Does all the magic for handling an image upload, and crops the
197 function uploadAvatar()
199 switch ($_FILES['avatarfile']['error']) {
200 case UPLOAD_ERR_OK: // success, jump out
202 case UPLOAD_ERR_INI_SIZE:
203 case UPLOAD_ERR_FORM_SIZE:
204 $this->showForm(_('That file is too big.'));
206 case UPLOAD_ERR_PARTIAL:
207 @unlink($_FILES['avatarfile']['tmp_name']);
208 $this->showForm(_('Partial upload.'));
211 $this->showForm(_('System error uploading file.'));
215 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
218 @unlink($_FILES['avatarfile']['tmp_name']);
219 $this->showForm(_('Not an image or corrupt file.'));
229 $this->showForm(_('Unsupported image file format.'));
233 $user = common_current_user();
235 $profile = $user->getProfile();
237 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
238 $this->showForm(_('Avatar updated.'), true);
240 $this->showForm(_('Failed updating avatar.'));
243 @unlink($_FILES['avatarfile']['tmp_name']);
247 * Handle the results of jcrop.
252 function cropAvatar()
254 $user = common_current_user();
256 $profile = $user->getProfile();
258 $x = $this->arg('avatar_crop_x');
259 $y = $this->arg('avatar_crop_y');
260 $w = $this->arg('avatar_crop_w');
261 $h = $this->arg('avatar_crop_h');
263 if ($profile->crop_avatars($x, $y, $w, $h)) {
264 $this->showForm(_('Avatar updated.'), true);
266 $this->showForm(_('Failed updating avatar.'));
271 * Add the jCrop stylesheet
276 function showStylesheets()
278 parent::showStylesheets();
280 common_path('js/jcrop/jquery.Jcrop.css?version='.LACONICA_VERSION);
282 $this->element('link', array('rel' => 'stylesheet',
283 'type' => 'text/css',
284 'href' => $jcropStyle,
285 'media' => 'screen, projection, tv'));
289 * Add the jCrop scripts
294 function showScripts()
296 parent::showScripts();
298 $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
299 $jcropGo = common_path('js/jcrop/jquery.Jcrop.go.js');
301 $this->element('script', array('type' => 'text/javascript',
302 'src' => $jcropPack));
303 $this->element('script', array('type' => 'text/javascript',