UserFlagPlugin.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Allows users to flag content and accounts as offensive/spam/whatever
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Allows users to flag content and accounts as offensive/spam/whatever
  34. *
  35. * @category Plugin
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://status.net/
  40. */
  41. class UserFlagPlugin extends Plugin
  42. {
  43. const PLUGIN_VERSION = '2.0.0';
  44. const REVIEWFLAGS = 'UserFlagPlugin::reviewflags';
  45. const CLEARFLAGS = 'UserFlagPlugin::clearflags';
  46. public $flagOnBlock = true;
  47. /**
  48. * Hook for ensuring our tables are created
  49. *
  50. * Ensures that the user_flag_profile table exists
  51. * and has the right columns.
  52. *
  53. * @return boolean hook return
  54. */
  55. function onCheckSchema()
  56. {
  57. $schema = Schema::get();
  58. // For storing user-submitted flags on profiles
  59. $schema->ensureTable('user_flag_profile', User_flag_profile::schemaDef());
  60. return true;
  61. }
  62. /**
  63. * Add our actions to the URL router
  64. *
  65. * @param URLMapper $m URL mapper for this hit
  66. *
  67. * @return boolean hook return
  68. */
  69. public function onRouterInitialized(URLMapper $m)
  70. {
  71. $m->connect('main/flag/profile', ['action' => 'flagprofile']);
  72. $m->connect('main/flag/clear', ['action' => 'clearflag']);
  73. $m->connect('panel/profile/flag', ['action' => 'adminprofileflag']);
  74. return true;
  75. }
  76. /**
  77. * Add a 'flag' button to profile page
  78. *
  79. * @param Action $action The action being called
  80. * @param Profile $profile Profile being shown
  81. *
  82. * @return boolean hook result
  83. */
  84. function onEndProfilePageActionsElements($action, $profile)
  85. {
  86. $this->showFlagButton($action, $profile,
  87. array('action' => 'showstream',
  88. 'nickname' => $profile->nickname));
  89. return true;
  90. }
  91. /**
  92. * Add a 'flag' button to profiles in a list
  93. *
  94. * @param ProfileListItem $item item being shown
  95. *
  96. * @return boolean hook result
  97. */
  98. function onEndProfileListItemActionElements($item)
  99. {
  100. list($action, $args) = $item->action->returnToArgs();
  101. $args['action'] = $action;
  102. $this->showFlagButton($item->action, $item->profile, $args);
  103. return true;
  104. }
  105. /**
  106. * Actually output a flag button. If the target profile has already been
  107. * flagged by the current user, a null-action faux button is shown.
  108. *
  109. * @param Action $action
  110. * @param Profile $profile
  111. * @param array $returnToArgs
  112. */
  113. protected function showFlagButton($action, $profile, $returnToArgs)
  114. {
  115. $user = common_current_user();
  116. if (!empty($user) && ($user->id != $profile->id)) {
  117. $action->elementStart('li', 'entity_flag');
  118. if (User_flag_profile::exists($profile->id, $user->id)) {
  119. // @todo FIXME: Add a title explaining what 'flagged' means?
  120. // TRANS: Message added to a profile if it has been flagged for review.
  121. $action->element('p', 'flagged', _m('Flagged'));
  122. } else {
  123. $form = new FlagProfileForm($action, $profile, $returnToArgs);
  124. $form->show();
  125. }
  126. $action->elementEnd('li');
  127. }
  128. }
  129. /**
  130. * Check whether a user has one of our defined rights
  131. *
  132. * We define extra rights; this function checks to see if a
  133. * user has one of them.
  134. *
  135. * @param User $user User being checked
  136. * @param string $right Right we're checking
  137. * @param boolean &$result out, result of the check
  138. *
  139. * @return boolean hook result
  140. */
  141. function onUserRightsCheck($user, $right, &$result)
  142. {
  143. switch ($right) {
  144. case self::REVIEWFLAGS:
  145. case self::CLEARFLAGS:
  146. $result = $user->hasRole('moderator');
  147. return false; // done processing!
  148. }
  149. return true; // unchanged!
  150. }
  151. /**
  152. * Optionally flag profile when a block happens
  153. *
  154. * We optionally add a flag when a profile has been blocked
  155. *
  156. * @param User $user User doing the block
  157. * @param Profile $profile Profile being blocked
  158. *
  159. * @return boolean hook result
  160. */
  161. function onEndBlockProfile($user, $profile)
  162. {
  163. if ($this->flagOnBlock && !User_flag_profile::exists($profile->id,
  164. $user->id)) {
  165. User_flag_profile::create($user->id, $profile->id);
  166. }
  167. return true;
  168. }
  169. /**
  170. * Ensure that flag entries for a profile are deleted
  171. * along with the profile when deleting users.
  172. * This prevents breakage of the admin profile flag UI.
  173. *
  174. * @param Profile $profile
  175. * @param array &$related list of related tables; entries
  176. * with matching profile_id will be deleted.
  177. *
  178. * @return boolean hook result
  179. */
  180. function onProfileDeleteRelated($profile, &$related)
  181. {
  182. $related[] = 'user_flag_profile';
  183. return true;
  184. }
  185. /**
  186. * Ensure that flag entries created by a user are deleted
  187. * when that user gets deleted.
  188. *
  189. * @param User $user
  190. * @param array &$related list of related tables; entries
  191. * with matching user_id will be deleted.
  192. *
  193. * @return boolean hook result
  194. */
  195. function onUserDeleteRelated($user, &$related)
  196. {
  197. $related[] = 'user_flag_profile';
  198. return true;
  199. }
  200. /**
  201. * Provide plugin version information.
  202. *
  203. * This data is used when showing the version page.
  204. *
  205. * @param array &$versions array of version data arrays; see EVENTS.txt
  206. *
  207. * @return boolean hook value
  208. */
  209. public function onPluginVersion(array &$versions): bool
  210. {
  211. $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/UserFlag';
  212. $versions[] = array('name' => 'UserFlag',
  213. 'version' => self::PLUGIN_VERSION,
  214. 'author' => 'Evan Prodromou',
  215. 'homepage' => $url,
  216. 'rawdescription' =>
  217. // TRANS: Plugin description.
  218. _m('This plugin allows flagging of profiles for review and reviewing flagged profiles.'));
  219. return true;
  220. }
  221. }