UserFlagPlugin.php 7.6 KB

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