ModLogPlugin.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 VIEWMODLOG = 'ModLogPlugin::VIEWMODLOG';
  50. /**
  51. * Database schema setup
  52. *
  53. * We keep a moderation log table
  54. *
  55. * @see Schema
  56. * @see ColumnDef
  57. *
  58. * @return boolean hook value; true means continue processing, false means stop.
  59. */
  60. function onCheckSchema()
  61. {
  62. $schema = Schema::get();
  63. $schema->ensureTable('mod_log', ModLog::schemaDef());
  64. return true;
  65. }
  66. function onEndGrantRole($profile, $role)
  67. {
  68. $modlog = new ModLog();
  69. $modlog->id = UUID::gen();
  70. $modlog->profile_id = $profile->id;
  71. $cur = common_current_user();
  72. if (!empty($cur)) {
  73. $modlog->moderator_id = $cur->id;
  74. }
  75. $modlog->role = $role;
  76. $modlog->is_grant = 1;
  77. $modlog->created = common_sql_now();
  78. $modlog->insert();
  79. return true;
  80. }
  81. function onEndRevokeRole($profile, $role)
  82. {
  83. $modlog = new ModLog();
  84. $modlog->id = UUID::gen();
  85. $modlog->profile_id = $profile->id;
  86. $scoped = Profile::current();
  87. if ($scoped instanceof Profile) {
  88. $modlog->moderator_id = $scoped->getID();
  89. }
  90. $modlog->role = $role;
  91. $modlog->is_grant = 0;
  92. $modlog->created = common_sql_now();
  93. $modlog->insert();
  94. return true;
  95. }
  96. function onEndShowSections(Action $action)
  97. {
  98. if (!$action instanceof ShowstreamAction) {
  99. // early return for actions we're not interested in
  100. return true;
  101. }
  102. $scoped = $action->getScoped();
  103. if (!$scoped instanceof Profile || !$scoped->hasRight(self::VIEWMODLOG)) {
  104. // only continue if we are allowed to VIEWMODLOG
  105. return true;
  106. }
  107. $profile = $action->getTarget();
  108. $ml = new ModLog();
  109. $ml->profile_id = $profile->getID();
  110. $ml->orderBy("created");
  111. $cnt = $ml->find();
  112. if ($cnt > 0) {
  113. $action->elementStart('div', array('id' => 'entity_mod_log',
  114. 'class' => 'section'));
  115. $action->element('h2', null, _('Moderation'));
  116. $action->elementStart('table');
  117. while ($ml->fetch()) {
  118. $action->elementStart('tr');
  119. $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created)));
  120. $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
  121. $action->elementStart('td');
  122. if ($ml->moderator_id) {
  123. $mod = Profile::getByID($ml->moderator_id);
  124. if (empty($mod)) {
  125. $action->text(_('[unknown]'));
  126. } else {
  127. $action->element('a', array('href' => $mod->getUrl(),
  128. 'title' => $mod->getFullname()),
  129. $mod->getNickname());
  130. }
  131. } else {
  132. $action->text(_('[unknown]'));
  133. }
  134. $action->elementEnd('td');
  135. $action->elementEnd('tr');
  136. }
  137. $action->elementEnd('table');
  138. $action->elementEnd('div');
  139. }
  140. }
  141. function onUserRightsCheck($profile, $right, &$result) {
  142. switch ($right) {
  143. case self::VIEWMODLOG:
  144. $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
  145. return false;
  146. default:
  147. return true;
  148. }
  149. }
  150. function onPluginVersion(array &$versions)
  151. {
  152. $versions[] = array('name' => 'ModLog',
  153. 'version' => GNUSOCIAL_VERSION,
  154. 'author' => 'Evan Prodromou',
  155. 'homepage' => 'http://status.net/wiki/Plugin:ModLog',
  156. 'description' =>
  157. _m('Show the moderation history for a profile in the sidebar'));
  158. return true;
  159. }
  160. }