db backup prior to drupal security update
[yaffs-website] / vendor / zendframework / zend-feed / src / PubSubHubbub / Publisher.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
5  * @link      http://github.com/zendframework/zf2 for the canonical source repository
6  * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7  * @license   http://framework.zend.com/license/new-bsd New BSD License
8  */
9
10 namespace Zend\Feed\PubSubHubbub;
11
12 use Traversable;
13 use Zend\Feed\Uri;
14 use Zend\Http\Request as HttpRequest;
15 use Zend\Stdlib\ArrayUtils;
16
17 class Publisher
18 {
19     /**
20      * An array of URLs for all Hub Servers used by the Publisher, and to
21      * which all topic update notifications will be sent.
22      *
23      * @var array
24      */
25     protected $hubUrls = [];
26
27     /**
28      * An array of topic (Atom or RSS feed) URLs which have been updated and
29      * whose updated status will be notified to all Hub Servers.
30      *
31      * @var array
32      */
33     protected $updatedTopicUrls = [];
34
35     /**
36      * An array of any errors including keys for 'response', 'hubUrl'.
37      * The response is the actual Zend\Http\Response object.
38      *
39      * @var array
40      */
41     protected $errors = [];
42
43     /**
44      * An array of topic (Atom or RSS feed) URLs which have been updated and
45      * whose updated status will be notified to all Hub Servers.
46      *
47      * @var array
48      */
49     protected $parameters = [];
50
51     /**
52      * Constructor; accepts an array or Zend\Config\Config instance to preset
53      * options for the Publisher without calling all supported setter
54      * methods in turn.
55      *
56      * @param  array|Traversable $options
57      */
58     public function __construct($options = null)
59     {
60         if ($options !== null) {
61             $this->setOptions($options);
62         }
63     }
64
65     /**
66      * Process any injected configuration options
67      *
68      * @param  array|Traversable $options Options array or Traversable object
69      * @return Publisher
70      * @throws Exception\InvalidArgumentException
71      */
72     public function setOptions($options)
73     {
74         if ($options instanceof Traversable) {
75             $options = ArrayUtils::iteratorToArray($options);
76         }
77
78         if (! is_array($options)) {
79             throw new Exception\InvalidArgumentException('Array or Traversable object'
80                                 . 'expected, got ' . gettype($options));
81         }
82         if (array_key_exists('hubUrls', $options)) {
83             $this->addHubUrls($options['hubUrls']);
84         }
85         if (array_key_exists('updatedTopicUrls', $options)) {
86             $this->addUpdatedTopicUrls($options['updatedTopicUrls']);
87         }
88         if (array_key_exists('parameters', $options)) {
89             $this->setParameters($options['parameters']);
90         }
91         return $this;
92     }
93
94     /**
95      * Add a Hub Server URL supported by Publisher
96      *
97      * @param  string $url
98      * @return Publisher
99      * @throws Exception\InvalidArgumentException
100      */
101     public function addHubUrl($url)
102     {
103         if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) {
104             throw new Exception\InvalidArgumentException('Invalid parameter "url"'
105                 . ' of "' . $url . '" must be a non-empty string and a valid'
106                 . 'URL');
107         }
108         $this->hubUrls[] = $url;
109         return $this;
110     }
111
112     /**
113      * Add an array of Hub Server URLs supported by Publisher
114      *
115      * @param  array $urls
116      * @return Publisher
117      */
118     public function addHubUrls(array $urls)
119     {
120         foreach ($urls as $url) {
121             $this->addHubUrl($url);
122         }
123         return $this;
124     }
125
126     /**
127      * Remove a Hub Server URL
128      *
129      * @param  string $url
130      * @return Publisher
131      */
132     public function removeHubUrl($url)
133     {
134         if (! in_array($url, $this->getHubUrls())) {
135             return $this;
136         }
137         $key = array_search($url, $this->hubUrls);
138         unset($this->hubUrls[$key]);
139         return $this;
140     }
141
142     /**
143      * Return an array of unique Hub Server URLs currently available
144      *
145      * @return array
146      */
147     public function getHubUrls()
148     {
149         $this->hubUrls = array_unique($this->hubUrls);
150         return $this->hubUrls;
151     }
152
153     /**
154      * Add a URL to a topic (Atom or RSS feed) which has been updated
155      *
156      * @param  string $url
157      * @return Publisher
158      * @throws Exception\InvalidArgumentException
159      */
160     public function addUpdatedTopicUrl($url)
161     {
162         if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) {
163             throw new Exception\InvalidArgumentException('Invalid parameter "url"'
164                 . ' of "' . $url . '" must be a non-empty string and a valid'
165                 . 'URL');
166         }
167         $this->updatedTopicUrls[] = $url;
168         return $this;
169     }
170
171     /**
172      * Add an array of Topic URLs which have been updated
173      *
174      * @param  array $urls
175      * @return Publisher
176      */
177     public function addUpdatedTopicUrls(array $urls)
178     {
179         foreach ($urls as $url) {
180             $this->addUpdatedTopicUrl($url);
181         }
182         return $this;
183     }
184
185     /**
186      * Remove an updated topic URL
187      *
188      * @param  string $url
189      * @return Publisher
190      */
191     public function removeUpdatedTopicUrl($url)
192     {
193         if (! in_array($url, $this->getUpdatedTopicUrls())) {
194             return $this;
195         }
196         $key = array_search($url, $this->updatedTopicUrls);
197         unset($this->updatedTopicUrls[$key]);
198         return $this;
199     }
200
201     /**
202      * Return an array of unique updated topic URLs currently available
203      *
204      * @return array
205      */
206     public function getUpdatedTopicUrls()
207     {
208         $this->updatedTopicUrls = array_unique($this->updatedTopicUrls);
209         return $this->updatedTopicUrls;
210     }
211
212     /**
213      * Notifies a single Hub Server URL of changes
214      *
215      * @param  string $url The Hub Server's URL
216      * @return void
217      * @throws Exception\InvalidArgumentException
218      * @throws Exception\RuntimeException
219      */
220     public function notifyHub($url)
221     {
222         if (empty($url) || ! is_string($url) || ! Uri::factory($url)->isValid()) {
223             throw new Exception\InvalidArgumentException('Invalid parameter "url"'
224                 . ' of "' . $url . '" must be a non-empty string and a valid'
225                 . 'URL');
226         }
227         $client = $this->_getHttpClient();
228         $client->setUri($url);
229         $response = $client->getResponse();
230         if ($response->getStatusCode() !== 204) {
231             throw new Exception\RuntimeException('Notification to Hub Server '
232                 . 'at "' . $url . '" appears to have failed with a status code of "'
233                 . $response->getStatusCode() . '" and message "'
234                 . $response->getContent() . '"');
235         }
236     }
237
238     /**
239      * Notifies all Hub Server URLs of changes
240      *
241      * If a Hub notification fails, certain data will be retained in an
242      * an array retrieved using getErrors(), if a failure occurs for any Hubs
243      * the isSuccess() check will return FALSE. This method is designed not
244      * to needlessly fail with an Exception/Error unless from Zend\Http\Client.
245      *
246      * @return void
247      * @throws Exception\RuntimeException
248      */
249     public function notifyAll()
250     {
251         $client = $this->_getHttpClient();
252         $hubs   = $this->getHubUrls();
253         if (empty($hubs)) {
254             throw new Exception\RuntimeException('No Hub Server URLs'
255                 . ' have been set so no notifications can be sent');
256         }
257         $this->errors = [];
258         foreach ($hubs as $url) {
259             $client->setUri($url);
260             $response = $client->getResponse();
261             if ($response->getStatusCode() !== 204) {
262                 $this->errors[] = [
263                     'response' => $response,
264                     'hubUrl' => $url
265                 ];
266             }
267         }
268     }
269
270     /**
271      * Add an optional parameter to the update notification requests
272      *
273      * @param  string $name
274      * @param  string|null $value
275      * @return Publisher
276      * @throws Exception\InvalidArgumentException
277      */
278     public function setParameter($name, $value = null)
279     {
280         if (is_array($name)) {
281             $this->setParameters($name);
282             return $this;
283         }
284         if (empty($name) || ! is_string($name)) {
285             throw new Exception\InvalidArgumentException('Invalid parameter "name"'
286                 . ' of "' . $name . '" must be a non-empty string');
287         }
288         if ($value === null) {
289             $this->removeParameter($name);
290             return $this;
291         }
292         if (empty($value) || (! is_string($value) && $value !== null)) {
293             throw new Exception\InvalidArgumentException('Invalid parameter "value"'
294                 . ' of "' . $value . '" must be a non-empty string');
295         }
296         $this->parameters[$name] = $value;
297         return $this;
298     }
299
300     /**
301      * Add an optional parameter to the update notification requests
302      *
303      * @param  array $parameters
304      * @return Publisher
305      */
306     public function setParameters(array $parameters)
307     {
308         foreach ($parameters as $name => $value) {
309             $this->setParameter($name, $value);
310         }
311         return $this;
312     }
313
314     /**
315      * Remove an optional parameter for the notification requests
316      *
317      * @param  string $name
318      * @return Publisher
319      * @throws Exception\InvalidArgumentException
320      */
321     public function removeParameter($name)
322     {
323         if (empty($name) || ! is_string($name)) {
324             throw new Exception\InvalidArgumentException('Invalid parameter "name"'
325                 . ' of "' . $name . '" must be a non-empty string');
326         }
327         if (array_key_exists($name, $this->parameters)) {
328             unset($this->parameters[$name]);
329         }
330         return $this;
331     }
332
333     /**
334      * Return an array of optional parameters for notification requests
335      *
336      * @return array
337      */
338     public function getParameters()
339     {
340         return $this->parameters;
341     }
342
343     /**
344      * Returns a boolean indicator of whether the notifications to Hub
345      * Servers were ALL successful. If even one failed, FALSE is returned.
346      *
347      * @return bool
348      */
349     public function isSuccess()
350     {
351         return ! (count($this->errors) != 0);
352     }
353
354     /**
355      * Return an array of errors met from any failures, including keys:
356      * 'response' => the Zend\Http\Response object from the failure
357      * 'hubUrl' => the URL of the Hub Server whose notification failed
358      *
359      * @return array
360      */
361     public function getErrors()
362     {
363         return $this->errors;
364     }
365
366     /**
367      * Get a basic prepared HTTP client for use
368      *
369      * @return \Zend\Http\Client
370      * @throws Exception\RuntimeException
371      */
372     // @codingStandardsIgnoreStart
373     protected function _getHttpClient()
374     {
375         // @codingStandardsIgnoreEnd
376         $client = PubSubHubbub::getHttpClient();
377         $client->setMethod(HttpRequest::METHOD_POST);
378         $client->setOptions([
379             'useragent' => 'Zend_Feed_Pubsubhubbub_Publisher/' . Version::VERSION,
380         ]);
381         $params   = [];
382         $params[] = 'hub.mode=publish';
383         $topics   = $this->getUpdatedTopicUrls();
384         if (empty($topics)) {
385             throw new Exception\RuntimeException('No updated topic URLs'
386                 . ' have been set');
387         }
388         foreach ($topics as $topicUrl) {
389             $params[] = 'hub.url=' . urlencode($topicUrl);
390         }
391         $optParams = $this->getParameters();
392         foreach ($optParams as $name => $value) {
393             $params[] = urlencode($name) . '=' . urlencode($value);
394         }
395         $paramString = implode('&', $params);
396         $client->setRawBody($paramString);
397         return $client;
398     }
399 }