X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=actions%2Fsubscribe.php;h=4002c9fbb0164267250333f87255f782489616b1;hb=0a20abf1d8a9a068df9310d6903cc303f39b25ed;hp=1ca57a43ba0ccdff6e5a0e1bd75a5ff939d98391;hpb=52600ce0b063e68e622b19699841e41b5ddbf2d1;p=quix0rs-gnu-social.git diff --git a/actions/subscribe.php b/actions/subscribe.php index 1ca57a43ba..4002c9fbb0 100644 --- a/actions/subscribe.php +++ b/actions/subscribe.php @@ -1,7 +1,9 @@ . + * + * PHP version 5 + * + * @category Action + * @package StatusNet + * @author Evan Prodromou + * @copyright 2008-2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Subscription action + * + * Subscribing to a profile. Likely to work for OStatus profiles. + * + * Takes parameters: + * + * - subscribeto: a profile ID + * - token: session token to prevent CSRF attacks + * - ajax: boolean; whether to return Ajax or full-browser results + * + * Only works if the current user is logged in. + * + * @category Action + * @package StatusNet + * @author Evan Prodromou + * @copyright 2008-2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ */ +class SubscribeAction extends Action +{ + var $user; + var $other; + + /** + * Check pre-requisites and instantiate attributes + * + * @param Array $args array of arguments (URL, GET, POST) + * + * @return boolean success flag + */ + function prepare($args) + { + parent::prepare($args); + + // Only allow POST requests + + if ($_SERVER['REQUEST_METHOD'] != 'POST') { + // TRANS: Client error displayed trying to perform any request method other than POST. + // TRANS: Do not translate POST. + $this->clientError(_('This action only accepts POST requests.')); + } -if (!defined('LACONICA')) { exit(1); } + // CSRF protection -class SubscribeAction extends Action { - function handle($args) { - parent::handle($args); + $token = $this->trimmed('token'); - if (!common_logged_in()) { - common_user_error(_t('Not logged in.')); - return; - } + if (!$token || $token != common_session_token()) { + // TRANS: Client error displayed when the session token is not okay. + $this->clientError(_('There was a problem with your session token.'. + ' Try again, please.')); + } - $other_nickname = $this->arg('subscribeto'); + // Only for logged-in users - $other = User::staticGet('nickname', $other_nickname); + $this->user = common_current_user(); - if (!$other) { - common_user_error(_t('No such user.')); - return; - } + if (empty($this->user)) { + // TRANS: Error message displayed when trying to perform an action that requires a logged in user. + $this->clientError(_('Not logged in.')); + } - $user = common_current_user(); + // Profile to subscribe to - if ($user->isSubscribed($other)) { - common_user_error(_t('Already subscribed!.')); - return; - } + $other_id = $this->arg('subscribeto'); - $sub = new Subscription(); - $sub->subscriber = $user->id; - $sub->subscribed = $other->id; + $this->other = Profile::getKV('id', $other_id); - $sub->created = DB_DataObject_Cast::dateTime(); # current time + if (empty($this->other)) { + // TRANS: Client error displayed trying to subscribe to a non-existing profile. + $this->clientError(_('No such profile.')); + } - $val = $sub->validate(); + return true; + } - if ($val !== TRUE) { - # XXX: give some error notice - common_server_error(_t('Subscription did not validate.')); - return; - } + /** + * Handle request + * + * Does the subscription and returns results. + * + * @param Array $args unused. + * + * @return void + */ + function handle($args) + { + // Throws exception on error - if (!$sub->insert()) { - common_server_error(_t('Couldn\'t create subscription.')); - return; - } + $sub = Subscription::start($this->user->getProfile(), + $this->other); - common_redirect(common_local_url('all', array('nickname' => - $user->nickname))); - } -} \ No newline at end of file + if ($this->boolean('ajax')) { + $this->startHTML('text/xml;charset=utf-8'); + $this->elementStart('head'); + // TRANS: Page title when subscription succeeded. + $this->element('title', null, _('Subscribed')); + $this->elementEnd('head'); + $this->elementStart('body'); + if ($sub instanceof Subscription) { + $form = new UnsubscribeForm($this, $this->other); + } else { + $form = new CancelSubscriptionForm($this, $this->other); + } + $form->show(); + $this->elementEnd('body'); + $this->endHTML(); + } else { + $url = common_local_url('subscriptions', + array('nickname' => $this->user->nickname)); + common_redirect($url, 303); + } + } +}