Przeglądaj źródła

Adding Zend_Feed_Pubsubhubbub promoted to trunk.
7 tests currently fail - WIP for later


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19794 44c647ce-9c0f-0410-b52a-842ac1e357ba

padraic 16 lat temu
rodzic
commit
02a2a72478

+ 152 - 0
library/Zend/Feed/Pubsubhubbub.php

@@ -0,0 +1,152 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * @see Zend_Http_Client
+ */
+require_once 'Zend/Http/Client.php';
+
+/**
+ * @see Zend_Uri
+ */
+require_once 'Zend/Uri.php';
+
+/**
+ * @see Zend_Version
+ */
+require_once 'Zend/Version.php';
+
+/**
+ * @see Zend_Feed_Reader
+ */
+require_once 'Zend/Feed/Reader.php';
+
+/**
+ * @see Zend_Feed_Abstract
+ */
+require_once 'Zend/Feed/Abstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed_Pubsubhubbub
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub
+{
+    /**
+     * Verification Modes
+     */
+    const VERIFICATION_MODE_SYNC  = 'sync';
+    const VERIFICATION_MODE_ASYNC = 'async';
+    
+    /**
+     * Subscription States
+     */
+    const SUBSCRIPTION_VERIFIED    = 'verified';
+    const SUBSCRIPTION_NOTVERIFIED = 'not_verified';
+    const SUBSCRIPTION_TODELETE    = 'to_delete';
+
+    /**
+     * Singleton instance if required of the HTTP client
+     *
+     * @var Zend_Http_Client
+     */
+    protected static $httpClient = null;
+
+    /**
+     * Simple utility function which imports any feed URL and
+     * determines the existence of Hub Server endpoints. This works
+     * best if directly given an instance of Zend_Feed_Reader_Atom|Rss
+     * to leverage off.
+     *
+     * @param  Zend_Feed_Reader_FeedAbstract|Zend_Feed_Abstract|string $source
+     * @return array
+     */
+    public static function detectHubs($source)
+    {
+        if (is_string($source)) {
+            $feed = Zend_Feed_Reader::import($source);
+        } elseif (is_object($source) && $source instanceof Zend_Feed_Reader_FeedAbstract) {
+            $feed = $source;
+        } elseif (is_object($source) && $source instanceof Zend_Feed_Abstract) {
+            $feed = Zend_Feed_Reader::importFeed($source);
+        } else {
+            require_once 'Zend/Feed/Pubsubhubbub/Exception.php';
+            throw new Zend_Feed_Pubsubhubbub_Exception('The source parameter was'
+            . ' invalid, i.e. not a URL string or an instance of type'
+            . ' Zend_Feed_Reader_FeedAbstract or Zend_Feed_Abstract');
+        }
+        return $feed->getHubs();
+    }
+
+    /**
+     * Allows the external environment to make Zend_Oauth use a specific
+     * Client instance.
+     *
+     * @param  Zend_Http_Client $httpClient
+     * @return void
+     */
+    public static function setHttpClient(Zend_Http_Client $httpClient)
+    {
+        self::$httpClient = $httpClient;
+    }
+
+    /**
+     * Return the singleton instance of the HTTP Client. Note that
+     * the instance is reset and cleared of previous parameters GET/POST.
+     * Headers are NOT reset but handled by this component if applicable.
+     *
+     * @return Zend_Http_Client
+     */
+    public static function getHttpClient()
+    {
+        if (!isset(self::$httpClient)):
+            self::$httpClient = new Zend_Http_Client;
+        else:
+            self::$httpClient->resetParameters();
+        endif;
+        return self::$httpClient;
+    }
+
+    /**
+     * Simple mechanism to delete the entire singleton HTTP Client instance
+     * which forces an new instantiation for subsequent requests.
+     *
+     * @return void
+     */
+    public static function clearHttpClient()
+    {
+        self::$httpClient = null;
+    }
+
+    /**
+     * RFC 3986 safe url encoding method
+     *
+     * @param  string $string
+     * @return string
+     */
+    public static function urlencode($string)
+    {
+        $rawencoded = rawurlencode($string);
+        $rfcencoded = str_replace('%7E', '~', $rawencoded);
+        return $rfcencoded;
+    }
+}

+ 307 - 0
library/Zend/Feed/Pubsubhubbub/CallbackAbstract.php

@@ -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-2009 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-2009 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_ModelAbstract used 
+     * to background save any verification tokens associated with a subscription
+     * or other.
+     *
+     * @var Zend_Feed_Pubsubhubbub_Model_ModelAbstract
+     */
+    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_Storage_StorageInterface used
+     * to background save any verification tokens associated with a subscription
+     * or other.
+     *
+     * @param  Zend_Feed_Pubsubhubbub_Storage_StorageInterface $storage
+     * @return Zend_Feed_Pubsubhubbub_CallbackAbstract
+     */
+    public function setStorage(Zend_Feed_Pubsubhubbub_Model_ModelAbstract $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_ModelAbstract
+     */
+    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_ModelAbstract');
+        }
+        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;
+    }
+}

+ 68 - 0
library/Zend/Feed/Pubsubhubbub/CallbackInterface.php

@@ -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-2009 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-2009 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();
+}

+ 33 - 0
library/Zend/Feed/Pubsubhubbub/Exception.php

@@ -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-2009 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-2009 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
+{}

+ 233 - 0
library/Zend/Feed/Pubsubhubbub/HttpResponse.php

@@ -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-2009 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-2009 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;
+    }
+}

+ 64 - 0
library/Zend/Feed/Pubsubhubbub/Model/ModelAbstract.php

@@ -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-2009 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-2009 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;
+        }
+    }
+
+}

+ 122 - 0
library/Zend/Feed/Pubsubhubbub/Model/Subscription.php

