ModLogPlugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $cur = common_current_user();
  87. if (!empty($cur)) {
  88. $modlog->moderator_id = $cur->id;
  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->arg('action') != 'showstream') {
  99. return true;
  100. }
  101. $cur = common_current_user();
  102. if (empty($cur) || !$cur->hasRight(self::VIEWMODLOG)) {
  103. return true;
  104. }
  105. $profile = $action->profile;
  106. $ml = new ModLog();
  107. $ml->profile_id = $profile->id;
  108. $ml->orderBy("created");
  109. $cnt = $ml->find();
  110. if ($cnt > 0) {
  111. $action->elementStart('div', array('id' => 'entity_mod_log',
  112. 'class' => 'section'));
  113. $action->element('h2', null, _('Moderation'));
  114. $action->elementStart('table');
  115. while ($ml->fetch()) {
  116. $action->elementStart('tr');
  117. $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created)));
  118. $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
  119. $action->elementStart('td');
  120. if ($ml->moderator_id) {
  121. $mod = Profile::getKV('id', $ml->moderator_id);
  122. if (empty($mod)) {
  123. $action->text(_('[unknown]'));
  124. } else {
  125. $action->element('a', array('href' => $mod->profileurl,
  126. 'title' => $mod->fullname),
  127. $mod->nickname);
  128. }
  129. } else {
  130. $action->text(_('[unknown]'));
  131. }
  132. $action->elementEnd('td');
  133. $action->elementEnd('tr');
  134. }
  135. $action->elementEnd('table');
  136. $action->elementEnd('div');
  137. }
  138. }
  139. function onUserRightsCheck($profile, $right, &$result) {
  140. switch ($right) {
  141. case self::VIEWMODLOG:
  142. $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
  143. return false;
  144. default:
  145. return true;
  146. }
  147. }
  148. function onPluginVersion(&$versions)
  149. {
  150. $versions[] = array('name' => 'ModLog',
  151. 'version' => GNUSOCIAL_VERSION,
  152. 'author' => 'Evan Prodromou',
  153. 'homepage' => 'http://status.net/wiki/Plugin:ModLog',
  154. 'description' =>
  155. _m('Show the moderation history for a profile in the sidebar'));
  156. return true;
  157. }
  158. }