SubscriptionThrottlePlugin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Subscription throttle
  37. *
  38. * @category Throttle
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class SubscriptionThrottlePlugin extends Plugin
  46. {
  47. public $subLimits = array(86400 => 100,
  48. 3600 => 50);
  49. public $groupLimits = array(86400 => 50,
  50. 3600 => 25);
  51. /**
  52. * Filter subscriptions to see if they're coming too fast.
  53. *
  54. * @param Profile $profile The profile subscribing
  55. * @param Profile $other The profile being subscribed to
  56. *
  57. * @return boolean hook value
  58. */
  59. function onStartSubscribe(Profile $profile, $other)
  60. {
  61. foreach ($this->subLimits as $seconds => $limit) {
  62. $sub = $this->_getNthSub($profile, $limit);
  63. if (!empty($sub)) {
  64. $subtime = strtotime($sub->created);
  65. $now = time();
  66. if ($now - $subtime < $seconds) {
  67. // TRANS: Exception thrown when subscribing too quickly.
  68. throw new Exception(_m('Too many subscriptions. Take a break and try again later.'));
  69. }
  70. }
  71. }
  72. return true;
  73. }
  74. /**
  75. * Filter group joins to see if they're coming too fast.
  76. *
  77. * @param Group $group The group being joined
  78. * @param Profile $profile The profile joining
  79. *
  80. * @return boolean hook value
  81. */
  82. function onStartJoinGroup($group, $profile)
  83. {
  84. foreach ($this->groupLimits as $seconds => $limit) {
  85. $mem = $this->_getNthMem($profile, $limit);
  86. if (!empty($mem)) {
  87. $jointime = strtotime($mem->created);
  88. $now = time();
  89. if ($now - $jointime < $seconds) {
  90. // TRANS: Exception thrown when joing groups too quickly.
  91. throw new Exception(_m('Too many memberships. Take a break and try again later.'));
  92. }
  93. }
  94. }
  95. return true;
  96. }
  97. /**
  98. * Get the Nth most recent subscription for this profile
  99. *
  100. * @param Profile $profile profile to get subscriptions for
  101. * @param integer $n How far to count back
  102. *
  103. * @return Subscription a subscription or null
  104. */
  105. private function _getNthSub(Profile $profile, $n)
  106. {
  107. $sub = new Subscription();
  108. $sub->subscriber = $profile->id;
  109. $sub->orderBy('created DESC');
  110. $sub->limit($n - 1, 1);
  111. if ($sub->find(true)) {
  112. return $sub;
  113. } else {
  114. return null;
  115. }
  116. }
  117. /**
  118. * Get the Nth most recent group membership for this profile
  119. *
  120. * @param Profile $profile The user to get memberships for
  121. * @param integer $n How far to count back
  122. *
  123. * @return Group_member a membership or null
  124. */
  125. private function _getNthMem(Profile $profile, $n)
  126. {
  127. $mem = new Group_member();
  128. $mem->profile_id = $profile->id;
  129. $mem->orderBy('created DESC');
  130. $mem->limit($n - 1, 1);
  131. if ($mem->find(true)) {
  132. return $mem;
  133. } else {
  134. return null;
  135. }
  136. }
  137. /**
  138. * Return plugin version data for display
  139. *
  140. * @param array &$versions Array of version arrays
  141. *
  142. * @return boolean hook value
  143. */
  144. function onPluginVersion(array &$versions)
  145. {
  146. $versions[] = array('name' => 'SubscriptionThrottle',
  147. 'version' => GNUSOCIAL_VERSION,
  148. 'author' => 'Evan Prodromou',
  149. 'homepage' => 'http://status.net/wiki/Plugin:SubscriptionThrottle',
  150. 'rawdescription' =>
  151. // TRANS: Plugin description.
  152. _m('Configurable limits for subscriptions and group memberships.'));
  153. return true;
  154. }
  155. }