]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatarsettings.php
1db35237ed2d3d64eb3e34ce505c1070b1462892
[quix0rs-gnu-social.git] / actions / avatarsettings.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
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/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36
37 /**
38  * Upload an avatar
39  *
40  * We use jCrop plugin for jQuery to crop the image after upload.
41  *
42  * @category Settings
43  * @package  Laconica
44  * @author   Evan Prodromou <evan@controlyourself.ca>
45  * @author   Zach Copley <zach@controlyourself.ca>
46  * @author   Sarven Capadisli <csarven@controlyourself.ca>
47  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48  * @link     http://laconi.ca/
49  */
50
51 class AvatarsettingsAction extends AccountSettingsAction
52 {
53     var $mode = null;
54     var $imagefile = null;
55     var $filename = null;
56
57     /**
58      * Title of the page
59      *
60      * @return string Title of the page
61      */
62
63     function title()
64     {
65         return _('Avatar');
66     }
67
68     /**
69      * Instructions for use
70      *
71      * @return instructions for use
72      */
73
74     function getInstructions()
75     {
76         return _('You can upload your personal avatar.');
77     }
78
79     /**
80      * Content area of the page
81      *
82      * Shows a form for uploading an avatar.
83      *
84      * @return void
85      */
86
87     function showContent()
88     {
89         if ($this->mode == 'crop') {
90             $this->showCropForm();
91         } else {
92             $this->showUploadForm();
93         }
94     }
95
96     function showUploadForm()
97     {
98         $user = common_current_user();
99
100         $profile = $user->getProfile();
101
102         if (!$profile) {
103             common_log_db_error($user, 'SELECT', __FILE__);
104             $this->serverError(_('User without matching profile'));
105             return;
106         }
107
108         $original = $profile->getOriginalAvatar();
109
110         $this->elementStart('form', array('enctype' => 'multipart/form-data',
111                                           'method' => 'post',
112                                           'id' => 'form_settings_avatar',
113                                           'class' => 'form_settings',
114                                           'action' =>
115                                           common_local_url('avatarsettings')));
116         $this->elementStart('fieldset');
117         $this->element('legend', null, _('Avatar settings'));
118         $this->hidden('token', common_session_token());
119
120         $this->elementStart('ul', 'form_data');
121         if ($original) {
122             $this->elementStart('li',
123                                 array('id' => 'avatar_original',
124                                       'class' => 'avatar_view'));
125             $this->element('h2', null, _("Original"));
126             $this->elementStart('div', array('id'=>'avatar_original_view'));
127             $this->element('img', array('src' => $original->url,
128                                         'width' => $original->width,
129                                         'height' => $original->height,
130                                         'alt' => $user->nickname));
131             $this->elementEnd('div');
132             $this->elementEnd('li');
133         }
134
135         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
136
137         if ($avatar) {
138             $this->elementStart('li',
139                                 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,//$avatar->url,
144                                         'width' => AVATAR_PROFILE_SIZE,
145                                         'height' => AVATAR_PROFILE_SIZE,
146                                         'alt' => $user->nickname));
147             $this->elementEnd('div');
148         }
149
150         $this->elementStart('li', array ('id' => 'settings_attach'));
151         $this->element('input', array('name' => 'avatarfile',
152                                       'type' => 'file',
153                                       'id' => 'avatarfile'));
154         $this->element('input', array('name' => 'MAX_FILE_SIZE',
155                                       'type' => 'hidden',
156                                       'id' => 'MAX_FILE_SIZE',
157                                       'value' => MAX_AVATAR_SIZE));
158         $this->elementEnd('li');
159         $this->elementEnd('ul');
160
161         $this->elementStart('ul', 'form_actions');
162         $this->elementStart('li');
163         $this->submit('upload', _('Upload'));
164         $this->elementEnd('li');
165         $this->elementEnd('ul');
166
167         $this->elementEnd('fieldset');
168         $this->elementEnd('form');
169
170     }
171
172     function showCropForm()
173     {
174         $user = common_current_user();
175
176         $profile = $user->getProfile();
177
178         if (!$profile) {
179             common_log_db_error($user, 'SELECT', __FILE__);
180             $this->serverError(_('User without matching profile'));
181             return;
182         }
183
184         $original = $profile->getOriginalAvatar();
185
186         $this->elementStart('form', array('method' => 'post',
187                                           'id' => 'form_settings_avatar',
188                                           'class' => 'form_settings',
189                                           'action' =>
190                                           common_local_url('avatarsettings')));
191         $this->elementStart('fieldset');
192         $this->element('legend', null, _('Avatar settings'));
193         $this->hidden('token', common_session_token());
194
195         $this->elementStart('ul', 'form_data');
196
197         $this->elementStart('li',
198                             array('id' => 'avatar_original',
199                                   'class' => 'avatar_view'));
200         $this->element('h2', null, _("Original"));
201         $this->elementStart('div', array('id'=>'avatar_original_view'));
202         $this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
203                                     'width' => $this->filedata['width'],
204                                     'height' => $this->filedata['height'],
205                                     'alt' => $user->nickname));
206         $this->elementEnd('div');
207         $this->elementEnd('li');
208
209         $this->elementStart('li',
210                             array('id' => 'avatar_preview',
211                                   'class' => 'avatar_view'));
212         $this->element('h2', null, _("Preview"));
213         $this->elementStart('div', array('id'=>'avatar_preview_view'));
214         $this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
215                                     'width' => AVATAR_PROFILE_SIZE,
216                                     'height' => AVATAR_PROFILE_SIZE,
217                                     'alt' => $user->nickname));
218         $this->elementEnd('div');
219
220         foreach (array('avatar_crop_x', 'avatar_crop_y',
221                        'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
222             $this->element('input', array('name' => $crop_info,
223                                           'type' => 'hidden',
224                                           'id' => $crop_info));
225         }
226         $this->submit('crop', _('Crop'));
227
228         $this->elementEnd('li');
229         $this->elementEnd('ul');
230         $this->elementEnd('fieldset');
231         $this->elementEnd('form');
232
233     }
234
235     /**
236      * Handle a post
237      *
238      * We mux on the button name to figure out what the user actually wanted.
239      *
240      * @return void
241      */
242
243     function handlePost()
244     {
245         // CSRF protection
246
247         $token = $this->trimmed('token');
248         if (!$token || $token != common_session_token()) {
249             $this->show_form(_('There was a problem with your session token. '.
250                                'Try again, please.'));
251             return;
252         }
253
254         if ($this->arg('upload')) {
255             $this->uploadAvatar();
256         } else if ($this->arg('crop')) {
257             $this->cropAvatar();
258         } else {
259             $this->showForm(_('Unexpected form submission.'));
260         }
261     }
262
263     /**
264      * Handle an image upload
265      *
266      * Does all the magic for handling an image upload, and crops the
267      * image by default.
268      *
269      * @return void
270      */
271
272     function uploadAvatar()
273     {
274         try {
275             $imagefile = ImageFile::fromUpload('avatarfile');
276         } catch (Exception $e) {
277             $this->showForm($e->getMessage());
278             return;
279         }
280
281         $cur = common_current_user();
282
283         $filename = common_avatar_filename($cur->id,
284                                            image_type_to_extension($imagefile->type),
285                                            null,
286                                            'tmp'.common_timestamp());
287
288         $filepath = common_avatar_path($filename);
289
290         move_uploaded_file($imagefile->filename, $filepath);
291
292         $filedata = array('filename' => $filename,
293                           'filepath' => $filepath,
294                           'width' => $imagefile->width,
295                           'height' => $imagefile->height,
296                           'type' => $imagefile->type);
297
298         $_SESSION['FILEDATA'] = $filedata;
299
300         $this->filedata = $filedata;
301
302         $this->mode = 'crop';
303
304         $this->showForm(_('Pick a square area of the image to be your avatar'),
305                         true);
306     }
307
308     /**
309      * Handle the results of jcrop.
310      *
311      * @return void
312      */
313
314     function cropAvatar()
315     {
316         $user = common_current_user();
317
318         $profile = $user->getProfile();
319
320         $x = $this->arg('avatar_crop_x');
321         $y = $this->arg('avatar_crop_y');
322         $w = $this->arg('avatar_crop_w');
323         $h = $this->arg('avatar_crop_h');
324
325         $filedata = $_SESSION['FILEDATA'];
326
327         if (!$filedata) {
328             $this->serverError(_('Lost our file data.'));
329             return;
330         }
331
332         $filepath = common_avatar_path($filedata['filename']);
333
334         if (!file_exists($filepath)) {
335             $this->serverError(_('Lost our file.'));
336             return;
337         }
338
339         switch ($filedata['type']) {
340         case IMAGETYPE_GIF:
341             $image_src = imagecreatefromgif($filepath);
342             break;
343         case IMAGETYPE_JPEG:
344             $image_src = imagecreatefromjpeg($filepath);
345             break;
346         case IMAGETYPE_PNG:
347             $image_src = imagecreatefrompng($filepath);
348             break;
349          default:
350             $this->serverError(_('Unknown file type'));
351             return;
352         }
353
354         common_debug("W = $w, H = $h, X = $x, Y = $y");
355
356         $image_dest = imagecreatetruecolor($w, $h);
357
358         $background = imagecolorallocate($image_dest, 0, 0, 0);
359         ImageColorTransparent($image_dest, $background);
360         imagealphablending($image_dest, false);
361
362         imagecopyresized($image_dest, $image_src, 0, 0, $x, $y, $w, $h, $w, $h);
363
364         $cur = common_current_user();
365
366         $filename = common_avatar_filename($cur->id,
367                                            image_type_to_extension($filedata['type']),
368                                            null,
369                                            common_timestamp());
370
371         $filepath = common_avatar_path($filename);
372
373         switch ($filedata['type']) {
374         case IMAGETYPE_GIF:
375             imagegif($image_dest, $filepath);
376             break;
377         case IMAGETYPE_JPEG:
378             imagejpeg($image_dest, $filepath);
379             break;
380         case IMAGETYPE_PNG:
381             imagepng($image_dest, $filepath);
382             break;
383          default:
384             $this->serverError(_('Unknown file type'));
385             return;
386         }
387
388         $user = common_current_user();
389
390         $profile = $cur->getProfile();
391
392         if ($profile->setOriginal($filepath)) {
393             @unlink(common_avatar_path($filedata['filename']));
394             unset($_SESSION['FILEDATA']);
395             $this->mode = 'upload';
396             $this->showForm(_('Avatar updated.'), true);
397         } else {
398             $this->showForm(_('Failed updating avatar.'));
399         }
400     }
401
402     /**
403      * Add the jCrop stylesheet
404      *
405      * @return void
406      */
407
408     function showStylesheets()
409     {
410         parent::showStylesheets();
411         $jcropStyle =
412           common_path('theme/base/css/jquery.Jcrop.css?version='.LACONICA_VERSION);
413
414         $this->element('link', array('rel' => 'stylesheet',
415                                      'type' => 'text/css',
416                                      'href' => $jcropStyle,
417                                      'media' => 'screen, projection, tv'));
418     }
419
420     /**
421      * Add the jCrop scripts
422      *
423      * @return void
424      */
425
426     function showScripts()
427     {
428         parent::showScripts();
429
430         $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
431         $jcropGo   = common_path('js/jcrop/jquery.Jcrop.go.js');
432
433         $this->element('script', array('type' => 'text/javascript',
434                                        'src' => $jcropPack));
435         $this->element('script', array('type' => 'text/javascript',
436                                        'src' => $jcropGo));
437     }
438 }