]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/grouplogo.php
Merge remote-tracking branch 'upstream/master' into nightly
[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             if (!empty($this->group->homepage_logo)) {
215                 // TRANS: Button on group logo upload page to delete current group logo.
216                 $this->submit('delete', _('Delete'));
217             }
218             $this->elementEnd('li');
219         }
220
221         $this->elementStart('li', array ('id' => 'settings_attach'));
222         $this->element('input', array('name' => 'MAX_FILE_SIZE',
223                                       'type' => 'hidden',
224                                       'id' => 'MAX_FILE_SIZE',
225                                       'value' => ImageFile::maxFileSizeInt()));
226         $this->element('input', array('name' => 'avatarfile',
227                                       'type' => 'file',
228                                       'id' => 'avatarfile'));
229         $this->elementEnd('li');
230         $this->elementEnd('ul');
231
232         $this->elementStart('ul', 'form_actions');
233         $this->elementStart('li');
234         // TRANS: Submit button for uploading a group logo.
235         $this->submit('upload', _('Upload'));
236         $this->elementEnd('li');
237         $this->elementEnd('ul');
238
239         $this->elementEnd('fieldset');
240         $this->elementEnd('form');
241     }
242
243     function showCropForm()
244     {
245         $this->elementStart('form', array('method' => 'post',
246                                           'id' => 'form_settings_avatar',
247                                           'class' => 'form_settings',
248                                           'action' =>
249                                           common_local_url('grouplogo',
250                                                            array('nickname' => $this->group->nickname))));
251         $this->elementStart('fieldset');
252         // TRANS: Legend for group logo settings fieldset.
253         $this->element('legend', null, _('Avatar settings'));
254         $this->hidden('token', common_session_token());
255
256         $this->elementStart('ul', 'form_data');
257
258         $this->elementStart('li',
259                             array('id' => 'avatar_original',
260                                   'class' => 'avatar_view'));
261         // TRANS: Header for originally uploaded file before a crop on the group logo page.
262         $this->element('h2', null, _('Original'));
263         $this->elementStart('div', array('id'=>'avatar_original_view'));
264         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
265                                     'width' => $this->filedata['width'],
266                                     'height' => $this->filedata['height'],
267                                     'alt' => $this->group->nickname));
268         $this->elementEnd('div');
269         $this->elementEnd('li');
270
271         $this->elementStart('li',
272                             array('id' => 'avatar_preview',
273                                   'class' => 'avatar_view'));
274         // TRANS: Header for the cropped group logo on the group logo page.
275         $this->element('h2', null, _('Preview'));
276         $this->elementStart('div', array('id'=>'avatar_preview_view'));
277         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
278                                     'width' => AVATAR_PROFILE_SIZE,
279                                     'height' => AVATAR_PROFILE_SIZE,
280                                     'alt' => $this->group->nickname));
281         $this->elementEnd('div');
282
283         foreach (array('avatar_crop_x', 'avatar_crop_y',
284                        'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
285             $this->element('input', array('name' => $crop_info,
286                                           'type' => 'hidden',
287                                           'id' => $crop_info));
288         }
289
290         // TRANS: Button text for cropping an uploaded group logo.
291         $this->submit('crop', _('Crop'));
292
293         $this->elementEnd('li');
294         $this->elementEnd('ul');
295         $this->elementEnd('fieldset');
296         $this->elementEnd('form');
297     }
298
299     /**
300      * Handle a post
301      *
302      * We mux on the button name to figure out what the user actually wanted.
303      *
304      * @return void
305      */
306     function handlePost()
307     {
308         // CSRF protection
309
310         $token = $this->trimmed('token');
311         if (!$token || $token != common_session_token()) {
312             // TRANS: Form validation error message.
313             $this->show_form(_('There was a problem with your session token. '.
314                                'Try again, please.'));
315             return;
316         }
317
318         if ($this->arg('upload')) {
319             $this->uploadLogo();
320         } else if ($this->arg('crop')) {
321             $this->cropLogo();
322         } else if ($this->arg('delete')) {
323             $this->deleteLogo();
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, common_config('avatar', 'maxsize'));
394         $box = array('width' => $size, 'height' => $size,
395                      'x' => $dest_x,   'y' => $dest_y,
396                      'w' => $dest_w,   'h' => $dest_h);
397
398         $profile = $this->group->getProfile();
399
400         $imagefile = new ImageFile(null, $filedata['filepath']);
401         $filename = Avatar::filename($profile->getID(), image_type_to_extension($imagefile->preferredType()),
402                                      $size, common_timestamp());
403
404         $imagefile->resizeTo(Avatar::path($filename), $box);
405
406         if ($profile->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     /**
419      * Get rid of the current group logo.
420      *
421      * @return void
422      */
423     function deleteLogo()
424     {
425         $orig = clone($this->group);
426         Avatar::deleteFromProfile($this->group->getProfile());
427         @unlink(Avatar::path(basename($this->group->original_logo)));
428         @unlink(Avatar::path(basename($this->group->homepage_logo)));
429         @unlink(Avatar::path(basename($this->group->stream_logo)));
430         @unlink(Avatar::path(basename($this->group->mini_logo)));
431         $this->group->original_logo=User_group::defaultLogo(AVATAR_PROFILE_SIZE);
432         $this->group->homepage_logo=User_group::defaultLogo(AVATAR_PROFILE_SIZE);
433         $this->group->stream_logo=User_group::defaultLogo(AVATAR_STREAM_SIZE);
434         $this->group->mini_logo=User_group::defaultLogo(AVATAR_MINI_SIZE);
435         $this->group->update($orig);
436
437         // TRANS: Success message for deleting the group logo.
438         $this->showForm(_('Logo deleted.'));
439     }
440
441     function showPageNotice()
442     {
443         if ($this->msg) {
444             $this->element('div', ($this->success) ? 'success' : 'error',
445                            $this->msg);
446         } else {
447             $inst   = $this->getInstructions();
448             $output = common_markup_to_html($inst);
449
450             $this->elementStart('div', 'instructions');
451             $this->raw($output);
452             $this->elementEnd('div');
453         }
454     }
455
456     /**
457      * Add the jCrop stylesheet
458      *
459      * @return void
460      */
461     function showStylesheets()
462     {
463         parent::showStylesheets();
464         $this->cssLink('js/extlib/jquery-jcrop/css/jcrop.css','base','screen, projection, tv');
465     }
466
467     /**
468      * Add the jCrop scripts
469      *
470      * @return void
471      */
472     function showScripts()
473     {
474         parent::showScripts();
475
476         if ($this->mode == 'crop') {
477             $this->script('extlib/jquery-jcrop/jcrop.js');
478             $this->script('jcrop.go.js');
479         }
480
481         $this->autofocus('avatarfile');
482     }
483 }