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
83 $schema->ensureTable('fave_tally', Fave_tally::schemaDef());
88 function onEndShowHTML($action)
90 if (!common_logged_in()) {
91 // Set a place to return to when submitting forms
92 common_set_returnto($action->selfUrl());
96 function onEndShowScripts($action)
98 // Setup ajax calls for favoriting. Usually this is only done when
99 // a user is logged in.
100 $action->inlineScript('SN.U.NoticeFavor();');
103 function onAutoload($cls)
105 $dir = dirname(__FILE__);
109 include_once $dir . '/' . $cls . '.php';
111 case 'AnonFavorAction':
112 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
114 case 'AnonDisFavorAction':
115 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
117 case 'AnonFavorForm':
118 include_once $dir . '/anonfavorform.php';
120 case 'AnonDisFavorForm':
121 include_once $dir . '/anondisfavorform.php';
128 function onStartInitializeRouter($m)
130 $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
131 $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
136 function onStartShowNoticeOptions($item)
138 if (!common_logged_in()) {
139 $item->out->elementStart('div', 'notice-options');
140 $item->showFaveForm();
141 $item->out->elementEnd('div');
147 function onStartShowFaveForm($item)
149 if (!common_logged_in() && $this->hasAnonFaving($item)) {
151 $profile = AnonymousFavePlugin::getAnonProfile();
152 if (!empty($profile)) {
153 if ($profile->hasFave($item->notice)) {
154 $disfavor = new AnonDisFavorForm($item->out, $item->notice);
157 $favor = new AnonFavorForm($item->out, $item->notice);
166 function onEndFavorNoticeForm($form, $notice)
168 $this->showTally($form->out, $notice);
171 function onEndDisFavorNoticeForm($form, $notice)
173 $this->showTally($form->out, $notice);
176 function showTally($out, $notice)
178 $tally = Fave_tally::ensureTally($notice->id);
180 if (!empty($tally)) {
184 'id' => 'notice-' . $notice->id . '-tally',
185 'class' => 'notice-tally'
188 $out->elementStart('span', array('class' => 'fave-tally-title'));
189 // TRANS: Label for tally for number of times a notice was favored.
190 $out->raw(sprintf(_m("Favored")));
191 $out->elementEnd('span');
192 $out->elementStart('span', array('class' => 'fave-tally'));
193 $out->raw($tally->count);
194 $out->elementEnd('span');
195 $out->elementEnd('div');
199 function onEndFavorNotice($profile, $notice)
201 $tally = Fave_tally::increment($notice->id);
204 function onEndDisfavorNotice($profile, $notice)
206 $tally = Fave_tally::decrement($notice->id);
209 static function createAnonProfile()
211 // Get the anon user's IP, and turn it into a nickname
212 list($proxy, $ip) = common_client_ip();
214 // IP + time + random number should help to avoid collisions
215 $baseNickname = $ip . '-' . time() . '-' . common_good_rand(5);
217 $profile = new Profile();
218 $profile->nickname = $baseNickname;
219 $id = $profile->insert();
222 // TRANS: Server exception.
223 throw new ServerException(_m("Could not create anonymous user session."));
226 // Stick the Profile ID into the nickname
227 $orig = clone($profile);
229 $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
230 $result = $profile->update($orig);
233 // TRANS: Server exception.
234 throw new ServerException(_m("Could not create anonymous user session."));
239 "AnonymousFavePlugin - created profile for anonymous user from IP: "
248 static function getAnonProfile()
251 $token = $_SESSION['anon_token'];
252 $anon = base64_decode($token);
256 if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
257 $parts = explode('-', $anon);
259 // Do Profile lookup by ID instead of nickname for safety/performance
260 $profile = Profile::getKV('id', $id);
262 $profile = AnonymousFavePlugin::createAnonProfile();
263 // Obfuscate so it's hard to figure out the Profile ID
264 $_SESSION['anon_token'] = base64_encode($profile->nickname);
271 * Determine whether a given NoticeListItem should have the
272 * anonymous fave/disfave form
274 * @param NoticeListItem $item
276 * @return boolean false if the profile associated with the notice is
277 * in the list of restricted profiles, otherwise
280 function hasAnonFaving($item)
282 $profile = Profile::getKV('id', $item->notice->profile_id);
283 if (in_array($profile->nickname, $this->restricted)) {
291 * Provide plugin version information.
293 * This data is used when showing the version page.
295 * @param array &$versions array of version data arrays; see EVENTS.txt
297 * @return boolean hook value
299 function onPluginVersion(&$versions)
301 $url = 'http://status.net/wiki/Plugin:AnonymousFave';
303 $versions[] = array('name' => 'AnonymousFave',
304 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
305 'author' => 'Zach Copley',
308 // TRANS: Plugin description.
309 _m('Allow anonymous users to favorite notices.'));