]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatarsettings.php
Localisation updates from translatewiki.net
[quix0rs-gnu-social.git] / actions / avatarsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Upload an avatar
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36
37 define('MAX_ORIGINAL', 480);
38
39 /**
40  * Upload an avatar
41  *
42  * We use jCrop plugin for jQuery to crop the image after upload.
43  *
44  * @category Settings
45  * @package  StatusNet
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/
51  */
52
53 class AvatarsettingsAction extends AccountSettingsAction
54 {
55     var $mode = null;
56     var $imagefile = null;
57     var $filename = null;
58
59     /**
60      * Title of the page
61      *
62      * @return string Title of the page
63      */
64
65     function title()
66     {
67         return _('Avatar');
68     }
69
70     /**
71      * Instructions for use
72      *
73      * @return instructions for use
74      */
75
76     function getInstructions()
77     {
78         return sprintf(_('You can upload your personal avatar. The maximum file size is %s.'), ImageFile::maxFileSize());
79     }
80
81     /**
82      * Content area of the page
83      *
84      * Shows a form for uploading an avatar.
85      *
86      * @return void
87      */
88
89     function showContent()
90     {
91         if ($this->mode == 'crop') {
92             $this->showCropForm();
93         } else {
94             $this->showUploadForm();
95         }
96     }
97
98     function showUploadForm()
99     {
100         $user = common_current_user();
101
102         $profile = $user->getProfile();
103
104         if (!$profile) {
105             common_log_db_error($user, 'SELECT', __FILE__);
106             $this->serverError(_('User without matching profile'));
107             return;
108         }
109
110         $original = $profile->getOriginalAvatar();
111
112         $this->elementStart('form', array('enctype' => 'multipart/form-data',
113                                           'method' => 'post',
114                                           'id' => 'form_settings_avatar',
115                                           'class' => 'form_settings',
116                                           'action' =>
117                                           common_local_url('avatarsettings')));
118         $this->elementStart('fieldset');
119         $this->element('legend', null, _('Avatar settings'));
120         $this->hidden('token', common_session_token());
121
122         $this->elementStart('ul', 'form_data');
123         if ($original) {
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');
134         }
135
136         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
137
138         if ($avatar) {
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->submit('delete', _('Delete'));
149             $this->elementEnd('li');
150         }
151
152         $this->elementStart('li', array ('id' => 'settings_attach'));
153         $this->element('input', array('name' => 'avatarfile',
154                                       'type' => 'file',
155                                       'id' => 'avatarfile'));
156         $this->element('input', array('name' => 'MAX_FILE_SIZE',
157                                       'type' => 'hidden',
158                                       'id' => 'MAX_FILE_SIZE',
159                                       'value' => ImageFile::maxFileSizeInt()));
160         $this->elementEnd('li');
161         $this->elementEnd('ul');
162
163         $this->elementStart('ul', 'form_actions');
164         $this->elementStart('li');
165         $this->submit('upload', _('Upload'));
166         $this->elementEnd('li');
167         $this->elementEnd('ul');
168
169         $this->elementEnd('fieldset');
170         $this->elementEnd('form');
171
172     }
173
174     function showCropForm()
175     {
176         $user = common_current_user();
177
178         $profile = $user->getProfile();
179
180         if (!$profile) {
181             common_log_db_error($user, 'SELECT', __FILE__);
182             $this->serverError(_('User without matching profile'));
183             return;
184         }
185
186         $original = $profile->getOriginalAvatar();
187
188         $this->elementStart('form', array('method' => 'post',
189                                           'id' => 'form_settings_avatar',
190                                           'class' => 'form_settings',
191                                           'action' =>
192                                           common_local_url('avatarsettings')));
193         $this->elementStart('fieldset');
194         $this->element('legend', null, _('Avatar settings'));
195         $this->hidden('token', common_session_token());
196
197         $this->elementStart('ul', 'form_data');
198
199         $this->elementStart('li',
200                             array('id' => 'avatar_original',
201                                   'class' => 'avatar_view'));
202         $this->element('h2', null, _("Original"));
203         $this->elementStart('div', array('id'=>'avatar_original_view'));
204         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
205                                     'width' => $this->filedata['width'],
206                                     'height' => $this->filedata['height'],
207                                     'alt' => $user->nickname));
208         $this->elementEnd('div');
209         $this->elementEnd('li');
210
211         $this->elementStart('li',
212                             array('id' => 'avatar_preview',
213                                   'class' => 'avatar_view'));
214         $this->element('h2', null, _("Preview"));
215         $this->elementStart('div', array('id'=>'avatar_preview_view'));
216         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
217                                     'width' => AVATAR_PROFILE_SIZE,
218                                     'height' => AVATAR_PROFILE_SIZE,
219                                     'alt' => $user->nickname));
220         $this->elementEnd('div');
221
222         foreach (array('avatar_crop_x', 'avatar_crop_y',
223                        'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
224             $this->element('input', array('name' => $crop_info,
225                                           'type' => 'hidden',
226                                           'id' => $crop_info));
227         }
228         $this->submit('crop', _('Crop'));
229
230         $this->elementEnd('li');
231         $this->elementEnd('ul');
232         $this->elementEnd('fieldset');
233         $this->elementEnd('form');
234
235     }
236
237     /**
238      * Handle a post
239      *
240      * We mux on the button name to figure out what the user actually wanted.
241      *
242      * @return void
243      */
244
245     function handlePost()
246     {
247         // Workaround for PHP returning empty $_POST and $_FILES when POST
248         // length > post_max_size in php.ini
249
250         if (empty($_FILES)
251             && empty($_POST)
252             && ($_SERVER['CONTENT_LENGTH'] > 0)
253         ) {
254             $msg = _('The server was unable to handle that much POST ' .
255                 'data (%s bytes) due to its current configuration.');
256
257             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
258             return;
259         }
260
261         // CSRF protection
262
263         $token = $this->trimmed('token');
264         if (!$token || $token != common_session_token()) {
265             $this->showForm(_('There was a problem with your session token. '.
266                                'Try again, please.'));
267             return;
268         }
269
270         if ($this->arg('upload')) {
271             $this->uploadAvatar();
272         } else if ($this->arg('crop')) {
273             $this->cropAvatar();
274         } else if ($this->arg('delete')) {
275             $this->deleteAvatar();
276         } else {
277             $this->showForm(_('Unexpected form submission.'));
278         }
279     }
280
281     /**
282      * Handle an image upload
283      *
284      * Does all the magic for handling an image upload, and crops the
285      * image by default.
286      *
287      * @return void
288      */
289
290     function uploadAvatar()
291     {
292         try {
293             $imagefile = ImageFile::fromUpload('avatarfile');
294         } catch (Exception $e) {
295             $this->showForm($e->getMessage());
296             return;
297         }
298
299         $cur = common_current_user();
300
301         $filename = Avatar::filename($cur->id,
302                                      image_type_to_extension($imagefile->type),
303                                      null,
304                                      'tmp'.common_timestamp());
305
306         $filepath = Avatar::path($filename);
307
308         move_uploaded_file($imagefile->filepath, $filepath);
309
310         $filedata = array('filename' => $filename,
311                           'filepath' => $filepath,
312                           'width' => $imagefile->width,
313                           'height' => $imagefile->height,
314                           'type' => $imagefile->type);
315
316         $_SESSION['FILEDATA'] = $filedata;
317
318         $this->filedata = $filedata;
319
320         $this->mode = 'crop';
321
322         $this->showForm(_('Pick a square area of the image to be your avatar'),
323                         true);
324     }
325
326     /**
327      * Handle the results of jcrop.
328      *
329      * @return void
330      */
331
332     function cropAvatar()
333     {
334         $filedata = $_SESSION['FILEDATA'];
335
336         if (!$filedata) {
337             $this->serverError(_('Lost our file data.'));
338             return;
339         }
340
341         $file_d = ($filedata['width'] > $filedata['height'])
342                      ? $filedata['height'] : $filedata['width'];
343
344         $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
345         $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
346         $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
347         $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
348         $size = min($dest_w, $dest_h, MAX_ORIGINAL);
349
350         $user = common_current_user();
351         $profile = $user->getProfile();
352
353         $imagefile = new ImageFile($user->id, $filedata['filepath']);
354         $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
355
356         if ($profile->setOriginal($filename)) {
357             @unlink($filedata['filepath']);
358             unset($_SESSION['FILEDATA']);
359             $this->mode = 'upload';
360             $this->showForm(_('Avatar updated.'), true);
361             common_broadcast_profile($profile);
362         } else {
363             $this->showForm(_('Failed updating avatar.'));
364         }
365     }
366     
367     /**
368      * Get rid of the current avatar.
369      *
370      * @return void
371      */
372     
373     function deleteAvatar()
374     {
375         $user = common_current_user();
376         $profile = $user->getProfile();
377         
378         $avatar = $profile->getOriginalAvatar();
379         if($avatar) $avatar->delete();
380         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
381         if($avatar) $avatar->delete();
382         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
383         if($avatar) $avatar->delete();
384         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
385         if($avatar) $avatar->delete();
386
387         $this->showForm(_('Avatar deleted.'), true);
388     }
389
390     /**
391      * Add the jCrop stylesheet
392      *
393      * @return void
394      */
395
396     function showStylesheets()
397     {
398         parent::showStylesheets();
399         $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
400     }
401
402     /**
403      * Add the jCrop scripts
404      *
405      * @return void
406      */
407
408     function showScripts()
409     {
410         parent::showScripts();
411
412         if ($this->mode == 'crop') {
413             $this->script('js/jcrop/jquery.Jcrop.min.js');
414             $this->script('js/jcrop/jquery.Jcrop.go.js');
415         }
416
417         $this->autofocus('avatarfile');
418     }
419 }