]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ModLog/ModLogPlugin.php
Merge branch 'subscribers_as_addressees' into 'nightly'
[quix0rs-gnu-social.git] / plugins / ModLog / ModLogPlugin.php
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
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Moderation logging
39  *
40  * Shows a history of moderation for this user in the sidebar
41  *
42  * @category  Moderation
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2012 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49
50 class ModLogPlugin extends Plugin
51 {
52     const VIEWMODLOG = 'ModLogPlugin::VIEWMODLOG';
53
54     /**
55      * Database schema setup
56      *
57      * We keep a moderation log table
58      *
59      * @see Schema
60      * @see ColumnDef
61      *
62      * @return boolean hook value; true means continue processing, false means stop.
63      */
64
65     function onCheckSchema()
66     {
67         $schema = Schema::get();
68
69         $schema->ensureTable('mod_log', ModLog::schemaDef());
70
71         return true;
72     }
73
74     function onEndGrantRole($profile, $role)
75     {
76         $modlog = new ModLog();
77
78         $modlog->id         = UUID::gen();
79         $modlog->profile_id = $profile->id;
80
81         $cur = common_current_user();
82         
83         if (!empty($cur)) {
84             $modlog->moderator_id = $cur->id;
85         }
86
87         $modlog->role     = $role;
88         $modlog->is_grant = 1;
89         $modlog->created  = common_sql_now();
90
91         $modlog->insert();
92
93         return true;
94     }
95
96     function onEndRevokeRole($profile, $role)
97     {
98         $modlog = new ModLog();
99
100         $modlog->id = UUID::gen();
101
102         $modlog->profile_id = $profile->id;
103
104         $cur = common_current_user();
105         
106         if (!empty($cur)) {
107             $modlog->moderator_id = $cur->id;
108         }
109
110         $modlog->role     = $role;
111         $modlog->is_grant = 0;
112         $modlog->created  = common_sql_now();
113
114         $modlog->insert();
115
116         return true;
117     }
118
119     function onEndShowSections(Action $action)
120     {
121         if ($action->arg('action') != 'showstream') {
122             return true;
123         }
124
125         $cur = common_current_user();
126
127         if (empty($cur) || !$cur->hasRight(self::VIEWMODLOG)) {
128             return true;
129         }
130
131         $profile = $action->profile;
132
133         $ml = new ModLog();
134
135         $ml->profile_id = $profile->id;
136         $ml->orderBy("created");
137
138         $cnt = $ml->find();
139
140         if ($cnt > 0) {
141
142             $action->elementStart('div', array('id' => 'entity_mod_log',
143                                                'class' => 'section'));
144
145             $action->element('h2', null, _('Moderation'));
146
147             $action->elementStart('table');
148
149             while ($ml->fetch()) {
150                 $action->elementStart('tr');
151                 $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created)));
152                 $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
153                 $action->elementStart('td');
154                 if ($ml->moderator_id) {
155                     $mod = Profile::getKV('id', $ml->moderator_id);
156                     if (empty($mod)) {
157                         $action->text(_('[unknown]'));
158                     } else {
159                         $action->element('a', array('href' => $mod->profileurl,
160                                                     'title' => $mod->fullname),
161                                          $mod->nickname);
162                     }
163                 } else {
164                     $action->text(_('[unknown]'));
165                 }
166                 $action->elementEnd('td');
167                 $action->elementEnd('tr');
168             }
169
170             $action->elementEnd('table');
171
172             $action->elementEnd('div');
173         }
174     }
175
176     function onUserRightsCheck($profile, $right, &$result) {
177         switch ($right) {
178         case self::VIEWMODLOG:
179             $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
180             return false;
181         default:
182             return true;
183         }
184     }
185
186     function onPluginVersion(array &$versions)
187     {
188         $versions[] = array('name' => 'ModLog',
189                             'version' => GNUSOCIAL_VERSION,
190                             'author' => 'Evan Prodromou',
191                             'homepage' => 'http://status.net/wiki/Plugin:ModLog',
192                             'description' =>
193                             _m('Show the moderation history for a profile in the sidebar'));
194         return true;
195     }
196 }