@@ -0,0 +1,122 @@
+<?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-2009 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';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed_Pubsubhubbub
+ * @subpackage Entity
+ * @copyright  Copyright (c) 2005-2009 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
+{
+    
+    /**
+     * 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;
+            $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;
+    }
+    
+}

+ 417 - 0
library/Zend/Feed/Pubsubhubbub/Publisher.php

@@ -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-2009 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-2009 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;
+    }
+}

+ 849 - 0
library/Zend/Feed/Pubsubhubbub/Subscriber.php

@@ -0,0 +1,849 @@
+<?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-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+ 
+require_once 'Zend/Feed/Pubsubhubbub/Entity/TopicSubscription.php';
+
+/**
+ * @see Zend_Feed_Pubsubhubbub
+ */
+require_once 'Zend/Feed/Pubsubhubbub.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed_Pubsubhubbub
+ * @copyright  Copyright (c) 2005-2009 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_Entity used to background
+     * save any verification tokens associated with a subscription or other.
+     *
+     * @var Zend_Feed_Pubsubhubbub_Entity
+     */
+    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_Entity used to background
+     * save any verification tokens associated with a subscription or other.
+     *
+     * @param  Zend_Feed_Pubsubhubbub_Entity $storage
+     * @return Zend_Feed_Pubsubhubbub_Subscriber
+     */
+    public function setStorage(Zend_Feed_Pubsubhubbub_Model_ModelAbstract $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_Storage_StorageInterface
+     */
+    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();
+            echo $client->getLastRequest();
+            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);
+        $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'] = $this->getCallbackUrl()
+                . '/' . Zend_Feed_Pubsubhubbub::urlencode($key);
+        }
+        if ($mode == 'subscribe' && !is_null($this->getLeaseSeconds())) {
+            $params['hub.lease_seconds'] = $this->getLeaseSeconds();
+        }
+
+        // TODO: hub.secret not currently supported
+        $optParams = $this->getParameters();
+        foreach ($optParams as $name => $value) {
+            $params[$name] = $value;
+        }
+        
+        // store subscription to storage
+        $data = array(
+            'id'                 => $key,
+            'topic_url'          => $params['hub.topic'],
+            'hub_url'            => $hubUrl,
+            'created_time'       => time(),
+            'last_modified'      => time(),
+            'lease_seconds'      => isset($params['hub.lease_seconds']) ? $params['hub.lease_seconds'] : null,
+            'verify_token'       => hash('sha256', $params['hub.verify_token']),
+            'secret'             => null,
+            'expiration_time'    => isset($params['hub.lease_seconds']) ? time() + $params['hub.lease_seconds'] : null,
+            '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)
+    {
+        $keyBase = $params['hub.topic'] . $params['hub.callback'];
+        $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;
+    }
+}

+ 323 - 0
library/Zend/Feed/Pubsubhubbub/Subscriber/Callback.php

@@ -0,0 +1,323 @@
+<?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-2009 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-2009 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/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['verified'] = 1;
+            $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;
+    }
+}

+ 4 - 0
tests/Zend/Feed/AllTests.php

@@ -61,6 +61,8 @@ require_once 'Zend/Feed/Writer/Renderer/Entry/RssTest.php';
 require_once 'Zend/Feed/Writer/Extension/ITunes/EntryTest.php';
 require_once 'Zend/Feed/Writer/Extension/ITunes/FeedTest.php';
 
