]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/grouplogo.php
Localisation updates from translatewiki.net
[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 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
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
53 class GrouplogoAction extends GroupDesignAction
54 {
55     var $mode = null;
56     var $imagefile = null;
57     var $filename = null;
58     var $msg = null;
59     var $success = null;
60
61     /**
62      * Prepare to run
63      */
64
65     function prepare($args)
66     {
67         parent::prepare($args);
68
69         if (!common_logged_in()) {
70             $this->clientError(_('You must be logged in to create a group.'));
71             return false;
72         }
73
74         $nickname_arg = $this->trimmed('nickname');
75         $nickname = common_canonical_nickname($nickname_arg);
76
77         // Permanent redirect on non-canonical nickname
78
79         if ($nickname_arg != $nickname) {
80             $args = array('nickname' => $nickname);
81             common_redirect(common_local_url('grouplogo', $args), 301);
82             return false;
83         }
84
85         if (!$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             $this->group = User_group::staticGet('nickname', $nickname);
96         }
97
98         if (!$this->group) {
99             $this->clientError(_('No such group'), 404);
100             return false;
101         }
102
103         $cur = common_current_user();
104
105         if (!$cur->isAdmin($this->group)) {
106             $this->clientError(_('You must be an admin to edit the group'), 403);
107             return false;
108         }
109
110         return true;
111     }
112
113     function handle($args)
114     {
115         parent::handle($args);
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
137     function title()
138     {
139         return _('Group logo');
140     }
141
142     /**
143      * Instructions for use
144      *
145      * @return instructions for use
146      */
147
148     function getInstructions()
149     {
150         return sprintf(_('You can upload a logo image for your group. The maximum file size is %s.'), ImageFile::maxFileSize());
151     }
152
153     /**
154      * Content area of the page
155      *
156      * Shows a form for uploading an avatar.
157      *
158      * @return void
159      */
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             $this->serverError(_('User without matching profile'));
179             return;
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         $this->element('legend', null, _('Group logo'));
193         $this->hidden('token', common_session_token());
194
195         $this->elementStart('ul', 'form_data');
196         if ($original) {
197             $this->elementStart('li', array('id' => 'avatar_original',
198                                             'class' => 'avatar_view'));
199             $this->element('h2', null, _("Original"));
200             $this->elementStart('div', array('id'=>'avatar_original_view'));
201             $this->element('img', array('src' => $this->group->original_logo,
202                                         'alt' => $this->group->nickname));
203             $this->elementEnd('div');
204             $this->elementEnd('li');
205         }
206
207         if ($this->group->homepage_logo) {
208             $this->elementStart('li', array('id' => 'avatar_preview',
209                                             'class' => 'avatar_view'));
210             $this->element('h2', null, _("Preview"));
211             $this->elementStart('div', array('id'=>'avatar_preview_view'));
212             $this->element('img', array('src' => $this->group->homepage_logo,
213                                         'width' => AVATAR_PROFILE_SIZE,
214                                         'height' => AVATAR_PROFILE_SIZE,
215                                         'alt' => $this->group->nickname));
216             $this->elementEnd('div');
217             $this->elementEnd('li');
218         }
219
220         $this->elementStart('li', array ('id' => 'settings_attach'));
221         $this->element('input', array('name' => 'avatarfile',
222                                       'type' => 'file',
223                                       'id' => 'avatarfile'));
224         $this->element('input', array('name' => 'MAX_FILE_SIZE',
225                                       'type' => 'hidden',
226                                       'id' => 'MAX_FILE_SIZE',
227                                       'value' => ImageFile::maxFileSizeInt()));
228         $this->elementEnd('li');
229         $this->elementEnd('ul');
230
231         $this->elementStart('ul', 'form_actions');
232         $this->elementStart('li');
233         $this->submit('upload', _('Upload'));
234         $this->elementEnd('li');
235         $this->elementEnd('ul');
236
237         $this->elementEnd('fieldset');
238         $this->elementEnd('form');
239
240     }
241
242     function showCropForm()
243     {
244         $this->elementStart('form', array('method' => 'post',
245                                           'id' => 'form_settings_avatar',
246                                           'class' => 'form_settings',
247                                           'action' =>
248                                           common_local_url('grouplogo',
249                                                            array('nickname' => $this->group->nickname))));
250         $this->elementStart('fieldset');
251         $this->element('legend', null, _('Avatar settings'));
252         $this->hidden('token', common_session_token());
253
254         $this->elementStart('ul', 'form_data');
255
256         $this->elementStart('li',
257                             array('id' => 'avatar_original',
258                                   'class' => 'avatar_view'));
259         $this->element('h2', null, _("Original"));
260         $this->elementStart('div', array('id'=>'avatar_original_view'));
261         $this->element('img', array('src' => Avatar::url($this->filedata['filename']),
262                                     'width' => $this->filedata['width'],
263                                     'height' => $this->filedata['height'],
264                                     'alt' => $this->group->nickname));
265         $this->elementEnd('div');
266         $this->elementEnd('li');
267
268         $this->elementStart('li',
269                             array('id' => 'avatar_preview',
270                                   'class' => 'avatar_view'));
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         $this->submit('crop', _('Crop'));
287
288         $this->elementEnd('li');
289         $this->elementEnd('ul');
290         $this->elementEnd('fieldset');
291         $this->elementEnd('form');
292
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
303     function handlePost()
304     {
305         // CSRF protection
306
307         $token = $this->trimmed('token');
308         if (!$token || $token != common_session_token()) {
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             $this->showForm(_('Unexpected form submission.'));
320         }
321     }
322
323     /**
324      * Handle an image upload
325      *
326      * Does all the magic for handling an image upload, and crops the
327      * image by default.
328      *
329      * @return void
330      */
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         $filename = Avatar::filename($this->group->id,
342                                      image_type_to_extension($imagefile->type),
343                                      null,
344                                      'group-temp-'.common_timestamp());
345
346         $filepath = Avatar::path($filename);
347
348         move_uploaded_file($imagefile->filepath, $filepath);
349
350         $filedata = array('filename' => $filename,
351                           'filepath' => $filepath,
352                           'width' => $imagefile->width,
353                           'height' => $imagefile->height,
354                           'type' => $imagefile->type);
355
356         $_SESSION['FILEDATA'] = $filedata;
357
358         $this->filedata = $filedata;
359
360         $this->mode = 'crop';
361
362         $this->showForm(_('Pick a square area of the image to be the logo.'),
363                         true);
364     }
365
366     /**
367      * Handle the results of jcrop.
368      *
369      * @return void
370      */
371
372     function cropLogo()
373     {
374         $filedata = $_SESSION['FILEDATA'];
375
376         if (!$filedata) {
377             $this->serverError(_('Lost our file data.'));
378             return;
379         }
380
381         // If image is not being cropped assume pos & dimentions of original
382         $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
383         $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
384         $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
385         $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
386         $size = min($dest_w, $dest_h);
387         $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
388
389         $imagefile = new ImageFile($this->group->id, $filedata['filepath']);
390         $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
391
392         if ($this->group->setOriginal($filename)) {
393             @unlink($filedata['filepath']);
394             unset($_SESSION['FILEDATA']);
395             $this->mode = 'upload';
396             $this->showForm(_('Logo updated.'), true);
397         } else {
398             $this->showForm(_('Failed updating logo.'));
399         }
400     }
401
402     function showPageNotice()
403     {
404         if ($this->msg) {
405             $this->element('div', ($this->success) ? 'success' : 'error',
406                            $this->msg);
407         } else {
408             $inst   = $this->getInstructions();
409             $output = common_markup_to_html($inst);
410
411             $this->elementStart('div', 'instructions');
412             $this->raw($output);
413             $this->elementEnd('div');
414         }
415     }
416
417     /**
418      * Add the jCrop stylesheet
419      *
420      * @return void
421      */
422
423     function showStylesheets()
424     {
425         parent::showStylesheets();
426         $this->cssLink('css/jquery.Jcrop.css','base','screen, projection, tv');
427     }
428
429     /**
430      * Add the jCrop scripts
431      *
432      * @return void
433      */
434
435     function showScripts()
436     {
437         parent::showScripts();
438
439         if ($this->mode == 'crop') {
440             $this->script('js/jcrop/jquery.Jcrop.min.js');
441             $this->script('js/jcrop/jquery.Jcrop.go.js');
442         }
443
444         $this->autofocus('avatarfile');
445     }
446
447     function showLocalNav()
448     {
449         $nav = new GroupNav($this, $this->group);
450         $nav->show();
451     }
452 }