3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A plugin to allow anonymous users to favorite notices
8 * If you want to keep certain users from having anonymous faving for their
9 * notices initialize the plugin with the restricted array, e.g.:
13 * array('restricted' => array('spock', 'kirk', 'bones'))
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU Affero General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU Affero General Public License for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 * @author Zach Copley <zach@status.net>
35 * @copyright 2010 StatusNet, Inc.
36 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
37 * @link http://status.net/
40 if (!defined('STATUSNET')) {
41 // This check helps protect against security problems;
42 // your code file can't be executed directly from the web.
46 define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1');
49 * Anonymous Fave plugin to allow anonymous (not logged in) users
54 * @author Zach Copley <zach@status.net>
55 * @copyright 2010 StatusNet, Inc.
56 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
57 * @link http://status.net/
59 class AnonymousFavePlugin extends Plugin
61 // Array of users who should not have anon faving. The default is
62 // that anonymous faving is allowed for all users.
63 public $restricted = array();
65 function onArgsInitialize() {
66 // We always want a session because we're tracking anon users
67 common_ensure_session();
71 * Hook for ensuring our tables are created
73 * Ensures the fave_tally table is there and has the right columns
75 * @return boolean hook return
78 function onCheckSchema()
80 $schema = Schema::get();
82 // For storing total number of times a notice has been faved
84 $schema->ensureTable('fave_tally',
86 new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
87 new ColumnDef('count', 'integer', null, false),
95 'on update CURRENT_TIMESTAMP'
103 function onEndShowHTML($action)
105 if (!common_logged_in()) {
106 // Set a place to return to when submitting forms
107 common_set_returnto($action->selfUrl());
111 function onEndShowScripts($action)
113 // Setup ajax calls for favoriting. Usually this is only done when
114 // a user is logged in.
115 $action->inlineScript('SN.U.NoticeFavor();');
118 function onAutoload($cls)
120 $dir = dirname(__FILE__);
124 include_once $dir . '/' . $cls . '.php';
126 case 'AnonFavorAction':
127 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
129 case 'AnonDisFavorAction':
130 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
132 case 'AnonFavorForm':
133 include_once $dir . '/anonfavorform.php';
135 case 'AnonDisFavorForm':
136 include_once $dir . '/anondisfavorform.php';
143 function onStartInitializeRouter($m)
145 $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
146 $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
151 function onStartShowNoticeOptions($item)
153 if (!common_logged_in()) {
154 $item->out->elementStart('div', 'notice-options');
155 $item->showFaveForm();
156 $item->out->elementEnd('div');
162 function onStartShowFaveForm($item)
164 if (!common_logged_in() && $this->hasAnonFaving($item)) {
166 $profile = AnonymousFavePlugin::getAnonProfile();
167 if (!empty($profile)) {
168 if ($profile->hasFave($item->notice)) {
169 $disfavor = new AnonDisFavorForm($item->out, $item->notice);
172 $favor = new AnonFavorForm($item->out, $item->notice);
181 function onEndFavorNoticeForm($form, $notice)
183 $this->showTally($form->out, $notice);
186 function onEndDisFavorNoticeForm($form, $notice)
188 $this->showTally($form->out, $notice);
191 function showTally($out, $notice)
193 $tally = Fave_tally::ensureTally($notice->id);
195 if (!empty($tally)) {
199 'id' => 'notice-' . $notice->id . '-tally',
200 'class' => 'notice-tally'
203 $out->elementStart('span', array('class' => 'fave-tally-title'));
204 // TRANS: Label for tally for number of times a notice was favored.
205 $out->raw(sprintf(_m("Favored")));
206 $out->elementEnd('span');
207 $out->elementStart('span', array('class' => 'fave-tally'));
208 $out->raw($tally->count);
209 $out->elementEnd('span');
210 $out->elementEnd('div');
214 function onEndFavorNotice($profile, $notice)
216 $tally = Fave_tally::increment($notice->id);
219 function onEndDisfavorNotice($profile, $notice)
221 $tally = Fave_tally::decrement($notice->id);
224 static function createAnonProfile()
226 // Get the anon user's IP, and turn it into a nickname
227 list($proxy, $ip) = common_client_ip();
229 // IP + time + random number should help to avoid collisions
230 $baseNickname = $ip . '-' . time() . '-' . common_good_rand(5);
232 $profile = new Profile();
233 $profile->nickname = $baseNickname;
234 $id = $profile->insert();
237 // TRANS: Server exception.
238 throw new ServerException(_m("Could not create anonymous user session."));
241 // Stick the Profile ID into the nickname
242 $orig = clone($profile);
244 $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
245 $result = $profile->update($orig);
248 // TRANS: Server exception.
249 throw new ServerException(_m("Could not create anonymous user session."));
254 "AnonymousFavePlugin - created profile for anonymous user from IP: "
263 static function getAnonProfile()
266 $token = $_SESSION['anon_token'];
267 $anon = base64_decode($token);
271 if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
272 $parts = explode('-', $anon);
274 // Do Profile lookup by ID instead of nickname for safety/performance
275 $profile = Profile::staticGet('id', $id);
277 $profile = AnonymousFavePlugin::createAnonProfile();
278 // Obfuscate so it's hard to figure out the Profile ID
279 $_SESSION['anon_token'] = base64_encode($profile->nickname);
286 * Determine whether a given NoticeListItem should have the
287 * anonymous fave/disfave form
289 * @param NoticeListItem $item
291 * @return boolean false if the profile associated with the notice is
292 * in the list of restricted profiles, otherwise
295 function hasAnonFaving($item)
297 $profile = Profile::staticGet('id', $item->notice->profile_id);
298 if (in_array($profile->nickname, $this->restricted)) {
306 * Provide plugin version information.
308 * This data is used when showing the version page.
310 * @param array &$versions array of version data arrays; see EVENTS.txt
312 * @return boolean hook value
314 function onPluginVersion(&$versions)
316 $url = 'http://status.net/wiki/Plugin:AnonymousFave';
318 $versions[] = array('name' => 'AnonymousFave',
319 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
320 'author' => 'Zach Copley',
323 // TRANS: Plugin description.
324 _m('Allow anonymous users to favorite notices.'));