CC-2166: Packaging Improvements. Moved the Zend app into airtime_mvc. It is now installed to /var/www/airtime. Storage is now set to /srv/airtime/stor. Utils are now installed to /usr/lib/airtime/utils/. Added install/airtime-dircheck.php as a simple test to see if everything is install/uninstalled correctly.

This commit is contained in:
Paul Baranowski 2011-04-14 18:55:04 -04:00
parent 514777e8d2
commit b11cbd8159
4546 changed files with 138 additions and 51 deletions

View file

@ -0,0 +1,307 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Callback
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Feed_Pubsubhubbub_CallbackInterface
*/
require_once 'Zend/Feed/Pubsubhubbub/CallbackInterface.php';
/**
* @see Zend_Feed_Pubsubhubbub_HttpResponse
*/
require_once 'Zend/Feed/Pubsubhubbub/HttpResponse.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Callback
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Zend_Feed_Pubsubhubbub_CallbackAbstract
implements Zend_Feed_Pubsubhubbub_CallbackInterface
{
/**
* An instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used
* to background save any verification tokens associated with a subscription
* or other.
*
* @var Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
*/
protected $_storage = null;
/**
* An instance of a class handling Http Responses. This is implemented in
* Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
* (i.e. not inherited from) Zend_Controller_Response_Http.
*
* @var Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http
*/
protected $_httpResponse = null;
/**
* The number of Subscribers for which any updates are on behalf of.
*
* @var int
*/
protected $_subscriberCount = 1;
/**
* Constructor; accepts an array or Zend_Config instance to preset
* options for the Subscriber without calling all supported setter
* methods in turn.
*
* @param array|Zend_Config $options Options array or Zend_Config instance
*/
public function __construct($config = null)
{
if (!is_null($config)) {
$this->setConfig($config);
}
}
/**
* Process any injected configuration options
*
* @param array|Zend_Config $options Options array or Zend_Config instance
* @return Zend_Feed_Pubsubhubbub_CallbackAbstract
*/
public function setConfig($config)
{
if ($config instanceof Zend_Config) {
$config = $config->toArray();
} elseif (!is_array($config)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object'
. 'expected, got ' . gettype($config));
}
if (array_key_exists('storage', $config)) {
$this->setStorage($config['storage']);
}
return $this;
}
/**
* Send the response, including all headers.
* If you wish to handle this via Zend_Controller, use the getter methods
* to retrieve any data needed to be set on your HTTP Response object, or
* simply give this object the HTTP Response instance to work with for you!
*
* @return void
*/
public function sendResponse()
{
$this->getHttpResponse()->sendResponse();
}
/**
* Sets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used
* to background save any verification tokens associated with a subscription
* or other.
*
* @param Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage
* @return Zend_Feed_Pubsubhubbub_CallbackAbstract
*/
public function setStorage(Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage)
{
$this->_storage = $storage;
return $this;
}
/**
* Gets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used
* to background save any verification tokens associated with a subscription
* or other.
*
* @return Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
*/
public function getStorage()
{
if ($this->_storage === null) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('No storage object has been'
. ' set that subclasses Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface');
}
return $this->_storage;
}
/**
* An instance of a class handling Http Responses. This is implemented in
* Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
* (i.e. not inherited from) Zend_Controller_Response_Http.
*
* @param Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http $httpResponse
* @return Zend_Feed_Pubsubhubbub_CallbackAbstract
*/
public function setHttpResponse($httpResponse)
{
if (!is_object($httpResponse)
|| (!$httpResponse instanceof Zend_Feed_Pubsubhubbub_HttpResponse
&& !$httpResponse instanceof Zend_Controller_Response_Http)
) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('HTTP Response object must'
. ' implement one of Zend_Feed_Pubsubhubbub_HttpResponse or'
. ' Zend_Controller_Response_Http');
}
$this->_httpResponse = $httpResponse;
return $this;
}
/**
* An instance of a class handling Http Responses. This is implemented in
* Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
* (i.e. not inherited from) Zend_Controller_Response_Http.
*
* @return Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http
*/
public function getHttpResponse()
{
if ($this->_httpResponse === null) {
$this->_httpResponse = new Zend_Feed_Pubsubhubbub_HttpResponse;
}
return $this->_httpResponse;
}
/**
* Sets the number of Subscribers for which any updates are on behalf of.
* In other words, is this class serving one or more subscribers? How many?
* Defaults to 1 if left unchanged.
*
* @param string|int $count
* @return Zend_Feed_Pubsubhubbub_CallbackAbstract
*/
public function setSubscriberCount($count)
{
$count = intval($count);
if ($count <= 0) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Subscriber count must be'
. ' greater than zero');
}
$this->_subscriberCount = $count;
return $this;
}
/**
* Gets the number of Subscribers for which any updates are on behalf of.
* In other words, is this class serving one or more subscribers? How many?
*
* @return int
*/
public function getSubscriberCount()
{
return $this->_subscriberCount;
}
/**
* Attempt to detect the callback URL (specifically the path forward)
*/
protected function _detectCallbackUrl()
{
$callbackUrl = '';
if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
$callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
} elseif (isset($_SERVER['REQUEST_URI'])) {
$callbackUrl = $_SERVER['REQUEST_URI'];
$scheme = 'http';
if ($_SERVER['HTTPS'] == 'on') {
$scheme = 'https';
}
$schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
$callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
}
} elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
$callbackUrl= $_SERVER['ORIG_PATH_INFO'];
if (!empty($_SERVER['QUERY_STRING'])) {
$callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
}
}
return $callbackUrl;
}
/**
* Get the HTTP host
*
* @return string
*/
protected function _getHttpHost()
{
if (!empty($_SERVER['HTTP_HOST'])) {
return $_SERVER['HTTP_HOST'];
}
$scheme = 'http';
if ($_SERVER['HTTPS'] == 'on') {
$scheme = 'https';
}
$name = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
if (($scheme == 'http' && $port == 80)
|| ($scheme == 'https' && $port == 443)
) {
return $name;
} else {
return $name . ':' . $port;
}
}
/**
* Retrieve a Header value from either $_SERVER or Apache
*
* @param string $header
*/
protected function _getHeader($header)
{
$temp = strtoupper(str_replace('-', '_', $header));
if (!empty($_SERVER[$temp])) {
return $_SERVER[$temp];
}
$temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
if (!empty($_SERVER[$temp])) {
return $_SERVER[$temp];
}
if (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
if (!empty($headers[$header])) {
return $headers[$header];
}
}
return false;
}
/**
* Return the raw body of the request
*
* @return string|false Raw body, or false if not present
*/
protected function _getRawBody()
{
$body = file_get_contents('php://input');
if (strlen(trim($body)) == 0 && isset($GLOBALS['HTTP_RAW_POST_DATA'])) {
$body = $GLOBALS['HTTP_RAW_POST_DATA'];
}
if (strlen(trim($body)) > 0) {
return $body;
}
return false;
}
}