+require_once 'Zend/Feed/Pubsubhubbub/AllTests.php';
+
 /**
  * @category   Zend
  * @package    Zend_Feed
@@ -125,6 +127,8 @@ class Zend_Feed_AllTests
         
         $suite->addTestSuite('Zend_Feed_Writer_Extension_ITunes_EntryTest');
         $suite->addTestSuite('Zend_Feed_Writer_Extension_ITunes_FeedTest');
+        
+        $suite->addTest(Zend_Feed_Pubsubhubbub_AllTests::suite());
 
         return $suite;
     }

+ 71 - 0
tests/Zend/Feed/Pubsubhubbub/AllTests.php

@@ -0,0 +1,71 @@
+<?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    UnitTests
+ * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Feed_Pubsubhubbub_AllTests::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+require_once 'PHPUnit/Framework/TestSuite.php';
+require_once 'PHPUnit/TextUI/TestRunner.php';
+
+require_once 'Zend/Feed/Pubsubhubbub/PubsubhubbubTest.php';
+require_once 'Zend/Feed/Pubsubhubbub/PublisherTest.php';
+require_once 'Zend/Feed/Pubsubhubbub/SubscriberTest.php';
+require_once 'Zend/Feed/Pubsubhubbub/SubscriberHttpTest.php';
+require_once 'Zend/Feed/Pubsubhubbub/Subscriber/CallbackTest.php';
+
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub_AllTests
+{
+    public static function main()
+    {
+        PHPUnit_TextUI_TestRunner::run(self::suite());
+    }
+
+    public static function suite()
+    {
+        $suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend');
+
+        $suite->addTestSuite('Zend_Feed_Pubsubhubbub_PubsubhubbubTest');
+        $suite->addTestSuite('Zend_Feed_Pubsubhubbub_PublisherTest');
+        $suite->addTestSuite('Zend_Feed_Pubsubhubbub_SubscriberTest');
+        $suite->addTestSuite('Zend_Feed_Pubsubhubbub_SubscriberHttpTest');
+        $suite->addTestSuite('Zend_Feed_Pubsubhubbub_Subscriber_CallbackTest');
+
+        return $suite;
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Feed_Pubsubhubbub_AllTests::main') {
+    Zend_Feed_Pubsubhubbub_AllTests::main();
+}

+ 321 - 0
tests/Zend/Feed/Pubsubhubbub/PublisherTest.php

@@ -0,0 +1,321 @@
+<?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    UnitTests
+ * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'Zend/Feed/Pubsubhubbub/Publisher.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub_PublisherTest extends PHPUnit_Framework_TestCase
+{
+
+    protected $_publisher = null;
+
+    public function setUp()
+    {
+        $client = new Zend_Http_Client;
+        Zend_Feed_Pubsubhubbub::setHttpClient($client);
+        $this->_publisher = new Zend_Feed_Pubsubhubbub_Publisher;
+    }
+
+    public function testAddsHubServerUrl()
+    {
+        $this->_publisher->addHubUrl('http://www.example.com/hub');
+        $this->assertEquals(array('http://www.example.com/hub'), $this->_publisher->getHubUrls());
+    }
+
+    public function testAddsHubServerUrlsFromArray()
+    {
+        $this->_publisher->addHubUrls(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ));
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), $this->_publisher->getHubUrls());
+    }
+
+    public function testAddsHubServerUrlsFromArrayUsingSetConfig()
+    {
+        $this->_publisher->setConfig(array('hubUrls' => array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        )));
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), $this->_publisher->getHubUrls());
+    }
+
+    public function testRemovesHubServerUrl()
+    {
+        $this->_publisher->addHubUrls(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ));
+        $this->_publisher->removeHubUrl('http://www.example.com/hub');
+        $this->assertEquals(array(
+            1 => 'http://www.example.com/hub2'
+        ), $this->_publisher->getHubUrls());
+    }
+
+    public function testRetrievesUniqueHubServerUrlsOnly()
+    {
+        $this->_publisher->addHubUrls(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2',
+            'http://www.example.com/hub'
+        ));
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), $this->_publisher->getHubUrls());
+    }
+
+    public function testThrowsExceptionOnSettingEmptyHubServerUrl()
+    {
+        try {
+            $this->_publisher->addHubUrl('');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingNonStringHubServerUrl()
+    {
+        try {
+            $this->_publisher->addHubUrl(123);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingInvalidHubServerUrl()
+    {
+        try {
+            $this->_publisher->addHubUrl('http://');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testAddsUpdatedTopicUrl()
+    {
+        $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
+        $this->assertEquals(array('http://www.example.com/topic'), $this->_publisher->getUpdatedTopicUrls());
+    }
+
+    public function testAddsUpdatedTopicUrlsFromArray()
+    {
+        $this->_publisher->addUpdatedTopicUrls(array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2'
+        ));
+        $this->assertEquals(array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2'
+        ), $this->_publisher->getUpdatedTopicUrls());
+    }
+
+    public function testAddsUpdatedTopicUrlsFromArrayUsingSetConfig()
+    {
+        $this->_publisher->setConfig(array('updatedTopicUrls' => array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2'
+        )));
+        $this->assertEquals(array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2'
+        ), $this->_publisher->getUpdatedTopicUrls());
+    }
+
+    public function testRemovesUpdatedTopicUrl()
+    {
+        $this->_publisher->addUpdatedTopicUrls(array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2'
+        ));
+        $this->_publisher->removeUpdatedTopicUrl('http://www.example.com/topic');
+        $this->assertEquals(array(
+            1 => 'http://www.example.com/topic2'
+        ), $this->_publisher->getUpdatedTopicUrls());
+    }
+
+    public function testRetrievesUniqueUpdatedTopicUrlsOnly()
+    {
+        $this->_publisher->addUpdatedTopicUrls(array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2',
+            'http://www.example.com/topic'
+        ));
+        $this->assertEquals(array(
+            'http://www.example.com/topic', 'http://www.example.com/topic2'
+        ), $this->_publisher->getUpdatedTopicUrls());
+    }
+
+    public function testThrowsExceptionOnSettingEmptyUpdatedTopicUrl()
+    {
+        try {
+            $this->_publisher->addUpdatedTopicUrl('');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingNonStringUpdatedTopicUrl()
+    {
+        try {
+            $this->_publisher->addUpdatedTopicUrl(123);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingInvalidUpdatedTopicUrl()
+    {
+        try {
+            $this->_publisher->addUpdatedTopicUrl('http://');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testAddsParameter()
+    {
+        $this->_publisher->setParameter('foo', 'bar');
+        $this->assertEquals(array('foo'=>'bar'), $this->_publisher->getParameters());
+    }
+
+    public function testAddsParametersFromArray()
+    {
+        $this->_publisher->setParameters(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->assertEquals(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ), $this->_publisher->getParameters());
+    }
+
+    public function testAddsParametersFromArrayInSingleMethod()
+    {
+        $this->_publisher->setParameter(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->assertEquals(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ), $this->_publisher->getParameters());
+    }
+
+    public function testAddsParametersFromArrayUsingSetConfig()
+    {
+        $this->_publisher->setConfig(array('parameters' => array(
+            'foo' => 'bar', 'boo' => 'baz'
+        )));
+        $this->assertEquals(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ), $this->_publisher->getParameters());
+    }
+
+    public function testRemovesParameter()
+    {
+        $this->_publisher->setParameters(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->_publisher->removeParameter('boo');
+        $this->assertEquals(array(
+            'foo' => 'bar'
+        ), $this->_publisher->getParameters());
+    }
+
+    public function testRemovesParameterIfSetToNull()
+    {
+        $this->_publisher->setParameters(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->_publisher->setParameter('boo', null);
+        $this->assertEquals(array(
+            'foo' => 'bar'
+        ), $this->_publisher->getParameters());
+    }
+
+    public function testNotifiesHubWithCorrectParameters()
+    {
+        Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess);
+        $client = Zend_Feed_Pubsubhubbub::getHttpClient();
+        $this->_publisher->addHubUrl('http://www.example.com/hub');
+        $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
+        $this->_publisher->setParameter('foo', 'bar');
+        $this->_publisher->notifyAll();
+        $this->assertEquals('hub.mode=publish&hub.url=http%3A%2F%2Fwww.example.com%2Ftopic&foo=bar', $client->getBody());
+    }
+
+    public function testNotifiesHubWithCorrectParametersAndMultipleTopics()
+    {
+        Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess);
+        $client = Zend_Feed_Pubsubhubbub::getHttpClient();
+        $this->_publisher->addHubUrl('http://www.example.com/hub');
+        $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
+        $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic2');
+        $this->_publisher->notifyAll();
+        $this->assertEquals('hub.mode=publish&hub.url=http%3A%2F%2Fwww.example.com%2Ftopic&hub.url=http%3A%2F%2Fwww.example.com%2Ftopic2', $client->getBody());
+    }
+
+    public function testNotifiesHubAndReportsSuccess()
+    {
+        Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess);
+        $client = Zend_Feed_Pubsubhubbub::getHttpClient();
+        $this->_publisher->addHubUrl('http://www.example.com/hub');
+        $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
+        $this->_publisher->setParameter('foo', 'bar');
+        $this->_publisher->notifyAll();
+        $this->assertTrue($this->_publisher->isSuccess());
+    }
+
+    public function testNotifiesHubAndReportsFail()
+    {
+        Zend_Feed_Pubsubhubbub::setHttpClient(new Zend_Feed_Pubsubhubbub_PublisherTest_ClientFail);
+        $client = Zend_Feed_Pubsubhubbub::getHttpClient();
+        $this->_publisher->addHubUrl('http://www.example.com/hub');
+        $this->_publisher->addUpdatedTopicUrl('http://www.example.com/topic');
+        $this->_publisher->setParameter('foo', 'bar');
+        $this->_publisher->notifyAll();
+        $this->assertFalse($this->_publisher->isSuccess());
+    }
+
+}
+
+// Some stubs for what Http_Client would be doing
+
+class Zend_Feed_Pubsubhubbub_PublisherTest_ClientSuccess extends Zend_Http_Client
+{
+    public function request($method = null) {
+        $response = new Zend_Feed_Pubsubhubbub_PublisherTest_ResponseSuccess;
+        return $response;
+    }
+    public function getBody(){return $this->_prepareBody();}
+}
+class Zend_Feed_Pubsubhubbub_PublisherTest_ClientFail extends Zend_Http_Client
+{
+    public function request($method = null) {
+        $response = new Zend_Feed_Pubsubhubbub_PublisherTest_ResponseFail;
+        return $response;
+    }
+    public function getBody(){return $this->_prepareBody();}
+}
+class Zend_Feed_Pubsubhubbub_PublisherTest_ResponseSuccess
+{
+    public function getStatus(){return 204;}
+}
+class Zend_Feed_Pubsubhubbub_PublisherTest_ResponseFail
+{
+    public function getStatus(){return 404;}
+}

+ 56 - 0
tests/Zend/Feed/Pubsubhubbub/PubsubhubbubTest.php

@@ -0,0 +1,56 @@
+<?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    UnitTests
+ * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'Zend/Feed/Pubsubhubbub.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub_PubsubhubbubTest extends PHPUnit_Framework_TestCase
+{
+
+    public function teardown()
+    {
+        Zend_Feed_Pubsubhubbub::clearHttpClient();
+    }
+
+    public function testCanSetCustomHttpClient()
+    {
+        Zend_Feed_Pubsubhubbub::setHttpClient(new Test_Http_Client_Pubsub());
+        $this->assertType('Test_Http_Client_Pubsub', Zend_Feed_Pubsubhubbub::getHttpClient());
+    }
+
+    public function testCanDetectHubs()
+    {
+        $feed = Zend_Feed_Reader::importFile(dirname(__FILE__) . '/_files/rss20.xml');
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), Zend_Feed_Pubsubhubbub::detectHubs($feed));
+    }
+
+}
+
+class Test_Http_Client_Pubsub extends Zend_Http_Client {}

+ 366 - 0
tests/Zend/Feed/Pubsubhubbub/Subscriber/CallbackTest.php

@@ -0,0 +1,366 @@
+<?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    UnitTests
+ * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'Zend/Feed/Pubsubhubbub/Subscriber/Callback.php';
+require_once 'Zend/Feed/Pubsubhubbub/Model/Subscription.php';
+require_once 'Zend/Db/Table/Rowset/Abstract.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTest extends PHPUnit_Framework_TestCase
+{
+
+    protected $_originalServer = null;
+
+    public function setUp()
+    {
+        $this->_callback = new Zend_Feed_Pubsubhubbub_Subscriber_Callback;
+        
+        $this->_adapter = $this->_getCleanMock(
+            'Zend_Db_Adapter_Abstract'
+        );
+        $this->_tableGateway = $this->_getCleanMock(
+            'Zend_Db_Table_Abstract'
+        );
+        $this->_rowset = $this->_getCleanMock(
+            'Zend_Db_Table_Rowset_Abstract'
+        );
+        
+        $this->_tableGateway->expects($this->any())->method('getAdapter')
+            ->will($this->returnValue($this->_adapter));
+        $storage = new Zend_Feed_Pubsubhubbub_Model_Subscription($this->_tableGateway);
+        $this->_callback->setStorage($storage);
+
+        $this->_get = array(
+            'hub_mode' => 'subscribe',
+            'hub_topic' => 'http://www.example.com/topic',
+            'hub_challenge' => 'abc',
+            'hub_verify_token' => 'cba',
+            'hub_mode' => 'subscribe',
+            'hub_lease_seconds' => '1234567'
+        );
+
+        $this->_originalServer = $_SERVER;
+        $_SERVER['REQUEST_METHOD'] = 'get';
+        $_SERVER['QUERY_STRING'] = 'xhub.subscription=verifytokenkey';
+    }
+
+    public function tearDown()
+    {
+        $_SERVER = $this->_originalServer;
+    }
+
+
+    public function testCanSetHttpResponseObject()
+    {
+        $this->_callback->setHttpResponse(new Zend_Feed_Pubsubhubbub_HttpResponse);
+        $this->assertTrue($this->_callback->getHttpResponse() instanceof Zend_Feed_Pubsubhubbub_HttpResponse);
+    }
+
+    public function testCanUsesDefaultHttpResponseObject()
+    {
+        $this->assertTrue($this->_callback->getHttpResponse() instanceof Zend_Feed_Pubsubhubbub_HttpResponse);
+    }
+
+    public function testThrowsExceptionOnInvalidHttpResponseObjectSet()
+    {
+        try {
+            $this->_callback->setHttpResponse(new stdClass);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionIfNonObjectSetAsHttpResponseObject()
+    {
+        try {
+            $this->_callback->setHttpResponse('');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testCanSetSubscriberCount()
+    {
+        $this->_callback->setSubscriberCount('10000');
+        $this->assertEquals(10000, $this->_callback->getSubscriberCount());
+    }
+
+    public function testDefaultSubscriberCountIsOne()
+    {
+        $this->assertEquals(1, $this->_callback->getSubscriberCount());
+    }
+
+    public function testThrowsExceptionOnSettingZeroAsSubscriberCount()
+    {
+        try {
+            $this->_callback->setSubscriberCount(0);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnSettingLessThanZeroAsSubscriberCount()
+    {
+        try {
+            $this->_callback->setSubscriberCount(-1);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnSettingAnyScalarTypeCastToAZeroOrLessIntegerAsSubscriberCount()
+    {
+        try {
+            $this->_callback->setSubscriberCount('0aa');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testCanSetStorageImplementation()
+    {
+	    $storage = new Zend_Feed_Pubsubhubbub_Model_ModelAbstract($this->_tableGateway);
+        $this->_callback->setStorage($storage);
+        $this->assertThat($this->_callback->getStorage(), $this->identicalTo($storage));
+    }
+
+    /**
+     * @group ZFP001
+     */
+    public function testValidatesValidHttpGetData()
+    {
+        $this->_adapter->expects($this->once())
+            ->method('find')
+            ->with($this->equalTo('verifytokenkey'))
+            ->will($this->returnValue($this->_rowset));
+        $this->_rowset->expects($this->once())
+            ->method('current')
+            ->will($this->returnValue(array(
+                'verify_token' => hash('sha256', 'verifytokenkey')
+            )));
+        $this->assertTrue($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfHubVerificationNotAGetRequest()
+    {
+        $_SERVER['REQUEST_METHOD'] = 'POST';
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfModeMissingFromHttpGetData()
+    {
+        unset($this->_get['hub_mode']);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfTopicMissingFromHttpGetData()
+    {
+        unset($this->_get['hub_topic']);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfChallengeMissingFromHttpGetData()
+    {
+        unset($this->_get['hub_challenge']);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfVerifyTokenMissingFromHttpGetData()
+    {
+        unset($this->_get['hub_verify_token']);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsTrueIfModeSetAsUnsubscribeFromHttpGetData()
+    {
+        $this->_get['hub_mode'] = 'unsubscribe';
+        $this->assertTrue($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfModeNotRecognisedFromHttpGetData()
+    {
+        $this->_get['hub_mode'] = 'abc';
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfLeaseSecondsMissedWhenModeIsSubscribeFromHttpGetData()
+    {
+        unset($this->_get['hub_lease_seconds']);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfHubTopicInvalidFromHttpGetData()
+    {
+        $this->_get['hub_topic'] = 'http://';
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfVerifyTokenRecordDoesNotExistForConfirmRequest()
+    {
+        //$this->_callback->setStorage(new Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testReturnsFalseIfVerifyTokenRecordDoesNotAgreeWithConfirmRequest()
+    {
+        //$this->_callback->setStorage(new Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong);
+        $this->assertFalse($this->_callback->isValidHubVerification($this->_get));
+    }
+
+    public function testRespondsToInvalidConfirmationWith404Response()
+    {
+        unset($this->_get['hub_mode']);
+        $this->_callback->handle($this->_get);
+        $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
+    }
+
+    public function testRespondsToValidConfirmationWith200Response()
+    {
+        $this->_callback->handle($this->_get);
+        $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
+    }
+
+    public function testRespondsToValidConfirmationWithBodyContainingHubChallenge()
+    {
+        $this->_callback->handle($this->_get);
+        $this->assertTrue($this->_callback->getHttpResponse()->getBody() == 'abc');
+    }
+
+    public function testRespondsToValidFeedUpdateRequestWith200Response()
+    {
+        $_SERVER['REQUEST_METHOD'] = 'POST';
+        $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
+        $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
+        $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
+        $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml; // dirty  alternative to php://input
+        $this->_callback->handle(array());
+        $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
+    }
+
+    public function testRespondsToInvalidFeedUpdateNotPostWith404Response()
+    {   // yes, this example makes no sense for GET - I know!!!
+        $_SERVER['REQUEST_METHOD'] = 'GET';
+        $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
+        $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
+        $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
+        $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
+        $this->_callback->handle(array());
+        $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
+    }
+
+    public function testRespondsToInvalidFeedUpdateWrongMimeWith404Response()
+    {
+        $_SERVER['REQUEST_METHOD'] = 'POST';
+        $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
+        $_SERVER['CONTENT_TYPE'] = 'application/kml+xml';
+        $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
+        $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
+        $this->_callback->handle(array());
+        $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 404);
+    }
+
+    /**
+     * As a judgement call, we must respond to any successful request, regardless
+     * of the wellformedness of any XML payload, by returning a 2xx response code.
+     * The validation of feeds and their processing must occur outside the Hubbub
+     * protocol.
+     */
+    public function testRespondsToInvalidFeedUpdateWrongFeedTypeForMimeWith200Response()
+    {
+        $_SERVER['REQUEST_METHOD'] = 'POST';
+        $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
+        $_SERVER['CONTENT_TYPE'] = 'application/rss+xml';
+        $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
+        $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
+        $this->_callback->handle(array());
+        $this->assertTrue($this->_callback->getHttpResponse()->getHttpResponseCode() == 200);
+    }
+
+    public function testRespondsToValidFeedUpdateWithXHubOnBehalfOfHeader()
+    {
+        $_SERVER['REQUEST_METHOD'] = 'POST';
+        $_SERVER['REQUEST_URI'] = '/some/path/callback/verifytokenkey';
+        $_SERVER['CONTENT_TYPE'] = 'application/atom+xml';
+        $feedXml = file_get_contents(dirname(__FILE__) . '/_files/atom10.xml');
+        $GLOBALS['HTTP_RAW_POST_DATA'] = $feedXml;
+        $this->_callback->handle(array());
+        $this->assertTrue($this->_callback->getHttpResponse()->getHeader('X-Hub-On-Behalf-Of') == 1);
+    }
+    
+    protected function _getCleanMock($className) {
+        $class = new ReflectionClass($className);
+        $methods = $class->getMethods();
+        $stubMethods = array();
+        foreach ($methods as $method) {
+            if ($method->isPublic() || ($method->isProtected()
+            && $method->isAbstract())) {
+            $stubMethods[] = $method->getName();
+            }
+        }
+        $mocked = $this->getMock(
+            $className,
+            $stubMethods,
+            array(),
+            $className . '_PubsubSubscriberMock_' . uniqid(),
+            false
+        );
+        return $mocked;
+    }
+
+}
+
+/**
+ * Stubs for storage access
+ * DEPRECATED
+class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHas implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
+{
+    public function setSubscription($key, array $data){}
+    public function getSubscription($key){
+        if ($key == 'verifytokenkey') {
+            return array(
+                'id' => 'verifytokenkey',
+                'verify_token' => hash('sha256', 'cba')
+            );
+        }
+    }
+    public function hasSubscription($key){return true;}
+    public function removeSubscription($key){}
+    public function cleanup($type){}
+}
+class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasNot implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
+{
+    public function setSubscription($key, array $data){}
+    public function getSubscription($key){}
+    public function hasSubscription($key){return false;}
+    public function removeSubscription($key){}
+    public function cleanup($type){}
+}
+class Zend_Feed_Pubsubhubbub_Subscriber_CallbackTestStorageHasButWrong implements Zend_Feed_Pubsubhubbub_Storage_StorageInterface
+{
+    public function setSubscription($key, array $data){}
+    public function getSubscription($key){return 'wrong';}
+    public function hasSubscription($key){return true;}
+    public function removeSubscription($key){}
+    public function cleanup($type){}
+}*/

+ 4 - 0
tests/Zend/Feed/Pubsubhubbub/Subscriber/_files/atom10.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+    <link href="http://www.example.com"/>
+</feed>

+ 136 - 0
tests/Zend/Feed/Pubsubhubbub/SubscriberHttpTest.php

@@ -0,0 +1,136 @@
+<?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    UnitTests
+ * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'Zend/Feed/Pubsubhubbub/Subscriber.php';
+require_once 'Zend/Http/Client.php';
+require_once 'Zend/Http/Client/Adapter/Socket.php';
+require_once 'Zend/Uri/Http.php';
+
+/**
+ * Note that $this->_baseuri must point to a directory on a web server
+ * containing all the files under the _files directory. You should symlink
+ * or copy these files and set '_baseuri' properly using the constant in
+ * TestConfiguration.php (based on TestConfiguration.php.dist)
+ *
+ * You can also set the proper constant in your test configuration file to
+ * point to the right place.
+ *
+ * @category   Zend
+ * @package    Zend_Feed
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub_SubscriberHttpTest extends PHPUnit_Framework_TestCase
+{
+
+    protected $_subscriber = null;
+
+    protected $_baseuri;
+
+    protected $_client = null;
+
+    protected $_adapter = null;
+
+    protected $_config = array(
+        'adapter'     => 'Zend_Http_Client_Adapter_Socket'
+    );
+
+    public function setUp()
+    {
+        if (defined('TESTS_Zend_Feed_Pubsubhubbub_BASEURI') &&
+            Zend_Uri_Http::check(TESTS_Zend_Feed_Pubsubhubbub_BASEURI)) {
+            $this->_baseuri = TESTS_Zend_Feed_Pubsubhubbub_BASEURI;
+            if (substr($this->_baseuri, -1) != '/') $this->_baseuri .= '/';
+            $name = $this->getName();
+            if (($pos = strpos($name, ' ')) !== false) {
+                $name = substr($name, 0, $pos);
+            }
+            $uri = $this->_baseuri . $name . '.php';
+            $this->_adapter = new $this->_config['adapter'];
+            $this->_client = new Zend_Http_Client($uri, $this->_config);
+            $this->_client->setAdapter($this->_adapter);
+            Zend_Feed_Pubsubhubbub::setHttpClient($this->_client);
+            $this->_subscriber = new Zend_Feed_Pubsubhubbub_Subscriber;
+            
+            
+            $this->_storage = $this->_getCleanMock('Zend_Feed_Pubsubhubbub_Entity_TopicSubscription');
+            $this->_subscriber->setStorage($this->_storage);
+
+        } else {
+            // Skip tests
+            $this->markTestSkipped("Zend_Feed_Pubsubhubbub_Subscriber dynamic tests'
+            . ' are not enabled in TestConfiguration.php");
+        }
+    }
+
+    public function testSubscriptionRequestSendsExpectedPostData()
+    {
+        $this->_subscriber->setTopicUrl('http://www.example.com/topic');
+        $this->_subscriber->addHubUrl($this->_baseuri . '/testRawPostData.php');
+        $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
+        $this->_subscriber->setTestStaticToken('abc'); // override for testing
+        $this->_subscriber->subscribeAll();
+        $this->assertEquals(
+            'hub.callback=http%3A%2F%2Fwww.example.com%2Fcallback%3Fxhub.subscription%3D5536df06b5d'
+            .'cb966edab3a4c4d56213c16a8184b&hub.lease_seconds=2592000&hub.mode='
+            .'subscribe&hub.topic=http%3A%2F%2Fwww.example.com%2Ftopic&hub.veri'
+            .'fy=sync&hub.verify=async&hub.verify_token=abc',
+            $this->_client->getLastResponse()->getBody());
+    }
+
+    public function testUnsubscriptionRequestSendsExpectedPostData()
+    {
+        $this->_subscriber->setTopicUrl('http://www.example.com/topic');
+        $this->_subscriber->addHubUrl($this->_baseuri . '/testRawPostData.php');
+        $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
+        $this->_subscriber->setTestStaticToken('abc'); //override for testing
+        $this->_subscriber->unsubscribeAll();
+        $this->assertEquals(
+            'hub.callback=http%3A%2F%2Fwww.example.com%2Fcallback%3Fxhub.subscription%3D5536df06b5d'
+            .'cb966edab3a4c4d56213c16a8184b&hub.mode=unsubscribe&hub.topic=http'
+            .'%3A%2F%2Fwww.example.com%2Ftopic&hub.verify=sync&hub.verify=async'
+            .'&hub.verify_token=abc',
+            $this->_client->getLastResponse()->getBody());
+    }
+    
+    protected function _getCleanMock($className) {
+        $class = new ReflectionClass($className);
+        $methods = $class->getMethods();
+        $stubMethods = array();
+        foreach ($methods as $method) {
+            if ($method->isPublic() || ($method->isProtected()
+            && $method->isAbstract())) {
+                $stubMethods[] = $method->getName();
+            }
+        }
+        $mocked = $this->getMock(
+            $className,
+            $stubMethods,
+            array(),
+            $className . '_SubscriberHttpTestMock_' . uniqid(),
+            false
+        );
+        return $mocked;
+    }
+
+}

+ 352 - 0
tests/Zend/Feed/Pubsubhubbub/SubscriberTest.php

@@ -0,0 +1,352 @@
+<?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    UnitTests
+ * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'Zend/Feed/Pubsubhubbub/Subscriber.php';
+require_once 'Zend/Feed/Pubsubhubbub/Model/Subscription.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Feed
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Feed_Pubsubhubbub_SubscriberTest extends PHPUnit_Framework_TestCase
+{
+
+    protected $_subscriber = null;
+    
+    protected $_adapter = null;
+    
+    protected $_tableGateway = null;
+
+    public function setUp()
+    {
+        $client = new Zend_Http_Client;
+        Zend_Feed_Pubsubhubbub::setHttpClient($client);
+        $this->_subscriber = new Zend_Feed_Pubsubhubbub_Subscriber;
+        $this->_adapter = $this->_getCleanMock(
+            'Zend_Db_Adapter_Abstract'
+        );
+        $this->_tableGateway = $this->_getCleanMock(
+            'Zend_Db_Table_Abstract'
+        );
+        $this->_tableGateway->expects($this->any())->method('getAdapter')
+            ->will($this->returnValue($this->_adapter));
+    }
+
+
+    public function testAddsHubServerUrl()
+    {
+        $this->_subscriber->addHubUrl('http://www.example.com/hub');
+        $this->assertEquals(array('http://www.example.com/hub'), $this->_subscriber->getHubUrls());
+    }
+
+    public function testAddsHubServerUrlsFromArray()
+    {
+        $this->_subscriber->addHubUrls(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ));
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), $this->_subscriber->getHubUrls());
+    }
+
+    public function testAddsHubServerUrlsFromArrayUsingSetConfig()
+    {
+        $this->_subscriber->setConfig(array('hubUrls' => array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        )));
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), $this->_subscriber->getHubUrls());
+    }
+
+    public function testRemovesHubServerUrl()
+    {
+        $this->_subscriber->addHubUrls(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ));
+        $this->_subscriber->removeHubUrl('http://www.example.com/hub');
+        $this->assertEquals(array(
+            1 => 'http://www.example.com/hub2'
+        ), $this->_subscriber->getHubUrls());
+    }
+
+    public function testRetrievesUniqueHubServerUrlsOnly()
+    {
+        $this->_subscriber->addHubUrls(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2',
+            'http://www.example.com/hub'
+        ));
+        $this->assertEquals(array(
+            'http://www.example.com/hub', 'http://www.example.com/hub2'
+        ), $this->_subscriber->getHubUrls());
+    }
+
+    public function testThrowsExceptionOnSettingEmptyHubServerUrl()
+    {
+        try {
+            $this->_subscriber->addHubUrl('');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnSettingNonStringHubServerUrl()
+    {
+        try {
+            $this->_subscriber->addHubUrl(123);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnSettingInvalidHubServerUrl()
+    {
+        try {
+            $this->_subscriber->addHubUrl('http://');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testAddsParameter()
+    {
+        $this->_subscriber->setParameter('foo', 'bar');
+        $this->assertEquals(array('foo'=>'bar'), $this->_subscriber->getParameters());
+    }
+
+    public function testAddsParametersFromArray()
+    {
+        $this->_subscriber->setParameters(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->assertEquals(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ), $this->_subscriber->getParameters());
+    }
+
+    public function testAddsParametersFromArrayInSingleMethod()
+    {
+        $this->_subscriber->setParameter(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->assertEquals(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ), $this->_subscriber->getParameters());
+    }
+
+    public function testAddsParametersFromArrayUsingSetConfig()
+    {
+        $this->_subscriber->setConfig(array('parameters' => array(
+            'foo' => 'bar', 'boo' => 'baz'
+        )));
+        $this->assertEquals(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ), $this->_subscriber->getParameters());
+    }
+
+    public function testRemovesParameter()
+    {
+        $this->_subscriber->setParameters(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->_subscriber->removeParameter('boo');
+        $this->assertEquals(array(
+            'foo' => 'bar'
+        ), $this->_subscriber->getParameters());
+    }
+
+    public function testRemovesParameterIfSetToNull()
+    {
+        $this->_subscriber->setParameters(array(
+            'foo' => 'bar', 'boo' => 'baz'
+        ));
+        $this->_subscriber->setParameter('boo', null);
+        $this->assertEquals(array(
+            'foo' => 'bar'
+        ), $this->_subscriber->getParameters());
+    }
+
+    public function testCanSetTopicUrl()
+    {
+        $this->_subscriber->setTopicUrl('http://www.example.com/topic');
+        $this->assertEquals('http://www.example.com/topic', $this->_subscriber->getTopicUrl());
+    }
+
+    public function testThrowsExceptionOnSettingEmptyTopicUrl()
+    {
+        try {
+            $this->_subscriber->setTopicUrl('');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingNonStringTopicUrl()
+    {
+        try {
+            $this->_subscriber->setTopicUrl(123);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingInvalidTopicUrl()
+    {
+        try {
+            $this->_subscriber->setTopicUrl('http://');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnMissingTopicUrl()
+    {
+        try {
+            $this->_subscriber->getTopicUrl();
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testCanSetCallbackUrl()
+    {
+        $this->_subscriber->setCallbackUrl('http://www.example.com/callback');
+        $this->assertEquals('http://www.example.com/callback', $this->_subscriber->getCallbackUrl());
+    }
+
+    public function testThrowsExceptionOnSettingEmptyCallbackUrl()
+    {
+        try {
+            $this->_subscriber->setCallbackUrl('');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingNonStringCallbackUrl()
+    {
+        try {
+            $this->_subscriber->setCallbackUrl(123);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+
+    public function testThrowsExceptionOnSettingInvalidCallbackUrl()
+    {
+        try {
+            $this->_subscriber->setCallbackUrl('http://');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnMissingCallbackUrl()
+    {
+        try {
+            $this->_subscriber->getCallbackUrl();
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testCanSetLeaseSeconds()
+    {
+        $this->_subscriber->setLeaseSeconds('10000');
+        $this->assertEquals(10000, $this->_subscriber->getLeaseSeconds());
+    }
+
+    public function testThrowsExceptionOnSettingZeroAsLeaseSeconds()
+    {
+        try {
+            $this->_subscriber->setLeaseSeconds(0);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnSettingLessThanZeroAsLeaseSeconds()
+    {
+        try {
+            $this->_subscriber->setLeaseSeconds(-1);
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testThrowsExceptionOnSettingAnyScalarTypeCastToAZeroOrLessIntegerAsLeaseSeconds()
+    {
+        try {
+            $this->_subscriber->setLeaseSeconds('0aa');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testCanSetPreferredVerificationMode()
+    {
+        $this->_subscriber->setPreferredVerificationMode(Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC);
+        $this->assertEquals(Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_ASYNC, $this->_subscriber->getPreferredVerificationMode());
+    }
+
+    public function testSetsPreferredVerificationModeThrowsExceptionOnSettingBadMode()
+    {
+        try {
+            $this->_subscriber->setPreferredVerificationMode('abc');
+            $this->fail('Should not fail as an Exception would be raised and caught');
+        } catch (Zend_Feed_Pubsubhubbub_Exception $e) {}
+    }
+
+    public function testPreferredVerificationModeDefaultsToSync()
+    {
+        $this->assertEquals(Zend_Feed_Pubsubhubbub::VERIFICATION_MODE_SYNC, $this->_subscriber->getPreferredVerificationMode());
+    }
+
+    public function testCanSetStorageImplementation()
+    {
+	    $storage = new Zend_Feed_Pubsubhubbub_Model_Subscription($this->_tableGateway);
+        $this->_subscriber->setStorage($storage);
+        $this->assertThat($this->_subscriber->getStorage(), $this->identicalTo($storage));
+    }
+
+    /**
+     * @expectedException Zend_Feed_Pubsubhubbub_Exception
+     */
+    public function testGetStorageThrowsExceptionIfNoneSet()
+    {
+        $this->_subscriber->getStorage();
+    }
+    
+    protected function _getCleanMock($className) {
+        $class = new ReflectionClass($className);
+        $methods = $class->getMethods();
+        $stubMethods = array();
+        foreach ($methods as $method) {
+            if ($method->isPublic() || ($method->isProtected()
+            && $method->isAbstract())) {
+            $stubMethods[] = $method->getName();
+            }
+        }
+        $mocked = $this->getMock(
+            $className,
+            $stubMethods,
+            array(),
+            $className . '_PubsubSubscriberMock_' . uniqid(),
+            false
+        );
+        return $mocked;
+    }
+
+}

+ 8 - 0
tests/Zend/Feed/Pubsubhubbub/_files/rss20.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<rss version="2.0"
+xmlns:atom="http://www.w3.org/2005/Atom">
+    <channel>
+        <atom:link rel="hub" href="http://www.example.com/hub"/>
+        <atom:link rel="hub" href="http://www.example.com/hub2"/>
+    </channel>
+</rss>

+ 3 - 0
tests/Zend/Feed/Pubsubhubbub/_files/testRawPostData.php

@@ -0,0 +1,3 @@
+<?php
+
+readfile('php://input');