4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2010, StatusNet, Inc.
7 * A plugin to allow anonymous users to favorite notices
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * @author Zach Copley <zach@status.net>
27 * @copyright 2010 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29 * @link http://status.net/
32 if (!defined('STATUSNET')) {
33 // This check helps protect against security problems;
34 // your code file can't be executed directly from the web.
38 define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1');
41 * Anonymous Fave plugin to allow anonymous (not logged in) users
46 * @author Zach Copley <zach@status.net>
47 * @copyright 2010 StatusNet, Inc.
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49 * @link http://status.net/
51 class AnonymousFavePlugin extends Plugin {
53 function onArgsInitialize() {
54 // We always want a session because we're tracking anon users
55 common_ensure_session();
58 function onEndShowHTML($action)
60 if (!common_logged_in()) {
61 // Set a place to return to when submitting forms
62 common_set_returnto($action->selfUrl());
66 function onEndShowScripts($action)
68 // Setup ajax calls for favoriting. Usually this is only done when
69 // a user is logged in.
70 $action->inlineScript('SN.U.NoticeFavor();');
73 function onAutoload($cls)
75 $dir = dirname(__FILE__);
78 case 'AnonFavorAction':
79 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
81 case 'AnonDisFavorAction':
82 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
85 include_once $dir . '/anonfavorform.php';
87 case 'AnonDisFavorForm':
88 include_once $dir . '/anondisfavorform.php';
95 function onStartInitializeRouter($m) {
97 $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
98 $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
103 function onStartShowNoticeOptions($item) {
105 if (!common_logged_in()) {
106 $item->out->elementStart('div', 'notice-options');
107 $item->showFaveForm();
108 $item->out->elementEnd('div');
114 function onStartShowFaveForm($item) {
116 if (!common_logged_in()) {
118 $profile = $this->getAnonProfile();
119 if (!empty($profile)) {
120 if ($profile->hasFave($item->notice)) {
121 $disfavor = new AnonDisFavorForm($item->out, $item->notice);
124 $favor = new AnonFavorForm($item->out, $item->notice);
133 function createAnonProfile() {
135 // Get the anon user's IP, and turn it into a nickname
136 list($proxy, $ip) = common_client_ip();
137 // IP + time + random number should avoid collisions
138 $nickname = 'anonymous-' . $ip . '-' . time() . '-' . common_good_rand(5);
140 $profile = new Profile();
141 $profile->nickname = $nickname;
142 $id = $profile->insert();
147 "AnonymousFavePlugin - created profile for anonymous user from IP: "
157 function getAnonProfile() {
159 $anon = $_SESSION['anon_nickname'];
164 $profile = Profile::staticGet('nickname', $anon);
166 $profile = $this->createAnonProfile();
167 $_SESSION['anon_nickname'] = $profile->nickname;
170 if (!empty($profile)) {
176 * Provide plugin version information.
178 * This data is used when showing the version page.
180 * @param array &$versions array of version data arrays; see EVENTS.txt
182 * @return boolean hook value
184 function onPluginVersion(&$versions)
186 $url = 'http://status.net/wiki/Plugin:AnonymousFave';
188 $versions[] = array('name' => 'AnonymousFave',
189 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
190 'author' => 'Zach Copley',
193 _m('Allow anonymous users to favorite notices.'));