diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2016-11-24 22:09:28 +0100 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2016-11-24 22:09:28 +0100 |
| commit | 9928b96a20f17fe532dd0ac26914f83cbe34867a (patch) | |
| tree | 4873023bc93a6776c8075272b4a4f3bffbebd042 /bin/subscribe.php | |
| parent | f98e891b454e5677bdf61f476e366b01af713b50 (diff) | |
| download | phinde-9928b96a20f17fe532dd0ac26914f83cbe34867a.tar.gz phinde-9928b96a20f17fe532dd0ac26914f83cbe34867a.zip | |
websub subcriptions work
Diffstat (limited to 'bin/subscribe.php')
| -rwxr-xr-x | bin/subscribe.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/bin/subscribe.php b/bin/subscribe.php new file mode 100755 index 0000000..768ee10 --- /dev/null +++ b/bin/subscribe.php @@ -0,0 +1,75 @@ +#!/usr/bin/env php +<?php +namespace phinde; +/** + * Subscribe with PubSubHubbub to an URL + */ +require_once __DIR__ . '/../src/init.php'; + +$cc = new \Console_CommandLine(); +$cc->description = 'Subscribe to URL updates'; +$cc->version = '0.0.1'; +$cc->addArgument( + 'url', + array( + 'description' => 'URL to process', + 'multiple' => false + ) +); +try { + $res = $cc->parse(); +} catch (\Exception $e) { + $cc->displayError($e->getMessage()); +} + +$url = $res->args['url']; +$url = Helper::addSchema($url); +$urlObj = new \Net_URL2($url); +$url = $urlObj->getNormalizedURL(); +if (!Helper::isUrlAllowed($url)) { + Log::error("Domain is not allowed; not crawling"); + exit(2); +} + +$subDb = new Subscriptions(); + +list($topic, $hub) = $subDb->detectHub($url); +if ($hub === null) { + Log::error('No hub URL found for topic'); + exit(10); +} +if ($topic != $url) { + Log::info('Topic URL differs from URL: ' . $topic); +} + +$sub = $subDb->get($topic); +if ($sub !== false) { + Log::error('Topic exists already in subscription table'); + Log::info('Current status: ' . $sub->sub_status); + exit(3); +} +$subDb->create($topic); +$sub = $subDb->get($topic); + +$callbackUrl = $GLOBALS['phinde']['baseurl'] . 'push-subscription.php' + . '?hub.topic=' . urlencode($topic) + . '&capkey=' . urlencode($sub->sub_capkey); +$req = new HttpRequest($hub, 'POST'); +$req->addPostParameter('hub.callback', $callbackUrl); +$req->addPostParameter('hub.mode', 'subscribe'); +$req->addPostParameter('hub.topic', $topic); +$req->addPostParameter('hub.lease_seconds', $sub->sub_lease_seconds); +$req->addPostParameter('hub.secret', $sub->sub_secret); +$res = $req->send(); + +if (intval($res->getStatus()) == 202) { + Log::info('Subscription initiated'); + exit(0); +} + +Log::error( + 'Error: Subscription response status code was not 202 but ' + . $res->getStatus() +); +Log::error($res->getBody()); +?> |