View file

@ -0,0 +1,68 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Callback
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Callback
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Zend_Feed_Pubsubhubbub_CallbackInterface
{
/**
* Handle any callback from a Hub Server responding to a subscription or
* unsubscription request. This should be the Hub Server confirming the
* the request prior to taking action on it.
*
* @param array $httpData GET/POST data if available and not in $_GET/POST
* @param bool $sendResponseNow Whether to send response now or when asked
*/
public function handle(array $httpData = null, $sendResponseNow = false);
/**
* Send the response, including all headers.
* If you wish to handle this via Zend_Controller, use the getter methods
* to retrieve any data needed to be set on your HTTP Response object, or
* simply give this object the HTTP Response instance to work with for you!
*
* @return void
*/
public function sendResponse();
/**
* An instance of a class handling Http Responses. This is implemented in
* Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
* (i.e. not inherited from) Zend_Controller_Response_Http.
*
* @param Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http $httpResponse
*/
public function setHttpResponse($httpResponse);
/**
* An instance of a class handling Http Responses. This is implemented in
* Zend_Feed_Pubsubhubbub_HttpResponse which shares an unenforced interface with
* (i.e. not inherited from) Zend_Controller_Response_Http.
*
* @return Zend_Feed_Pubsubhubbub_HttpResponse|Zend_Controller_Response_Http
*/
public function getHttpResponse();
}

View file

@ -0,0 +1,33 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Exception
*/
require_once 'Zend/Exception.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_Exception extends Zend_Exception
{}

View file

@ -0,0 +1,233 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Feed_Pubsubhubbub
*/
require_once 'Zend/Feed/Pubsubhubbub.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_HttpResponse
{
/**
* The body of any response to the current callback request
*
* @var string
*/
protected $_body = '';
/**
* Array of headers. Each header is an array with keys 'name' and 'value'
*
* @var array
*/
protected $_headers = array();
/**
* HTTP response code to use in headers
*
* @var int
*/
protected $_httpResponseCode = 200;
/**
* Send the response, including all headers
*
* @return void
*/
public function sendResponse()
{
$this->sendHeaders();
echo $this->getBody();
}
/**
* Send all headers
*
* Sends any headers specified. If an {@link setHttpResponseCode() HTTP response code}
* has been specified, it is sent with the first header.
*
* @return void
*/
public function sendHeaders()
{
if (count($this->_headers) || (200 != $this->_httpResponseCode)) {
$this->canSendHeaders(true);
} elseif (200 == $this->_httpResponseCode) {
return;
}
$httpCodeSent = false;
foreach ($this->_headers as $header) {
if (!$httpCodeSent && $this->_httpResponseCode) {
header($header['name'] . ': ' . $header['value'], $header['replace'], $this->_httpResponseCode);
$httpCodeSent = true;
} else {
header($header['name'] . ': ' . $header['value'], $header['replace']);
}
}
if (!$httpCodeSent) {
header('HTTP/1.1 ' . $this->_httpResponseCode);
$httpCodeSent = true;
}
}
/**
* Set a header
*
* If $replace is true, replaces any headers already defined with that
* $name.
*
* @param string $name
* @param string $value
* @param boolean $replace
* @return Zend_Feed_Pubsubhubbub_HttpResponse
*/
public function setHeader($name, $value, $replace = false)
{
$name = $this->_normalizeHeader($name);
$value = (string) $value;
if ($replace) {
foreach ($this->_headers as $key => $header) {
if ($name == $header['name']) {
unset($this->_headers[$key]);
}
}
}
$this->_headers[] = array(
'name' => $name,
'value' => $value,
'replace' => $replace,
);
return $this;
}
/**
* Check if a specific Header is set and return its value
*
* @param string $name
* @return string|null
*/
public function getHeader($name)
{
$name = $this->_normalizeHeader($name);
foreach ($this->_headers as $header) {
if ($header['name'] == $name) {
return $header['value'];
}
}
}
/**
* Return array of headers; see {@link $_headers} for format
*
* @return array
*/
public function getHeaders()
{
return $this->_headers;
}
/**
* Can we send headers?
*
* @param boolean $throw Whether or not to throw an exception if headers have been sent; defaults to false
* @return boolean
* @throws Zend_Feed_Pubsubhubbub_Exception
*/
public function canSendHeaders($throw = false)
{
$ok = headers_sent($file, $line);
if ($ok && $throw) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Cannot send headers; headers already sent in ' . $file . ', line ' . $line);
}
return !$ok;
}
/**
* Set HTTP response code to use with headers
*
* @param int $code
* @return Zend_Feed_Pubsubhubbub_HttpResponse
*/
public function setHttpResponseCode($code)
{
if (!is_int($code) || (100 > $code) || (599 < $code)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid HTTP response'
. ' code:' . $code);
}
$this->_httpResponseCode = $code;
return $this;
}
/**
* Retrieve HTTP response code
*
* @return int
*/
public function getHttpResponseCode()
{
return $this->_httpResponseCode;
}
/**
* Set body content
*
* @param string $content
* @return Zend_Feed_Pubsubhubbub_HttpResponse
*/
public function setBody($content)
{
$this->_body = (string) $content;
$this->setHeader('content-length', strlen($content));
return $this;
}
/**
* Return the body content
*
* @return string
*/
public function getBody()
{
return $this->_body;
}
/**
* Normalizes a header name to X-Capitalized-Names
*
* @param string $name
* @return string
*/
protected function _normalizeHeader($name)
{
$filtered = str_replace(array('-', '_'), ' ', (string) $name);
$filtered = ucwords(strtolower($filtered));
$filtered = str_replace(' ', '-', $filtered);
return $filtered;
}
}

View file

