]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubscriptionThrottlePlugin.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / SubscriptionThrottlePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Plugin to throttle subscriptions by a user
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Throttle
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Subscription throttle
39  *
40  * @category  Throttle
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2010 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47
48 class SubscriptionThrottlePlugin extends Plugin
49 {
50     public $subLimits = array(86400 => 100,
51                               3600 => 50);
52
53     public $groupLimits = array(86400 => 50,
54                                 3600 => 25);
55
56     /**
57      * Filter subscriptions to see if they're coming too fast.
58      *
59      * @param User $user  The user subscribing
60      * @param User $other The user being subscribed to
61      *
62      * @return boolean hook value
63      */
64
65     function onStartSubscribe($user, $other)
66     {
67         foreach ($this->subLimits as $seconds => $limit) {
68             $sub = $this->_getNthSub($user, $limit);
69
70             if (!empty($sub)) {
71                 $subtime = strtotime($sub->created);
72                 $now     = time();
73                 if ($now - $subtime < $seconds) {
74                     throw new Exception(_("Too many subscriptions. Take a break and try again later."));
75                 }
76             }
77         }
78
79         return true;
80     }
81
82     /**
83      * Filter group joins to see if they're coming too fast.
84      *
85      * @param Group $group The group being joined
86      * @param User  $user  The user joining
87      *
88      * @return boolean hook value
89      */
90
91     function onStartJoinGroup($group, $user)
92     {
93         foreach ($this->groupLimits as $seconds => $limit) {
94             $mem = $this->_getNthMem($user, $limit);
95             if (!empty($mem)) {
96
97                 $jointime = strtotime($mem->created);
98                 $now      = time();
99                 if ($now - $jointime < $seconds) {
100                     throw new Exception(_("Too many memberships. Take a break and try again later."));
101                 }
102             }
103         }
104
105         return true;
106     }
107
108     /**
109      * Get the Nth most recent subscription for this user
110      *
111      * @param User    $user The user to get subscriptions for
112      * @param integer $n    How far to count back
113      *
114      * @return Subscription a subscription or null
115      */
116
117     private function _getNthSub($user, $n)
118     {
119         $sub = new Subscription();
120
121         $sub->subscriber = $user->id;
122         $sub->orderBy('created DESC');
123         $sub->limit($n - 1, 1);
124
125         if ($sub->find(true)) {
126             return $sub;
127         } else {
128             return null;
129         }
130     }
131
132     /**
133      * Get the Nth most recent group membership for this user
134      *
135      * @param User    $user The user to get memberships for
136      * @param integer $n    How far to count back
137      *
138      * @return Group_member a membership or null
139      */
140
141     private function _getNthMem($user, $n)
142     {
143         $mem = new Group_member();
144
145         $mem->profile_id = $user->id;
146         $mem->orderBy('created DESC');
147         $mem->limit($n - 1, 1);
148
149         if ($mem->find(true)) {
150             return $mem;
151         } else {
152             return null;
153         }
154     }
155
156     /**
157      * Return plugin version data for display
158      *
159      * @param array &$versions Array of version arrays
160      *
161      * @return boolean hook value
162      */
163
164     function onPluginVersion(&$versions)
165     {
166         $versions[] = array('name' => 'SubscriptionThrottle',
167                             'version' => STATUSNET_VERSION,
168                             'author' => 'Evan Prodromou',
169                             'homepage' => 'http://status.net/wiki/Plugin:SubscriptionThrottle',
170                             'rawdescription' =>
171                             _m('Configurable limits for subscriptions and group memberships.'));
172         return true;
173     }
174 }
175