3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET')) {
25 * PuSH feed subscription record
27 * @author Brion Vibber <brion@status.net>
29 class HubSub extends Memcached_DataObject
31 public $__table = 'hubsub';
33 public $hashkey; // sha1(topic . '|' . $callback); (topic, callback) key is too long for myisam in utf8
43 public /*static*/ function staticGet($topic, $callback)
45 return parent::staticGet(__CLASS__, 'hashkey', self::hashkey($topic, $callback));
48 protected static function hashkey($topic, $callback)
50 return sha1($topic . '|' . $callback);
54 * return table definition for DB_DataObject
56 * DB_DataObject needs to know something about the table to manipulate
57 * instances. This method provides all the DB_DataObject needs to know.
59 * @return array array of column definitions
63 return array('hashkey' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
64 'topic' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
65 'callback' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
66 'secret' => DB_DATAOBJECT_STR,
67 'lease' => DB_DATAOBJECT_INT,
68 'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
69 'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
70 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
71 'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
74 static function schemaDef()
76 return array(new ColumnDef('hashkey', 'char',
80 new ColumnDef('topic', 'varchar',
84 new ColumnDef('callback', 'varchar',
86 new ColumnDef('secret', 'text',
88 new ColumnDef('lease', 'int',
90 new ColumnDef('sub_start', 'datetime',
92 new ColumnDef('sub_end', 'datetime',
94 new ColumnDef('created', 'datetime',
96 new ColumnDef('modified', 'datetime',
102 return array_keys($this->keyTypes());
105 function sequenceKey()
107 return array(false, false, false);
111 * return key definitions for DB_DataObject
113 * DB_DataObject needs to know about keys that the table has; this function
116 * @return array key definitions
120 return array('hashkey' => 'K');
124 * Validates a requested lease length, sets length plus
125 * subscription start & end dates.
127 * Does not save to database -- use before insert() or update().
129 * @param int $length in seconds
131 function setLease($length)
133 assert(is_int($length));
139 // We want to garbage collect dead subscriptions!
141 } elseif( $length < $min) {
143 } else if ($length > $max) {
147 $this->lease = $length;
148 $this->start_sub = common_sql_now();
149 $this->end_sub = common_sql_date(time() + $length);
153 * Schedule a future verification ping to the subscriber.
154 * If queues are disabled, will be immediate.
156 * @param string $mode 'subscribe' or 'unsubscribe'
157 * @param string $token hub.verify_token value, if provided by client
159 function scheduleVerify($mode, $token=null, $retries=null)
161 if ($retries === null) {
162 $retries = intval(common_config('ostatus', 'hub_retries'));
164 $data = array('sub' => clone($this),
167 'retries' => $retries);
168 $qm = QueueManager::get();
169 $qm->enqueue($data, 'hubconf');
173 * Send a verification ping to subscriber, and if confirmed apply the changes.
174 * This may create, update, or delete the database record.
176 * @param string $mode 'subscribe' or 'unsubscribe'
177 * @param string $token hub.verify_token value, if provided by client
178 * @throws ClientException on failure
180 function verify($mode, $token=null)
182 assert($mode == 'subscribe' || $mode == 'unsubscribe');
184 $challenge = common_good_rand(32);
185 $params = array('hub.mode' => $mode,
186 'hub.topic' => $this->topic,
187 'hub.challenge' => $challenge);
188 if ($mode == 'subscribe') {
189 $params['hub.lease_seconds'] = $this->lease;
191 if ($token !== null) {
192 $params['hub.verify_token'] = $token;
195 // Any existing query string parameters must be preserved
196 $url = $this->callback;
197 if (strpos($url, '?') !== false) {
202 $url .= http_build_query($params, '', '&');
204 $request = new HTTPClient();
205 $response = $request->get($url);
206 $status = $response->getStatus();
208 if ($status >= 200 && $status < 300) {
209 common_log(LOG_INFO, "Verified $mode of $this->callback:$this->topic");
211 // TRANS: Client exception. %s is a HTTP status code.
212 throw new ClientException(sprintf(_m('Hub subscriber verification returned HTTP %s.'),$status));
215 $old = HubSub::staticGet($this->topic, $this->callback);
216 if ($mode == 'subscribe') {
220 $ok = $this->insert();
222 } else if ($mode == 'unsubscribe') {
226 // That's ok, we're already unsubscribed.
232 * Insert wrapper; transparently set the hash key from topic and callback columns.
233 * @return mixed success
237 $this->hashkey = self::hashkey($this->topic, $this->callback);
238 $this->created = common_sql_now();
239 $this->modified = common_sql_now();
240 return parent::insert();
244 * Update wrapper; transparently update modified column.
245 * @return boolean success
247 function update($old=null)
249 $this->modified = common_sql_now();
250 return parent::update($old);
254 * Schedule delivery of a 'fat ping' to the subscriber's callback
255 * endpoint. If queues are disabled, this will run immediately.
257 * @param string $atom well-formed Atom feed
258 * @param int $retries optional count of retries if POST fails; defaults to hub_retries from config or 0 if unset
260 function distribute($atom, $retries=null)
262 if ($retries === null) {
263 $retries = intval(common_config('ostatus', 'hub_retries'));
266 if (common_config('ostatus', 'local_push_bypass')) {
267 // If target is a local site, bypass the web server and drop the
268 // item directly into the target's input queue.
269 $url = parse_url($this->callback);
270 $wildcard = common_config('ostatus', 'local_wildcard');
271 $site = Status_network::getFromHostname($url['host'], $wildcard);
275 $hmac = 'sha1=' . hash_hmac('sha1', $atom, $this->secret);
280 // Hack: at the moment we stick the subscription ID in the callback
281 // URL so we don't have to look inside the Atom to route the subscription.
282 // For now this means we need to extract that from the target URL
283 // so we can include it in the data.
284 $parts = explode('/', $url['path']);
285 $subId = intval(array_pop($parts));
287 $data = array('feedsub_id' => $subId,
290 common_log(LOG_DEBUG, "Cross-site PuSH bypass enqueueing straight to $site->nickname feed $subId");
291 $qm = QueueManager::get();
292 $qm->enqueue($data, 'pushin', $site->nickname);
297 // We dare not clone() as when the clone is discarded it'll
298 // destroy the result data for the parent query.
299 // @fixme use clone() again when it's safe to copy an
300 // individual item from a multi-item query again.
301 $sub = HubSub::staticGet($this->topic, $this->callback);
302 $data = array('sub' => $sub,
304 'retries' => $retries);
305 common_log(LOG_INFO, "Queuing PuSH: $this->topic to $this->callback");
306 $qm = QueueManager::get();
307 $qm->enqueue($data, 'hubout');
311 * Queue up a large batch of pushes to multiple subscribers
312 * for this same topic update.
314 * If queues are disabled, this will run immediately.
316 * @param string $atom well-formed Atom feed
317 * @param array $pushCallbacks list of callback URLs
319 function bulkDistribute($atom, $pushCallbacks)
321 $data = array('atom' => $atom,
322 'topic' => $this->topic,
323 'pushCallbacks' => $pushCallbacks);
324 common_log(LOG_INFO, "Queuing PuSH batch: $this->topic to " .
325 count($pushCallbacks) . " sites");
326 $qm = QueueManager::get();
327 $qm->enqueue($data, 'hubprep');
331 * Send a 'fat ping' to the subscriber's callback endpoint
332 * containing the given Atom feed chunk.
334 * Determination of which items to send should be done at
335 * a higher level; don't just shove in a complete feed!
337 * @param string $atom well-formed Atom feed
338 * @throws Exception (HTTP or general)
342 $headers = array('Content-Type: application/atom+xml');
344 $hmac = hash_hmac('sha1', $atom, $this->secret);
345 $headers[] = "X-Hub-Signature: sha1=$hmac";
349 common_log(LOG_INFO, "About to push feed to $this->callback for $this->topic, HMAC $hmac");
351 $request = new HTTPClient();
352 $request->setBody($atom);
353 $response = $request->post($this->callback, $headers);
355 if ($response->isOk()) {
358 // TRANS: Exception. %1$s is a response status code, %2$s is the body of the response.
359 throw new Exception(sprintf(_m('Callback returned status: %1$s. Body: %2$s'),
360 $response->getStatus(),trim($response->getBody())));