]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ModLog/ModLogPlugin.php
Merge 1.1.x into master
[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     /**
75      * Load related modules when needed
76      *
77      * @param string $cls Name of the class to be loaded
78      *
79      * @return boolean hook value; true means continue processing, false means stop.
80      */
81
82     function onAutoload($cls)
83     {
84         $dir = dirname(__FILE__);
85
86         switch ($cls)
87         {
88         case 'ModLog':
89             include_once $dir . '/'.$cls.'.php';
90             return false;
91         default:
92             return true;
93         }
94     }
95
96     function onEndGrantRole($profile, $role)
97     {
98         $modlog = new ModLog();
99
100         $modlog->id         = UUID::gen();
101         $modlog->profile_id = $profile->id;
102
103         $cur = common_current_user();
104         
105         if (!empty($cur)) {
106             $modlog->moderator_id = $cur->id;
107         }
108
109         $modlog->role     = $role;
110         $modlog->is_grant = 1;
111         $modlog->created  = common_sql_now();
112
113         $modlog->insert();
114
115         return true;
116     }
117
118     function onEndRevokeRole($profile, $role)
119     {
120         $modlog = new ModLog();
121
122         $modlog->id = UUID::gen();
123
124         $modlog->profile_id = $profile->id;
125
126         $cur = common_current_user();
127         
128         if (!empty($cur)) {
129             $modlog->moderator_id = $cur->id;
130         }
131
132         $modlog->role     = $role;
133         $modlog->is_grant = 0;
134         $modlog->created  = common_sql_now();
135
136         $modlog->insert();
137
138         return true;
139     }
140
141     function onEndShowSections($action)
142     {
143         if ($action->arg('action') != 'showstream') {
144             return true;
145         }
146
147         $cur = common_current_user();
148
149         if (empty($cur) || !$cur->hasRight(self::VIEWMODLOG)) {
150             return true;
151         }
152
153         $profile = $action->profile;
154
155         $ml = new ModLog();
156
157         $ml->profile_id = $profile->id;
158         $ml->orderBy("created");
159
160         $cnt = $ml->find();
161
162         if ($cnt > 0) {
163
164             $action->elementStart('div', array('id' => 'entity_mod_log',
165                                                'class' => 'section'));
166
167             $action->element('h2', null, _('Moderation'));
168
169             $action->elementStart('table');
170
171             while ($ml->fetch()) {
172                 $action->elementStart('tr');
173                 $action->element('td', null, strftime('%y-%m-%d', strtotime($ml->created)));
174                 $action->element('td', null, sprintf(($ml->is_grant) ? _('+%s') : _('-%s'), $ml->role));
175                 $action->elementStart('td');
176                 if ($ml->moderator_id) {
177                     $mod = Profile::staticGet('id', $ml->moderator_id);
178                     if (empty($mod)) {
179                         $action->text(_('[unknown]'));
180                     } else {
181                         $action->element('a', array('href' => $mod->profileurl,
182                                                     'title' => $mod->fullname),
183                                          $mod->nickname);
184                     }
185                 } else {
186                     $action->text(_('[unknown]'));
187                 }
188                 $action->elementEnd('td');
189                 $action->elementEnd('tr');
190             }
191
192             $action->elementEnd('table');
193
194             $action->elementEnd('div');
195         }
196     }
197
198     function onUserRightsCheck($profile, $right, &$result) {
199         switch ($right) {
200         case self::VIEWMODLOG:
201             $result = ($profile->hasRole(Profile_role::MODERATOR) || $profile->hasRole('modhelper'));
202             return false;
203         default:
204             return true;
205         }
206     }
207
208     function onPluginVersion(&$versions)
209     {
210         $versions[] = array('name' => 'ModLog',
211                             'version' => STATUSNET_VERSION,
212                             'author' => 'Evan Prodromou',
213                             'homepage' => 'http://status.net/wiki/Plugin:ModLog',
214                             'description' =>
215                             _m('Show the moderation history for a profile in the sidebar'));
216         return true;
217     }
218 }