Simplified by adding an abstract AtompubAction

This commit is contained in:
Mikael Nordfeldth 2015-01-12 03:15:41 +01:00
parent 1c3d61885d
commit c7df5594d0
6 changed files with 161 additions and 316 deletions

View File

@ -28,11 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/** /**
* Feed of group memberships for a user, in ActivityStreams format * Feed of group memberships for a user, in ActivityStreams format
@ -44,67 +40,34 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class AtompubmembershipfeedAction extends ApiAuthAction class AtompubmembershipfeedAction extends AtompubAction
{ {
private $_profile = null; private $_profile = null;
private $_memberships = null; private $_memberships = null;
/** protected function atompubPrepare()
* For initializing members of the class.
*
* @param array $argarray misc. arguments
*
* @return boolean true
*/
function prepare($argarray)
{ {
parent::prepare($argarray); $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
$profileId = $this->trimmed('profile'); if (!$this->_profile instanceof Profile) {
$this->_profile = Profile::getKV('id', $profileId);
if (empty($this->_profile)) {
// TRANS: Client exception. // TRANS: Client exception.
throw new ClientException(_('No such profile.'), 404); throw new ClientException(_('No such profile.'), 404);
} }
$offset = ($this->page-1) * $this->count;
$limit = $this->count + 1;
$this->_memberships = Group_member::byMember($this->_profile->id, $this->_memberships = Group_member::byMember($this->_profile->id,
$offset, $this->offset,
$limit); $this->limit);
return true; return true;
} }
/** protected function handleGet()
* Handler method
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
*/
function handle($argarray=null)
{ {
parent::handle($argarray); return $this->showFeed();
switch ($_SERVER['REQUEST_METHOD']) {
case 'HEAD':
case 'GET':
$this->showFeed();
break;
case 'POST':
$this->addMembership();
break;
default:
// TRANS: Client exception thrown when using an unsupported HTTP method.
throw new ClientException(_('HTTP method not supported.'), 405);
return;
} }
return; protected function handlePost()
{
return $this->addMembership();
} }
/** /**
@ -287,25 +250,6 @@ class AtompubmembershipfeedAction extends ApiAuthAction
} }
} }
/**
* Return true if read only.
*
* MAY override
*
* @param array $args other arguments
*
* @return boolean is read only action?
*/
function isReadOnly($args)
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {
return true;
} else {
return false;
}
}
/** /**
* Return last modified, if applicable. * Return last modified, if applicable.
* *
@ -331,19 +275,4 @@ class AtompubmembershipfeedAction extends ApiAuthAction
{ {
return null; return null;
} }
/**
* Does this require authentication?
*
* @return boolean true if delete, else false
*/
function requiresAuth()
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {
return false;
} else {
return true;
}
}
} }

View File

