adminprofileflag.php 11 KB

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