From a130bb9d6da4b1b01a3ff9b0c674494dd465d512 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 23 Jan 2009 06:10:25 +0100 Subject: A section --- lib/tagcloudsection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tagcloudsection.php b/lib/tagcloudsection.php index 178dd88ca..a46c73846 100644 --- a/lib/tagcloudsection.php +++ b/lib/tagcloudsection.php @@ -76,7 +76,7 @@ class TagCloudSection extends Section $this->out->elementStart('ul', 'tags xoxo tag-cloud'); foreach ($tw as $tag => $weight) { - $this->showTag($tag, $weight, $weight/$sum); + $this->showTag($tag, $weight, ($sum == 0) ? $weight : $weight/$sum); } $this->out->elementEnd('ul'); -- cgit v1.2.3-54-g00ecf From a7b80ef2304c281d65ef7d0fda395ef904cd4d55 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 23 Jan 2009 06:11:05 +0100 Subject: Better math for seciton --- lib/tagcloudsection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tagcloudsection.php b/lib/tagcloudsection.php index a46c73846..ff2aca6d6 100644 --- a/lib/tagcloudsection.php +++ b/lib/tagcloudsection.php @@ -76,7 +76,7 @@ class TagCloudSection extends Section $this->out->elementStart('ul', 'tags xoxo tag-cloud'); foreach ($tw as $tag => $weight) { - $this->showTag($tag, $weight, ($sum == 0) ? $weight : $weight/$sum); + $this->showTag($tag, $weight, ($sum == 0) ? 0 : $weight/$sum); } $this->out->elementEnd('ul'); -- cgit v1.2.3-54-g00ecf From 9dc5d8d0f6db1249a62162877c81d240fcf32a0a Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 23 Jan 2009 06:16:55 +0100 Subject: Made avatar upload a two-stage process; first upload, then crop --- actions/avatarsettings.php | 190 ++++++++++++++++++++++++++++++++++++++++----- lib/imagefile.php | 10 ++- 2 files changed, 179 insertions(+), 21 deletions(-) diff --git a/actions/avatarsettings.php b/actions/avatarsettings.php index 2c7af9b7a..1db35237e 100644 --- a/actions/avatarsettings.php +++ b/actions/avatarsettings.php @@ -50,6 +50,10 @@ require_once INSTALLDIR.'/lib/accountsettingsaction.php'; class AvatarsettingsAction extends AccountSettingsAction { + var $mode = null; + var $imagefile = null; + var $filename = null; + /** * Title of the page * @@ -69,7 +73,7 @@ class AvatarsettingsAction extends AccountSettingsAction function getInstructions() { - return _('Set your personal avatar.'); + return _('You can upload your personal avatar.'); } /** @@ -81,6 +85,15 @@ class AvatarsettingsAction extends AccountSettingsAction */ function showContent() + { + if ($this->mode == 'crop') { + $this->showCropForm(); + } else { + $this->showUploadForm(); + } + } + + function showUploadForm() { $user = common_current_user(); @@ -132,16 +145,6 @@ class AvatarsettingsAction extends AccountSettingsAction 'height' => AVATAR_PROFILE_SIZE, 'alt' => $user->nickname)); $this->elementEnd('div'); - - foreach (array('avatar_crop_x', 'avatar_crop_y', - 'avatar_crop_w', 'avatar_crop_h') as $crop_info) { - $this->element('input', array('name' => $crop_info, - 'type' => 'hidden', - 'id' => $crop_info)); - } - $this->submit('crop', _('Crop')); - - $this->elementEnd('li'); } $this->elementStart('li', array ('id' => 'settings_attach')); @@ -166,6 +169,69 @@ class AvatarsettingsAction extends AccountSettingsAction } + function showCropForm() + { + $user = common_current_user(); + + $profile = $user->getProfile(); + + if (!$profile) { + common_log_db_error($user, 'SELECT', __FILE__); + $this->serverError(_('User without matching profile')); + return; + } + + $original = $profile->getOriginalAvatar(); + + $this->elementStart('form', array('method' => 'post', + 'id' => 'form_settings_avatar', + 'class' => 'form_settings', + 'action' => + common_local_url('avatarsettings'))); + $this->elementStart('fieldset'); + $this->element('legend', null, _('Avatar settings')); + $this->hidden('token', common_session_token()); + + $this->elementStart('ul', 'form_data'); + + $this->elementStart('li', + array('id' => 'avatar_original', + 'class' => 'avatar_view')); + $this->element('h2', null, _("Original")); + $this->elementStart('div', array('id'=>'avatar_original_view')); + $this->element('img', array('src' => common_avatar_url($this->filedata['filename']), + 'width' => $this->filedata['width'], + 'height' => $this->filedata['height'], + 'alt' => $user->nickname)); + $this->elementEnd('div'); + $this->elementEnd('li'); + + $this->elementStart('li', + array('id' => 'avatar_preview', + 'class' => 'avatar_view')); + $this->element('h2', null, _("Preview")); + $this->elementStart('div', array('id'=>'avatar_preview_view')); + $this->element('img', array('src' => common_avatar_url($this->filedata['filename']), + 'width' => AVATAR_PROFILE_SIZE, + 'height' => AVATAR_PROFILE_SIZE, + 'alt' => $user->nickname)); + $this->elementEnd('div'); + + foreach (array('avatar_crop_x', 'avatar_crop_y', + 'avatar_crop_w', 'avatar_crop_h') as $crop_info) { + $this->element('input', array('name' => $crop_info, + 'type' => 'hidden', + 'id' => $crop_info)); + } + $this->submit('crop', _('Crop')); + + $this->elementEnd('li'); + $this->elementEnd('ul'); + $this->elementEnd('fieldset'); + $this->elementEnd('form'); + + } + /** * Handle a post * @@ -212,17 +278,31 @@ class AvatarsettingsAction extends AccountSettingsAction return; } - $user = common_current_user(); + $cur = common_current_user(); - $profile = $user->getProfile(); + $filename = common_avatar_filename($cur->id, + image_type_to_extension($imagefile->type), + null, + 'tmp'.common_timestamp()); - if ($profile->setOriginal($imagefile->filename)) { - $this->showForm(_('Avatar updated.'), true); - } else { - $this->showForm(_('Failed updating avatar.')); - } + $filepath = common_avatar_path($filename); - $imagefile->unlink(); + move_uploaded_file($imagefile->filename, $filepath); + + $filedata = array('filename' => $filename, + 'filepath' => $filepath, + 'width' => $imagefile->width, + 'height' => $imagefile->height, + 'type' => $imagefile->type); + + $_SESSION['FILEDATA'] = $filedata; + + $this->filedata = $filedata; + + $this->mode = 'crop'; + + $this->showForm(_('Pick a square area of the image to be your avatar'), + true); } /** @@ -242,7 +322,77 @@ class AvatarsettingsAction extends AccountSettingsAction $w = $this->arg('avatar_crop_w'); $h = $this->arg('avatar_crop_h'); - if ($profile->crop_avatars($x, $y, $w, $h)) { + $filedata = $_SESSION['FILEDATA']; + + if (!$filedata) { + $this->serverError(_('Lost our file data.')); + return; + } + + $filepath = common_avatar_path($filedata['filename']); + + if (!file_exists($filepath)) { + $this->serverError(_('Lost our file.')); + return; + } + + switch ($filedata['type']) { + case IMAGETYPE_GIF: + $image_src = imagecreatefromgif($filepath); + break; + case IMAGETYPE_JPEG: + $image_src = imagecreatefromjpeg($filepath); + break; + case IMAGETYPE_PNG: + $image_src = imagecreatefrompng($filepath); + break; + default: + $this->serverError(_('Unknown file type')); + return; + } + + common_debug("W = $w, H = $h, X = $x, Y = $y"); + + $image_dest = imagecreatetruecolor($w, $h); + + $background = imagecolorallocate($image_dest, 0, 0, 0); + ImageColorTransparent($image_dest, $background); + imagealphablending($image_dest, false); + + imagecopyresized($image_dest, $image_src, 0, 0, $x, $y, $w, $h, $w, $h); + + $cur = common_current_user(); + + $filename = common_avatar_filename($cur->id, + image_type_to_extension($filedata['type']), + null, + common_timestamp()); + + $filepath = common_avatar_path($filename); + + switch ($filedata['type']) { + case IMAGETYPE_GIF: + imagegif($image_dest, $filepath); + break; + case IMAGETYPE_JPEG: + imagejpeg($image_dest, $filepath); + break; + case IMAGETYPE_PNG: + imagepng($image_dest, $filepath); + break; + default: + $this->serverError(_('Unknown file type')); + return; + } + + $user = common_current_user(); + + $profile = $cur->getProfile(); + + if ($profile->setOriginal($filepath)) { + @unlink(common_avatar_path($filedata['filename'])); + unset($_SESSION['FILEDATA']); + $this->mode = 'upload'; $this->showForm(_('Avatar updated.'), true); } else { $this->showForm(_('Failed updating avatar.')); diff --git a/lib/imagefile.php b/lib/imagefile.php index f15a72c76..7f1db892c 100644 --- a/lib/imagefile.php +++ b/lib/imagefile.php @@ -48,12 +48,17 @@ if (!defined('LACONICA')) { class ImageFile { var $filename = null; + var $barename = null; var $type = null; + var $height = null; + var $width = null; - function __construct($filename=null, $type=null) + function __construct($filename=null, $type=null, $width=null, $height=null) { $this->filename = $filename; $this->type = $type; + $this->width = $type; + $this->height = $type; } static function fromUpload($param='upload') @@ -83,6 +88,9 @@ class ImageFile return; } + $imagefile->width = $info[0]; + $imagefile->height = $info[1]; + switch ($info[2]) { case IMAGETYPE_GIF: case IMAGETYPE_JPEG: -- cgit v1.2.3-54-g00ecf From fbd1cf4dfa452166a5985d9a9177d57e8554f09f Mon Sep 17 00:00:00 2001 From: Robin Millette Date: Fri, 23 Jan 2009 05:57:08 +0000 Subject: use new error actions classes to display error --- actions/clienterror.php | 94 ----------------------------------------------- actions/servererror.php | 92 ---------------------------------------------- lib/clienterroraction.php | 94 +++++++++++++++++++++++++++++++++++++++++++++++ lib/servererroraction.php | 92 ++++++++++++++++++++++++++++++++++++++++++++++ lib/util.php | 53 ++------------------------ 5 files changed, 190 insertions(+), 235 deletions(-) delete mode 100644 actions/clienterror.php delete mode 100644 actions/servererror.php create mode 100644 lib/clienterroraction.php create mode 100644 lib/servererroraction.php diff --git a/actions/clienterror.php b/actions/clienterror.php deleted file mode 100644 index ef6fd51df..000000000 --- a/actions/clienterror.php +++ /dev/null @@ -1,94 +0,0 @@ - - * @author Zach Copley - * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 - * @link http://laconi.ca/ - * - * Laconica - a distributed open-source microblogging tool - * Copyright (C) 2008, Controlez-Vous, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -if (!defined('LACONICA')) { - exit(1); -} - -require_once INSTALLDIR.'/lib/error.php'; - -/** - * Class for displaying HTTP client errors - * - * @category Action - * @package Laconica - * @author Zach Copley - * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 - * @link http://laconi.ca/ - */ -class ClientErrorAction extends ErrorAction -{ - function __construct($message='Error', $code=400) - { - parent::__construct($message, $code); - - $this->status = array(400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed'); - $this->default = 400; - } - - // XXX: Should these error actions even be invokable via URI? - - function handle($args) - { - parent::handle($args); - - $this->code = $this->trimmed('code'); - - if (!$this->code || $code < 400 || $code > 499) { - $this->code = $this->default; - } - - $this->message = $this->trimmed('message'); - - if (!$this->message) { - $this->message = "Client Error $this->code"; - } - - $this->showPage(); - } -} diff --git a/actions/servererror.php b/actions/servererror.php deleted file mode 100644 index a39886591..000000000 --- a/actions/servererror.php +++ /dev/null @@ -1,92 +0,0 @@ - - * @author Zach Copley - * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 - * @link http://laconi.ca/ - * - * Laconica - a distributed open-source microblogging tool - * Copyright (C) 2008, Controlez-Vous, Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -if (!defined('LACONICA')) { - exit(1); -} - -require_once INSTALLDIR.'/lib/error.php'; - -/** - * Class for displaying HTTP server errors - * - * Note: The older util.php class simply printed a string, but the spec - * says that 500 errors should be treated similarly to 400 errors, and - * it's easier to give an HTML response. Maybe we can customize these - * to display some funny animal cartoons. If not, we can probably role - * these classes up into a single class. - * - * See: http://tools.ietf.org/html/rfc2616#section-10 - * - * @category Action - * @package Laconica - * @author Zach Copley - * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 - * @link http://laconi.ca/ - */ -class ServerErrorAction extends ErrorAction -{ - function __construct($message='Error', $code=500) - { - parent::__construct($message, $code); - - $this->status = array(500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported'); - - $this->default = 500; - } - - // XXX: Should these error actions even be invokable via URI? - - function handle($args) - { - parent::handle($args); - - $this->code = $this->trimmed('code'); - - if (!$this->code || $code < 500 || $code > 599) { - $this->code = $this->default; - } - - $this->message = $this->trimmed('message'); - - if (!$this->message) { - $this->message = "Server Error $this->code"; - } - - $this->showPage(); - } - -} diff --git a/lib/clienterroraction.php b/lib/clienterroraction.php new file mode 100644 index 000000000..ef6fd51df --- /dev/null +++ b/lib/clienterroraction.php @@ -0,0 +1,94 @@ + + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + * + * Laconica - a distributed open-source microblogging tool + * Copyright (C) 2008, Controlez-Vous, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/lib/error.php'; + +/** + * Class for displaying HTTP client errors + * + * @category Action + * @package Laconica + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + */ +class ClientErrorAction extends ErrorAction +{ + function __construct($message='Error', $code=400) + { + parent::__construct($message, $code); + + $this->status = array(400 => 'Bad Request', + 401 => 'Unauthorized', + 402 => 'Payment Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Request Entity Too Large', + 414 => 'Request-URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Requested Range Not Satisfiable', + 417 => 'Expectation Failed'); + $this->default = 400; + } + + // XXX: Should these error actions even be invokable via URI? + + function handle($args) + { + parent::handle($args); + + $this->code = $this->trimmed('code'); + + if (!$this->code || $code < 400 || $code > 499) { + $this->code = $this->default; + } + + $this->message = $this->trimmed('message'); + + if (!$this->message) { + $this->message = "Client Error $this->code"; + } + + $this->showPage(); + } +} diff --git a/lib/servererroraction.php b/lib/servererroraction.php new file mode 100644 index 000000000..a39886591 --- /dev/null +++ b/lib/servererroraction.php @@ -0,0 +1,92 @@ + + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + * + * Laconica - a distributed open-source microblogging tool + * Copyright (C) 2008, Controlez-Vous, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/lib/error.php'; + +/** + * Class for displaying HTTP server errors + * + * Note: The older util.php class simply printed a string, but the spec + * says that 500 errors should be treated similarly to 400 errors, and + * it's easier to give an HTML response. Maybe we can customize these + * to display some funny animal cartoons. If not, we can probably role + * these classes up into a single class. + * + * See: http://tools.ietf.org/html/rfc2616#section-10 + * + * @category Action + * @package Laconica + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + */ +class ServerErrorAction extends ErrorAction +{ + function __construct($message='Error', $code=500) + { + parent::__construct($message, $code); + + $this->status = array(500 => 'Internal Server Error', + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported'); + + $this->default = 500; + } + + // XXX: Should these error actions even be invokable via URI? + + function handle($args) + { + parent::handle($args); + + $this->code = $this->trimmed('code'); + + if (!$this->code || $code < 500 || $code > 599) { + $this->code = $this->default; + } + + $this->message = $this->trimmed('message'); + + if (!$this->message) { + $this->message = "Server Error $this->code"; + } + + $this->showPage(); + } + +} diff --git a/lib/util.php b/lib/util.php index 42bc08e7e..0b5abfa48 100644 --- a/lib/util.php +++ b/lib/util.php @@ -23,60 +23,15 @@ function common_server_error($msg, $code=500) { - static $status = array(500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported'); - - if (!array_key_exists($code, $status)) { - $code = 500; - } - - $status_string = $status[$code]; - - header('HTTP/1.1 '.$code.' '.$status_string); - header('Content-type: text/plain'); - - print $msg; - print "\n"; - exit(); + $err = new ServerErrorAction($msg, $code); + $err->showPage(); } // Show a user error function common_user_error($msg, $code=400) { - static $status = array(400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed'); - - if (!array_key_exists($code, $status)) { - $code = 400; - } - - $status_string = $status[$code]; - - header('HTTP/1.1 '.$code.' '.$status_string); - - common_show_header('Error'); - common_element('div', array('class' => 'error'), $msg); - common_show_footer(); + $err = new ClientErrorAction($msg, $code); + $err->showPage(); } function common_init_locale($language=null) -- cgit v1.2.3-54-g00ecf