Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / zendframework / zend-feed / src / PubSubHubbub / AbstractCallback.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\Http\PhpEnvironment\Response as PhpResponse;
14 use Zend\Stdlib\ArrayUtils;
15
16 abstract class AbstractCallback implements CallbackInterface
17 {
18     /**
19      * An instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistenceInterface
20      * used to background save any verification tokens associated with a subscription
21      * or other.
22      *
23      * @var Model\SubscriptionPersistenceInterface
24      */
25     protected $storage = null;
26
27     /**
28      * An instance of a class handling Http Responses. This is implemented in
29      * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
30      * (i.e. not inherited from) Zend\Controller\Response\Http.
31      *
32      * @var HttpResponse|PhpResponse
33      */
34     protected $httpResponse = null;
35
36     /**
37      * The input stream to use when retrieving the request body. Defaults to
38      * php://input, but can be set to another value in order to force usage
39      * of another input method. This should primarily be used for testing
40      * purposes.
41      *
42      * @var string|resource String indicates a filename or stream to open;
43      *     resource indicates an already created stream to use.
44      */
45     protected $inputStream = 'php://input';
46
47     /**
48      * The number of Subscribers for which any updates are on behalf of.
49      *
50      * @var int
51      */
52     protected $subscriberCount = 1;
53
54     /**
55      * Constructor; accepts an array or Traversable object to preset
56      * options for the Subscriber without calling all supported setter
57      * methods in turn.
58      *
59      * @param  array|Traversable $options Options array or Traversable object
60      */
61     public function __construct($options = null)
62     {
63         if ($options !== null) {
64             $this->setOptions($options);
65         }
66     }
67
68     /**
69      * Process any injected configuration options
70      *
71      * @param  array|Traversable $options Options array or Traversable object
72      * @return AbstractCallback
73      * @throws Exception\InvalidArgumentException
74      */
75     public function setOptions($options)
76     {
77         if ($options instanceof Traversable) {
78             $options = ArrayUtils::iteratorToArray($options);
79         }
80
81         if (! is_array($options)) {
82             throw new Exception\InvalidArgumentException('Array or Traversable object'
83             . 'expected, got ' . gettype($options));
84         }
85
86         if (is_array($options)) {
87             $this->setOptions($options);
88         }
89
90         if (array_key_exists('storage', $options)) {
91             $this->setStorage($options['storage']);
92         }
93         return $this;
94     }
95
96     /**
97      * Send the response, including all headers.
98      * If you wish to handle this via Zend\Http, use the getter methods
99      * to retrieve any data needed to be set on your HTTP Response object, or
100      * simply give this object the HTTP Response instance to work with for you!
101      *
102      * @return void
103      */
104     public function sendResponse()
105     {
106         $this->getHttpResponse()->send();
107     }
108
109     /**
110      * Sets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
111      * to background save any verification tokens associated with a subscription
112      * or other.
113      *
114      * @param  Model\SubscriptionPersistenceInterface $storage
115      * @return AbstractCallback
116      */
117     public function setStorage(Model\SubscriptionPersistenceInterface $storage)
118     {
119         $this->storage = $storage;
120         return $this;
121     }
122
123     /**
124      * Gets an instance of Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence used
125      * to background save any verification tokens associated with a subscription
126      * or other.
127      *
128      * @return Model\SubscriptionPersistenceInterface
129      * @throws Exception\RuntimeException
130      */
131     public function getStorage()
132     {
133         if ($this->storage === null) {
134             throw new Exception\RuntimeException('No storage object has been'
135                 . ' set that subclasses Zend\Feed\Pubsubhubbub\Model\SubscriptionPersistence');
136         }
137         return $this->storage;
138     }
139
140     /**
141      * An instance of a class handling Http Responses. This is implemented in
142      * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
143      * (i.e. not inherited from) Zend\Controller\Response\Http.
144      *
145      * @param  HttpResponse|PhpResponse $httpResponse
146      * @return AbstractCallback
147      * @throws Exception\InvalidArgumentException
148      */
149     public function setHttpResponse($httpResponse)
150     {
151         if (! $httpResponse instanceof HttpResponse && ! $httpResponse instanceof PhpResponse) {
152             throw new Exception\InvalidArgumentException('HTTP Response object must'
153                 . ' implement one of Zend\Feed\Pubsubhubbub\HttpResponse or'
154                 . ' Zend\Http\PhpEnvironment\Response');
155         }
156         $this->httpResponse = $httpResponse;
157         return $this;
158     }
159
160     /**
161      * An instance of a class handling Http Responses. This is implemented in
162      * Zend\Feed\Pubsubhubbub\HttpResponse which shares an unenforced interface with
163      * (i.e. not inherited from) Zend\Controller\Response\Http.
164      *
165      * @return HttpResponse|PhpResponse
166      */
167     public function getHttpResponse()
168     {
169         if ($this->httpResponse === null) {
170             $this->httpResponse = new HttpResponse;
171         }
172         return $this->httpResponse;
173     }
174
175     /**
176      * Sets the number of Subscribers for which any updates are on behalf of.
177      * In other words, is this class serving one or more subscribers? How many?
178      * Defaults to 1 if left unchanged.
179      *
180      * @param  string|int $count
181      * @return AbstractCallback
182      * @throws Exception\InvalidArgumentException
183      */
184     public function setSubscriberCount($count)
185     {
186         $count = intval($count);
187         if ($count <= 0) {
188             throw new Exception\InvalidArgumentException('Subscriber count must be'
189                 . ' greater than zero');
190         }
191         $this->subscriberCount = $count;
192         return $this;
193     }
194
195     /**
196      * Gets the number of Subscribers for which any updates are on behalf of.
197      * In other words, is this class serving one or more subscribers? How many?
198      *
199      * @return int
200      */
201     public function getSubscriberCount()
202     {
203         return $this->subscriberCount;
204     }
205
206     /**
207      * Attempt to detect the callback URL (specifically the path forward)
208      * @return string
209      */
210     // @codingStandardsIgnoreStart
211     protected function _detectCallbackUrl()
212     {
213         // @codingStandardsIgnoreEnd
214         $callbackUrl = '';
215         if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
216             $callbackUrl = $_SERVER['HTTP_X_ORIGINAL_URL'];
217         } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
218             $callbackUrl = $_SERVER['HTTP_X_REWRITE_URL'];
219         } elseif (isset($_SERVER['REQUEST_URI'])) {
220             $callbackUrl = $_SERVER['REQUEST_URI'];
221             $scheme = 'http';
222             if ($_SERVER['HTTPS'] == 'on') {
223                 $scheme = 'https';
224             }
225             $schemeAndHttpHost = $scheme . '://' . $this->_getHttpHost();
226             if (strpos($callbackUrl, $schemeAndHttpHost) === 0) {
227                 $callbackUrl = substr($callbackUrl, strlen($schemeAndHttpHost));
228             }
229         } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
230             $callbackUrl = $_SERVER['ORIG_PATH_INFO'];
231             if (! empty($_SERVER['QUERY_STRING'])) {
232                 $callbackUrl .= '?' . $_SERVER['QUERY_STRING'];
233             }
234         }
235         return $callbackUrl;
236     }
237
238     /**
239      * Get the HTTP host
240      *
241      * @return string
242      */
243     // @codingStandardsIgnoreStart
244     protected function _getHttpHost()
245     {
246         // @codingStandardsIgnoreEnd
247         if (! empty($_SERVER['HTTP_HOST'])) {
248             return $_SERVER['HTTP_HOST'];
249         }
250         $scheme = 'http';
251         if ($_SERVER['HTTPS'] == 'on') {
252             $scheme = 'https';
253         }
254         $name = $_SERVER['SERVER_NAME'];
255         $port = $_SERVER['SERVER_PORT'];
256         if (($scheme == 'http' && $port == 80)
257             || ($scheme == 'https' && $port == 443)
258         ) {
259             return $name;
260         }
261
262         return $name . ':' . $port;
263     }
264
265     /**
266      * Retrieve a Header value from either $_SERVER or Apache
267      *
268      * @param string $header
269      * @return bool|string
270      */
271     // @codingStandardsIgnoreStart
272     protected function _getHeader($header)
273     {
274         // @codingStandardsIgnoreEnd
275         $temp = strtoupper(str_replace('-', '_', $header));
276         if (! empty($_SERVER[$temp])) {
277             return $_SERVER[$temp];
278         }
279         $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
280         if (! empty($_SERVER[$temp])) {
281             return $_SERVER[$temp];
282         }
283         if (function_exists('apache_request_headers')) {
284             $headers = apache_request_headers();
285             if (! empty($headers[$header])) {
286                 return $headers[$header];
287             }
288         }
289         return false;
290     }
291
292     /**
293      * Return the raw body of the request
294      *
295      * @return string|false Raw body, or false if not present
296      */
297     // @codingStandardsIgnoreStart
298     protected function _getRawBody()
299     {
300         // @codingStandardsIgnoreEnd
301         $body = is_resource($this->inputStream)
302             ? stream_get_contents($this->inputStream)
303             : file_get_contents($this->inputStream);
304
305         return strlen(trim($body)) > 0 ? $body : false;
306     }
307 }