SubscriptionThrottlePlugin.php 5.3 KB

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