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();
59 * Hook for ensuring our tables are created
61 * Ensures the fave_tally table is there and has the right columns
63 * @return boolean hook return
66 function onCheckSchema()
68 $schema = Schema::get();
70 // For storing total number of times a notice has been faved
72 $schema->ensureTable('fave_tally',
74 new ColumnDef('notice_id', 'integer', null, false, 'PRI'),
75 new ColumnDef('count', 'integer', null, false),
83 'on update CURRENT_TIMESTAMP'
91 function onEndShowHTML($action)
93 if (!common_logged_in()) {
94 // Set a place to return to when submitting forms
95 common_set_returnto($action->selfUrl());
99 function onEndShowScripts($action)
101 // Setup ajax calls for favoriting. Usually this is only done when
102 // a user is logged in.
103 $action->inlineScript('SN.U.NoticeFavor();');
106 function onAutoload($cls)
108 $dir = dirname(__FILE__);
112 include_once $dir . '/' . $cls . '.php';
114 case 'AnonFavorAction':
115 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
117 case 'AnonDisFavorAction':
118 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
120 case 'AnonFavorForm':
121 include_once $dir . '/anonfavorform.php';
123 case 'AnonDisFavorForm':
124 include_once $dir . '/anondisfavorform.php';
131 function onStartInitializeRouter($m) {
133 $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
134 $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
139 function onStartShowNoticeOptions($item) {
141 if (!common_logged_in()) {
142 $item->out->elementStart('div', 'notice-options');
143 $item->showFaveForm();
144 $item->out->elementEnd('div');
150 function onStartShowFaveForm($item) {
152 if (!common_logged_in()) {
154 $profile = $this->getAnonProfile();
155 if (!empty($profile)) {
156 if ($profile->hasFave($item->notice)) {
157 $disfavor = new AnonDisFavorForm($item->out, $item->notice);
160 $favor = new AnonFavorForm($item->out, $item->notice);
169 function createAnonProfile() {
171 // Get the anon user's IP, and turn it into a nickname
172 list($proxy, $ip) = common_client_ip();
173 // IP + time + random number should avoid collisions
174 $nickname = 'anonymous-' . $ip . '-' . time() . '-' . common_good_rand(5);
176 $profile = new Profile();
177 $profile->nickname = $nickname;
178 $id = $profile->insert();
183 "AnonymousFavePlugin - created profile for anonymous user from IP: "
193 function getAnonProfile() {
195 $anon = $_SESSION['anon_nickname'];
200 $profile = Profile::staticGet('nickname', $anon);
202 $profile = $this->createAnonProfile();
203 $_SESSION['anon_nickname'] = $profile->nickname;
206 if (!empty($profile)) {
212 * Provide plugin version information.
214 * This data is used when showing the version page.
216 * @param array &$versions array of version data arrays; see EVENTS.txt
218 * @return boolean hook value
220 function onPluginVersion(&$versions)
222 $url = 'http://status.net/wiki/Plugin:AnonymousFave';
224 $versions[] = array('name' => 'AnonymousFave',
225 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
226 'author' => 'Zach Copley',
229 _m('Allow anonymous users to favorite notices.'));