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/
60 class AnonymousFavePlugin extends Plugin
63 // Array of users who should not have anon faving. The default is
64 // that anonymous faving is allowed for all users.
65 public $restricted = array();
67 function onArgsInitialize() {
68 // We always want a session because we're tracking anon users
69 common_ensure_session();
73 * Hook for ensuring our tables are created
75 * Ensures the fave_tally table is there and has the right columns
77 * @return boolean hook return
80 function onCheckSchema()
82 $schema = Schema::get();
84 // For storing total number of times a notice has been faved
86 $schema->ensureTable('fave_tally',
88 new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
89 new ColumnDef('count', 'integer', null, false),
97 'on update CURRENT_TIMESTAMP'
105 function onEndShowHTML($action)
107 if (!common_logged_in()) {
108 // Set a place to return to when submitting forms
109 common_set_returnto($action->selfUrl());
113 function onEndShowScripts($action)
115 // Setup ajax calls for favoriting. Usually this is only done when
116 // a user is logged in.
117 $action->inlineScript('SN.U.NoticeFavor();');
120 function onAutoload($cls)
122 $dir = dirname(__FILE__);
126 include_once $dir . '/' . $cls . '.php';
128 case 'AnonFavorAction':
129 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
131 case 'AnonDisFavorAction':
132 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
134 case 'AnonFavorForm':
135 include_once $dir . '/anonfavorform.php';
137 case 'AnonDisFavorForm':
138 include_once $dir . '/anondisfavorform.php';
145 function onStartInitializeRouter($m)
147 $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
148 $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
153 function onStartShowNoticeOptions($item)
155 if (!common_logged_in()) {
156 $item->out->elementStart('div', 'notice-options');
157 $item->showFaveForm();
158 $item->out->elementEnd('div');
164 function onStartShowFaveForm($item)
166 if (!common_logged_in() && $this->hasAnonFaving($item)) {
168 $profile = AnonymousFavePlugin::getAnonProfile();
169 if (!empty($profile)) {
170 if ($profile->hasFave($item->notice)) {
171 $disfavor = new AnonDisFavorForm($item->out, $item->notice);
174 $favor = new AnonFavorForm($item->out, $item->notice);
183 function onEndFavorNoticeForm($form, $notice)
185 $this->showTally($form->out, $notice);
188 function onEndDisFavorNoticeForm($form, $notice)
190 $this->showTally($form->out, $notice);
193 function showTally($out, $notice)
195 $tally = Fave_tally::ensureTally($notice->id);
197 if (!empty($tally)) {
201 'id' => 'notice-' . $notice->id . '-tally',
202 'class' => 'notice-tally'
205 $out->elementStart('span', array('class' => 'fave-tally-title'));
206 // TRANS: Label for tally for number of times a notice was favored.
207 $out->raw(sprintf(_m("Favored")));
208 $out->elementEnd('span');
209 $out->elementStart('span', array('class' => 'fave-tally'));
210 $out->raw($tally->count);
211 $out->elementEnd('span');
212 $out->elementEnd('div');
216 function onEndFavorNotice($profile, $notice)
218 $tally = Fave_tally::increment($notice->id);
221 function onEndDisfavorNotice($profile, $notice)
223 $tally = Fave_tally::decrement($notice->id);
226 static function createAnonProfile()
228 // Get the anon user's IP, and turn it into a nickname
229 list($proxy, $ip) = common_client_ip();
231 // IP + time + random number should help to avoid collisions
232 $baseNickname = $ip . '-' . time() . '-' . common_good_rand(5);
234 $profile = new Profile();
235 $profile->nickname = $baseNickname;
236 $id = $profile->insert();
239 // TRANS: Server exception.
240 throw new ServerException(_m("Couldn't create anonymous user session."));
243 // Stick the Profile ID into the nickname
244 $orig = clone($profile);
246 $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
247 $result = $profile->update($orig);
250 // TRANS: Server exception.
251 throw new ServerException(_m("Couldn't create anonymous user session."));
256 "AnonymousFavePlugin - created profile for anonymous user from IP: "
265 static function getAnonProfile()
268 $token = $_SESSION['anon_token'];
269 $anon = base64_decode($token);
273 if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
274 $parts = explode('-', $anon);
276 // Do Profile lookup by ID instead of nickname for safety/performance
277 $profile = Profile::staticGet('id', $id);
279 $profile = AnonymousFavePlugin::createAnonProfile();
280 // Obfuscate so it's hard to figure out the Profile ID
281 $_SESSION['anon_token'] = base64_encode($profile->nickname);
288 * Determine whether a given NoticeListItem should have the
289 * anonymous fave/disfave form
291 * @param NoticeListItem $item
293 * @return boolean false if the profile associated with the notice is
294 * in the list of restricted profiles, otherwise
297 function hasAnonFaving($item)
299 $profile = Profile::staticGet('id', $item->notice->profile_id);
300 if (in_array($profile->nickname, $this->restricted)) {
308 * Provide plugin version information.
310 * This data is used when showing the version page.
312 * @param array &$versions array of version data arrays; see EVENTS.txt
314 * @return boolean hook value
316 function onPluginVersion(&$versions)
318 $url = 'http://status.net/wiki/Plugin:AnonymousFave';
320 $versions[] = array('name' => 'AnonymousFave',
321 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
322 'author' => 'Zach Copley',
325 // TRANS: Plugin description.
326 _m('Allow anonymous users to favorite notices.'));