@ -28,11 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/** /**
* Show (or delete) a single membership event as an ActivityStreams entry * Show (or delete) a single membership event as an ActivityStreams entry
@ -44,47 +40,34 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class AtompubshowmembershipAction extends ApiAuthAction class AtompubshowmembershipAction extends AtompubAction
{ {
private $_profile = null; private $_private = null;
private $_group = null; private $_group = null;
private $_membership = null; private $_membership = null;
/** protected function atompubPrepare()
* For initializing members of the class.
*
* @param array $argarray misc. arguments
*
* @return boolean true
*/
function prepare($argarray)
{ {
parent::prepare($argarray); $this->_profile = Profile::getKV('id', $this->trimmed('profile'));
$profileId = $this->trimmed('profile'); if (!$this->_profile instanceof Profile) {
$this->_profile = Profile::getKV('id', $profileId);
if (empty($this->_profile)) {
// TRANS: Client exception. // TRANS: Client exception.
throw new ClientException(_('No such profile.'), 404); throw new ClientException(_('No such profile.'), 404);
} }
$groupId = $this->trimmed('group'); $this->_group = User_group::getKV('id', $this->trimmed('group'));
$this->_group = User_group::getKV('id', $groupId); if (!$this->_group instanceof User_group) {
if (empty($this->_group)) {
// TRANS: Client exception thrown when referencing a non-existing group. // TRANS: Client exception thrown when referencing a non-existing group.
throw new ClientException(_('No such group.'), 404); throw new ClientException(_('No such group.'), 404);
} }
$kv = array('group_id' => $groupId, $kv = array('group_id' => $groupId,
'profile_id' => $profileId); 'profile_id' => $this->_profile->id);
$this->_membership = Group_member::pkeyGet($kv); $this->_membership = Group_member::pkeyGet($kv);
if (empty($this->_membership)) { if (!$this->_profile->isMembmer($this->_group)) {
// TRANS: Client exception thrown when trying to show membership of a non-subscribed group // TRANS: Client exception thrown when trying to show membership of a non-subscribed group
throw new ClientException(_('Not a member.'), 404); throw new ClientException(_('Not a member.'), 404);
} }
@ -92,29 +75,12 @@ class AtompubshowmembershipAction extends ApiAuthAction
return true; return true;
} }
/** protected function handleGet() {
* Handler method return $this->showMembership();
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
*/
function handle($argarray=null)
{
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
case 'HEAD':
$this->showMembership();
break;
case 'DELETE':
$this->deleteMembership();
break;
default:
// TRANS: Client exception thrown when using an unsupported HTTP method.
throw new ClientException(_('HTTP method not supported.'), 405);
break;
} }
return;
protected function handleDelete() {
return $this->deleteMembership();
} }
/** /**
@ -154,25 +120,6 @@ class AtompubshowmembershipAction extends ApiAuthAction
return; return;
} }
/**
* Return true if read only.
*
* MAY override
*
* @param array $args other arguments
*
* @return boolean is read only action?
*/
function isReadOnly($args)
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {
return true;
} else {
return false;
}
}
/** /**
* Return last modified, if applicable. * Return last modified, if applicable.
* *
@ -209,19 +156,4 @@ class AtompubshowmembershipAction extends ApiAuthAction
$adminflag, $adminflag,
$ctime)) . '"'; $ctime)) . '"';
} }
/**
* Does this require authentication?
*
* @return boolean true if delete, else false
*/
function requiresAuth()
{
if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
$_SERVER['REQUEST_METHOD'] == 'HEAD') {
return false;
} else {
return true;
}
}
} }

View File

