autofocus input field if there is no query
[phinde.git] / bin / subscribe.php
1 #!/usr/bin/env php
2 <?php
3 namespace phinde;
4 /**
5  * Subscribe with PubSubHubbub to an URL
6  */
7 require_once __DIR__ . '/../src/init.php';
8
9 $cc = new \Console_CommandLine();
10 $cc->description = 'Subscribe to URL updates';
11 $cc->version = '0.0.1';
12 $cc->addArgument(
13     'url',
14     array(
15         'description' => 'URL to process',
16         'multiple'    => false
17     )
18 );
19 try {
20     $res = $cc->parse();
21 } catch (\Exception $e) {
22     $cc->displayError($e->getMessage());
23 }
24
25 $url = $res->args['url'];
26 $url = Helper::addSchema($url);
27 $urlObj = new \Net_URL2($url);
28 $url = $urlObj->getNormalizedURL();
29 if (!Helper::isUrlAllowed($url)) {
30     Log::error("Domain is not allowed; not crawling");
31     exit(2);
32 }
33
34 $subDb = new Subscriptions();
35
36 list($topic, $hub) = $subDb->detectHub($url);
37 if ($hub === null) {
38     Log::error('No hub URL found for topic');
39     exit(10);
40 }
41 if ($topic != $url) {
42     Log::info('Topic URL differs from URL: ' . $topic);
43 }
44
45 $sub = $subDb->get($topic);
46 if ($sub !== false) {
47     Log::error('Topic exists already in subscription table');
48     Log::info('Current status: ' . $sub->sub_status);
49     exit(3);
50 }
51 $subDb->create($topic);
52 $sub = $subDb->get($topic);
53
54 $callbackUrl = $GLOBALS['phinde']['baseurl'] . 'push-subscription.php'
55     . '?hub.topic=' . urlencode($topic)
56     . '&capkey=' . urlencode($sub->sub_capkey);
57 $req = new HttpRequest($hub, 'POST');
58 $req->addPostParameter('hub.callback', $callbackUrl);
59 $req->addPostParameter('hub.mode', 'subscribe');
60 $req->addPostParameter('hub.topic', $topic);
61 $req->addPostParameter('hub.lease_seconds', $sub->sub_lease_seconds);
62 $req->addPostParameter('hub.secret', $sub->sub_secret);
63 $res = $req->send();
64
65 if (intval($res->getStatus()) == 202) {
66     Log::info('Subscription initiated');
67     exit(0);
68 }
69
70 Log::error(
71     'Error: Subscription response status code was not 202 but '
72     . $res->getStatus()
73 );
74 Log::error($res->getBody());
75 ?>