SubscriptionThrottlePlugin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Plugin to throttle subscriptions by a user
  18. *
  19. * @category Throttle
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Subscription throttle
  28. *
  29. * @category Throttle
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @copyright 2010 StatusNet, Inc.
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class SubscriptionThrottlePlugin extends Plugin
  36. {
  37. const PLUGIN_VERSION = '2.0.0';
  38. public $subLimits = [86400 => 100, 3600 => 50];
  39. public $groupLimits = [86400 => 50, 3600 => 25];
  40. /**
  41. * Filter subscriptions to see if they're coming too fast.
  42. *
  43. * @param Profile $profile The profile subscribing
  44. * @param Profile $other The profile being subscribed to
  45. *
  46. * @return boolean hook value
  47. */
  48. public function onStartSubscribe(Profile $profile, $other)
  49. {
  50. foreach ($this->subLimits as $seconds => $limit) {
  51. $sub = $this->_getNthSub($profile, $limit);
  52. if (!empty($sub)) {
  53. $subtime = strtotime($sub->created);
  54. $now = time();
  55. if ($now - $subtime < $seconds) {
  56. // TRANS: Exception thrown when subscribing too quickly.
  57. throw new Exception(_m('Too many subscriptions. Take a break and try again later.'));
  58. }
  59. }
  60. }
  61. return true;
  62. }
  63. /**
  64. * Filter group joins to see if they're coming too fast.
  65. *
  66. * @param Group $group The group being joined
  67. * @param Profile $profile The profile joining
  68. *
  69. * @return boolean hook value
  70. */
  71. public function onStartJoinGroup($group, $profile)
  72. {
  73. foreach ($this->groupLimits as $seconds => $limit) {
  74. $mem = $this->_getNthMem($profile, $limit);
  75. if (!empty($mem)) {
  76. $jointime = strtotime($mem->created);
  77. $now = time();
  78. if ($now - $jointime < $seconds) {
  79. // TRANS: Exception thrown when joing groups too quickly.
  80. throw new Exception(_m('Too many memberships. Take a break and try again later.'));
  81. }
  82. }
  83. }
  84. return true;
  85. }
  86. /**
  87. * Get the Nth most recent subscription for this profile
  88. *
  89. * @param Profile $profile profile to get subscriptions for
  90. * @param integer $n How far to count back
  91. *
  92. * @return Subscription a subscription or null
  93. */
  94. private function _getNthSub(Profile $profile, $n)
  95. {
  96. $sub = new Subscription();
  97. $sub->subscriber = $profile->id;
  98. $sub->orderBy('created DESC');
  99. $sub->limit($n - 1, 1);
  100. if ($sub->find(true)) {
  101. return $sub;
  102. } else {
  103. return null;
  104. }
  105. }
  106. /**
  107. * Get the Nth most recent group membership for this profile
  108. *
  109. * @param Profile $profile The user to get memberships for
  110. * @param integer $n How far to count back
  111. *
  112. * @return Group_member a membership or null
  113. */
  114. private function _getNthMem(Profile $profile, $n)
  115. {
  116. $mem = new Group_member();
  117. $mem->profile_id = $profile->id;
  118. $mem->orderBy('created DESC, group_id DESC');
  119. $mem->limit($n - 1, 1);
  120. if ($mem->find(true)) {
  121. return $mem;
  122. } else {
  123. return null;
  124. }
  125. }
  126. /**
  127. * Return plugin version data for display
  128. *
  129. * @param array &$versions Array of version arrays
  130. *
  131. * @return boolean hook value
  132. */
  133. public function onPluginVersion(array &$versions): bool
  134. {
  135. $versions[] = array('name' => 'SubscriptionThrottle',
  136. 'version' => self::PLUGIN_VERSION,
  137. 'author' => 'Evan Prodromou',
  138. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SubscriptionThrottle',
  139. 'rawdescription' =>
  140. // TRANS: Plugin description.
  141. _m('Configurable limits for subscriptions and group memberships.'));
  142. return true;
  143. }
  144. }