]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/grouplogo.php
Cosmetic changes to common_redirect, clientError, serverError
[quix0rs-gnu-social.git] / actions / grouplogo.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-2011 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
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 class GrouplogoAction extends GroupAction
53 {
54     var $mode = null;
55     var $imagefile = null;
56     var $filename = null;
57     var $msg = null;
58     var $success = null;
59
60     /**
61      * Prepare to run
62      */
63     protected function prepare(array $args=array())
64     {
65         parent::prepare($args);
66
67         if (!common_logged_in()) {
68             // TRANS: Client error displayed when trying to create a group while not logged in.
69             $this->clientError(_('You must be logged in to create a group.'));
70         }
71
72         $nickname_arg = $this->trimmed('nickname');
73         $nickname = common_canonical_nickname($nickname_arg);
74
75         // Permanent redirect on non-canonical nickname
76
77         if ($nickname_arg != $nickname) {
78             $args = array('nickname' => $nickname);
79             common_redirect(common_local_url('grouplogo', $args), 301);
80         }
81
82         if (!$nickname) {
83             // TRANS: Client error displayed when trying to change group logo settings without providing a nickname.
84             $this->clientError(_('No nickname.'), 404);
85         }
86
87         $groupid = $this->trimmed('groupid');
88
89         if ($groupid) {
90             $this->group = User_group::getKV('id', $groupid);
91         } else {
92             $local = Local_group::getKV('nickname', $nickname);
93             if ($local) {
94                 $this->group = User_group::getKV('id', $local->group_id);
95             }
96         }
97
98         if (!$this->group) {
99             // TRANS: Client error displayed when trying to update logo settings for a non-existing group.
100             $this->clientError(_('No such group.'), 404);
101         }
102
103         $cur = common_current_user();
104
105         if (!$cur->isAdmin($this->group)) {
106             // TRANS: Client error displayed when trying to change group logo settings while not being a group admin.
107             $this->clientError(_('You must be an admin to edit the group.'), 403);
108         }
109
110         return true;
111     }
112
113     protected function handle()
114     {
115         parent::handle();
116         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
117             $this->handlePost();
118         } else {
119             $this->showForm();
120         }
121     }
122
123     function showForm($msg = null, $success = false)
124     {
125         $this->msg     = $msg;
126         $this->success = $success;
127
128         $this->showPage();
129     }
130
131     /**
132      * Title of the page
133      *
134      * @return string Title of the page
135      */
136     function title()
137     {
138         // TRANS: Title for group logo settings page.
139         return _('Group logo');
140     }
141
142     /**
143      * Instructions for use
144      *
145      * @return instructions for use
146      */
147     function getInstructions()
148     {
149         // TRANS: Instructions for group logo page.
150         // TRANS: %s is the maximum file size for that site.
151         return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
152     }
153
154     /**
155      * Content area of the page
156      *
157      * Shows a form for uploading an avatar.
158      *
159      * @return void
160      */
161     function showContent()
162     {
163         if ($this->mode == 'crop') {
164             $this->showCropForm();
165         } else {
166             $this->showUploadForm();
167         }
168     }
169
170     function showUploadForm()
171     {
172         $user = common_current_user();
173
174         $profile = $user->getProfile();
175
176         if (!$profile) {
177             common_log_db_error($user, 'SELECT', __FILE__);
178             // TRANS: Error message displayed when referring to a user without a profile.
179             $this->serverError(_('User has no profile.'));
180         }
181
182         $original = $this->group->original_logo;
183
184         $this->elementStart('form', array('enctype' => 'multipart/form-data',
185                                           'method' => 'post',
186                                           'id' => 'form_settings_avatar',
187                                           'class' => 'form_settings',
188                                           'action' =>
189                                           common_local_url('grouplogo',
190                                                            array('nickname' => $this->group->nickname))));
191         $this->elementStart('fieldset');
192         // TRANS: Group logo form legend.
193         $this->element('legend', null, _('Group logo'));
194         $this->hidden('token', common_session_token());
195
196         $this->elementStart('ul', 'form_data');
197         if ($original) {
198             $this->elementStart('li', array('id' => 'avatar_original',
199                                             'class' => 'avatar_view'));
200             // TRANS: Uploaded original file in group logo form.
201             $this->element('h2', null, _('Original'));
202             $this->elementStart('div', array('id'=>'avatar_original_view'));
203             $this->element('img', array('src' => $this->group->original_logo,
204                                         'alt' => $this->group->nickname));
205             $this->elementEnd('div');
206             $this->elementEnd('li');
207         }
208
209         if ($this->group->homepage_logo) {
210             $this->elementStart('li', array('id' => 'avatar_preview',
211                                             'class' => 'avatar_view'));
212             // TRANS: Header for preview of to be displayed group logo.
213             $this->element('h2', null, _('Preview'));
214             $this->elementStart('div', array('id'=>'avatar_preview_view'));
215             $this->element('img', array('src' => $this->group->homepage_logo,
216                                         'width' => AVATAR_PROFILE_SIZE,
217                                         'height' => AVATAR_PROFILE_SIZE,
218                                         'alt' => $this->group->nickname));
219             $this->elementEnd('div');
220             $this->elementEnd('li');
221         }
222
223         $this->elementStart('li', array ('id' => 'settings_attach'));
224         $this->element('input', array('name' => 'MAX_FILE_SIZE',
225                                       'type' => 'hidden',
226                                       'id' => 'MAX_FILE_SIZE',
227                                       'value' => ImageFile::maxFileSizeInt()));
228         $this->element('input', array('name' => 'avatarfile',
229                                       'type' => 'file',
230                                       'id' => 'avatarfile'));
231         $this->elementEnd('li');
232         $this->elementEnd('ul');
233
234         $this->elementStart('ul', 'form_actions');
235         $this->elementStart('li');
236         // TRANS: Submit button for uploading a group logo.
237         $this->submit('upload', _('Upload'));
238         $this->elementEnd('li');
239         $this->elementEnd('ul');
240
241         $this->elementEnd('fieldset');
242         $this->elementEnd('form');
243     }
244
245     function showCropForm()
246     {
247         $this->elementStart('form', array('method' => 'post',
248                                           'id' => 'form_settings_avatar',
249                                           'class' => 'form_settings',
250                                           'action' =>
251                                           common_local_url('grouplogo',
252                                                            array('nickname' => $this->group->nickname))));
253         $this->elementStart('fieldset');
254         // TRANS: Legend for group logo settings fieldset.
255         $this->element('legend', null, _('Avatar settings'));
256         $this->hidden('token', common_session_token());
257
258         $this->elementStart('ul', 'form_data');
259
260         $this->elementStart('li',
261                             array('id' => 'avatar_original',
262                                   'class' => 'avatar_view'));
263         // TRANS: Header for originally uploaded file before a crop on the group logo page.
264         $this->element('h2', null, _('Original'));
265         $this->elementStart('div', array('id'=>'avatar_original_view'));
266         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
267                                     'width' => $this->filedata['width'],
268                                     'height' => $this->filedata['height'],
269                                     'alt' => $this->group->nickname));
270         $this->elementEnd('div');
271         $this->elementEnd('li');
272
273         $this->elementStart('li',
274                             array('id' => 'avatar_preview',
275                                   'class' => 'avatar_view'));
276         // TRANS: Header for the cropped group logo on the group logo page.
277         $this->element('h2', null, _('Preview'));
278         $this->elementStart('div', array('id'=>'avatar_preview_view'));
279         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
280                                     'width' => AVATAR_PROFILE_SIZE,
281                                     'height' => AVATAR_PROFILE_SIZE,
282                                     'alt' => $this->group->nickname));
283         $this->elementEnd('div');
284
285         foreach (array('avatar_crop_x', 'avatar_crop_y',
286                        'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
287             $this->element('input', array('name' => $crop_info,
288                                           'type' => 'hidden',
289                                           'id' => $crop_info));
290         }
291
292         // TRANS: Button text for cropping an uploaded group logo.
293         $this->submit('crop', _('Crop'));
294
295         $this->elementEnd('li');
296         $this->elementEnd('ul');
297         $this->elementEnd('fieldset');
298         $this->elementEnd('form');
299     }
300
301     /**
302      * Handle a post
303      *
304      * We mux on the button name to figure out what the user actually wanted.
305      *
306      * @return void
307      */
308     function handlePost()
309     {
310         // CSRF protection
311
312         $token = $this->trimmed('token');
313         if (!$token || $token != common_session_token()) {
314             // TRANS: Form validation error message.
315             $this->show_form(_('There was a problem with your session token. '.
316                                'Try again, please.'));
317             return;
318         }
319
320         if ($this->arg('upload')) {
321             $this->uploadLogo();
322         } else if ($this->arg('crop')) {
323             $this->cropLogo();
324         } else {
325             // TRANS: Form validation error message when an unsupported argument is used.
326             $this->showForm(_('Unexpected form submission.'));
327         }
328     }
329
330     /**
331      * Handle an image upload
332      *
333      * Does all the magic for handling an image upload, and crops the
334      * image by default.
335      *
336      * @return void
337      */
338     function uploadLogo()
339     {
340         try {
341             $imagefile = ImageFile::fromUpload('avatarfile');
342         } catch (Exception $e) {
343             $this->showForm($e->getMessage());
344             return;
345         }
346
347         $type = $imagefile->preferredType();
348         $filename = Avatar::filename($this->group->id,
349                                      image_type_to_extension($type),
350                                      null,
351                                      'group-temp-'.common_timestamp());
352
353         $filepath = Avatar::path($filename);
354
355         $imagefile->copyTo($filepath);
356
357         $filedata = array('filename' => $filename,
358                           'filepath' => $filepath,
359                           'width' => $imagefile->width,
360                           'height' => $imagefile->height,
361                           'type' => $type);
362
363         $_SESSION['FILEDATA'] = $filedata;
364
365         $this->filedata = $filedata;
366
367         $this->mode = 'crop';
368
369         // TRANS: Form instructions on the group logo page.
370         $this->showForm(_('Pick a square area of the image to be the logo.'),
371                         true);
372     }
373
374     /**
375      * Handle the results of jcrop.
376      *
377      * @return void
378      */
379     function cropLogo()
380     {
381         $filedata = $_SESSION['FILEDATA'];
382
383         if (!$filedata) {
384             // TRANS: Server error displayed trying to crop an uploaded group logo that is no longer present.
385             $this->serverError(_('Lost our file data.'));
386         }
387
388         // If image is not being cropped assume pos & dimentions of original
389         $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
390         $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
391         $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
392         $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
393         $size = min($dest_w, $dest_h);
394         $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
395
396         $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
397         $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
398
399         if ($this->group->setOriginal($filename)) {
400             @unlink($filedata['filepath']);
401             unset($_SESSION['FILEDATA']);
402             $this->mode = 'upload';
403             // TRANS: Form success message after updating a group logo.
404             $this->showForm(_('Logo updated.'), true);
405         } else {
406             // TRANS: Form failure message after failing to update a group logo.
407             $this->showForm(_('Failed updating logo.'));
408         }
409     }
410
411     function showPageNotice()
412     {
413         if ($this->msg) {
414             $this->element('div', ($this->success) ? 'success' : 'error',
415                            $this->msg);
416         } else {
417             $inst   = $this->getInstructions();
418             $output = common_markup_to_html($inst);
419
420             $this->elementStart('div', 'instructions');
421             $this->raw($output);
422             $this->elementEnd('div');
423         }
424     }
425
426     /**
427      * Add the jCrop stylesheet
428      *
429      * @return void
430      */
431     function showStylesheets()
432     {
433         parent::showStylesheets();
434         $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
435     }
436
437     /**
438      * Add the jCrop scripts
439      *
440      * @return void
441      */
442     function showScripts()
443     {
444         parent::showScripts();
445
446         if ($this->mode == 'crop') {
447             $this->script('extlib/jquery-jcrop/jcrop.js');
448             $this->script('jcrop.go.js');
449         }
450
451         $this->autofocus('avatarfile');
452     }
453 }