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