]>
Commit | Line | Data |
---|---|---|
becd215a AK |
1 | <?php |
2 | /** | |
3 | * A PHP client library for pubsubhubbub. | |
4 | * | |
5 | * @link http://code.google.com/p/pubsubhubbub/ | |
6 | * | |
7 | * @author Josh Fraser | joshfraser.com | josh@eventvue.com | |
8 | * @license Apache License 2.0 | |
9 | */ | |
10 | namespace Pubsubhubbub\Subscriber; | |
11 | ||
12 | use InvalidArgumentException; | |
13 | ||
14 | class Subscriber | |
15 | { | |
16 | /** | |
17 | * Put your google key here. | |
18 | * Required if you want to use the google feed API to lookup RSS feeds. | |
19 | * | |
20 | * @var string | |
21 | */ | |
22 | protected $google_key = ''; | |
23 | ||
24 | /** | |
25 | * @var string | |
26 | */ | |
27 | protected $hub_url; | |
28 | ||
29 | /** | |
30 | * @var string | |
31 | */ | |
32 | protected $callback_url; | |
33 | ||
34 | /** | |
35 | * @var string | |
36 | */ | |
37 | protected $credentials; | |
38 | ||
39 | /** | |
40 | * @var string accepted values are "async" and "sync" | |
41 | */ | |
42 | protected $verify = 'async'; | |
43 | ||
44 | /** | |
45 | * @var string | |
46 | */ | |
47 | protected $verify_token; | |
48 | ||
49 | /** | |
50 | * @var string | |
51 | */ | |
52 | protected $lease_seconds; | |
53 | ||
54 | /** | |
55 | * Create a new Subscriber (credentials added for SuperFeedr support). | |
56 | * | |
57 | * @param string $hub_url | |
58 | * @param string $callback_url | |
59 | * @param string $credentials | |
60 | */ | |
61 | public function __construct($hub_url, $callback_url, $credentials = false) | |
62 | { | |
63 | if (! isset($hub_url)) { | |
64 | throw new InvalidArgumentException('Please specify a hub url'); | |
65 | } | |
66 | ||
67 | if (! preg_match('|^https?://|i', $hub_url)) { | |
68 | throw new InvalidArgumentException('The specified hub url does not appear to be valid: ' . $hub_url); | |
69 | } | |
70 | ||
71 | if (! isset($callback_url)) { | |
72 | throw new InvalidArgumentException('Please specify a callback'); | |
73 | } | |
74 | ||
75 | $this->hub_url = $hub_url; | |
76 | $this->callback_url = $callback_url; | |
77 | $this->credentials = $credentials; | |
78 | } | |
79 | ||
80 | /** | |
81 | * $use_regexp lets you choose whether to use google AJAX feed api (faster, but cached) or a regexp to read from site. | |
82 | * | |
83 | * @param string $url | |
84 | * @param callable $http_function | |
85 | * | |
86 | * @return string | |
87 | */ | |
88 | public function find_feed($url, $http_function = false) | |
89 | { | |
90 | // using google feed API | |
91 | $url = "http://ajax.googleapis.com/ajax/services/feed/lookup?key={$this->google_key}&v=1.0&q=" . urlencode($url); | |
92 | // fetch the content | |
93 | if ($http_function) { | |
94 | $response = $http_function($url); | |
95 | } else { | |
96 | $response = $this->http($url); | |
97 | } | |
98 | ||
99 | $result = json_decode($response, true); | |
100 | $rss_url = $result['responseData']['url']; | |
101 | ||
102 | return $rss_url; | |
103 | } | |
104 | ||
105 | /** | |
106 | * Subscribe to a topic. | |
107 | * | |
108 | * @param string $topic_url | |
109 | * @param callable $http_function | |
110 | * | |
111 | * @return mixed | |
112 | */ | |
113 | public function subscribe($topic_url, $http_function = false) | |
114 | { | |
115 | return $this->change_subscription('subscribe', $topic_url, $http_function); | |
116 | } | |
117 | ||
118 | /** | |
119 | * Unsubscribe from a topic. | |
120 | * | |
121 | * @param string $topic_url | |
122 | * @param callable $http_function | |
123 | * | |
124 | * @return mixed | |
125 | */ | |
126 | public function unsubscribe($topic_url, $http_function = false) | |
127 | { | |
128 | return $this->change_subscription('unsubscribe', $topic_url, $http_function); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Helper function since sub/unsub are handled the same way. | |
133 | * | |
134 | * @param string $mode | |
135 | * @param string $topic_url | |
136 | * @param callable $http_function | |
137 | * | |
138 | * @return mixed | |
139 | */ | |
140 | private function change_subscription($mode, $topic_url, $http_function = false) | |
141 | { | |
142 | if (! isset($topic_url)) { | |
143 | throw new InvalidArgumentException('Please specify a topic url'); | |
144 | } | |
145 | ||
146 | // lightweight check that we're actually working w/ a valid url | |
147 | if (! preg_match('|^https?://|i', $topic_url)) { | |
148 | throw new InvalidArgumentException('The specified topic url does not appear to be valid: ' . $topic_url); | |
149 | } | |
150 | ||
151 | // set the mode subscribe/unsubscribe | |
152 | $post_string = 'hub.mode=' . $mode; | |
153 | $post_string .= '&hub.callback=' . urlencode($this->callback_url); | |
154 | $post_string .= '&hub.verify=' . $this->verify; | |
155 | $post_string .= '&hub.verify_token=' . $this->verify_token; | |
156 | $post_string .= '&hub.lease_seconds=' . $this->lease_seconds; | |
157 | ||
158 | // append the topic url parameters | |
159 | $post_string .= '&hub.topic=' . urlencode($topic_url); | |
160 | ||
161 | // make the http post request and return true/false | |
162 | // easy to over-write to use your own http function | |
163 | if ($http_function) { | |
164 | return call_user_func_array($http_function, [$this->hub_url, $post_string]); | |
165 | } | |
166 | ||
167 | return $this->http($this->hub_url, $post_string); | |
168 | } | |
169 | ||
170 | /** | |
171 | * Default http function that uses curl to post to the hub endpoint. | |
172 | * | |
173 | * @param string $url | |
174 | * @param string $post_string | |
175 | * | |
176 | * @return mixed | |
177 | */ | |
178 | private function http($url, $post_string) | |
179 | { | |
180 | ||
181 | // add any additional curl options here | |
182 | $options = [ | |
183 | CURLOPT_URL => $url, | |
184 | CURLOPT_USERAGENT => 'PubSubHubbub-Subscriber-PHP/1.0', | |
185 | CURLOPT_RETURNTRANSFER => true, | |
186 | ]; | |
187 | ||
188 | if ($post_string) { | |
189 | $options[CURLOPT_POST] = true; | |
190 | $options[CURLOPT_POSTFIELDS] = $post_string; | |
191 | } | |
192 | ||
193 | if ($this->credentials) { | |
194 | $options[CURLOPT_USERPWD] = $this->credentials; | |
195 | } | |
196 | ||
197 | $ch = curl_init(); | |
198 | curl_setopt_array($ch, $options); | |
199 | ||
200 | $response = curl_exec($ch); | |
201 | $info = curl_getinfo($ch); | |
202 | ||
203 | // all good -- anything in the 200 range | |
204 | if (substr($info['http_code'], 0, 1) == '2') { | |
205 | return $response; | |
206 | } | |
207 | ||
208 | return false; | |
209 | } | |
210 | } |