@ -28,11 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/** /**
* Show a single subscription * Show a single subscription
@ -44,27 +40,19 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class AtompubshowsubscriptionAction extends ApiAuthAction class AtompubshowsubscriptionAction extends AtompubAction
{ {
private $_subscriber = null; private $_subscriber = null;
private $_subscribed = null; private $_subscribed = null;
private $_subscription = null; private $_subscription = null;
/** protected function atompubPrepare()
* For initializing members of the class.
*
* @param array $argarray misc. arguments
*
* @return boolean true
*/
function prepare($argarray)
{ {
parent::prepare($argarray);
$subscriberId = $this->trimmed('subscriber'); $subscriberId = $this->trimmed('subscriber');
$this->_subscriber = Profile::getKV('id', $subscriberId); $this->_subscriber = Profile::getKV('id', $subscriberId);
if (empty($this->_subscriber)) { if (!$this->_subscriber instanceof Profile) {
// TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID. // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
// TRANS: %d is the non-existing profile ID number. // TRANS: %d is the non-existing profile ID number.
throw new ClientException(sprintf(_('No such profile id: %d.'), throw new ClientException(sprintf(_('No such profile id: %d.'),
@ -75,18 +63,14 @@ class AtompubshowsubscriptionAction extends ApiAuthAction
$this->_subscribed = Profile::getKV('id', $subscribedId); $this->_subscribed = Profile::getKV('id', $subscribedId);
if (empty($this->_subscribed)) { if (!$this->_subscribed instanceof Profile) {
// TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID. // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
// TRANS: %d is the non-existing profile ID number. // TRANS: %d is the non-existing profile ID number.
throw new ClientException(sprintf(_('No such profile id: %d.'), throw new ClientException(sprintf(_('No such profile id: %d.'),
$subscribedId), 404); $subscribedId), 404);
} }
$this->_subscription = if (!$this->_subscriber->isSubscribed($this->_subscribed)) {
Subscription::pkeyGet(array('subscriber' => $subscriberId,
'subscribed' => $subscribedId));
if (empty($this->_subscription)) {
// TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID. // TRANS: Client exception thrown when trying to display a subscription for a non-subscribed profile ID.
// TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to. // TRANS: %1$d is the non-existing subscriber ID number, $2$d is the ID of the profile that was not subscribed to.
$msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'), $msg = sprintf(_('Profile %1$d not subscribed to profile %2$d.'),
@ -97,30 +81,14 @@ class AtompubshowsubscriptionAction extends ApiAuthAction
return true; return true;
} }
/** protected function handleGet()
* Handler method
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
*/
function handle($argarray=null)
{ {
parent::handle($argarray);
switch ($_SERVER['REQUEST_METHOD']) {
case 'HEAD':
case 'GET':
$this->showSubscription(); $this->showSubscription();
break;
case 'DELETE':
$this->deleteSubscription();
break;
default:
// TRANS: Client error shown when using a non-supported HTTP method.
$this->clientError(_('HTTP method not supported.'), 405);
} }
return; protected function handleDelete()
{
$this->deleteSubscription();
} }
/** /**
@ -137,8 +105,6 @@ class AtompubshowsubscriptionAction extends ApiAuthAction
$this->startXML(); $this->startXML();
$this->raw($activity->asString(true, true, true)); $this->raw($activity->asString(true, true, true));
$this->endXML(); $this->endXML();
return;
} }
/** /**
@ -148,17 +114,13 @@ class AtompubshowsubscriptionAction extends ApiAuthAction
*/ */
function deleteSubscription() function deleteSubscription()
{ {
if (empty($this->auth_user) || if (!$this->scoped instanceof Profile ||
$this->auth_user->id != $this->_subscriber->id) { $this->scoped->id != $this->_subscriber->id) {
// TRANS: Client exception thrown when trying to delete a subscription of another user. // TRANS: Client exception thrown when trying to delete a subscription of another user.
throw new ClientException(_("Cannot delete someone else's ". throw new ClientException(_("Cannot delete someone else's subscription."), 403);
"subscription."), 403);
} }
Subscription::cancel($this->_subscriber, Subscription::cancel($this->_subscriber, $this->_subscribed);
$this->_subscribed);
return;
} }
/** /**
@ -172,9 +134,9 @@ class AtompubshowsubscriptionAction extends ApiAuthAction
{ {
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') { if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
return false; return false;
} else {
return true;
} }
return true;
} }
/** /**

View File

@ -28,11 +28,7 @@
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/** /**
* Subscription feed class for AtomPub * Subscription feed class for AtomPub
@ -46,68 +42,39 @@ if (!defined('STATUSNET')) {
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class AtompubsubscriptionfeedAction extends ApiAuthAction class AtompubsubscriptionfeedAction extends AtompubAction
{ {
private $_profile = null; private $_profile = null;
private $_subscriptions = null; private $_subscriptions = null;
/** protected function atompubPrepare()
* For initializing members of the class.
*
* @param array $argarray misc. arguments
*
* @return boolean true
*/
function prepare($argarray)
{ {
parent::prepare($argarray);
$subscriber = $this->trimmed('subscriber'); $subscriber = $this->trimmed('subscriber');
$this->_profile = Profile::getKV('id', $subscriber); $this->_profile = Profile::getKV('id', $subscriber);
if (empty($this->_profile)) { if (!$this->_profile instanceof Profile) {
// TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID. // TRANS: Client exception thrown when trying to display a subscription for a non-existing profile ID.
// TRANS: %d is the non-existing profile ID number. // TRANS: %d is the non-existing profile ID number.
throw new ClientException(sprintf(_('No such profile id: %d.'), throw new ClientException(sprintf(_('No such profile id: %d.'),
$subscriber), 404); $subscriber), 404);
} }
// page and count from ApiAction $this->_subscriptions = Subscription::bySubscriber($this->_profile->id,
$this->offset,
$offset = ($this->page-1) * $this->count; $this->limit);
$this->_subscriptions = Subscription::bySubscriber($subscriber,
$offset,
$this->count + 1);
return true; return true;
} }
/** protected function handleGet()
* Handler method
*
* @param array $argarray is ignored since it's now passed in in prepare()
*
* @return void
*/
function handle($argarray=null)
{ {
parent::handle($argarray);
switch ($_SERVER['REQUEST_METHOD']) {
case 'HEAD':
case 'GET':
$this->showFeed(); $this->showFeed();
break;
case 'POST':
$this->addSubscription();
break;
default:
// TRANS: Client exception thrown when using an unsupported HTTP method.
$this->clientError(_('HTTP method not supported.'), 405);
} }
return; protected function handlePost()
{
$this->addSubscription();
} }
/** /**
@ -291,50 +258,4 @@ class AtompubsubscriptionfeedAction extends ApiAuthAction
$this->endXML(); $this->endXML();
} }
} }
/**
* Return true if read only.
*
* @param array $args other arguments
*
* @return boolean is read only action?
*/
function isReadOnly($args)
{
return $_SERVER['REQUEST_METHOD'] != 'POST';
}
/**
* Return last modified, if applicable.
*
* @return string last modified http header
*/
function lastModified()
{
return null;
}
/**
* Return etag, if applicable.
*
* @return string etag http header
*/
function etag()
{
return null;
}
/**
* Does this require authentication?
*
* @return boolean true if delete, else false
*/
function requiresAuth()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
return true;
} else {
return false;
}
}
} }

View File

@ -123,6 +123,8 @@ class ApiAction extends Action
var $auth_user = null; var $auth_user = null;
var $page = null; var $page = null;
var $count = null; var $count = null;
var $offset = null;
var $limit = null;
var $max_id = null; var $max_id = null;
var $since_id = null; var $since_id = null;
var $source = null; var $source = null;
@ -152,6 +154,10 @@ class ApiAction extends Action
$this->max_id = (int)$this->arg('max_id', 0); $this->max_id = (int)$this->arg('max_id', 0);
$this->since_id = (int)$this->arg('since_id', 0); $this->since_id = (int)$this->arg('since_id', 0);
// These two are not used everywhere, mainly just AtompubAction extensions
$this->offset = ($this->page-1) * $this->count;
$this->limit = $this->count + 1;
if ($this->arg('since')) { if ($this->arg('since')) {
header('X-StatusNet-Warning: since parameter is disabled; use since_id'); header('X-StatusNet-Warning: since parameter is disabled; use since_id');
} }

95
lib/atompubaction.php Normal file
View File

@ -0,0 +1,95 @@
<?php
/*
* GNU social - a federating social network
* Copyright (C) 2015, Free Software Foundation, Inc.
* Copyright (C) before that, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('GNUSOCIAL') && !defined('STATUSNET')) { exit(1); }
abstract class AtompubAction extends ApiAuthAction
{
protected $_profile = null;
protected $target = null;
protected function prepare(array $args=array())
{
parent::prepare($args);
return $this->atompubPrepare();
}
protected function atompubPrepare() {
return true;
}
protected function handle()
{
parent::handle();
switch ($_SERVER['REQUEST_METHOD']) {
case 'HEAD':
$this->handleHead();
break;
case 'GET':
$this->handleGet();
break;
case 'POST':
$this->handlePost();
break;
case 'DELETE':
$this->handleDelete();
break;
default:
// TRANS: Client exception thrown when using an unsupported HTTP method.
throw new ClientException(_('HTTP method not supported.'), 405);
}
return true;
}
protected function handleHead()
{
$this->handleGet();
}
protected function handleGet()
{
throw new ClientException(_('HTTP method not supported.'), 405);
}
protected function handlePost()
{
throw new ClientException(_('HTTP method not supported.'), 405);
}
protected function handleDelete()
{
throw new ClientException(_('HTTP method not supported.'), 405);
}
function isReadOnly($args)
{
// GET/HEAD is readonly, POST and DELETE (etc?) are readwrite.
return in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'));
}
function requiresAuth()
{
// GET/HEAD don't require auth, POST and DELETE (etc?) require it.
return !in_array($_SERVER['REQUEST_METHOD'], array('GET', 'HEAD'));
}
}