]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatarsettings.php
Update message formatting for serverError to use a starting capital and a leading...
[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         if ($imagefile === null) {
305             $this->showForm(_('No file uploaded.'));
306             return;
307         }
308
309         $cur = common_current_user();
310
311         $filename = Avatar::filename($cur->id,
312                                      image_type_to_extension($imagefile->type),
313                                      null,
314                                      'tmp'.common_timestamp());
315
316         $filepath = Avatar::path($filename);
317
318         move_uploaded_file($imagefile->filepath, $filepath);
319
320         $filedata = array('filename' => $filename,
321                           'filepath' => $filepath,
322                           'width' => $imagefile->width,
323                           'height' => $imagefile->height,
324                           'type' => $imagefile->type);
325
326         $_SESSION['FILEDATA'] = $filedata;
327
328         $this->filedata = $filedata;
329
330         $this->mode = 'crop';
331
332         $this->showForm(_('Pick a square area of the image to be your avatar'),
333                         true);
334     }
335
336     /**
337      * Handle the results of jcrop.
338      *
339      * @return void
340      */
341
342     function cropAvatar()
343     {
344         $filedata = $_SESSION['FILEDATA'];
345
346         if (!$filedata) {
347             $this->serverError(_('Lost our file data.'));
348             return;
349         }
350
351         $file_d = ($filedata['width'] > $filedata['height'])
352                      ? $filedata['height'] : $filedata['width'];
353
354         $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
355         $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
356         $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$file_d;
357         $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$file_d;
358         $size = min($dest_w, $dest_h, MAX_ORIGINAL);
359
360         $user = common_current_user();
361         $profile = $user->getProfile();
362
363         $imagefile = new ImageFile($user->id, $filedata['filepath']);
364         $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
365
366         if ($profile->setOriginal($filename)) {
367             @unlink($filedata['filepath']);
368             unset($_SESSION['FILEDATA']);
369             $this->mode = 'upload';
370             $this->showForm(_('Avatar updated.'), true);
371             common_broadcast_profile($profile);
372         } else {
373             $this->showForm(_('Failed updating avatar.'));
374         }
375     }
376     
377     /**
378      * Get rid of the current avatar.
379      *
380      * @return void
381      */
382     
383     function deleteAvatar()
384     {
385         $user = common_current_user();
386         $profile = $user->getProfile();
387         
388         $avatar = $profile->getOriginalAvatar();
389         if($avatar) $avatar->delete();
390         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
391         if($avatar) $avatar->delete();
392         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
393         if($avatar) $avatar->delete();
394         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
395         if($avatar) $avatar->delete();
396
397         $this->showForm(_('Avatar deleted.'), true);
398     }
399
400     /**
401      * Add the jCrop stylesheet
402      *
403      * @return void
404      */
405
406     function showStylesheets()
407     {
408         parent::showStylesheets();
409         $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
410     }
411
412     /**
413      * Add the jCrop scripts
414      *
415      * @return void
416      */
417
418     function showScripts()
419     {
420         parent::showScripts();
421
422         if ($this->mode == 'crop') {
423             $this->script('jcrop/jquery.Jcrop.min.js');
424             $this->script('jcrop/jquery.Jcrop.go.js');
425         }
426
427         $this->autofocus('avatarfile');
428     }
429 }