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