]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SubscriptionThrottle/SubscriptionThrottlePlugin.php
Opps, PEAR sucks. Need to call find() before fetch() ... :-(
[quix0rs-gnu-social.git] / plugins / SubscriptionThrottle / 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 class SubscriptionThrottlePlugin extends Plugin
48 {
49     public $subLimits = array(86400 => 100,
50                               3600 => 50);
51
52     public $groupLimits = array(86400 => 50,
53                                 3600 => 25);
54
55     /**
56      * Filter subscriptions to see if they're coming too fast.
57      *
58      * @param Profile $profile  The profile subscribing
59      * @param Profile $other    The profile being subscribed to
60      *
61      * @return boolean hook value
62      */
63     function onStartSubscribe(Profile $profile, $other)
64     {
65         foreach ($this->subLimits as $seconds => $limit) {
66             $sub = $this->_getNthSub($profile, $limit);
67
68             if (!empty($sub)) {
69                 $subtime = strtotime($sub->created);
70                 $now     = time();
71                 if ($now - $subtime < $seconds) {
72                     // TRANS: Exception thrown when subscribing too quickly.
73                     throw new Exception(_m('Too many subscriptions. Take a break and try again later.'));
74                 }
75             }
76         }
77
78         return true;
79     }
80
81     /**
82      * Filter group joins to see if they're coming too fast.
83      *
84      * @param Group   $group   The group being joined
85      * @param Profile $profile The profile joining
86      *
87      * @return boolean hook value
88      */
89     function onStartJoinGroup($group, $profile)
90     {
91         foreach ($this->groupLimits as $seconds => $limit) {
92             $mem = $this->_getNthMem($profile, $limit);
93             if (!empty($mem)) {
94
95                 $jointime = strtotime($mem->created);
96                 $now      = time();
97                 if ($now - $jointime < $seconds) {
98                     // TRANS: Exception thrown when joing groups too quickly.
99                     throw new Exception(_m('Too many memberships. Take a break and try again later.'));
100                 }
101             }
102         }
103
104         return true;
105     }
106
107     /**
108      * Get the Nth most recent subscription for this profile
109      *
110      * @param Profile $profile profile to get subscriptions for
111      * @param integer $n       How far to count back
112      *
113      * @return Subscription a subscription or null
114      */
115     private function _getNthSub(Profile $profile, $n)
116     {
117         $sub = new Subscription();
118
119         $sub->subscriber = $profile->id;
120         $sub->orderBy('created DESC');
121         $sub->limit($n - 1, 1);
122
123         if ($sub->find(true)) {
124             return $sub;
125         } else {
126             return null;
127         }
128     }
129
130     /**
131      * Get the Nth most recent group membership for this profile
132      *
133      * @param Profile $profile The user to get memberships for
134      * @param integer $n       How far to count back
135      *
136      * @return Group_member a membership or null
137      */
138     private function _getNthMem(Profile $profile, $n)
139     {
140         $mem = new Group_member();
141
142         $mem->profile_id = $profile->id;
143         $mem->orderBy('created DESC');
144         $mem->limit($n - 1, 1);
145
146         if ($mem->find(true)) {
147             return $mem;
148         } else {
149             return null;
150         }
151     }
152
153     /**
154      * Return plugin version data for display
155      *
156      * @param array &$versions Array of version arrays
157      *
158      * @return boolean hook value
159      */
160     function onPluginVersion(array &$versions)
161     {
162         $versions[] = array('name' => 'SubscriptionThrottle',
163                             'version' => GNUSOCIAL_VERSION,
164                             'author' => 'Evan Prodromou',
165                             'homepage' => 'http://status.net/wiki/Plugin:SubscriptionThrottle',
166                             'rawdescription' =>
167                             // TRANS: Plugin description.
168                             _m('Configurable limits for subscriptions and group memberships.'));
169         return true;
170     }
171 }