]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatarsettings.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline
[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         if (Event::handle('StartAvatarFormData', array($this))) {
123             $this->elementStart('ul', 'form_data');
124             if ($original) {
125                 $this->elementStart('li', array('id' => 'avatar_original',
126                                                 'class' => 'avatar_view'));
127                 $this->element('h2', null, _("Original"));
128                 $this->elementStart('div', array('id'=>'avatar_original_view'));
129                 $this->element('img', array('src' => $original->url,
130                                             'width' => $original->width,
131                                             'height' => $original->height,
132                                             'alt' => $user->nickname));
133                 $this->elementEnd('div');
134                 $this->elementEnd('li');
135             }
136
137             $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
138
139             if ($avatar) {
140                 $this->elementStart('li', array('id' => 'avatar_preview',
141                                                 'class' => 'avatar_view'));
142                 $this->element('h2', null, _("Preview"));
143                 $this->elementStart('div', array('id'=>'avatar_preview_view'));
144                 $this->element('img', array('src' => $original->url,
145                                             'width' => AVATAR_PROFILE_SIZE,
146                                             'height' => AVATAR_PROFILE_SIZE,
147                                             'alt' => $user->nickname));
148                 $this->elementEnd('div');
149                 $this->submit('delete', _('Delete'));
150                 $this->elementEnd('li');
151             }
152
153             $this->elementStart('li', array ('id' => 'settings_attach'));
154             $this->element('input', array('name' => 'avatarfile',
155                                           'type' => 'file',
156                                           'id' => 'avatarfile'));
157             $this->element('input', array('name' => 'MAX_FILE_SIZE',
158                                           'type' => 'hidden',
159                                           'id' => 'MAX_FILE_SIZE',
160                                           'value' => ImageFile::maxFileSizeInt()));
161             $this->elementEnd('li');
162             $this->elementEnd('ul');
163
164             $this->elementStart('ul', 'form_actions');
165             $this->elementStart('li');
166             $this->submit('upload', _('Upload'));
167             $this->elementEnd('li');
168             $this->elementEnd('ul');
169         }
170         Event::handle('EndAvatarFormData', array($this));
171
172         $this->elementEnd('fieldset');
173         $this->elementEnd('form');
174
175     }
176
177     function showCropForm()
178     {
179         $user = common_current_user();
180
181         $profile = $user->getProfile();
182
183         if (!$profile) {
184             common_log_db_error($user, 'SELECT', __FILE__);
185             $this->serverError(_('User without matching profile'));
186             return;
187         }
188
189         $original = $profile->getOriginalAvatar();
190
191         $this->elementStart('form', array('method' => 'post',
192                                           'id' => 'form_settings_avatar',
193                                           'class' => 'form_settings',
194                                           'action' =>
195                                           common_local_url('avatarsettings')));
196         $this->elementStart('fieldset');
197         $this->element('legend', null, _('Avatar settings'));
198         $this->hidden('token', common_session_token());
199
200         $this->elementStart('ul', 'form_data');
201
202         $this->elementStart('li',
203                             array('id' => 'avatar_original',
204                                   'class' => 'avatar_view'));
205         $this->element('h2', null, _("Original"));
206         $this->elementStart('div', array('id'=>'avatar_original_view'));
207         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
208                                     'width' => $this->filedata['width'],
209                                     'height' => $this->filedata['height'],
210                                     'alt' => $user->nickname));
211         $this->elementEnd('div');
212         $this->elementEnd('li');
213
214         $this->elementStart('li',
215                             array('id' => 'avatar_preview',
216                                   'class' => 'avatar_view'));
217         $this->element('h2', null, _("Preview"));
218         $this->elementStart('div', array('id'=>'avatar_preview_view'));
219         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
220                                     'width' => AVATAR_PROFILE_SIZE,
221                                     'height' => AVATAR_PROFILE_SIZE,
222                                     'alt' => $user->nickname));
223         $this->elementEnd('div');
224
225         foreach (array('avatar_crop_x', 'avatar_crop_y',
226                        'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
227             $this->element('input', array('name' => $crop_info,
228                                           'type' => 'hidden',
229                                           'id' => $crop_info));
230         }
231         $this->submit('crop', _('Crop'));
232
233         $this->elementEnd('li');
234         $this->elementEnd('ul');
235         $this->elementEnd('fieldset');
236         $this->elementEnd('form');
237
238     }
239
240     /**
241      * Handle a post
242      *
243      * We mux on the button name to figure out what the user actually wanted.
244      *
245      * @return void
246      */
247
248     function handlePost()
249     {
250         // Workaround for PHP returning empty $_POST and $_FILES when POST
251         // length > post_max_size in php.ini
252
253         if (empty($_FILES)
254             && empty($_POST)
255             && ($_SERVER['CONTENT_LENGTH'] > 0)
256         ) {
257             $msg = _('The server was unable to handle that much POST ' .
258                 'data (%s bytes) due to its current configuration.');
259
260             $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
261             return;
262         }
263
264         // CSRF protection
265
266         $token = $this->trimmed('token');
267         if (!$token || $token != common_session_token()) {
268             $this->showForm(_('There was a problem with your session token. '.
269                                'Try again, please.'));
270             return;
271         }
272         
273         if (Event::handle('StartAvatarSaveForm', array($this))) {
274             if ($this->arg('upload')) {
275                 $this->uploadAvatar();
276                 } else if ($this->arg('crop')) {
277                     $this->cropAvatar();
278                 } else if ($this->arg('delete')) {
279                     $this->deleteAvatar();
280                 } else {
281                     $this->showForm(_('Unexpected form submission.'));
282                 }
283             Event::handle('EndAvatarSaveForm', array($this));
284         }
285     }
286
287     /**
288      * Handle an image upload
289      *
290      * Does all the magic for handling an image upload, and crops the
291      * image by default.
292      *
293      * @return void
294      */
295
296     function uploadAvatar()
297     {
298         try {
299             $imagefile = ImageFile::fromUpload('avatarfile');
300         } catch (Exception $e) {
301             $this->showForm($e->getMessage());
302             return;
303         }
304
305         $cur = common_current_user();
306
307         $filename = Avatar::filename($cur->id,
308                                      image_type_to_extension($imagefile->type),
309                                      null,
310                                      'tmp'.common_timestamp());
311
312         $filepath = Avatar::path($filename);
313
314         move_uploaded_file($imagefile->filepath, $filepath);
315
316         $filedata = array('filename' => $filename,
317                           'filepath' => $filepath,
318                           'width' => $imagefile->width,
319                           'height' => $imagefile->height,
320                           'type' => $imagefile->type);
321
322         $_SESSION['FILEDATA'] = $filedata;
323
324         $this->filedata = $filedata;
325
326         $this->mode = 'crop';
327
328         $this->showForm(_('Pick a square area of the image to be your avatar'),
329                         true);
330     }
331
332     /**
333      * Handle the results of jcrop.
334      *
335      * @return void
336      */
337
338     function cropAvatar()
339     {
340         $filedata = $_SESSION['FILEDATA'];
341
342         if (!$filedata) {
343             $this->serverError(_('Lost our file data.'));
344             return;
345         }
346
347         $file_d = ($filedata['width'] > $filedata['height'])
348                      ? $filedata['height'] : $filedata['width'];
349
350         $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
351         $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
352         $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
353         $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
354         $size = min($dest_w, $dest_h, MAX_ORIGINAL);
355
356         $user = common_current_user();
357         $profile = $user->getProfile();
358
359         $imagefile = new ImageFile($user->id, $filedata['filepath']);
360         $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
361
362         if ($profile->setOriginal($filename)) {
363             @unlink($filedata['filepath']);
364             unset($_SESSION['FILEDATA']);
365             $this->mode = 'upload';
366             $this->showForm(_('Avatar updated.'), true);
367             common_broadcast_profile($profile);
368         } else {
369             $this->showForm(_('Failed updating avatar.'));
370         }
371     }
372     
373     /**
374      * Get rid of the current avatar.
375      *
376      * @return void
377      */
378     
379     function deleteAvatar()
380     {
381         $user = common_current_user();
382         $profile = $user->getProfile();
383         
384         $avatar = $profile->getOriginalAvatar();
385         if($avatar) $avatar->delete();
386         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
387         if($avatar) $avatar->delete();
388         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
389         if($avatar) $avatar->delete();
390         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
391         if($avatar) $avatar->delete();
392
393         $this->showForm(_('Avatar deleted.'), true);
394     }
395
396     /**
397      * Add the jCrop stylesheet
398      *
399      * @return void
400      */
401
402     function showStylesheets()
403     {
404         parent::showStylesheets();
405         $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
406     }
407
408     /**
409      * Add the jCrop scripts
410      *
411      * @return void
412      */
413
414     function showScripts()
415     {
416         parent::showScripts();
417
418         if ($this->mode == 'crop') {
419             $this->script('jcrop/jquery.Jcrop.min.js');
420             $this->script('jcrop/jquery.Jcrop.go.js');
421         }
422
423         $this->autofocus('avatarfile');
424     }
425 }