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