3 * Laconica, the distributed open-source microblogging tool
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.
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.
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/>.
24 * @author Evan Prodromou <evan@controlyourself.ca>
25 * @author Zach Copley <zach@controlyourself.ca>
26 * @copyright 2008-2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
40 * We use jCrop plugin for jQuery to crop the image after upload.
44 * @author Evan Prodromou <evan@controlyourself.ca>
45 * @author Zach Copley <zach@controlyourself.ca>
46 * @author Sarven Capadisli <csarven@controlyourself.ca>
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://laconi.ca/
51 class GrouplogoAction extends Action
54 var $imagefile = null;
61 function prepare($args)
63 parent::prepare($args);
65 if (!common_config('inboxes','enabled')) {
66 $this->serverError(_('Inboxes must be enabled for groups to work'));
70 if (!common_logged_in()) {
71 $this->clientError(_('You must be logged in to create a group.'));
75 $nickname_arg = $this->trimmed('nickname');
76 $nickname = common_canonical_nickname($nickname_arg);
78 // Permanent redirect on non-canonical nickname
80 if ($nickname_arg != $nickname) {
81 $args = array('nickname' => $nickname);
82 common_redirect(common_local_url('editgroup', $args), 301);
87 $this->clientError(_('No nickname'), 404);
91 $groupid = $this->trimmed('groupid');
94 $this->group = User_group::staticGet('id', $groupid);
96 $this->group = User_group::staticGet('nickname', $nickname);
100 $this->clientError(_('No such group'), 404);
104 $cur = common_current_user();
106 if (!$cur->isAdmin($this->group)) {
107 $this->clientError(_('You must be an admin to edit the group'), 403);
114 function handle($args)
116 parent::handle($args);
117 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
124 function showForm($msg = null)
133 * @return string Title of the page
138 return _('Group logo');
142 * Instructions for use
144 * @return instructions for use
147 function getInstructions()
149 return _('You can upload a logo image for your group.');
153 * Content area of the page
155 * Shows a form for uploading an avatar.
160 function showContent()
162 if ($this->mode == 'crop') {
163 $this->showCropForm();
165 $this->showUploadForm();
169 function showUploadForm()
171 $user = common_current_user();
173 $profile = $user->getProfile();
176 common_log_db_error($user, 'SELECT', __FILE__);
177 $this->serverError(_('User without matching profile'));
181 $original = $this->group->original_logo;
183 $this->elementStart('form', array('enctype' => 'multipart/form-data',
185 'id' => 'form_settings_logo',
186 'class' => 'form_settings',
188 common_local_url('grouplogo',
189 array('nickname' => $this->group->nickname))));
190 $this->elementStart('fieldset');
191 $this->element('legend', null, _('Group logo'));
192 $this->hidden('token', common_session_token());
194 $this->elementStart('ul', 'form_data');
196 $this->elementStart('li', array('id' => 'avatar_original',
197 'class' => 'avatar_view'));
198 $this->element('h2', null, _("Original"));
199 $this->elementStart('div', array('id'=>'avatar_original_view'));
200 $this->element('img', array('src' => $this->group->original_logo,
201 'alt' => $this->group->nickname));
202 $this->elementEnd('div');
203 $this->elementEnd('li');
206 if ($this->group->homepage_logo) {
207 $this->elementStart('li', array('id' => 'avatar_preview',
208 'class' => 'avatar_view'));
209 $this->element('h2', null, _("Preview"));
210 $this->elementStart('div', array('id'=>'avatar_preview_view'));
211 $this->element('img', array('src' => $this->group->homepage_logo,
212 'width' => AVATAR_PROFILE_SIZE,
213 'height' => AVATAR_PROFILE_SIZE,
214 'alt' => $this->group->nickname));
215 $this->elementEnd('div');
216 $this->elementEnd('li');
219 $this->elementStart('li', array ('id' => 'settings_attach'));
220 $this->element('input', array('name' => 'avatarfile',
222 'id' => 'avatarfile'));
223 $this->element('input', array('name' => 'MAX_FILE_SIZE',
225 'id' => 'MAX_FILE_SIZE',
226 'value' => MAX_AVATAR_SIZE));
227 $this->elementEnd('li');
228 $this->elementEnd('ul');
230 $this->elementStart('ul', 'form_actions');
231 $this->elementStart('li');
232 $this->submit('upload', _('Upload'));
233 $this->elementEnd('li');
234 $this->elementEnd('ul');
236 $this->elementEnd('fieldset');
237 $this->elementEnd('form');
241 function showCropForm()
243 $this->elementStart('form', array('method' => 'post',
244 'id' => 'form_settings_avatar',
245 'class' => 'form_settings',
247 common_local_url('grouplogo',
248 array('nickname' => $this->group->nickname))));
249 $this->elementStart('fieldset');
250 $this->element('legend', null, _('Avatar settings'));
251 $this->hidden('token', common_session_token());
253 $this->elementStart('ul', 'form_data');
255 $this->elementStart('li',
256 array('id' => 'avatar_original',
257 'class' => 'avatar_view'));
258 $this->element('h2', null, _("Original"));
259 $this->elementStart('div', array('id'=>'avatar_original_view'));
260 $this->element('img', array('src' => common_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');
267 $this->elementStart('li',
268 array('id' => 'avatar_preview',
269 'class' => 'avatar_view'));
270 $this->element('h2', null, _("Preview"));
271 $this->elementStart('div', array('id'=>'avatar_preview_view'));
272 $this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
273 'width' => AVATAR_PROFILE_SIZE,
274 'height' => AVATAR_PROFILE_SIZE,
275 'alt' => $this->group->nickname));
276 $this->elementEnd('div');
278 foreach (array('avatar_crop_x', 'avatar_crop_y',
279 'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
280 $this->element('input', array('name' => $crop_info,
282 'id' => $crop_info));
284 $this->submit('crop', _('Crop'));
286 $this->elementEnd('li');
287 $this->elementEnd('ul');
288 $this->elementEnd('fieldset');
289 $this->elementEnd('form');
296 * We mux on the button name to figure out what the user actually wanted.
301 function handlePost()
305 $token = $this->trimmed('token');
306 if (!$token || $token != common_session_token()) {
307 $this->show_form(_('There was a problem with your session token. '.
308 'Try again, please.'));
312 if ($this->arg('upload')) {
313 $this->uploadAvatar();
314 } else if ($this->arg('crop')) {
317 $this->showForm(_('Unexpected form submission.'));
322 * Handle an image upload
324 * Does all the magic for handling an image upload, and crops the
330 function uploadAvatar()
333 $imagefile = ImageFile::fromUpload('avatarfile');
334 } catch (Exception $e) {
335 $this->showForm($e->getMessage());
339 $filename = common_avatar_filename($this->group->id,
340 image_type_to_extension($imagefile->type),
342 'group-temp-'.common_timestamp());
344 $filepath = common_avatar_path($filename);
346 move_uploaded_file($imagefile->filename, $filepath);
348 $filedata = array('filename' => $filename,
349 'filepath' => $filepath,
350 'width' => $imagefile->width,
351 'height' => $imagefile->height,
352 'type' => $imagefile->type);
354 $_SESSION['FILEDATA'] = $filedata;
356 $this->filedata = $filedata;
358 $this->mode = 'crop';
360 $this->showForm(_('Pick a square area of the image to be your avatar'),
365 * Handle the results of jcrop.
370 function cropAvatar()
372 $user = common_current_user();
374 $profile = $user->getProfile();
376 $x = $this->arg('avatar_crop_x');
377 $y = $this->arg('avatar_crop_y');
378 $w = $this->arg('avatar_crop_w');
379 $h = $this->arg('avatar_crop_h');
381 $filedata = $_SESSION['FILEDATA'];
384 $this->serverError(_('Lost our file data.'));
388 $filepath = common_avatar_path($filedata['filename']);
390 if (!file_exists($filepath)) {
391 $this->serverError(_('Lost our file.'));
395 switch ($filedata['type']) {
397 $image_src = imagecreatefromgif($filepath);
400 $image_src = imagecreatefromjpeg($filepath);
403 $image_src = imagecreatefrompng($filepath);
406 $this->serverError(_('Unknown file type'));
410 common_debug("W = $w, H = $h, X = $x, Y = $y");
412 $image_dest = imagecreatetruecolor($w, $h);
414 $background = imagecolorallocate($image_dest, 0, 0, 0);
415 ImageColorTransparent($image_dest, $background);
416 imagealphablending($image_dest, false);
418 imagecopyresized($image_dest, $image_src, 0, 0, $x, $y, $w, $h, $w, $h);
420 $cur = common_current_user();
422 $filename = common_avatar_filename($this->group->id,
423 image_type_to_extension($imagefile->type),
425 'group-'.common_timestamp());
427 $filepath = common_avatar_path($filename);
429 switch ($filedata['type']) {
431 imagegif($image_dest, $filepath);
434 imagejpeg($image_dest, $filepath);
437 imagepng($image_dest, $filepath);
440 $this->serverError(_('Unknown file type'));
444 if ($this->group->setOriginal($filename, $filedata['type'])) {
445 @unlink(common_avatar_path($filedata['filename']));
446 unset($_SESSION['FILEDATA']);
447 $this->mode = 'upload';
448 $this->showForm(_('Logo updated.'), true);
450 $this->showForm(_('Failed updating logo.'));
454 function showPageNotice()
457 $this->element('div', ($this->success) ? 'success' : 'error',
460 $inst = $this->getInstructions();
461 $output = common_markup_to_html($inst);
463 $this->elementStart('div', 'instructions');
465 $this->elementEnd('div');
470 * Add the jCrop stylesheet
475 function showStylesheets()
477 parent::showStylesheets();
479 common_path('theme/base/css/jquery.Jcrop.css?version='.LACONICA_VERSION);
481 $this->element('link', array('rel' => 'stylesheet',
482 'type' => 'text/css',
483 'href' => $jcropStyle,
484 'media' => 'screen, projection, tv'));
488 * Add the jCrop scripts
493 function showScripts()
495 parent::showScripts();
497 $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
498 $jcropGo = common_path('js/jcrop/jquery.Jcrop.go.js');
500 $this->element('script', array('type' => 'text/javascript',
501 'src' => $jcropPack));
502 $this->element('script', array('type' => 'text/javascript',
506 function showLocalNav()
508 $nav = new GroupNav($this, $this->group);