ModLogPlugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * @category Moderation
  18. * @package GNUsocial
  19. * @author Evan Prodromou <evan@status.net>
  20. * @copyright 2012 StatusNet, Inc.
  21. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  22. */
  23. defined('GNUSOCIAL') || die();
  24. /**
  25. * Moderation logging
  26. *
  27. * Shows a history of moderation for this user in the sidebar
  28. *
  29. * @copyright 2012 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class ModLogPlugin extends Plugin
  33. {
  34. const PLUGIN_VERSION = '2.0.0';
  35. const VIEWMODLOG = 'ModLogPlugin::VIEWMODLOG';
  36. /**
  37. * Database schema setup
  38. *
  39. * We keep a moderation log table
  40. *
  41. * @see Schema
  42. * @see ColumnDef
  43. *
  44. * @return boolean hook value; true means continue processing, false means stop.
  45. */
  46. public function onCheckSchema()
  47. {
  48. $schema = Schema::get();
  49. $schema->ensureTable('mod_log', ModLog::schemaDef());
  50. return true;
  51. }
  52. public function onEndGrantRole($profile, $role)
  53. {
  54. $modlog = new ModLog();
  55. $modlog->id = UUID::gen();
  56. $modlog->profile_id = $profile->id;
  57. $cur = common_current_user();
  58. if (!empty($cur)) {
  59. $modlog->moderator_id = $cur->id;
  60. }
  61. $modlog->role = $role;
  62. $modlog->is_grant = true;
  63. $modlog->created = common_sql_now();
  64. $modlog->insert();
  65. return true;
  66. }
  67. public function onEndRevokeRole($profile, $role)
  68. {
  69. $modlog = new ModLog();
  70. $modlog->id = UUID::gen();
  71. $modlog->profile_id = $profile->id;
  72. $scoped = Profile::current();
  73. if ($scoped instanceof Profile) {
  74. $modlog->moderator_id = $scoped->getID();
  75. }
  76. $modlog->role = $role;
  77. $modlog->is_grant = false;
  78. $modlog->created = common_sql_now();
  79. $modlog->insert();
  80. return true;
  81. }
  82. public function onEndShowSections(Action $action)
  83. {
  84. if (!$action instanceof ShowstreamAction) {
  85. // early return for actions we're not interested in
  86. return true;
  87. }
  88. $scoped = $action->getScoped();
  89. if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) {
  90. // only continue if we are allowed to VIEWMODLOG
  91. return true;
  92. }
  93. $profile = $action->getTarget();
  94. $ml = new ModLog();
  95. $ml->profile_id = $profile->getID();
  96. $ml->orderBy("created");
  97. $cnt = $ml->find();
  98. if ($cnt > 0) {
  99. $action->elementStart('div', array('id' => 'entity_mod_log',
  100. 'class' => 'section'));
  101. $action->element('h2', null, _('Moderation'));
  102. $action->elementStart('table');
  103. while ($ml->fetch()) {
  104. $action->elementStart('tr');
  105. $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created)));
  106. $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
  107. $action->elementStart('td');
  108. if ($ml->moderator_id) {
  109. $mod = Profile::getByID($ml->moderator_id);
  110. if (empty($mod)) {
  111. $action->text(_('[unknown]'));
  112. } else {
  113. $action->element(
  114. 'a',
  115. [
  116. 'href' => $mod->getUrl(),
  117. 'title' => $mod->getFullname(),
  118. ],
  119. $mod->getNickname()
  120. );
  121. }
  122. } else {
  123. $action->text(_('[unknown]'));
  124. }
  125. $action->elementEnd('td');
  126. $action->elementEnd('tr');
  127. }
  128. $action->elementEnd('table');
  129. $action->elementEnd('div');
  130. }
  131. }
  132. public function onUserRightsCheck($profile, $right, &$result)
  133. {
  134. switch ($right) {
  135. case self::VIEWMODLOG:
  136. $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
  137. return false;
  138. default:
  139. return true;
  140. }
  141. }
  142. public function onPluginVersion(array &$versions): bool
  143. {
  144. $versions[] = array('name' => 'ModLog',
  145. 'version' => self::PLUGIN_VERSION,
  146. 'author' => 'Evan Prodromou',
  147. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/ModLog',
  148. 'description' =>
  149. _m('Show the moderation history for a profile in the sidebar'));
  150. return true;
  151. }
  152. }