adminprofileflag.php 11 KB

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