]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatarsettings.php
2c7af9b7a88741404f4117e496c301a22ede739d
[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     /**
54      * Title of the page
55      *
56      * @return string Title of the page
57      */
58
59     function title()
60     {
61         return _('Avatar');
62     }
63
64     /**
65      * Instructions for use
66      *
67      * @return instructions for use
68      */
69
70     function getInstructions()
71     {
72         return _('Set your personal avatar.');
73     }
74
75     /**
76      * Content area of the page
77      *
78      * Shows a form for uploading an avatar.
79      *
80      * @return void
81      */
82
83     function showContent()
84     {
85         $user = common_current_user();
86
87         $profile = $user->getProfile();
88
89         if (!$profile) {
90             common_log_db_error($user, 'SELECT', __FILE__);
91             $this->serverError(_('User without matching profile'));
92             return;
93         }
94
95         $original = $profile->getOriginalAvatar();
96
97         $this->elementStart('form', array('enctype' => 'multipart/form-data',
98                                           'method' => 'post',
99                                           'id' => 'form_settings_avatar',
100                                           'class' => 'form_settings',
101                                           'action' =>
102                                           common_local_url('avatarsettings')));
103         $this->elementStart('fieldset');
104         $this->element('legend', null, _('Avatar settings'));
105         $this->hidden('token', common_session_token());
106
107         $this->elementStart('ul', 'form_data');
108         if ($original) {
109             $this->elementStart('li',
110                                 array('id' => 'avatar_original',
111                                       'class' => 'avatar_view'));
112             $this->element('h2', null, _("Original"));
113             $this->elementStart('div', array('id'=>'avatar_original_view'));
114             $this->element('img', array('src' => $original->url,
115                                         'width' => $original->width,
116                                         'height' => $original->height,
117                                         'alt' => $user->nickname));
118             $this->elementEnd('div');
119             $this->elementEnd('li');
120         }
121
122         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
123
124         if ($avatar) {
125             $this->elementStart('li',
126                                 array('id' => 'avatar_preview',
127                                       'class' => 'avatar_view'));
128             $this->element('h2', null, _("Preview"));
129             $this->elementStart('div', array('id'=>'avatar_preview_view'));
130             $this->element('img', array('src' => $original->url,//$avatar->url,
131                                         'width' => AVATAR_PROFILE_SIZE,
132                                         'height' => AVATAR_PROFILE_SIZE,
133                                         'alt' => $user->nickname));
134             $this->elementEnd('div');
135
136             foreach (array('avatar_crop_x', 'avatar_crop_y',
137                            'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
138                 $this->element('input', array('name' => $crop_info,
139                                               'type' => 'hidden',
140                                               'id' => $crop_info));
141             }
142             $this->submit('crop', _('Crop'));
143
144             $this->elementEnd('li');
145         }
146
147         $this->elementStart('li', array ('id' => 'settings_attach'));
148         $this->element('input', array('name' => 'avatarfile',
149                                       'type' => 'file',
150                                       'id' => 'avatarfile'));
151         $this->element('input', array('name' => 'MAX_FILE_SIZE',
152                                       'type' => 'hidden',
153                                       'id' => 'MAX_FILE_SIZE',
154                                       'value' => MAX_AVATAR_SIZE));
155         $this->elementEnd('li');
156         $this->elementEnd('ul');
157
158         $this->elementStart('ul', 'form_actions');
159         $this->elementStart('li');
160         $this->submit('upload', _('Upload'));
161         $this->elementEnd('li');
162         $this->elementEnd('ul');
163
164         $this->elementEnd('fieldset');
165         $this->elementEnd('form');
166
167     }
168
169     /**
170      * Handle a post
171      *
172      * We mux on the button name to figure out what the user actually wanted.
173      *
174      * @return void
175      */
176
177     function handlePost()
178     {
179         // CSRF protection
180
181         $token = $this->trimmed('token');
182         if (!$token || $token != common_session_token()) {
183             $this->show_form(_('There was a problem with your session token. '.
184                                'Try again, please.'));
185             return;
186         }
187
188         if ($this->arg('upload')) {
189             $this->uploadAvatar();
190         } else if ($this->arg('crop')) {
191             $this->cropAvatar();
192         } else {
193             $this->showForm(_('Unexpected form submission.'));
194         }
195     }
196
197     /**
198      * Handle an image upload
199      *
200      * Does all the magic for handling an image upload, and crops the
201      * image by default.
202      *
203      * @return void
204      */
205
206     function uploadAvatar()
207     {
208         try {
209             $imagefile = ImageFile::fromUpload('avatarfile');
210         } catch (Exception $e) {
211             $this->showForm($e->getMessage());
212             return;
213         }
214
215         $user = common_current_user();
216
217         $profile = $user->getProfile();
218
219         if ($profile->setOriginal($imagefile->filename)) {
220             $this->showForm(_('Avatar updated.'), true);
221         } else {
222             $this->showForm(_('Failed updating avatar.'));
223         }
224
225         $imagefile->unlink();
226     }
227
228     /**
229      * Handle the results of jcrop.
230      *
231      * @return void
232      */
233
234     function cropAvatar()
235     {
236         $user = common_current_user();
237
238         $profile = $user->getProfile();
239
240         $x = $this->arg('avatar_crop_x');
241         $y = $this->arg('avatar_crop_y');
242         $w = $this->arg('avatar_crop_w');
243         $h = $this->arg('avatar_crop_h');
244
245         if ($profile->crop_avatars($x, $y, $w, $h)) {
246             $this->showForm(_('Avatar updated.'), true);
247         } else {
248             $this->showForm(_('Failed updating avatar.'));
249         }
250     }
251
252     /**
253      * Add the jCrop stylesheet
254      *
255      * @return void
256      */
257
258     function showStylesheets()
259     {
260         parent::showStylesheets();
261         $jcropStyle =
262           common_path('theme/base/css/jquery.Jcrop.css?version='.LACONICA_VERSION);
263
264         $this->element('link', array('rel' => 'stylesheet',
265                                      'type' => 'text/css',
266                                      'href' => $jcropStyle,
267                                      'media' => 'screen, projection, tv'));
268     }
269
270     /**
271      * Add the jCrop scripts
272      *
273      * @return void
274      */
275
276     function showScripts()
277     {
278         parent::showScripts();
279
280         $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
281         $jcropGo   = common_path('js/jcrop/jquery.Jcrop.go.js');
282
283         $this->element('script', array('type' => 'text/javascript',
284                                        'src' => $jcropPack));
285         $this->element('script', array('type' => 'text/javascript',
286                                        'src' => $jcropGo));
287     }
288 }