]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/AnonymousFavePlugin.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / AnonymousFave / AnonymousFavePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * A plugin to allow anonymous users to favorite notices
7  *
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.:
10  *
11  * addPlugin(
12  *     'AnonymousFave',
13  *     array('restricted' => array('spock', 'kirk', 'bones'))
14  * );
15  *
16  *
17  * PHP version 5
18  *
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.
23  *
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.
28  *
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/>.
31  *
32  * @category  Plugin
33  * @package   StatusNet
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/
38  */
39
40 if (!defined('STATUSNET')) {
41     // This check helps protect against security problems;
42     // your code file can't be executed directly from the web.
43     exit(1);
44 }
45
46 define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1');
47
48 /**
49  * Anonymous Fave plugin to allow anonymous (not logged in) users
50  * to favorite notices
51  *
52  * @category  Plugin
53  * @package   StatusNet
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/
58  */
59 class AnonymousFavePlugin extends Plugin
60 {
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();
64
65     function onArgsInitialize() {
66         // We always want a session because we're tracking anon users
67         common_ensure_session();
68     }
69
70     /**
71      * Hook for ensuring our tables are created
72      *
73      * Ensures the fave_tally table is there and has the right columns
74      *
75      * @return boolean hook return
76      */
77
78     function onCheckSchema()
79     {
80         $schema = Schema::get();
81
82         // For storing total number of times a notice has been faved
83         $schema->ensureTable('fave_tally', Fave_tally::schemaDef());
84
85         return true;
86     }
87
88     function onEndShowHTML($action)
89     {
90         if (!common_logged_in()) {
91             // Set a place to return to when submitting forms
92             common_set_returnto($action->selfUrl());
93         }
94     }
95
96     function onEndShowScripts($action)
97     {
98         // Setup ajax calls for favoriting. Usually this is only done when
99         // a user is logged in.
100         $action->inlineScript('SN.U.NoticeFavor();');
101     }
102
103     function onAutoload($cls)
104     {
105         $dir = dirname(__FILE__);
106
107         switch ($cls) {
108             case 'Fave_tally':
109                 include_once $dir . '/' . $cls . '.php';
110                 return false;
111             case 'AnonFavorAction':
112                 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
113                 return false;
114             case 'AnonDisFavorAction':
115                 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
116                 return false;
117             case 'AnonFavorForm':
118                 include_once $dir . '/anonfavorform.php';
119                 return false;
120             case 'AnonDisFavorForm':
121                 include_once $dir . '/anondisfavorform.php';
122                 return false;
123             default:
124                 return true;
125         }
126     }
127
128     function onStartInitializeRouter($m)
129     {
130         $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
131         $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
132
133         return true;
134     }
135
136     function onStartShowNoticeOptions($item)
137     {
138         if (!common_logged_in()) {
139             $item->out->elementStart('div', 'notice-options');
140             $item->showFaveForm();
141             $item->out->elementEnd('div');
142         }
143
144         return true;
145     }
146
147     function onStartShowFaveForm($item)
148     {
149         if (!common_logged_in() && $this->hasAnonFaving($item)) {
150
151             $profile = AnonymousFavePlugin::getAnonProfile();
152             if (!empty($profile)) {
153                 if ($profile->hasFave($item->notice)) {
154                     $disfavor = new AnonDisFavorForm($item->out, $item->notice);
155                     $disfavor->show();
156                 } else {
157                     $favor = new AnonFavorForm($item->out, $item->notice);
158                     $favor->show();
159                 }
160             }
161         }
162
163         return true;
164     }
165
166     function onEndFavorNoticeForm($form, $notice)
167     {
168         $this->showTally($form->out, $notice);
169     }
170
171     function onEndDisFavorNoticeForm($form, $notice)
172     {
173         $this->showTally($form->out, $notice);
174     }
175
176     function showTally($out, $notice)
177     {
178         $tally = Fave_tally::ensureTally($notice->id);
179
180         if (!empty($tally)) {
181             $out->elementStart(
182                 'div',
183                 array(
184                     'id' => 'notice-' . $notice->id . '-tally',
185                     'class' => 'notice-tally'
186                 )
187             );
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');
196         }
197     }
198
199     function onEndFavorNotice($profile, $notice)
200     {
201         $tally = Fave_tally::increment($notice->id);
202     }
203
204     function onEndDisfavorNotice($profile, $notice)
205     {
206         $tally = Fave_tally::decrement($notice->id);
207     }
208
209     static function createAnonProfile()
210     {
211         // Get the anon user's IP, and turn it into a nickname
212         list($proxy, $ip) = common_client_ip();
213
214         // IP + time + random number should help to avoid collisions
215         $baseNickname = $ip . '-' . time() . '-' . common_good_rand(5);
216
217         $profile = new Profile();
218         $profile->nickname = $baseNickname;
219         $id = $profile->insert();
220
221         if (!$id) {
222             // TRANS: Server exception.
223             throw new ServerException(_m("Could not create anonymous user session."));
224         }
225
226         // Stick the Profile ID into the nickname
227         $orig = clone($profile);
228
229         $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
230         $result = $profile->update($orig);
231
232         if (!$result) {
233             // TRANS: Server exception.
234             throw new ServerException(_m("Could not create anonymous user session."));
235         }
236
237         common_log(
238             LOG_INFO,
239             "AnonymousFavePlugin - created profile for anonymous user from IP: "
240             . $ip
241             . ', nickname = '
242             . $profile->nickname
243         );
244
245         return $profile;
246     }
247
248     static function getAnonProfile()
249     {
250
251         $token = $_SESSION['anon_token'];
252         $anon = base64_decode($token);
253
254         $profile = null;
255
256         if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
257             $parts = explode('-', $anon);
258             $id = $parts[1];
259             // Do Profile lookup by ID instead of nickname for safety/performance
260             $profile = Profile::getKV('id', $id);
261         } else {
262             $profile = AnonymousFavePlugin::createAnonProfile();
263             // Obfuscate so it's hard to figure out the Profile ID
264             $_SESSION['anon_token'] = base64_encode($profile->nickname);
265         }
266
267         return $profile;
268     }
269
270     /**
271      * Determine whether a given NoticeListItem should have the
272      * anonymous fave/disfave form
273      *
274      * @param NoticeListItem $item
275      *
276      * @return boolean false if the profile associated with the notice is
277      *                       in the list of restricted profiles, otherwise
278      *                       return true
279      */
280     function hasAnonFaving($item)
281     {
282         $profile = Profile::getKV('id', $item->notice->profile_id);
283         if (in_array($profile->nickname, $this->restricted)) {
284             return false;
285         }
286
287         return true;
288     }
289
290     /**
291      * Provide plugin version information.
292      *
293      * This data is used when showing the version page.
294      *
295      * @param array &$versions array of version data arrays; see EVENTS.txt
296      *
297      * @return boolean hook value
298      */
299     function onPluginVersion(&$versions)
300     {
301         $url = 'http://status.net/wiki/Plugin:AnonymousFave';
302
303         $versions[] = array('name' => 'AnonymousFave',
304             'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
305             'author' => 'Zach Copley',
306             'homepage' => $url,
307             'rawdescription' =>
308             // TRANS: Plugin description.
309             _m('Allow anonymous users to favorite notices.'));
310
311         return true;
312     }
313 }