@ -0,0 +1,64 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Db_Table */
require_once 'Zend/Db/Table.php';
/**
* @see Zend_Registry
* Seems to fix the file not being included by Zend_Db_Table...
*/
require_once 'Zend/Registry.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_Model_ModelAbstract
{
/**
* Zend_Db_Table instance to host database methods
*
* @var Zend_Db_Table
*/
protected $_db = null;
/**
* Constructor
*
* @param array $data
* @param Zend_Db_Table_Abstract $tableGateway
* @return void
*/
public function __construct(Zend_Db_Table_Abstract $tableGateway = null)
{
if (is_null($tableGateway)) {
$parts = explode('_', get_class($this));
$table = strtolower(array_pop($parts));
$this->_db = new Zend_Db_Table($table);
} else {
$this->_db = $tableGateway;
}
}
}

View file

@ -0,0 +1,131 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Entity
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Feed_Pubsubhubbub_Model_ModelAbstract */
require_once 'Zend/Feed/Pubsubhubbub/Model/ModelAbstract.php';
/** @see Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface */
require_once 'Zend/Feed/Pubsubhubbub/Model/SubscriptionInterface.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Entity
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_Model_Subscription
extends Zend_Feed_Pubsubhubbub_Model_ModelAbstract
implements Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
{
/**
* Save subscription to RDMBS
*
* @param array $data
* @return bool
*/
public function setSubscription(array $data)
{
if (!isset($data['id'])) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception(
'ID must be set before attempting a save'
);
}
$result = $this->_db->find($data['id']);
if ($result) {
$data['created_time'] = $result->current()->created_time;
$now = new Zend_Date;
if ($data['lease_seconds']) {
$data['expiration_time'] = $now->add($data['lease_seconds'], Zend_Date::SECOND)
->get('yyyy-MM-dd HH:mm:ss');
}
$this->_db->update(
$data,
$this->_db->getAdapter()->quoteInto('id = ?', $data['id'])
);
return false;
}
$this->_db->insert($data);
return true;
}
/**
* Get subscription by ID/key
*
* @param string $key
* @return array
*/
public function getSubscription($key)
{
if (empty($key) || !is_string($key)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
.' of "' . $key . '" must be a non-empty string');
}
$result = $this->_db->find($key);
if ($result) {
return (array) $result->current();
}
return false;
}
/**
* Determine if a subscription matching the key exists
*
* @param string $key
* @return bool
*/
public function hasSubscription($key)
{
if (empty($key) || !is_string($key)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "key"'
.' of "' . $key . '" must be a non-empty string');
}
$result = $this->_db->find($key);
if ($result) {
return true;
}
return false;
}
/**
* Delete a subscription
*
* @param string $key
* @return bool
*/
public function deleteSubscription($key)
{
$result = $this->_db->find($key);
if ($result) {
$this->_db->delete(
$this->_db->getAdapter()->quoteInto('id = ?', $key)
);
return true;
}
return false;
}
}

View file

@ -0,0 +1,64 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Entity
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @subpackage Entity
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
{
/**
* Save subscription to RDMBS
*
* @param array $data The key must be stored here as a $data['id'] entry
* @return bool
*/
public function setSubscription(array $data);
/**
* Get subscription by ID/key
*
* @param string $key
* @return array
*/
public function getSubscription($key);
/**
* Determine if a subscription matching the key exists
*
* @param string $key
* @return bool
*/
public function hasSubscription($key);
/**
* Delete a subscription
*
* @param string $key
* @return bool
*/
public function deleteSubscription($key);
}

View file

