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