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