UserFlagPlugin.php 7.3 KB

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