editmirror.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * PHP version 5
  20. *
  21. * @category Action
  22. * @package StatusNet
  23. * @author Brion Vibber <brion@status.net>
  24. * @copyright 2010 StatusNet, Inc.
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  26. * @link http://status.net/
  27. */
  28. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
  29. /**
  30. * Takes parameters:
  31. *
  32. * - feed: a profile ID
  33. * - token: session token to prevent CSRF attacks
  34. * - ajax: boolean; whether to return Ajax or full-browser results
  35. *
  36. * Only works if the current user is logged in.
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @copyright 2010 StatusNet, Inc.
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  42. * @link http://status.net/
  43. */
  44. class EditMirrorAction extends BaseMirrorAction
  45. {
  46. /**
  47. * Check pre-requisites and instantiate attributes
  48. *
  49. * @param Array $args array of arguments (URL, GET, POST)
  50. *
  51. * @return boolean success flag
  52. */
  53. protected function prepare(array $args=array())
  54. {
  55. parent::prepare($args);
  56. $this->profile = $this->validateProfile($this->trimmed('profile'));
  57. $this->mirror = SubMirror::pkeyGet(array('subscriber' => $this->user->id,
  58. 'subscribed' => $this->profile->id));
  59. if (!$this->mirror instanceof SubMirror) {
  60. // TRANS: Client error displayed when trying to edit an object that is not a feed mirror.
  61. $this->clientError(_m('Requested invalid profile to edit.'));
  62. }
  63. $this->style = $this->validateStyle($this->trimmed('style'));
  64. // DO NOT change to $this->boolean(), it will be wrong.
  65. // We're checking for the presence of the setting, not its value.
  66. $this->delete = (bool)$this->arg('delete');
  67. return true;
  68. }
  69. protected function validateStyle($style)
  70. {
  71. $allowed = array('repeat', 'copy');
  72. if (in_array($style, $allowed)) {
  73. return $style;
  74. } else {
  75. // TRANS: Client error displayed when providing invalid input when editing a mirror.
  76. $this->clientError(_m('Bad form data.'));
  77. }
  78. }
  79. protected function saveMirror()
  80. {
  81. $mirror = SubMirror::getMirror($this->user, $this->profile);
  82. if (!$mirror) {
  83. // TRANS: Client error thrown when a mirror request is made and no result is retrieved.
  84. $this->clientError(_m('The mirror request failed, because no result was retrieved.'));
  85. }
  86. if ($this->delete) {
  87. $mirror->delete();
  88. $oprofile = Ostatus_profile::getKV('profile_id', $this->profile->id);
  89. if ($oprofile) {
  90. $oprofile->garbageCollect();
  91. }
  92. } else if ($this->style != $mirror->style) {
  93. $orig = clone($mirror);
  94. $mirror->style = $this->style;
  95. $mirror->modified = common_sql_now();
  96. $mirror->update($orig);
  97. }
  98. }
  99. }