@ -0,0 +1,417 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Feed_Pubsubhubbub
*/
require_once 'Zend/Feed/Pubsubhubbub.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_Publisher
{
/**
* An array of URLs for all Hub Servers used by the Publisher, and to
* which all topic update notifications will be sent.
*
* @var array
*/
protected $_hubUrls = array();
/**
* An array of topic (Atom or RSS feed) URLs which have been updated and
* whose updated status will be notified to all Hub Servers.
*
* @var array
*/
protected $_updatedTopicUrls = array();
/**
* An array of any errors including keys for 'response', 'hubUrl'.
* The response is the actual Zend_Http_Response object.
*
* @var array
*/
protected $_errors = array();
/**
* An array of topic (Atom or RSS feed) URLs which have been updated and
* whose updated status will be notified to all Hub Servers.
*
* @var array
*/
protected $_parameters = array();
/**
* Constructor; accepts an array or Zend_Config instance to preset
* options for the Publisher without calling all supported setter
* methods in turn.
*
* @param array|Zend_Config $options Options array or Zend_Config instance
* @return void
*/
public function __construct($config = null)
{
if (!is_null($config)) {
$this->setConfig($config);
}
}
/**
* Process any injected configuration options
*
* @param array|Zend_Config $options Options array or Zend_Config instance
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function setConfig($config)
{
if ($config instanceof Zend_Config) {
$config = $config->toArray();
} elseif (!is_array($config)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object'
. 'expected, got ' . gettype($config));
}
if (array_key_exists('hubUrls', $config)) {
$this->addHubUrls($config['hubUrls']);
}
if (array_key_exists('updatedTopicUrls', $config)) {
$this->addUpdatedTopicUrls($config['updatedTopicUrls']);
}
if (array_key_exists('parameters', $config)) {
$this->setParameters($config['parameters']);
}
return $this;
}
/**
* Add a Hub Server URL supported by Publisher
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function addHubUrl($url)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
.' of "' . $url . '" must be a non-empty string and a valid'
.'URL');
}
$this->_hubUrls[] = $url;
return $this;
}
/**
* Add an array of Hub Server URLs supported by Publisher
*
* @param array $urls
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function addHubUrls(array $urls)
{
foreach ($urls as $url) {
$this->addHubUrl($url);
}
return $this;
}
/**
* Remove a Hub Server URL
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function removeHubUrl($url)
{
if (!in_array($url, $this->getHubUrls())) {
return $this;
}
$key = array_search($url, $this->_hubUrls);
unset($this->_hubUrls[$key]);
return $this;
}
/**
* Return an array of unique Hub Server URLs currently available
*
* @return array
*/
public function getHubUrls()
{
$this->_hubUrls = array_unique($this->_hubUrls);
return $this->_hubUrls;
}
/**
* Add a URL to a topic (Atom or RSS feed) which has been updated
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function addUpdatedTopicUrl($url)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
.' of "' . $url . '" must be a non-empty string and a valid'
.'URL');
}
$this->_updatedTopicUrls[] = $url;
return $this;
}
/**
* Add an array of Topic URLs which have been updated
*
* @param array $urls
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function addUpdatedTopicUrls(array $urls)
{
foreach ($urls as $url) {
$this->addUpdatedTopicUrl($url);
}
return $this;
}
/**
* Remove an updated topic URL
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function removeUpdatedTopicUrl($url)
{
if (!in_array($url, $this->getUpdatedTopicUrls())) {
return $this;
}
$key = array_search($url, $this->_updatedTopicUrls);
unset($this->_updatedTopicUrls[$key]);
return $this;
}
/**
* Return an array of unique updated topic URLs currently available
*
* @return array
*/
public function getUpdatedTopicUrls()
{
$this->_updatedTopicUrls = array_unique($this->_updatedTopicUrls);
return $this->_updatedTopicUrls;
}
/**
* Notifies a single Hub Server URL of changes
*
* @param string $url The Hub Server's URL
* @return void
* @throws Zend_Feed_Pubsubhubbub_Exception Thrown on failure
*/
public function notifyHub($url)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
.' of "' . $url . '" must be a non-empty string and a valid'
.'URL');
}
$client = $this->_getHttpClient();
$client->setUri($url);
$response = $client->request();
if ($response->getStatus() !== 204) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Notification to Hub Server '
. 'at "' . $url . '" appears to have failed with a status code of "'
. $response->getStatus() . '" and message "'
. $response->getMessage() . '"');
}
}
/**
* Notifies all Hub Server URLs of changes
*
* If a Hub notification fails, certain data will be retained in an
* an array retrieved using getErrors(), if a failure occurs for any Hubs
* the isSuccess() check will return FALSE. This method is designed not
* to needlessly fail with an Exception/Error unless from Zend_Http_Client.
*
* @return void
* @throws Zend_Feed_Pubsubhubbub_Exception Thrown if no hubs attached
*/
public function notifyAll()
{
$client = $this->_getHttpClient();
$hubs = $this->getHubUrls();
if (empty($hubs)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('No Hub Server URLs'
. ' have been set so no notifcations can be sent');
}
$this->_errors = array();
foreach ($hubs as $url) {
$client->setUri($url);
$response = $client->request();
if ($response->getStatus() !== 204) {
$this->_errors[] = array(
'response' => $response,
'hubUrl' => $url
);
}
}
}
/**
* Add an optional parameter to the update notification requests
*
* @param string $name
* @param string|null $value
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function setParameter($name, $value = null)
{
if (is_array($name)) {
$this->setParameters($name);
return $this;
}
if (empty($name) || !is_string($name)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
.' of "' . $name . '" must be a non-empty string');
}
if ($value === null) {
$this->removeParameter($name);
return $this;
}
if (empty($value) || (!is_string($value) && !is_null($value))) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "value"'
.' of "' . $value . '" must be a non-empty string');
}
$this->_parameters[$name] = $value;
return $this;
}
/**
* Add an optional parameter to the update notification requests
*
* @param array $parameters
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function setParameters(array $parameters)
{
foreach ($parameters as $name => $value) {
$this->setParameter($name, $value);
}
return $this;
}
/**
* Remove an optional parameter for the notification requests
*
* @param string $name
* @return Zend_Feed_Pubsubhubbub_Publisher
*/
public function removeParameter($name)
{
if (empty($name) || !is_string($name)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
.' of "' . $name . '" must be a non-empty string');
}
if (array_key_exists($name, $this->_parameters)) {
unset($this->_parameters[$name]);
}
return $this;
}
/**
* Return an array of optional parameters for notification requests
*
* @return array
*/
public function getParameters()
{
return $this->_parameters;
}
/**
* Returns a boolean indicator of whether the notifications to Hub
* Servers were ALL successful. If even one failed, FALSE is returned.
*
* @return bool
*/
public function isSuccess()
{
if (count($this->_errors) > 0) {
return false;
}
return true;
}
/**
* Return an array of errors met from any failures, including keys:
* 'response' => the Zend_Http_Response object from the failure
* 'hubUrl' => the URL of the Hub Server whose notification failed
*
* @return array
*/
public function getErrors()
{
return $this->_errors;
}
/**
* Get a basic prepared HTTP client for use
*
* @return Zend_Http_Client
*/
protected function _getHttpClient()
{
$client = Zend_Feed_Pubsubhubbub::getHttpClient();
$client->setMethod(Zend_Http_Client::POST);
$client->setConfig(array(
'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Zend_Version::VERSION,
));
$params = array();
$params[] = 'hub.mode=publish';
$topics = $this->getUpdatedTopicUrls();
if (empty($topics)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('No updated topic URLs'
. ' have been set');
}
foreach ($topics as $topicUrl) {
$params[] = 'hub.url=' . urlencode($topicUrl);
}
$optParams = $this->getParameters();
foreach ($optParams as $name => $value) {
$params[] = urlencode($name) . '=' . urlencode($value);
}
$paramString = implode('&', $params);
$client->setRawData($paramString);
return $client;
}
}

View file

