From 08db50b24eaf9e6e5f651448c394f1f50dd2409b Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Fri, 6 Feb 2009 21:17:45 -0800 Subject: [PATCH 01/23] "Change your email address..." msg was printing out \n instead of a newline --- lib/mail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mail.php b/lib/mail.php index b424d579fe..a1faefc806 100644 --- a/lib/mail.php +++ b/lib/mail.php @@ -246,7 +246,7 @@ function mail_subscribe_notify_profile($listenee, $other) "\n".'Faithfully yours,'."\n".'%7$s.'."\n\n". "----\n". "Change your email address or ". - "notification options at ".'%8$s\n'), + "notification options at ".'%8$s' ."\n"), $long_name, common_config('site', 'name'), $other->profileurl, From c2905085c1b37998e3bfbdb7dc6ab2c78ef6d3c9 Mon Sep 17 00:00:00 2001 From: Robin Millette Date: Sat, 7 Feb 2009 19:33:18 +0000 Subject: [PATCH 02/23] trac #1155 ++ replace strlen with mb_strlen for all utf8 strings. --- actions/editgroup.php | 6 +++--- actions/finishopenidlogin.php | 2 +- actions/newgroup.php | 6 +++--- actions/profilesettings.php | 6 +++--- actions/register.php | 6 +++--- actions/twitapiaccount.php | 2 +- actions/updateprofile.php | 8 ++++---- actions/userauthorization.php | 8 ++++---- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/actions/editgroup.php b/actions/editgroup.php index 98ebcb87ac..e7e79040a4 100644 --- a/actions/editgroup.php +++ b/actions/editgroup.php @@ -191,13 +191,13 @@ class EditgroupAction extends Action array('http', 'https')))) { $this->showForm(_('Homepage is not a valid URL.')); return; - } else if (!is_null($fullname) && strlen($fullname) > 255) { + } else if (!is_null($fullname) && mb_strlen($fullname) > 255) { $this->showForm(_('Full name is too long (max 255 chars).')); return; - } else if (!is_null($description) && strlen($description) > 140) { + } else if (!is_null($description) && mb_strlen($description) > 140) { $this->showForm(_('description is too long (max 140 chars).')); return; - } else if (!is_null($location) && strlen($location) > 255) { + } else if (!is_null($location) && mb_strlen($location) > 255) { $this->showForm(_('Location is too long (max 255 chars).')); return; } diff --git a/actions/finishopenidlogin.php b/actions/finishopenidlogin.php index bc91511207..1e7b73a7f3 100644 --- a/actions/finishopenidlogin.php +++ b/actions/finishopenidlogin.php @@ -242,7 +242,7 @@ class FinishopenidloginAction extends Action } } - if ($sreg['fullname'] && strlen($sreg['fullname']) <= 255) { + if ($sreg['fullname'] && mb_strlen($sreg['fullname']) <= 255) { $fullname = $sreg['fullname']; } diff --git a/actions/newgroup.php b/actions/newgroup.php index 42fd380dfe..cbd8dfeec5 100644 --- a/actions/newgroup.php +++ b/actions/newgroup.php @@ -142,13 +142,13 @@ class NewgroupAction extends Action array('http', 'https')))) { $this->showForm(_('Homepage is not a valid URL.')); return; - } else if (!is_null($fullname) && strlen($fullname) > 255) { + } else if (!is_null($fullname) && mb_strlen($fullname) > 255) { $this->showForm(_('Full name is too long (max 255 chars).')); return; - } else if (!is_null($description) && strlen($description) > 140) { + } else if (!is_null($description) && mb_strlen($description) > 140) { $this->showForm(_('description is too long (max 140 chars).')); return; - } else if (!is_null($location) && strlen($location) > 255) { + } else if (!is_null($location) && mb_strlen($location) > 255) { $this->showForm(_('Location is too long (max 255 chars).')); return; } diff --git a/actions/profilesettings.php b/actions/profilesettings.php index 82e6c3c82f..60f7c0796e 100644 --- a/actions/profilesettings.php +++ b/actions/profilesettings.php @@ -198,13 +198,13 @@ class ProfilesettingsAction extends AccountSettingsAction !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) { $this->showForm(_('Homepage is not a valid URL.')); return; - } else if (!is_null($fullname) && strlen($fullname) > 255) { + } else if (!is_null($fullname) && mb_strlen($fullname) > 255) { $this->showForm(_('Full name is too long (max 255 chars).')); return; - } else if (!is_null($bio) && strlen($bio) > 140) { + } else if (!is_null($bio) && mb_strlen($bio) > 140) { $this->showForm(_('Bio is too long (max 140 chars).')); return; - } else if (!is_null($location) && strlen($location) > 255) { + } else if (!is_null($location) && mb_strlen($location) > 255) { $this->showForm(_('Location is too long (max 255 chars).')); return; } else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) { diff --git a/actions/register.php b/actions/register.php index 01d94f4884..5d7a8ce690 100644 --- a/actions/register.php +++ b/actions/register.php @@ -167,13 +167,13 @@ class RegisterAction extends Action array('http', 'https')))) { $this->showForm(_('Homepage is not a valid URL.')); return; - } else if (!is_null($fullname) && strlen($fullname) > 255) { + } else if (!is_null($fullname) && mb_strlen($fullname) > 255) { $this->showForm(_('Full name is too long (max 255 chars).')); return; - } else if (!is_null($bio) && strlen($bio) > 140) { + } else if (!is_null($bio) && mb_strlen($bio) > 140) { $this->showForm(_('Bio is too long (max 140 chars).')); return; - } else if (!is_null($location) && strlen($location) > 255) { + } else if (!is_null($location) && mb_strlen($location) > 255) { $this->showForm(_('Location is too long (max 255 chars).')); return; } else if (strlen($password) < 6) { diff --git a/actions/twitapiaccount.php b/actions/twitapiaccount.php index dc8e2e798b..b7c09cc9dc 100644 --- a/actions/twitapiaccount.php +++ b/actions/twitapiaccount.php @@ -56,7 +56,7 @@ class TwitapiaccountAction extends TwitterapiAction $location = trim($this->arg('location')); - if (!is_null($location) && strlen($location) > 255) { + if (!is_null($location) && mb_strlen($location) > 255) { // XXX: But Twitter just truncates and runs with it. -- Zach $this->clientError(_('That\'s too long. Max notice size is 255 chars.'), 406, $apidate['content-type']); diff --git a/actions/updateprofile.php b/actions/updateprofile.php index c79112dace..898c535432 100644 --- a/actions/updateprofile.php +++ b/actions/updateprofile.php @@ -93,22 +93,22 @@ class UpdateprofileAction extends Action } # optional stuff $fullname = $req->get_parameter('omb_listenee_fullname'); - if ($fullname && strlen($fullname) > 255) { + if ($fullname && mb_strlen($fullname) > 255) { $this->clientError(_("Full name is too long (max 255 chars).")); return false; } $homepage = $req->get_parameter('omb_listenee_homepage'); - if ($homepage && (!common_valid_http_url($homepage) || strlen($homepage) > 255)) { + if ($homepage && (!common_valid_http_url($homepage) || mb_strlen($homepage) > 255)) { $this->clientError(sprintf(_("Invalid homepage '%s'"), $homepage)); return false; } $bio = $req->get_parameter('omb_listenee_bio'); - if ($bio && strlen($bio) > 140) { + if ($bio && mb_strlen($bio) > 140) { $this->clientError(_("Bio is too long (max 140 chars).")); return false; } $location = $req->get_parameter('omb_listenee_location'); - if ($location && strlen($location) > 255) { + if ($location && mb_strlen($location) > 255) { $this->clientError(_("Location is too long (max 255 chars).")); return false; } diff --git a/actions/userauthorization.php b/actions/userauthorization.php index 58fc96c0eb..7455a41a6f 100644 --- a/actions/userauthorization.php +++ b/actions/userauthorization.php @@ -469,19 +469,19 @@ class UserauthorizationAction extends Action } # optional stuff $fullname = $req->get_parameter('omb_listenee_fullname'); - if ($fullname && strlen($fullname) > 255) { + if ($fullname && mb_strlen($fullname) > 255) { throw new OAuthException("Full name '$fullname' too long."); } $homepage = $req->get_parameter('omb_listenee_homepage'); - if ($homepage && (!common_valid_http_url($homepage) || strlen($homepage) > 255)) { + if ($homepage && (!common_valid_http_url($homepage) || mb_strlen($homepage) > 255)) { throw new OAuthException("Invalid homepage '$homepage'"); } $bio = $req->get_parameter('omb_listenee_bio'); - if ($bio && strlen($bio) > 140) { + if ($bio && mb_strlen($bio) > 140) { throw new OAuthException("Bio too long '$bio'"); } $location = $req->get_parameter('omb_listenee_location'); - if ($location && strlen($location) > 255) { + if ($location && mb_strlen($location) > 255) { throw new OAuthException("Location too long '$location'"); } $avatar = $req->get_parameter('omb_listenee_avatar'); From a64a88860908429a2e2297565f292ebdfe68415f Mon Sep 17 00:00:00 2001 From: Sarven Capadisli Date: Sat, 7 Feb 2009 23:47:37 +0000 Subject: [PATCH 03/23] Using rel="external" instead of class="exlink" --- lib/util.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util.php b/lib/util.php index 7ce4e229eb..c5a092f630 100644 --- a/lib/util.php +++ b/lib/util.php @@ -478,7 +478,7 @@ function common_linkify($url) { } else $title = ''; - return "$display"; + return "$display"; } function common_longurl($short_url) From 4b4ed63190428f2a81bda9c129f83190a381ff0c Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sat, 7 Feb 2009 18:16:34 -0800 Subject: [PATCH 04/23] Safer, better script for automatically updating Facebook statuses --- scripts/update_facebook.php | 90 +++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 48 deletions(-) diff --git a/scripts/update_facebook.php b/scripts/update_facebook.php index 141bcfe0ca..60e10417fa 100755 --- a/scripts/update_facebook.php +++ b/scripts/update_facebook.php @@ -34,22 +34,19 @@ require_once INSTALLDIR . '/lib/facebookutil.php'; $last_updated_file = INSTALLDIR . '/scripts/facebook_last_updated'; // Lock file name -$tmp_file = INSTALLDIR . '/scripts/update_facebook.lock'; +$lock_file = INSTALLDIR . '/scripts/update_facebook.lock'; // Make sure only one copy of the script is running at a time -if (!($tmp_file = @fopen($tmp_file, "w"))) -{ - die("Can't open lock file. Script already running?"); +$lock_file = @fopen($lock_file, "w+"); +if (!flock( $lock_file, LOCK_EX | LOCK_NB, &$wouldblock) || $wouldblock) { + die("Can't open lock file. Script already running?\n"); } $facebook = getFacebook(); - $current_time = time(); - $since = getLastUpdated(); - +updateLastUpdated($current_time); $notice = getFacebookNotices($since); - $cnt = 0; while($notice->fetch()) { @@ -73,26 +70,30 @@ while($notice->fetch()) { // Avoid a Loop if ($notice->source != 'Facebook') { - updateStatus($fbuid, $content); - updateProfileBox($facebook, $flink, $notice); - $cnt++; + + try { + $facebook->api_client->users_setStatus($content, + $fbuid, false, true); + updateProfileBox($facebook, $flink, $notice); + $cnt++; + } catch(FacebookRestClientException $e) { + print "Couldn't sent notice $notice->id!\n"; + print $e->getMessage(); + + // Remove flink? + } } - } + } } } if ($cnt > 0) { print date('r', $current_time) . - ": Found $cnt new notices to send to Facebook since last run at " . - date('Y-m-d H:i:s', $since) . "\n"; - + ": Found $cnt new notices for Facebook since last run at " . + date('r', $since) . "\n"; } -#Save the last updated time. It needs to do this even if there were no -#changes made, otherwise it will never create it and thus never send -#any updates at all. -updateLastUpdated($current_time); - +fclose($lock_file); exit(0); @@ -111,37 +112,30 @@ function userCanUpdate($fbuid) { return $result; } - -function updateStatus($fbuid, $content) { - global $facebook; - - try { - $result = $facebook->api_client->users_setStatus($content, $fbuid, false, true); - } catch(FacebookRestClientException $e){ - print_r($e); - } -} - function getLastUpdated(){ - global $last_updated_file, $current_time; + global $last_updated_file, $current_time; + $last = $current_time; - $file = fopen($last_updated_file, 'r'); - - if ($file) { - $last = fgets($file); - } else { - print "Unable to read $last_updated_file. Using current time.\n"; - return $current_time; - } - - fclose($file); - - return $last; + if (file_exists($last_updated_file) && + ($file = fopen($last_updated_file, 'r'))) { + $last = fgets($file); + } else { + print "$last_updated_file doesn't exit. Trying to create it...\n"; + $file = fopen($last_updated_file, 'w+') or + die("Can't open $last_updated_file for writing!\n"); + print 'Success. Using current time (' . date('r', $last) . + ") to look for new notices.\n"; + } + + fclose($file); + return $last; } function updateLastUpdated($time){ - global $last_updated_file; - $file = fopen($last_updated_file, 'w') or die("Can't open $last_updated_file for writing!"); - fwrite($file, $time); - fclose($file); + global $last_updated_file; + $file = fopen($last_updated_file, 'w') or + die("Can't open $last_updated_file for writing!"); + fwrite($file, $time); + fclose($file); } + From dec0461d5fdd67ae9b5250beb859d6fed5af5609 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sun, 8 Feb 2009 13:25:48 -0800 Subject: [PATCH 05/23] Trac #1159: Fix peopletag pagination. Also phpcs cleanup. --- actions/peopletag.php | 126 ++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 35 deletions(-) diff --git a/actions/peopletag.php b/actions/peopletag.php index 3578c53fdf..6b1e34f1ab 100644 --- a/actions/peopletag.php +++ b/actions/peopletag.php @@ -1,9 +1,12 @@ . + * + * @category Action + * @package Laconica + * @author Evan Prodromou + * @author Zach Copley + * @copyright 2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ */ -if (!defined('LACONICA')) { exit(1); } +if (!defined('LACONICA')) { + exit(1); +} require_once INSTALLDIR.'/lib/profilelist.php'; +/** + * This class outputs a paginated list of profiles self-tagged with a given tag + * + * @category Output + * @package Laconica + * @author Evan Prodromou + * @author Zach Copley + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * + * @see Action + */ + class PeopletagAction extends Action { - - var $tag = null; + + var $tag = null; var $page = null; - - function handle($args) + + /** + * For initializing members of the class. + * + * @param array $argarray misc. arguments + * + * @return boolean true + */ + function prepare($argarray) { - parent::handle($args); - - parent::prepare($args); + parent::prepare($argarray); $this->tag = $this->trimmed('tag'); if (!common_valid_profile_tag($this->tag)) { - $this->clientError(sprintf(_('Not a valid people tag: %s'), $this->tag)); + $this->clientError(sprintf(_('Not a valid people tag: %s'), + $this->tag)); return; } - $this->page = $this->trimmed('page'); + $this->page = ($this->arg('page')) ? $this->arg('page') : 1; - if (!$this->page) { - $this->page = 1; - } - + common_set_returnto($this->selfUrl()); + + return true; + } + + /** + * Handler method + * + * @param array $argarray is ignored since it's now passed in in prepare() + * + * @return boolean is read only action? + */ + function handle($argarray) + { + parent::handle($argarray); $this->showPage(); } - + + /** + * Whips up a query to get a list of profiles based on the provided + * people tag and page, initalizes a ProfileList widget, and displays + * it to the user. + * + * @return nothing + */ function showContent() { - + $profile = new Profile(); - $offset = ($page-1)*PROFILES_PER_PAGE; - $limit = PROFILES_PER_PAGE + 1; - - if (common_config('db','type') == 'pgsql') { + $offset = ($this->page - 1) * PROFILES_PER_PAGE; + $limit = PROFILES_PER_PAGE + 1; + + if (common_config('db', 'type') == 'pgsql') { $lim = ' LIMIT ' . $limit . ' OFFSET ' . $offset; } else { $lim = ' LIMIT ' . $offset . ', ' . $limit; } - # XXX: memcached this - + // XXX: memcached this + $qry = 'SELECT profile.* ' . 'FROM profile JOIN profile_tag ' . 'ON profile.id = profile_tag.tagger ' . 'WHERE profile_tag.tagger = profile_tag.tagged ' . 'AND tag = "%s" ' . - 'ORDER BY profile_tag.modified DESC'; - + 'ORDER BY profile_tag.modified DESC%s'; + $profile->query(sprintf($qry, $this->tag, $lim)); - $pl = new ProfileList($profile, null, $this); + $pl = new ProfileList($profile, null, $this); $cnt = $pl->show(); - + $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE, $this->page, - $this->trimmed('action'), + 'peopletag', array('tag' => $this->tag)); } - - function title() + + /** + * Returns the page title + * + * @return string page title + */ + function title() { - return sprintf( _('Users self-tagged with %s - page %d'), $this->tag, $this->page); + return sprintf(_('Users self-tagged with %s - page %d'), + $this->tag, $this->page); } - + } From 00fda7ae5f1529b71ce9ab3c56e5406d018de737 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sun, 8 Feb 2009 21:58:25 +0000 Subject: [PATCH 06/23] Minor update to the way Facebook app handles listing of friends you've invited. --- actions/facebookinvite.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actions/facebookinvite.php b/actions/facebookinvite.php index 3c872f94bf..ed59dda85f 100644 --- a/actions/facebookinvite.php +++ b/actions/facebookinvite.php @@ -71,13 +71,13 @@ class FacebookinviteAction extends FacebookAction common_config('site', 'name'))); $this->element('p', null, _('Invitations have been sent to the following users:')); - $friend_ids = $_POST['ids']; // XXX: Hmm... is this the best way to acces the list? + $friend_ids = $_POST['ids']; // XXX: Hmm... is this the best way to access the list? $this->elementStart('ul', array('id' => 'facebook-friends')); foreach ($friend_ids as $friend) { $this->elementStart('li'); - $this->element('fb:profile-pic', array('uid' => $friend)); + $this->element('fb:profile-pic', array('uid' => $friend, 'size' => 'square')); $this->element('fb:name', array('uid' => $friend, 'capitalize' => 'true')); $this->elementEnd('li'); From c604fa8d3722f85cd65c92dba8ef4e1dc589574f Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Sun, 8 Feb 2009 23:07:56 +0000 Subject: [PATCH 07/23] Ticket #1094 Facebook app invites page was failing if no friends had added the app yet --- actions/facebookinvite.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/actions/facebookinvite.php b/actions/facebookinvite.php index ed59dda85f..1302064ad7 100644 --- a/actions/facebookinvite.php +++ b/actions/facebookinvite.php @@ -92,6 +92,12 @@ class FacebookinviteAction extends FacebookAction // Get a list of users who are already using the app for exclusion $exclude_ids = $this->facebook->api_client->friends_getAppUsers(); + $exclude_ids_csv = null; + + // fbml needs these as a csv string, not an array + if ($exclude_ids) { + $exclude_ids_csv = implode(',', $exclude_ids); + } $content = sprintf(_('You have been invited to %s'), common_config('site', 'name')) . htmlentities(''); @@ -103,10 +109,17 @@ class FacebookinviteAction extends FacebookAction 'content' => $content)); $this->hidden('invite', 'true'); $actiontext = sprintf(_('Invite your friends to use %s'), common_config('site', 'name')); - $this->element('fb:multi-friend-selector', array('showborder' => 'false', - 'actiontext' => $actiontext, - 'exclude_ids' => implode(',', $exclude_ids), - 'bypass' => 'cancel')); + + $multi_params = array('showborder' => 'false'); + $multi_params['actiontext'] = $actiontext; + + if ($exclude_ids_csv) { + $multi_params['exclude_ids'] = $exclude_ids_csv; + } + + $multi_params['bypass'] = 'cancel'; + + $this->element('fb:multi-friend-selector', $multi_params); $this->elementEnd('fb:request-form'); From b1f9dec20e0a6c9c25fe873bffa81e632f2fc146 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 06:51:08 -0500 Subject: [PATCH 08/23] First events code Add the basic code for adding events. --- lib/event.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 lib/event.php diff --git a/lib/event.php b/lib/event.php new file mode 100644 index 0000000000..f5406d07a6 --- /dev/null +++ b/lib/event.php @@ -0,0 +1,113 @@ +. + * + * @category Event + * @package Laconica + * @author Evan Prodromou + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Class for events + * + * This "class" two static functions for managing events in the Laconica code. + * + * @category Event + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * + * @todo Define a system for using Event instances + */ + +class Event { + + /* Global array of hooks, mapping eventname => array of callables */ + + protected static $_handlers = array(); + + /** + * Add an event handler + * + * To run some code at a particular point in Laconica processing. + * Named events include receiving an XMPP message, adding a new notice, + * or showing part of an HTML page. + * + * The arguments to the handler vary by the event. Handlers can return + * two possible values: false means that the event has been replaced by + * the handler completely, and no default processing should be done. + * Non-false means successful handling, and that the default processing + * should succeed. (Note that this only makes sense for some events.) + * + * Handlers can also abort processing by throwing an exception; these will + * be caught by the closest code and displayed as errors. + * + * @param string $name Name of the event + * @param callable $handler Code to run + * + * @return void + */ + + public static function addHandler($name, $handler) { + if (array_key_exists($name, Event::$_handlers)) { + Event::$_handlers[$name][] = $handler; + } else { + Event::$_handlers[$name] = array($handler); + } + } + + /** + * Handle an event + * + * Events are any point in the code that we want to expose for admins + * or third-party developers to use. + * + * We pass in an array of arguments (including references, for stuff + * that can be changed), and each assigned handler gets run with those + * arguments. Exceptions can be thrown to indicate an error. + * + * @param string $name Name of the event that's happening + * @param array $args Arguments for handlers + * + * @return boolean flag saying whether to continue processing, based + * on results of handlers. + */ + + public static function handle($name, $args) { + $result = null; + if (array_key_exists($name, Event::$_handlers)) { + foreach (Event::$_handlers[$name] as $handler) { + $result = call_user_func_array($handler, $args); + if ($result === false) { + break; + } + } + } + return ($result === false); + } +} From 12d7c30ef79c5ad84b7bac5e4863db3ce4e23621 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 06:52:39 -0500 Subject: [PATCH 09/23] Add event.php before config.php is called --- lib/common.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/common.php b/lib/common.php index 5b4e3c40c8..7d3ec108ca 100644 --- a/lib/common.php +++ b/lib/common.php @@ -49,6 +49,11 @@ require_once('DB/DataObject/Cast.php'); # for dates require_once(INSTALLDIR.'/lib/language.php'); +// This gets included before the config file, so that admin code and plugins +// can use it + +require_once(INSTALLDIR.'/lib/event.php'); + // try to figure out where we are $_server = array_key_exists('SERVER_NAME', $_SERVER) ? From 9152c0bdc8e23ca0ff03c72c5891d3db9c5e9d4f Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 07:12:08 -0500 Subject: [PATCH 10/23] First steps to using exceptions for error handling Added two exception classes: one for client errors (= user can fix) and one for server errors (only admin or coder can fix). The web entry point now tries to catch exceptions and show them in the browser. The main code for showing errors in Action class now throws an exception and lets top-level handle it. --- lib/clientexception.php | 56 +++++++++++++++++++++++++++++++++++++++++ lib/serverexception.php | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 lib/clientexception.php create mode 100644 lib/serverexception.php diff --git a/lib/clientexception.php b/lib/clientexception.php new file mode 100644 index 0000000000..3020d7f506 --- /dev/null +++ b/lib/clientexception.php @@ -0,0 +1,56 @@ +. + * + * @category Exception + * @package Laconica + * @author Evan Prodromou + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Class for client exceptions + * + * Subclass of PHP Exception for user errors. + * + * @category Exception + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +class ClientException extends Exception +{ + public function __construct($message = null, $code = 400) { + parent::__construct($message, $code); + } + + // custom string representation of object + public function __toString() { + return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; + } +} diff --git a/lib/serverexception.php b/lib/serverexception.php new file mode 100644 index 0000000000..b8ed6846e7 --- /dev/null +++ b/lib/serverexception.php @@ -0,0 +1,55 @@ +. + * + * @category Exception + * @package Laconica + * @author Evan Prodromou + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Class for server exceptions + * + * Subclass of PHP Exception for server errors. The user typically can't fix these. + * + * @category Exception + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +class ServerException extends Exception +{ + public function __construct($message = null, $code = 400) { + parent::__construct($message, $code); + } + + public function __toString() { + return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; + } +} From aa06d760b375c0cc9bbc693bf4bd412e9fda8f50 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 07:15:52 -0500 Subject: [PATCH 11/23] Index and Action use Exceptions Main Web entry point accepts exceptions, and main code in Action throws them. --- index.php | 29 +++++++++++++++++++++++------ lib/action.php | 6 ++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/index.php b/index.php index 387b642e2c..075ee96768 100644 --- a/index.php +++ b/index.php @@ -47,7 +47,11 @@ if (!$user && common_config('site', 'private') && $actionfile = INSTALLDIR."/actions/$action.php"; -if (file_exists($actionfile)) { +if (!file_exists($actionfile)) { + $cac = new ClientErrorAction(); + $cac->handle(array('code' => 404, + 'message' => _('Unknown action'))); +} else { include_once $actionfile; @@ -66,9 +70,22 @@ if (file_exists($actionfile)) { } $config['db']['database'] = $mirror; } - if (call_user_func(array($action_obj, 'prepare'), $_REQUEST)) { - call_user_func(array($action_obj, 'handle'), $_REQUEST); + + try { + if ($action_obj->prepare($_REQUEST)) { + $action_obj->handle($_REQUEST); + } + } catch (ClientException cex) { + $cac = new ClientErrorAction(); + $cac->handle(array('code' => $cex->code, + 'message' => $cex->message)); + } catch (ServerException sex) { // snort snort guffaw + $sac = new ServerErrorAction(); + $sac->handle(array('code' => $sex->code, + 'message' => $sex->message)); + } catch (Exception ex) { + $sac = new ServerErrorAction(); + $sac->handle(array('code' => 500, + 'message' => $ex->message)); } -} else { - common_user_error(_('Unknown action')); -} \ No newline at end of file +} diff --git a/lib/action.php b/lib/action.php index c4172ada11..9fbabb4fcb 100644 --- a/lib/action.php +++ b/lib/action.php @@ -789,11 +789,12 @@ class Action extends HTMLOutputter // lawsuit * * @return nothing */ + function serverError($msg, $code=500) { $action = $this->trimmed('action'); common_debug("Server error '$code' on '$action': $msg", __FILE__); - common_server_error($msg, $code); + throw new ServerException($msg, $code); } /** @@ -804,11 +805,12 @@ class Action extends HTMLOutputter // lawsuit * * @return nothing */ + function clientError($msg, $code=400) { $action = $this->trimmed('action'); common_debug("User error '$code' on '$action': $msg", __FILE__); - common_user_error($msg, $code); + throw new ClientException($msg, $code); } /** From 5466f6a6d0950331a4cb54e09b44ea4524751fb4 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 07:25:35 -0500 Subject: [PATCH 12/23] Better exception handling in index Some better exception handling in Web entry point. --- index.php | 26 +++++++++++--------------- lib/common.php | 5 +++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/index.php b/index.php index 075ee96768..dac5a8071a 100644 --- a/index.php +++ b/index.php @@ -48,9 +48,8 @@ if (!$user && common_config('site', 'private') && $actionfile = INSTALLDIR."/actions/$action.php"; if (!file_exists($actionfile)) { - $cac = new ClientErrorAction(); - $cac->handle(array('code' => 404, - 'message' => _('Unknown action'))); + $cac = new ClientErrorAction(_('Unknown action'), 404); + $cac->showPage(); } else { include_once $actionfile; @@ -75,17 +74,14 @@ if (!file_exists($actionfile)) { if ($action_obj->prepare($_REQUEST)) { $action_obj->handle($_REQUEST); } - } catch (ClientException cex) { - $cac = new ClientErrorAction(); - $cac->handle(array('code' => $cex->code, - 'message' => $cex->message)); - } catch (ServerException sex) { // snort snort guffaw - $sac = new ServerErrorAction(); - $sac->handle(array('code' => $sex->code, - 'message' => $sex->message)); - } catch (Exception ex) { - $sac = new ServerErrorAction(); - $sac->handle(array('code' => 500, - 'message' => $ex->message)); + } catch (ClientException $cex) { + $cac = new ClientErrorAction($cex->getMessage(), $cex->getCode()); + $cac->showPage(); + } catch (ServerException $sex) { // snort snort guffaw + $sac = new ServerErrorAction($sex->getMessage(), $sex->getCode()); + $sac->showPage(); + } catch (Exception $ex) { + $sac = new ServerErrorAction($ex->getMessage()); + $sac->showPage(); } } diff --git a/lib/common.php b/lib/common.php index 7d3ec108ca..64c7f2410a 100644 --- a/lib/common.php +++ b/lib/common.php @@ -182,6 +182,8 @@ foreach ($_config_files as $_config_file) { } } +// XXX: how many of these could be auto-loaded on use? + require_once('Validate.php'); require_once('markdown.php'); @@ -193,6 +195,9 @@ require_once(INSTALLDIR.'/lib/subs.php'); require_once(INSTALLDIR.'/lib/Shorturl_api.php'); require_once(INSTALLDIR.'/lib/twitter.php'); +require_once(INSTALLDIR.'/lib/clientexception.php'); +require_once(INSTALLDIR.'/lib/serverexception.php'); + // XXX: other formats here define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER); From 55cba5007e37245de1059b3de4774040850076c2 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 07:51:23 -0500 Subject: [PATCH 13/23] Fix indentation in lib/action.php --- lib/action.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/action.php b/lib/action.php index 9fbabb4fcb..e905b091ef 100644 --- a/lib/action.php +++ b/lib/action.php @@ -329,10 +329,10 @@ class Action extends HTMLOutputter // lawsuit if (common_config('xmpp', 'enabled')) { $this->menuItem(common_local_url('imsettings'), - _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect'); + _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect'); } else { $this->menuItem(common_local_url('smssettings'), - _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect'); + _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect'); } $this->menuItem(common_local_url('logout'), _('Logout'), _('Logout from the site'), false, 'nav_logout'); From e40d503dfb1b929c7e91422e7ee103a2cf37288d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:02:08 -0500 Subject: [PATCH 14/23] had the logic on event handler reversed --- lib/event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event.php b/lib/event.php index f5406d07a6..10ef5ec0a9 100644 --- a/lib/event.php +++ b/lib/event.php @@ -108,6 +108,6 @@ class Event { } } } - return ($result === false); + return ($result !== false); } } From 05991e2206cab8008a981411b35224b9cd86fce7 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:02:23 -0500 Subject: [PATCH 15/23] First events for adding menu items --- lib/action.php | 87 +++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/lib/action.php b/lib/action.php index e905b091ef..36f598c57f 100644 --- a/lib/action.php +++ b/lib/action.php @@ -312,42 +312,46 @@ class Action extends HTMLOutputter // lawsuit */ function showPrimaryNav() { + $user = common_current_user(); + $this->elementStart('dl', array('id' => 'site_nav_global_primary')); $this->element('dt', null, _('Primary site navigation')); $this->elementStart('dd'); - $user = common_current_user(); $this->elementStart('ul', array('class' => 'nav')); - if ($user) { - $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)), - _('Home'), _('Personal profile and friends timeline'), false, 'nav_home'); - } - $this->menuItem(common_local_url('peoplesearch'), - _('Search'), _('Search for people or text'), false, 'nav_search'); - if ($user) { - $this->menuItem(common_local_url('profilesettings'), - _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account'); + if (Event::handle('StartPrimaryNav', array($this))) { + if ($user) { + $this->menuItem(common_local_url('all', array('nickname' => $user->nickname)), + _('Home'), _('Personal profile and friends timeline'), false, 'nav_home'); + } + $this->menuItem(common_local_url('peoplesearch'), + _('Search'), _('Search for people or text'), false, 'nav_search'); + if ($user) { + $this->menuItem(common_local_url('profilesettings'), + _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account'); - if (common_config('xmpp', 'enabled')) { - $this->menuItem(common_local_url('imsettings'), - _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect'); + if (common_config('xmpp', 'enabled')) { + $this->menuItem(common_local_url('imsettings'), + _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect'); + } else { + $this->menuItem(common_local_url('smssettings'), + _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect'); + } + $this->menuItem(common_local_url('logout'), + _('Logout'), _('Logout from the site'), false, 'nav_logout'); } else { - $this->menuItem(common_local_url('smssettings'), - _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect'); + $this->menuItem(common_local_url('login'), + _('Login'), _('Login to the site'), false, 'nav_login'); + if (!common_config('site', 'closed')) { + $this->menuItem(common_local_url('register'), + _('Register'), _('Create an account'), false, 'nav_register'); + } + $this->menuItem(common_local_url('openidlogin'), + _('OpenID'), _('Login with OpenID'), false, 'nav_openid'); } - $this->menuItem(common_local_url('logout'), - _('Logout'), _('Logout from the site'), false, 'nav_logout'); - } else { - $this->menuItem(common_local_url('login'), - _('Login'), _('Login to the site'), false, 'nav_login'); - if (!common_config('site', 'closed')) { - $this->menuItem(common_local_url('register'), - _('Register'), _('Create an account'), false, 'nav_register'); - } - $this->menuItem(common_local_url('openidlogin'), - _('OpenID'), _('Login with OpenID'), false, 'nav_openid'); + $this->menuItem(common_local_url('doc', array('title' => 'help')), + _('Help'), _('Help me!'), false, 'nav_help'); + Event::handle('EndPrimaryNav', array($this)); } - $this->menuItem(common_local_url('doc', array('title' => 'help')), - _('Help'), _('Help me!'), false, 'nav_help'); $this->elementEnd('ul'); $this->elementEnd('dd'); $this->elementEnd('dl'); @@ -570,18 +574,21 @@ class Action extends HTMLOutputter // lawsuit $this->element('dt', null, _('Secondary site navigation')); $this->elementStart('dd', null); $this->elementStart('ul', array('class' => 'nav')); - $this->menuItem(common_local_url('doc', array('title' => 'help')), - _('Help')); - $this->menuItem(common_local_url('doc', array('title' => 'about')), - _('About')); - $this->menuItem(common_local_url('doc', array('title' => 'faq')), - _('FAQ')); - $this->menuItem(common_local_url('doc', array('title' => 'privacy')), - _('Privacy')); - $this->menuItem(common_local_url('doc', array('title' => 'source')), - _('Source')); - $this->menuItem(common_local_url('doc', array('title' => 'contact')), - _('Contact')); + if (Event::handle('StartSecondaryNav', array($this))) { + $this->menuItem(common_local_url('doc', array('title' => 'help')), + _('Help')); + $this->menuItem(common_local_url('doc', array('title' => 'about')), + _('About')); + $this->menuItem(common_local_url('doc', array('title' => 'faq')), + _('FAQ')); + $this->menuItem(common_local_url('doc', array('title' => 'privacy')), + _('Privacy')); + $this->menuItem(common_local_url('doc', array('title' => 'source')), + _('Source')); + $this->menuItem(common_local_url('doc', array('title' => 'contact')), + _('Contact')); + Event::handle('EndSecondaryNav', array($this)); + } $this->elementEnd('ul'); $this->elementEnd('dd'); $this->elementEnd('dl'); From 60bf87bb34c4c0216fbbe42458d194e1e76c0aaa Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:11:52 -0500 Subject: [PATCH 16/23] notes about existing events; should probably put this in a separate doc --- EVENTS.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 EVENTS.txt diff --git a/EVENTS.txt b/EVENTS.txt new file mode 100644 index 0000000000..bd511d75e1 --- /dev/null +++ b/EVENTS.txt @@ -0,0 +1,11 @@ +StartPrimaryNav: Showing the primary nav menu +- $action: the current action + +EndPrimaryNav: At the end of the primary nav menu +- $action: the current action + +StartSecondaryNav: Showing the secondary nav menu +- $action: the current action + +EndSecondaryNav: At the end of the secondary nav menu +- $action: the current action From 5d246299b630c55d98cc03eeb650561defbeeff4 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:24:23 -0500 Subject: [PATCH 17/23] add hooks for JavaScript handling --- EVENTS.txt | 20 ++++++++++++++++++++ lib/action.php | 33 +++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/EVENTS.txt b/EVENTS.txt index bd511d75e1..68e25fa3b0 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -9,3 +9,23 @@ StartSecondaryNav: Showing the secondary nav menu EndSecondaryNav: At the end of the secondary nav menu - $action: the current action + +StartShowScripts: Showing JavaScript links +- $action: the current action + +EndShowScripts: End showing JavaScript links; good place to add custom + links like Google Analytics +- $action: the current action + +StartShowJQueryScripts: Showing JQuery script links (use this to link to e.g. Google mirrors) +- $action: the current action + +EndShowJQueryScripts: End showing JQuery script links +- $action: the current action + +StartShowLaconicaScripts: Showing Laconica script links (use this to link to a CDN or something) +- $action: the current action + +EndShowLaconicaScripts: End showing Laconica script links +- $action: the current action + diff --git a/lib/action.php b/lib/action.php index 36f598c57f..0628dc70db 100644 --- a/lib/action.php +++ b/lib/action.php @@ -179,18 +179,27 @@ class Action extends HTMLOutputter // lawsuit */ function showScripts() { - $this->element('script', array('type' => 'text/javascript', - 'src' => common_path('js/jquery.min.js')), - ' '); - $this->element('script', array('type' => 'text/javascript', - 'src' => common_path('js/jquery.form.js')), - ' '); - $this->element('script', array('type' => 'text/javascript', - 'src' => common_path('js/xbImportNode.js')), - ' '); - $this->element('script', array('type' => 'text/javascript', - 'src' => common_path('js/util.js?version='.LACONICA_VERSION)), - ' '); + if (Event::handle('StartShowScripts', array($this))) { + if (Event::handle('StartShowJQueryScripts', array($this))) { + $this->element('script', array('type' => 'text/javascript', + 'src' => common_path('js/jquery.min.js')), + ' '); + $this->element('script', array('type' => 'text/javascript', + 'src' => common_path('js/jquery.form.js')), + ' '); + Event::handle('EndShowJQueryScripts', array($this)); + } + if (Event::handle('StartShowLaconicaScripts', array($this))) { + $this->element('script', array('type' => 'text/javascript', + 'src' => common_path('js/xbImportNode.js')), + ' '); + $this->element('script', array('type' => 'text/javascript', + 'src' => common_path('js/util.js?version='.LACONICA_VERSION)), + ' '); + Event::handle('EndShowLaconicaScripts', array($this)); + } + Event::handle('EndShowScripts', array($this)); + } } /** From f4e8cc6d9f88e78876baa54d0ffba77694c56a1b Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:44:30 -0500 Subject: [PATCH 18/23] Add InitializePlugin and CleanupPlugin events We add two events to allow plugins to initialize and cleanup. --- EVENTS.txt | 5 +++++ index.php | 5 +++++ lib/common.php | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/EVENTS.txt b/EVENTS.txt index 68e25fa3b0..4b8260b3ce 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -1,3 +1,8 @@ +InitializePlugin: a chance to initialize a plugin in a complete + environment + +CleanupPlugin: a chance to cleanup a plugin at the end of a program + StartPrimaryNav: Showing the primary nav menu - $action: the current action diff --git a/index.php b/index.php index dac5a8071a..0a79b9731d 100644 --- a/index.php +++ b/index.php @@ -85,3 +85,8 @@ if (!file_exists($actionfile)) { $sac->showPage(); } } + +// XXX: cleanup exit() calls or add an exit handler so +// this always gets called + +Event::handle('CleanupPlugin'); diff --git a/lib/common.php b/lib/common.php index 64c7f2410a..2f85eb7c51 100644 --- a/lib/common.php +++ b/lib/common.php @@ -212,3 +212,7 @@ function __autoload($class) require_once(INSTALLDIR.'/lib/' . strtolower($class) . '.php'); } } + +// Give plugins a chance to initialize in a fully-prepared environment + +Event::handle('InitializePlugin'); From 175f1e73950ab80c799944af8f69391113906394 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:47:11 -0500 Subject: [PATCH 19/23] utility superclass for plugins --- lib/plugin.php | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/plugin.php diff --git a/lib/plugin.php b/lib/plugin.php new file mode 100644 index 0000000000..7b2436e543 --- /dev/null +++ b/lib/plugin.php @@ -0,0 +1,79 @@ +. + * + * @category Plugin + * @package Laconica + * @author Evan Prodromou + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Base class for plugins + * + * A base class for Laconica plugins. Mostly a light wrapper around + * the Event framework. + * + * Subclasses of Plugin will automatically handle an event if they define + * a method called "onEventName". (Well, OK -- only if they call parent::__construct() + * in their constructors.) + * + * They will also automatically handle the InitializePlugin and CleanupPlugin with the + * initialize() and cleanup() methods, respectively. + * + * @category Plugin + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * + * @see Event + */ + +class Plugin +{ + function __construct() + { + Event::addHandler('InitializePlugin', array($this, 'initialize')); + Event::addHandler('CleanupPlugin', array($this, 'cleanup')); + + foreach (get_class_methods($this) as $method) { + if (mb_substr($method, 0, 2) == 'on') { + Event::addHandler(mb_substr($method, 2), array($this, $method)); + } + } + } + + function initialize() + { + return true; + } + + function cleanup() + { + return true; + } +} From dce975e33b95e455c671207054a1d4c0237e075f Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:48:50 -0500 Subject: [PATCH 20/23] allow events without arguments --- lib/event.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/event.php b/lib/event.php index 10ef5ec0a9..d815ae54ba 100644 --- a/lib/event.php +++ b/lib/event.php @@ -98,7 +98,7 @@ class Event { * on results of handlers. */ - public static function handle($name, $args) { + public static function handle($name, $args=array()) { $result = null; if (array_key_exists($name, Event::$_handlers)) { foreach (Event::$_handlers[$name] as $handler) { From ee4ee388ff6d66e2e6198ec1b5d1100733b63fd8 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 08:49:28 -0500 Subject: [PATCH 21/23] include plugin.php early so config can use it --- lib/common.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/common.php b/lib/common.php index 2f85eb7c51..041459cf34 100644 --- a/lib/common.php +++ b/lib/common.php @@ -53,6 +53,7 @@ require_once(INSTALLDIR.'/lib/language.php'); // can use it require_once(INSTALLDIR.'/lib/event.php'); +require_once(INSTALLDIR.'/lib/plugin.php'); // try to figure out where we are From 2e518c9d5e3537b9fcb858510063ff815b7eecf7 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 09:14:44 -0500 Subject: [PATCH 22/23] Sample plugin for Google Analytics A common request is to use Google Analytics for Laconica servers. This plugin will add the correct spell to make Google Analytics work. --- plugins/GoogleAnalyticsPlugin.php | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 plugins/GoogleAnalyticsPlugin.php diff --git a/plugins/GoogleAnalyticsPlugin.php b/plugins/GoogleAnalyticsPlugin.php new file mode 100644 index 0000000000..87a70e31ea --- /dev/null +++ b/plugins/GoogleAnalyticsPlugin.php @@ -0,0 +1,77 @@ +. + * + * @category Plugin + * @package Laconica + * @author Evan Prodromou + * @copyright 2008 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +/** + * Plugin to use Google Analytics + * + * This plugin will spoot out the correct JavaScript spell to invoke Google Analytics on a page. + * + * Note that Google Analytics is not compatible with the Franklin Street Statement; consider using + * Pikiw (http://www.pikiw.org/) instead! + * + * @category Plugin + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + * + * @see Event + */ + +class GoogleAnalyticsPlugin extends Plugin +{ + var $code = null; + + function __construct($code=null) + { + $this->code = $code; + parent::__construct(); + } + + function onEndShowScripts($action) + { + $js1 = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");'. + 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));'; + $js2 = sprintf('try{'. + 'var pageTracker = _gat._getTracker("%s");'. + 'pageTracker._trackPageview();'. + '} catch(err) {}', + $this->code); + $action->elementStart('script', array('type' => 'text/javascript')); + $action->raw($js1); + $action->elementEnd('script'); + $action->elementStart('script', array('type' => 'text/javascript')); + $action->raw($js2); + $action->elementEnd('script'); + } +} From a476805dd96cf31af5331be5245a6d0af4d90dae Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 9 Feb 2009 09:20:55 -0500 Subject: [PATCH 23/23] give example of using Google Analytics --- config.php.sample | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config.php.sample b/config.php.sample index db1a216635..a2c5801f45 100644 --- a/config.php.sample +++ b/config.php.sample @@ -142,3 +142,7 @@ $config['sphinx']['port'] = 3312; # config section for the built-in Facebook application #$config['facebook']['apikey'] = 'APIKEY'; #$config['facebook']['secret'] = 'SECRET'; + +# Add Google Analytics +# require_once('plugins/GoogleAnalyticsPlugin.php'); +# $ga = new GoogleAnalyticsPlugin('your secret code');