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