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 onStartInitializeRouter($m)
105 $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
106 $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
111 function onStartShowNoticeOptions($item)
113 if (!common_logged_in()) {
114 $item->out->elementStart('div', 'notice-options');
115 $item->showFaveForm();
116 $item->out->elementEnd('div');
122 function onStartShowFaveForm($item)
124 if (!common_logged_in() && $this->hasAnonFaving($item)) {
126 $profile = AnonymousFavePlugin::getAnonProfile();
127 if ($profile instanceof Profile) {
128 if (Fave::existsForProfile($item->notice, $profile)) {
129 $disfavor = new AnonDisFavorForm($item->out, $item->notice);
132 $favor = new AnonFavorForm($item->out, $item->notice);
141 function onEndFavorNoticeForm($form, $notice)
143 $this->showTally($form->out, $notice);
146 function onEndDisFavorNoticeForm($form, $notice)
148 $this->showTally($form->out, $notice);
151 function showTally($out, $notice)
153 $tally = Fave_tally::ensureTally($notice->id);
155 if (!empty($tally)) {
159 'id' => 'notice-' . $notice->id . '-tally',
160 'class' => 'notice-tally'
163 $out->elementStart('span', array('class' => 'fave-tally-title'));
164 // TRANS: Label for tally for number of times a notice was favored.
165 $out->raw(sprintf(_m("Favored")));
166 $out->elementEnd('span');
167 $out->elementStart('span', array('class' => 'fave-tally'));
168 $out->raw($tally->count);
169 $out->elementEnd('span');
170 $out->elementEnd('div');
174 function onEndFavorNotice($profile, $notice)
176 $tally = Fave_tally::increment($notice->id);
179 function onEndDisfavorNotice($profile, $notice)
181 $tally = Fave_tally::decrement($notice->id);
184 static function createAnonProfile()
186 // Get the anon user's IP, and turn it into a nickname
187 list($proxy, $ip) = common_client_ip();
189 // IP + time + random number should help to avoid collisions
190 $baseNickname = $ip . '-' . time() . '-' . common_random_hexstr(5);
192 $profile = new Profile();
193 $profile->nickname = $baseNickname;
194 $id = $profile->insert();
197 // TRANS: Server exception.
198 throw new ServerException(_m("Could not create anonymous user session."));
201 // Stick the Profile ID into the nickname
202 $orig = clone($profile);
204 $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
205 $result = $profile->update($orig);
208 // TRANS: Server exception.
209 throw new ServerException(_m("Could not create anonymous user session."));
214 "AnonymousFavePlugin - created profile for anonymous user from IP: "
223 static function getAnonProfile()
226 $token = $_SESSION['anon_token'];
227 $anon = base64_decode($token);
231 if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
232 $parts = explode('-', $anon);
234 // Do Profile lookup by ID instead of nickname for safety/performance
235 $profile = Profile::getKV('id', $id);
237 $profile = AnonymousFavePlugin::createAnonProfile();
238 // Obfuscate so it's hard to figure out the Profile ID
239 $_SESSION['anon_token'] = base64_encode($profile->nickname);
246 * Determine whether a given NoticeListItem should have the
247 * anonymous fave/disfave form
249 * @param NoticeListItem $item
251 * @return boolean false if the profile associated with the notice is
252 * in the list of restricted profiles, otherwise
255 function hasAnonFaving($item)
257 $profile = Profile::getKV('id', $item->notice->profile_id);
258 if (in_array($profile->nickname, $this->restricted)) {
266 * Provide plugin version information.
268 * This data is used when showing the version page.
270 * @param array &$versions array of version data arrays; see EVENTS.txt
272 * @return boolean hook value
274 function onPluginVersion(array &$versions)
276 $url = 'http://status.net/wiki/Plugin:AnonymousFave';
278 $versions[] = array('name' => 'AnonymousFave',
279 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
280 'author' => 'Zach Copley',
283 // TRANS: Plugin description.
284 _m('Allow anonymous users to favorite notices.'));