@ -0,0 +1,856 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Feed_Pubsubhubbub
*/
require_once 'Zend/Feed/Pubsubhubbub.php';
/**
* @see Zend_Date
*/
require_once 'Zend/Date.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_Subscriber
{
/**
* An array of URLs for all Hub Servers to subscribe/unsubscribe.
*
* @var array
*/
protected $_hubUrls = array();
/**
* An array of optional parameters to be included in any
* (un)subscribe requests.
*
* @var array
*/
protected $_parameters = array();
/**
* The URL of the topic (Rss or Atom feed) which is the subject of
* our current intent to subscribe to/unsubscribe from updates from
* the currently configured Hub Servers.
*
* @var string
*/
protected $_topicUrl = '';
/**
* The URL Hub Servers must use when communicating with this Subscriber
*
* @var string
*/
protected $_callbackUrl = '';
/**
* The number of seconds for which the subscriber would like to have the
* subscription active. Defaults to null, i.e. not sent, to setup a
* permanent subscription if possible.
*
* @var int
*/
protected $_leaseSeconds = null;
/**
* The preferred verification mode (sync or async). By default, this
* Subscriber prefers synchronous verification, but is considered
* desireable to support asynchronous verification if possible.
*
* Zend_Feed_Pubsubhubbub_Subscriber will always send both modes, whose
* order of occurance in the parameter list determines this preference.
*
* @var string
*/
protected $_preferredVerificationMode
= Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC;
/**
* An array of any errors including keys for 'response', 'hubUrl'.
* The response is the actual Zend_Http_Response object.
*
* @var array
*/
protected $_errors = array();
/**
* An array of Hub Server URLs for Hubs operating at this time in
* asynchronous verification mode.
*
* @var array
*/
protected $_asyncHubs = array();
/**
* An instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used to background
* save any verification tokens associated with a subscription or other.
*
* @var Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
*/
protected $_storage = null;
/**
* An array of authentication credentials for HTTP Basic Authentication
* if required by specific Hubs. The array is indexed by Hub Endpoint URI
* and the value is a simple array of the username and password to apply.
*
* @var array
*/
protected $_authentications = array();
/**
* Tells the Subscriber to append any subscription identifier to the path
* of the base Callback URL. E.g. an identifier "subkey1" would be added
* to the callback URL "http://www.example.com/callback" to create a subscription
* specific Callback URL of "http://www.example.com/callback/subkey1".
*
* This is required for all Hubs using the Pubsubhubbub 0.1 Specification.
* It should be manually intercepted and passed to the Callback class using
* Zend_Feed_Pubsubhubbub_Subscriber_Callback::setSubscriptionKey(). Will
* require a route in the form "callback/:subkey" to allow the parameter be
* retrieved from an action using the Zend_Controller_Action::_getParam()
* method.
*
* @var string
*/
protected $_usePathParameter = false;
/**
* Constructor; accepts an array or Zend_Config instance to preset
* options for the Subscriber without calling all supported setter
* methods in turn.
*
* @param array|Zend_Config $options Options array or Zend_Config instance
* @return void
*/
public function __construct($config = null)
{
if (!is_null($config)) {
$this->setConfig($config);
}
}
/**
* Process any injected configuration options
*
* @param array|Zend_Config $options Options array or Zend_Config instance
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setConfig($config)
{
if ($config instanceof Zend_Config) {
$config = $config->toArray();
} elseif (!is_array($config)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Array or Zend_Config object'
. ' expected, got ' . gettype($config));
}
if (array_key_exists('hubUrls', $config)) {
$this->addHubUrls($config['hubUrls']);
}
if (array_key_exists('callbackUrl', $config)) {
$this->setCallbackUrl($config['callbackUrl']);
}
if (array_key_exists('topicUrl', $config)) {
$this->setTopicUrl($config['topicUrl']);
}
if (array_key_exists('storage', $config)) {
$this->setStorage($config['storage']);
}
if (array_key_exists('leaseSeconds', $config)) {
$this->setLeaseSeconds($config['leaseSeconds']);
}
if (array_key_exists('parameters', $config)) {
$this->setParameters($config['parameters']);
}
if (array_key_exists('authentications', $config)) {
$this->addAuthentications($config['authentications']);
}
if (array_key_exists('usePathParameter', $config)) {
$this->usePathParameter($config['usePathParameter']);
}
if (array_key_exists('preferredVerificationMode', $config)) {
$this->setPreferredVerificationMode(
$config['preferredVerificationMode']
);
}
return $this;
}
/**
* Set the topic URL (RSS or Atom feed) to which the intended (un)subscribe
* event will relate
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setTopicUrl($url)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
.' of "' . $url . '" must be a non-empty string and a valid'
.' URL');
}
$this->_topicUrl = $url;
return $this;
}
/**
* Set the topic URL (RSS or Atom feed) to which the intended (un)subscribe
* event will relate
*
* @return string
*/
public function getTopicUrl()
{
if (empty($this->_topicUrl)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('A valid Topic (RSS or Atom'
. ' feed) URL MUST be set before attempting any operation');
}
return $this->_topicUrl;
}
/**
* Set the number of seconds for which any subscription will remain valid
*
* @param int $seconds
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setLeaseSeconds($seconds)
{
$seconds = intval($seconds);
if ($seconds <= 0) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Expected lease seconds'
. ' must be an integer greater than zero');
}
$this->_leaseSeconds = $seconds;
return $this;
}
/**
* Get the number of lease seconds on subscriptions
*
* @return int
*/
public function getLeaseSeconds()
{
return $this->_leaseSeconds;
}
/**
* Set the callback URL to be used by Hub Servers when communicating with
* this Subscriber
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setCallbackUrl($url)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
. ' of "' . $url . '" must be a non-empty string and a valid'
. ' URL');
}
$this->_callbackUrl = $url;
return $this;
}
/**
* Get the callback URL to be used by Hub Servers when communicating with
* this Subscriber
*
* @return string
*/
public function getCallbackUrl()
{
if (empty($this->_callbackUrl)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('A valid Callback URL MUST be'
. ' set before attempting any operation');
}
return $this->_callbackUrl;
}
/**
* Set preferred verification mode (sync or async). By default, this
* Subscriber prefers synchronous verification, but does support
* asynchronous if that's the Hub Server's utilised mode.
*
* Zend_Feed_Pubsubhubbub_Subscriber will always send both modes, whose
* order of occurance in the parameter list determines this preference.
*
* @param string $mode Should be 'sync' or 'async'
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setPreferredVerificationMode($mode)
{
if ($mode !== Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC
&& $mode !== Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid preferred'
. ' mode specified: "' . $mode . '" but should be one of'
. ' Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC or'
. ' Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC');
}
$this->_preferredVerificationMode = $mode;
return $this;
}
/**
* Get preferred verification mode (sync or async).
*
* @return string
*/
public function getPreferredVerificationMode()
{
return $this->_preferredVerificationMode;
}
/**
* Add a Hub Server URL supported by Publisher
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function addHubUrl($url)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
. ' of "' . $url . '" must be a non-empty string and a valid'
. ' URL');
}
$this->_hubUrls[] = $url;
return $this;
}
/**
* Add an array of Hub Server URLs supported by Publisher
*
* @param array $urls
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function addHubUrls(array $urls)
{
foreach ($urls as $url) {
$this->addHubUrl($url);
}
return $this;
}
/**
* Remove a Hub Server URL
*
* @param string $url
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function removeHubUrl($url)
{
if (!in_array($url, $this->getHubUrls())) {
return $this;
}
$key = array_search($url, $this->_hubUrls);
unset($this->_hubUrls[$key]);
return $this;
}
/**
* Return an array of unique Hub Server URLs currently available
*
* @return array
*/
public function getHubUrls()
{
$this->_hubUrls = array_unique($this->_hubUrls);
return $this->_hubUrls;
}
/**
* Add authentication credentials for a given URL
*
* @param string $url
* @param array $authentication
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function addAuthentication($url, array $authentication)
{
if (empty($url) || !is_string($url) || !Zend_Uri::check($url)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "url"'
. ' of "' . $url . '" must be a non-empty string and a valid'
. ' URL');
}
$this->_authentications[$url] = $authentication;
return $this;
}
/**
* Add authentication credentials for hub URLs
*
* @param array $authentications
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function addAuthentications(array $authentications)
{
foreach ($authentications as $url => $authentication) {
$this->addAuthentication($url, $authentication);
}
return $this;
}
/**
* Get all hub URL authentication credentials
*
* @return array
*/
public function getAuthentications()
{
return $this->_authentications;
}
/**
* Set flag indicating whether or not to use a path parameter
*
* @param bool $bool
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function usePathParameter($bool = true)
{
$this->_usePathParameter = $bool;
return $this;
}
/**
* Add an optional parameter to the (un)subscribe requests
*
* @param string $name
* @param string|null $value
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setParameter($name, $value = null)
{
if (is_array($name)) {
$this->setParameters($name);
return $this;
}
if (empty($name) || !is_string($name)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
. ' of "' . $name . '" must be a non-empty string');
}
if ($value === null) {
$this->removeParameter($name);
return $this;
}
if (empty($value) || (!is_string($value) && !is_null($value))) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "value"'
. ' of "' . $value . '" must be a non-empty string');
}
$this->_parameters[$name] = $value;
return $this;
}
/**
* Add an optional parameter to the (un)subscribe requests
*
* @param string $name
* @param string|null $value
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setParameters(array $parameters)
{
foreach ($parameters as $name => $value) {
$this->setParameter($name, $value);
}
return $this;
}
/**
* Remove an optional parameter for the (un)subscribe requests
*
* @param string $name
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function removeParameter($name)
{
if (empty($name) || !is_string($name)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid parameter "name"'
. ' of "' . $name . '" must be a non-empty string');
}
if (array_key_exists($name, $this->_parameters)) {
unset($this->_parameters[$name]);
}
return $this;
}
/**
* Return an array of optional parameters for (un)subscribe requests
*
* @return array
*/
public function getParameters()
{
return $this->_parameters;
}
/**
* Sets an instance of Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface used to background
* save any verification tokens associated with a subscription or other.
*
* @param Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage
* @return Zend_Feed_Pubsubhubbub_Subscriber
*/
public function setStorage(Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface $storage)
{
$this->_storage = $storage;
return $this;
}
/**
* Gets an instance of Zend_Feed_Pubsubhubbub_Storage_StorageInterface used
* to background save any verification tokens associated with a subscription
* or other.
*
* @return Zend_Feed_Pubsubhubbub_Model_SubscriptionInterface
*/
public function getStorage()
{
if ($this->_storage === null) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('No storage vehicle '
. 'has been set.');
}
return $this->_storage;
}
/**
* Subscribe to one or more Hub Servers using the stored Hub URLs
* for the given Topic URL (RSS or Atom feed)
*
* @return void
*/
public function subscribeAll()
{
return $this->_doRequest('subscribe');
}
/**
* Unsubscribe from one or more Hub Servers using the stored Hub URLs
* for the given Topic URL (RSS or Atom feed)
*
* @return void
*/
public function unsubscribeAll()
{
return $this->_doRequest('unsubscribe');
}
/**
* Returns a boolean indicator of whether the notifications to Hub
* Servers were ALL successful. If even one failed, FALSE is returned.
*
* @return bool
*/
public function isSuccess()
{
if (count($this->_errors) > 0) {
return false;
}
return true;
}
/**
* Return an array of errors met from any failures, including keys:
* 'response' => the Zend_Http_Response object from the failure
* 'hubUrl' => the URL of the Hub Server whose notification failed
*
* @return array
*/
public function getErrors()
{
return $this->_errors;
}
/**
* Return an array of Hub Server URLs who returned a response indicating
* operation in Asynchronous Verification Mode, i.e. they will not confirm
* any (un)subscription immediately but at a later time (Hubs may be
* doing this as a batch process when load balancing)
*
* @return array
*/
public function getAsyncHubs()
{
return $this->_asyncHubs;
}
/**
* Executes an (un)subscribe request
*
* @param string $mode
* @return void
*/
protected function _doRequest($mode)
{
$client = $this->_getHttpClient();
$hubs = $this->getHubUrls();
if (empty($hubs)) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('No Hub Server URLs'
. ' have been set so no subscriptions can be attempted');
}
$this->_errors = array();
$this->_asyncHubs = array();
foreach ($hubs as $url) {
if (array_key_exists($url, $this->_authentications)) {
$auth = $this->_authentications[$url];
$client->setAuth($auth[0], $auth[1]);
}
$client->setUri($url);
$client->setRawData($this->_getRequestParameters($url, $mode));
$response = $client->request();
if ($response->getStatus() !== 204
&& $response->getStatus() !== 202
) {
$this->_errors[] = array(
'response' => $response,
'hubUrl' => $url,
);
/**
* At first I thought it was needed, but the backend storage will
* allow tracking async without any user interference. It's left
* here in case the user is interested in knowing what Hubs
* are using async verification modes so they may update Models and
* move these to asynchronous processes.
*/
} elseif ($response->getStatus() == 202) {
$this->_asyncHubs[] = array(
'response' => $response,
'hubUrl' => $url,
);
}
}
}
/**
* Get a basic prepared HTTP client for use
*
* @param string $mode Must be "subscribe" or "unsubscribe"
* @return Zend_Http_Client
*/
protected function _getHttpClient()
{
$client = Zend_Feed_Pubsubhubbub::getHttpClient();
$client->setMethod(Zend_Http_Client::POST);
$client->setConfig(array('useragent' => 'Zend_Feed_Pubsubhubbub_Subscriber/'
. Zend_Version::VERSION));
return $client;
}
/**
* Return a list of standard protocol/optional parameters for addition to
* client's POST body that are specific to the current Hub Server URL
*
* @param string $hubUrl
* @param mode $hubUrl
* @return string
*/
protected function _getRequestParameters($hubUrl, $mode)
{
if (!in_array($mode, array('subscribe', 'unsubscribe'))) {
require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
throw new Zend_Feed_Pubsubhubbub_Exception('Invalid mode specified: "'
. $mode . '" which should have been "subscribe" or "unsubscribe"');
}
$params = array(
'hub.mode' => $mode,
'hub.topic' => $this->getTopicUrl(),
);
if ($this->getPreferredVerificationMode()
== Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC
) {
$vmodes = array(
Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC,
Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC,
);
} else {
$vmodes = array(
Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC,
Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC,
);
}
$params['hub.verify'] = array();
foreach($vmodes as $vmode) {
$params['hub.verify'][] = $vmode;
}
/**
* Establish a persistent verify_token and attach key to callback
* URL's path/querystring
*/
$key = $this->_generateSubscriptionKey($params, $hubUrl);
$token = $this->_generateVerifyToken();
$params['hub.verify_token'] = $token;
// Note: query string only usable with PuSH 0.2 Hubs
if (!$this->_usePathParameter) {
$params['hub.callback'] = $this->getCallbackUrl()
. '?xhub.subscription=' . Zend_Feed_Pubsubhubbub::urlencode($key);
} else {
$params['hub.callback'] = rtrim($this->getCallbackUrl(), '/')
. '/' . Zend_Feed_Pubsubhubbub::urlencode($key);
}
if ($mode == 'subscribe' && !is_null($this->getLeaseSeconds())) {
$params['hub.lease_seconds'] = $this->getLeaseSeconds();
}
// hub.secret not currently supported
$optParams = $this->getParameters();
foreach ($optParams as $name => $value) {
$params[$name] = $value;
}
// store subscription to storage
$now = new Zend_Date;
$expires = null;
if (isset($params['hub.lease_seconds'])) {
$expires = $now->add($params['hub.lease_seconds'], Zend_Date::SECOND)
->get('yyyy-MM-dd HH:mm:ss');
}
$data = array(
'id' => $key,
'topic_url' => $params['hub.topic'],
'hub_url' => $hubUrl,
'created_time' => $now->get('yyyy-MM-dd HH:mm:ss'),
'lease_seconds' => $expires,
'verify_token' => hash('sha256', $params['hub.verify_token']),
'secret' => null,
'expiration_time' => $expires,
'subscription_state' => Zend_Feed_Pubsubhubbub::SUBSCRIPTION_NOTVERIFIED,
);
$this->getStorage()->setSubscription($data);
return $this->_toByteValueOrderedString(
$this->_urlEncode($params)
);
}
/**
* Simple helper to generate a verification token used in (un)subscribe
* requests to a Hub Server. Follows no particular method, which means
* it might be improved/changed in future.
*
* @param string $hubUrl The Hub Server URL for which this token will apply
* @return string
*/
protected function _generateVerifyToken()
{
if (!empty($this->_testStaticToken)) {
return $this->_testStaticToken;
}
return uniqid(rand(), true) . time();
}
/**
* Simple helper to generate a verification token used in (un)subscribe
* requests to a Hub Server.
*
* @param string $hubUrl The Hub Server URL for which this token will apply
* @return string
*/
protected function _generateSubscriptionKey(array $params, $hubUrl)
{
$keyBase = $params['hub.topic'] . $hubUrl;
$key = md5($keyBase);
return $key;
}
/**
* URL Encode an array of parameters
*
* @param array $params
* @return array
*/
protected function _urlEncode(array $params)
{
$encoded = array();
foreach ($params as $key => $value) {
if (is_array($value)) {
$ekey = Zend_Feed_Pubsubhubbub::urlencode($key);
$encoded[$ekey] = array();
foreach ($value as $duplicateKey) {
$encoded[$ekey][]
= Zend_Feed_Pubsubhubbub::urlencode($duplicateKey);
}
} else {
$encoded[Zend_Feed_Pubsubhubbub::urlencode($key)]
= Zend_Feed_Pubsubhubbub::urlencode($value);
}
}
return $encoded;
}
/**
* Order outgoing parameters
*
* @param array $params
* @return array
*/
protected function _toByteValueOrderedString(array $params)
{
$return = array();
uksort($params, 'strnatcmp');
foreach ($params as $key => $value) {
if (is_array($value)) {
foreach ($value as $keyduplicate) {
$return[] = $key . '=' . $keyduplicate;
}
} else {
$return[] = $key . '=' . $value;
}
}
return implode('&', $return);
}
/**
* This is STRICTLY for testing purposes only...
*/
protected $_testStaticToken = null;
final public function setTestStaticToken($token)
{
$this->_testStaticToken = (string) $token;
}
}

