]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/AnonymousFave/AnonymousFavePlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[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(array &$args) {
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 $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 $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 onStartInitializeRouter($m)
104     {
105         $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
106         $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
107
108         return true;
109     }
110
111     function onStartShowNoticeOptions($item)
112     {
113         if (!common_logged_in()) {
114             $item->out->elementStart('div', 'notice-options');
115             $item->showFaveForm();
116             $item->out->elementEnd('div');
117         }
118
119         return true;
120     }
121
122     function onStartShowFaveForm($item)
123     {
124         if (!common_logged_in() && $this->hasAnonFaving($item)) {
125
126             $profile = AnonymousFavePlugin::getAnonProfile();
127             if ($profile instanceof Profile) {
128                 if (Fave::existsForProfile($item->notice, $profile)) {
129                     $disfavor = new AnonDisFavorForm($item->out, $item->notice);
130                     $disfavor->show();
131                 } else {
132                     $favor = new AnonFavorForm($item->out, $item->notice);
133                     $favor->show();
134                 }
135             }
136         }
137
138         return true;
139     }
140
141     function onEndFavorNoticeForm($form, $notice)
142     {
143         $this->showTally($form->out, $notice);
144     }
145
146     function onEndDisFavorNoticeForm($form, $notice)
147     {
148         $this->showTally($form->out, $notice);
149     }
150
151     function showTally($out, $notice)
152     {
153         $tally = Fave_tally::ensureTally($notice->id);
154
155         if (!empty($tally)) {
156             $out->elementStart(
157                 'div',
158                 array(
159                     'id' => 'notice-' . $notice->id . '-tally',
160                     'class' => 'notice-tally'
161                 )
162             );
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');
171         }
172     }
173
174     function onEndFavorNotice($profile, $notice)
175     {
176         $tally = Fave_tally::increment($notice->id);
177     }
178
179     function onEndDisfavorNotice($profile, $notice)
180     {
181         $tally = Fave_tally::decrement($notice->id);
182     }
183
184     static function createAnonProfile()
185     {
186         // Get the anon user's IP, and turn it into a nickname
187         list($proxy, $ip) = common_client_ip();
188
189         // IP + time + random number should help to avoid collisions
190         $baseNickname = $ip . '-' . time() . '-' . common_random_hexstr(5);
191
192         $profile = new Profile();
193         $profile->nickname = $baseNickname;
194         $id = $profile->insert();
195
196         if (!$id) {
197             // TRANS: Server exception.
198             throw new ServerException(_m("Could not create anonymous user session."));
199         }
200
201         // Stick the Profile ID into the nickname
202         $orig = clone($profile);
203
204         $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
205         $result = $profile->update($orig);
206
207         if (!$result) {
208             // TRANS: Server exception.
209             throw new ServerException(_m("Could not create anonymous user session."));
210         }
211
212         common_log(
213             LOG_INFO,
214             "AnonymousFavePlugin - created profile for anonymous user from IP: "
215             . $ip
216             . ', nickname = '
217             . $profile->nickname
218         );
219
220         return $profile;
221     }
222
223     static function getAnonProfile()
224     {
225
226         $token = $_SESSION['anon_token'];
227         $anon = base64_decode($token);
228
229         $profile = null;
230
231         if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
232             $parts = explode('-', $anon);
233             $id = $parts[1];
234             // Do Profile lookup by ID instead of nickname for safety/performance
235             $profile = Profile::getKV('id', $id);
236         } else {
237             $profile = AnonymousFavePlugin::createAnonProfile();
238             // Obfuscate so it's hard to figure out the Profile ID
239             $_SESSION['anon_token'] = base64_encode($profile->nickname);
240         }
241
242         return $profile;
243     }
244
245     /**
246      * Determine whether a given NoticeListItem should have the
247      * anonymous fave/disfave form
248      *
249      * @param NoticeListItem $item
250      *
251      * @return boolean false if the profile associated with the notice is
252      *                       in the list of restricted profiles, otherwise
253      *                       return true
254      */
255     function hasAnonFaving($item)
256     {
257         $profile = Profile::getKV('id', $item->notice->profile_id);
258         if (in_array($profile->nickname, $this->restricted)) {
259             return false;
260         }
261
262         return true;
263     }
264
265     /**
266      * Provide plugin version information.
267      *
268      * This data is used when showing the version page.
269      *
270      * @param array &$versions array of version data arrays; see EVENTS.txt
271      *
272      * @return boolean hook value
273      */
274     function onPluginVersion(array &$versions)
275     {
276         $url = 'http://status.net/wiki/Plugin:AnonymousFave';
277
278         $versions[] = array('name' => 'AnonymousFave',
279             'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
280             'author' => 'Zach Copley',
281             'homepage' => $url,
282             'rawdescription' =>
283             // TRANS: Plugin description.
284             _m('Allow anonymous users to favorite notices.'));
285
286         return true;
287     }
288 }