]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/UserFlag/adminprofileflag.php
ab1a86ac4e18f9f3741335f314654a681aeba5e8
[quix0rs-gnu-social.git] / plugins / UserFlag / adminprofileflag.php
1 <?php
2 /**
3  * Show latest and greatest profile flags
4  *
5  * PHP version 5
6  *
7  * @category Action
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Show the latest and greatest profile flags
36  *
37  * @category Action
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  */
43
44 class AdminprofileflagAction extends Action
45 {
46     var $page     = null;
47     var $profiles = null;
48
49     /**
50      * Take arguments for running
51      *
52      * @param array $args $_REQUEST args
53      *
54      * @return boolean success flag
55      */
56
57     function prepare($args)
58     {
59         parent::prepare($args);
60
61         $user = common_current_user();
62
63         // User must be logged in.
64
65         if (!common_logged_in()) {
66             $this->clientError(_('Not logged in.'));
67             return;
68         }
69
70         $user = common_current_user();
71
72         // ...because they're logged in
73
74         assert(!empty($user));
75
76         // It must be a "real" login, not saved cookie login
77
78         if (!common_is_real_login()) {
79             // Cookie theft is too easy; we require automatic
80             // logins to re-authenticate before admining the site
81             common_set_returnto($this->selfUrl());
82             if (Event::handle('RedirectToLogin', array($this, $user))) {
83                 common_redirect(common_local_url('login'), 303);
84             }
85         }
86
87         // User must have the right to review flags
88
89         if (!$user->hasRight(UserFlagPlugin::REVIEWFLAGS)) {
90             $this->clientError(_('You cannot review profile flags.'));
91             return false;
92         }
93
94         $this->page = $this->trimmed('page');
95
96         if (empty($this->page)) {
97             $this->page = 1;
98         }
99
100         $this->profiles = $this->getProfiles();
101
102         return true;
103     }
104
105     /**
106      * Handle request
107      *
108      * @param array $args $_REQUEST args; handled in prepare()
109      *
110      * @return void
111      */
112
113     function handle($args)
114     {
115         parent::handle($args);
116
117         $this->showPage();
118     }
119
120     function title() {
121         return _('Flagged profiles');
122     }
123
124     /**
125      * save the profile flag
126      *
127      * @return void
128      */
129
130     function showContent()
131     {
132         $pl = new FlaggedProfileList($this->profiles, $this);
133
134         $cnt = $pl->show();
135
136         $this->pagination($this->page > 1, $cnt > PROFILES_PER_PAGE,
137                           $this->page, 'adminprofileflag');
138     }
139
140     function getProfiles()
141     {
142         $ufp = new User_flag_profile();
143
144         $ufp->selectAdd();
145         $ufp->selectAdd('profile_id');
146         $ufp->selectAdd('count(*) as flag_count');
147
148         $ufp->whereAdd('cleared is NULL');
149
150         $ufp->groupBy('profile_id');
151         $ufp->orderBy('flag_count DESC, profile_id DESC');
152
153         $offset = ($this->page-1) * PROFILES_PER_PAGE;
154         $limit =  PROFILES_PER_PAGE + 1;
155
156         $ufp->limit($offset, $limit);
157
158         $profiles = array();
159
160         if ($ufp->find()) {
161             while ($ufp->fetch()) {
162                 $profile = Profile::staticGet('id', $ufp->profile_id);
163                 if (!empty($profile)) {
164                     $profiles[] = $profile;
165                 }
166             }
167         }
168
169         $ufp->free();
170
171         return new ArrayWrapper($profiles);
172     }
173 }
174
175 class FlaggedProfileList extends ProfileList {
176
177     function newListItem($profile)
178     {
179         return new FlaggedProfileListItem($this->profile, $this->action);
180     }
181 }
182
183 class FlaggedProfileListItem extends ProfileListItem
184 {
185     const MAX_FLAGGERS = 5;
186
187     var $user = null;
188     var $r2args = null;
189
190     function showActions()
191     {
192         $this->user = common_current_user();
193
194         list($action, $this->r2args) = $this->out->returnToArgs();
195
196         $this->r2args['action'] = $action;
197
198         $this->startActions();
199         if (Event::handle('StartProfileListItemActionElements', array($this))) {
200             $this->out->elementStart('li', 'entity_moderation');
201             $this->out->element('p', null, _('Moderate'));
202             $this->out->elementStart('ul');
203             $this->showSandboxButton();
204             $this->showSilenceButton();
205             $this->showDeleteButton();
206             $this->showClearButton();
207             $this->out->elementEnd('ul');
208             $this->out->elementEnd('li');
209             Event::handle('EndProfileListItemActionElements', array($this));
210         }
211         $this->endActions();
212     }
213
214     function showSandboxButton()
215     {
216         if ($this->user->hasRight(Right::SANDBOXUSER)) {
217             $this->out->elementStart('li', 'entity_sandbox');
218             if ($this->profile->isSandboxed()) {
219                 $usf = new UnSandboxForm($this->out, $this->profile, $this->r2args);
220                 $usf->show();
221             } else {
222                 $sf = new SandboxForm($this->out, $this->profile, $this->r2args);
223                 $sf->show();
224             }
225             $this->out->elementEnd('li');
226         }
227     }
228
229     function showSilenceButton()
230     {
231         if ($this->user->hasRight(Right::SILENCEUSER)) {
232             $this->out->elementStart('li', 'entity_silence');
233             if ($this->profile->isSilenced()) {
234                 $usf = new UnSilenceForm($this->out, $this->profile, $this->r2args);
235                 $usf->show();
236             } else {
237                 $sf = new SilenceForm($this->out, $this->profile, $this->r2args);
238                 $sf->show();
239             }
240             $this->out->elementEnd('li');
241         }
242     }
243
244     function showDeleteButton()
245     {
246
247         if ($this->user->hasRight(Right::DELETEUSER)) {
248             $this->out->elementStart('li', 'entity_delete');
249             $df = new DeleteUserForm($this->out, $this->profile, $this->r2args);
250             $df->show();
251             $this->out->elementEnd('li');
252         }
253     }
254
255     function showClearButton()
256     {
257         if ($this->user->hasRight(UserFlagPlugin::CLEARFLAGS)) {
258             $this->out->elementStart('li', 'entity_clear');
259             $cf = new ClearFlagForm($this->out, $this->profile, $this->r2args);
260             $cf->show();
261             $this->out->elementEnd('li');
262         }
263     }
264
265     function endProfile()
266     {
267         $this->showFlaggersList();
268         parent::endProfile();
269     }
270
271     function showFlaggersList()
272     {
273         $flaggers = array();
274
275         $ufp = new User_flag_profile();
276
277         $ufp->selectAdd();
278         $ufp->selectAdd('user_id');
279         $ufp->profile_id = $this->profile->id;
280         $ufp->orderBy('created');
281
282         if ($ufp->find()) { // XXX: this should always happen
283             while ($ufp->fetch()) {
284                 $user = User::staticGet('id', $ufp->user_id);
285                 if (!empty($user)) { // XXX: this would also be unusual
286                     $flaggers[] = clone($user);
287                 }
288             }
289         }
290
291         $cnt = count($flaggers);
292         $others = 0;
293
294         if ($cnt > self::MAX_FLAGGERS) {
295             $flaggers = array_slice($flaggers, 0, self::MAX_FLAGGERS);
296             $others = $cnt - self::MAX_FLAGGERS;
297         }
298
299         $lnks = array();
300
301         foreach ($flaggers as $flagger) {
302
303             $url = common_local_url('showstream',
304                                     array('nickname' => $flagger->nickname));
305
306             $lnks[] = XMLStringer::estring('a', array('href' => $url,
307                                                       'class' => 'flagger'),
308                                            $flagger->nickname);
309         }
310
311         if ($cnt > 0) {
312             $text = _('Flagged by ');
313             $text .= implode(', ', $lnks);
314             if ($others > 0) {
315                 $text .= sprintf(_(' and %d others'), $others);
316             }
317
318             $this->out->elementStart('p', array('class' => 'flaggers'));
319             $this->out->raw($text);
320             $this->out->elementEnd('p');
321         }
322     }
323 }