View file

@ -0,0 +1,328 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Feed_Pubsubhubbub
*/
require_once 'Zend/Feed/Pubsubhubbub.php';
/**
* @see Zend_Feed_Pubsubhubbub
*/
require_once 'Zend/Feed/Pubsubhubbub/CallbackAbstract.php';
/**
* @see Zend_Feed_Reader
*/
require_once 'Zend/Feed/Reader.php';
/**
* @category Zend
* @package Zend_Feed_Pubsubhubbub
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Feed_Pubsubhubbub_Subscriber_Callback
extends Zend_Feed_Pubsubhubbub_CallbackAbstract
{
/**
* Contains the content of any feeds sent as updates to the Callback URL
*
* @var string
*/
protected $_feedUpdate = null;
/**
* Holds a manually set subscription key (i.e. identifies a unique
* subscription) which is typical when it is not passed in the query string
* but is part of the Callback URL path, requiring manual retrieval e.g.
* using a route and the Zend_Controller_Action::_getParam() method.
*
* @var string
*/
protected $_subscriptionKey = null;
/**
* After verification, this is set to the verified subscription's data.
*
* @var array
*/
protected $_currentSubscriptionData = null;
/**
* Set a subscription key to use for the current callback request manually.
* Required if usePathParameter is enabled for the Subscriber.
*
* @param string $key
* @return Zend_Feed_Pubsubhubbub_Subscriber_Callback
*/
public function setSubscriptionKey($key)
{
$this->_subscriptionKey = $key;
return $this;
}
/**
* Handle any callback from a Hub Server responding to a subscription or
* unsubscription request. This should be the Hub Server confirming the
* the request prior to taking action on it.
*
* @param array $httpGetData GET data if available and not in $_GET
* @param bool $sendResponseNow Whether to send response now or when asked
* @return void
*/
public function handle(array $httpGetData = null, $sendResponseNow = false)
{
if ($httpGetData === null) {
$httpGetData = $_GET;
}
/**
* Handle any feed updates (sorry for the mess :P)
*
* This DOES NOT attempt to process a feed update. Feed updates
* SHOULD be validated/processed by an asynchronous process so as
* to avoid holding up responses to the Hub.
*/
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post'
&& $this->_hasValidVerifyToken(null, false)
&& ($this->_getHeader('Content-Type') == 'application/atom+xml'
|| $this->_getHeader('Content-Type') == 'application/rss+xml'
|| $this->_getHeader('Content-Type') == 'application/xml'
|| $this->_getHeader('Content-Type') == 'text/xml'
|| $this->_getHeader('Content-Type') == 'application/rdf+xml')
) {
$this->setFeedUpdate($this->_getRawBody());
$this->getHttpResponse()
->setHeader('X-Hub-On-Behalf-Of', $this->getSubscriberCount());
/**
* Handle any (un)subscribe confirmation requests
*/
} elseif ($this->isValidHubVerification($httpGetData)) {
$data = $this->_currentSubscriptionData;
$this->getHttpResponse()->setBody($httpGetData['hub_challenge']);
$data['subscription_state'] = Zend_Feed_Pubsubhubbub::SUBSCRIPTION_VERIFIED;
if (isset($httpGetData['hub_lease_seconds'])) {
$data['lease_seconds'] = $httpGetData['hub_lease_seconds'];
}
$this->getStorage()->setSubscription($data);
/**
* Hey, C'mon! We tried everything else!
*/
} else {
$this->getHttpResponse()->setHttpResponseCode(404);
}
if ($sendResponseNow) {
$this->sendResponse();
}
}
/**
* Checks validity of the request simply by making a quick pass and
* confirming the presence of all REQUIRED parameters.
*
* @param array $httpGetData
* @return bool
*/
public function isValidHubVerification(array $httpGetData)
{
/**
* As per the specification, the hub.verify_token is OPTIONAL. This
* implementation of Pubsubhubbub considers it REQUIRED and will
* always send a hub.verify_token parameter to be echoed back
* by the Hub Server. Therefore, its absence is considered invalid.
*/
if (strtolower($_SERVER['REQUEST_METHOD']) !== 'get') {
return false;
}
$required = array(
'hub_mode',
'hub_topic',
'hub_challenge',
'hub_verify_token',
);
foreach ($required as $key) {
if (!array_key_exists($key, $httpGetData)) {
return false;
}
}
if ($httpGetData['hub_mode'] !== 'subscribe'
&& $httpGetData['hub_mode'] !== 'unsubscribe'
) {
return false;
}
if ($httpGetData['hub_mode'] == 'subscribe'
&& !array_key_exists('hub_lease_seconds', $httpGetData)
) {
return false;
}
if (!Zend_Uri::check($httpGetData['hub_topic'])) {
return false;
}
/**
* Attempt to retrieve any Verification Token Key attached to Callback
* URL's path by our Subscriber implementation
*/
if (!$this->_hasValidVerifyToken($httpGetData)) {
return false;
}
return true;
}
/**
* Sets a newly received feed (Atom/RSS) sent by a Hub as an update to a
* Topic we've subscribed to.
*
* @param string $feed
* @return Zend_Feed_Pubsubhubbub_Subscriber_Callback
*/
public function setFeedUpdate($feed)
{
$this->_feedUpdate = $feed;
return $this;
}
/**
* Check if any newly received feed (Atom/RSS) update was received
*
* @return bool
*/
public function hasFeedUpdate()
{
if (is_null($this->_feedUpdate)) {
return false;
}
return true;
}
/**
* Gets a newly received feed (Atom/RSS) sent by a Hub as an update to a
* Topic we've subscribed to.
*
* @return string
*/
public function getFeedUpdate()
{
return $this->_feedUpdate;
}
/**
* Check for a valid verify_token. By default attempts to compare values
* with that sent from Hub, otherwise merely ascertains its existence.
*
* @param array $httpGetData
* @param bool $checkValue
* @return bool
*/
protected function _hasValidVerifyToken(array $httpGetData = null, $checkValue = true)
{
$verifyTokenKey = $this->_detectVerifyTokenKey($httpGetData);
if (empty($verifyTokenKey)) {
return false;
}
$verifyTokenExists = $this->getStorage()->hasSubscription($verifyTokenKey);
if (!$verifyTokenExists) {
return false;
}
if ($checkValue) {
$data = $this->getStorage()->getSubscription($verifyTokenKey);
$verifyToken = $data['verify_token'];
if ($verifyToken !== hash('sha256', $httpGetData['hub_verify_token'])) {
return false;
}
$this->_currentSubscriptionData = $data;
return true;
}
return true;
}
/**
* Attempt to detect the verification token key. This would be passed in
* the Callback URL (which we are handling with this class!) as a URI
* path part (the last part by convention).
*
* @param null|array $httpGetData
* @return false|string
*/
protected function _detectVerifyTokenKey(array $httpGetData = null)
{
/**
* Available when sub keys encoding in Callback URL path
*/
if (isset($this->_subscriptionKey)) {
return $this->_subscriptionKey;
}
/**
* Available only if allowed by PuSH 0.2 Hubs
*/
if (is_array($httpGetData)
&& isset($httpGetData['xhub_subscription'])
) {
return $httpGetData['xhub_subscription'];
}
/**
* Available (possibly) if corrupted in transit and not part of $_GET
*/
$params = $this->_parseQueryString();
if (isset($params['xhub.subscription'])) {
return rawurldecode($params['xhub.subscription']);
}
return false;
}
/**
* Build an array of Query String parameters.
* This bypasses $_GET which munges parameter names and cannot accept
* multiple parameters with the same key.
*
* @return array|void
*/
protected function _parseQueryString()
{
$params = array();
$queryString = '';
if (isset($_SERVER['QUERY_STRING'])) {
$queryString = $_SERVER['QUERY_STRING'];
}
if (empty($queryString)) {
return array();
}
$parts = explode('&', $queryString);
foreach ($parts as $kvpair) {
$pair = explode('=', $kvpair);
$key = rawurldecode($pair[0]);
$value = rawurldecode($pair[1]);
if (isset($params[$key])) {
if (is_array($params[$key])) {
$params[$key][] = $value;
} else {
$params[$key] = array($params[$key], $value);
}
} else {
$params[$key] = $value;
}
}
return $params;
}
}