ModLogPlugin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2012, StatusNet, Inc.
  5. *
  6. * ModLogPlugin.php
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Moderation
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2012 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Moderation logging
  37. *
  38. * Shows a history of moderation for this user in the sidebar
  39. *
  40. * @category Moderation
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @copyright 2012 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. * @link http://status.net/
  46. */
  47. class ModLogPlugin extends Plugin
  48. {
  49. const PLUGIN_VERSION = '2.0.0';
  50. const VIEWMODLOG = 'ModLogPlugin::VIEWMODLOG';
  51. /**
  52. * Database schema setup
  53. *
  54. * We keep a moderation log table
  55. *
  56. * @see Schema
  57. * @see ColumnDef
  58. *
  59. * @return boolean hook value; true means continue processing, false means stop.
  60. */
  61. function onCheckSchema()
  62. {
  63. $schema = Schema::get();
  64. $schema->ensureTable('mod_log', ModLog::schemaDef());
  65. return true;
  66. }
  67. function onEndGrantRole($profile, $role)
  68. {
  69. $modlog = new ModLog();
  70. $modlog->id = UUID::gen();
  71. $modlog->profile_id = $profile->id;
  72. $cur = common_current_user();
  73. if (!empty($cur)) {
  74. $modlog->moderator_id = $cur->id;
  75. }
  76. $modlog->role = $role;
  77. $modlog->is_grant = 1;
  78. $modlog->created = common_sql_now();
  79. $modlog->insert();
  80. return true;
  81. }
  82. function onEndRevokeRole($profile, $role)
  83. {
  84. $modlog = new ModLog();
  85. $modlog->id = UUID::gen();
  86. $modlog->profile_id = $profile->id;
  87. $scoped = Profile::current();
  88. if ($scoped instanceof Profile) {
  89. $modlog->moderator_id = $scoped->getID();
  90. }
  91. $modlog->role = $role;
  92. $modlog->is_grant = 0;
  93. $modlog->created = common_sql_now();
  94. $modlog->insert();
  95. return true;
  96. }
  97. function onEndShowSections(Action $action)
  98. {
  99. if (!$action instanceof ShowstreamAction) {
  100. // early return for actions we're not interested in
  101. return true;
  102. }
  103. $scoped = $action->getScoped();
  104. if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) {
  105. // only continue if we are allowed to VIEWMODLOG
  106. return true;
  107. }
  108. $profile = $action->getTarget();
  109. $ml = new ModLog();
  110. $ml->profile_id = $profile->getID();
  111. $ml->orderBy("created");
  112. $cnt = $ml->find();
  113. if ($cnt > 0) {
  114. $action->elementStart('div', array('id' => 'entity_mod_log',
  115. 'class' => 'section'));
  116. $action->element('h2', null, _('Moderation'));
  117. $action->elementStart('table');
  118. while ($ml->fetch()) {
  119. $action->elementStart('tr');
  120. $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created)));
  121. $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
  122. $action->elementStart('td');
  123. if ($ml->moderator_id) {
  124. $mod = Profile::getByID($ml->moderator_id);
  125. if (empty($mod)) {
  126. $action->text(_('[unknown]'));
  127. } else {
  128. $action->element('a', array('href' => $mod->getUrl(),
  129. 'title' => $mod->getFullname()),
  130. $mod->getNickname());
  131. }
  132. } else {
  133. $action->text(_('[unknown]'));
  134. }
  135. $action->elementEnd('td');
  136. $action->elementEnd('tr');
  137. }
  138. $action->elementEnd('table');
  139. $action->elementEnd('div');
  140. }
  141. }
  142. function onUserRightsCheck($profile, $right, &$result) {
  143. switch ($right) {
  144. case self::VIEWMODLOG:
  145. $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
  146. return false;
  147. default:
  148. return true;
  149. }
  150. }
  151. function onPluginVersion(array &$versions)
  152. {
  153. $versions[] = array('name' => 'ModLog',
  154. 'version' => self::PLUGIN_VERSION,
  155. 'author' => 'Evan Prodromou',
  156. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ModLog',
  157. 'description' =>
  158. _m('Show the moderation history for a profile in the sidebar'));
  159. return true;
  160. }
  161. }