diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2020-03-06 18:37:30 +0100 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2020-03-06 18:37:30 +0100 |
| commit | c32d1b6ffe81afb36fdcaebe0254ad191b72bff6 (patch) | |
| tree | b4322c9d6e5be60faeb28b566b04d7d4b1dbb693 /bin/unsubscribe.php | |
| parent | 973ef8c4f8c15848dfc135e11053e5eb6f5dcdb7 (diff) | |
| download | phinde-c32d1b6ffe81afb36fdcaebe0254ad191b72bff6.tar.gz phinde-c32d1b6ffe81afb36fdcaebe0254ad191b72bff6.zip | |
Add cli tool to unsubscribe from a topic
Diffstat (limited to 'bin/unsubscribe.php')
| -rwxr-xr-x | bin/unsubscribe.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/bin/unsubscribe.php b/bin/unsubscribe.php new file mode 100755 index 0000000..f7eee9a --- /dev/null +++ b/bin/unsubscribe.php @@ -0,0 +1,66 @@ +#!/usr/bin/env php +<?php +namespace phinde; +/** + * Unsubscribe from a WebSub subscription + */ +require_once __DIR__ . '/../src/init.php'; + +$cc = new \Console_CommandLine(); +$cc->description = 'Unsubscribe from a WebSub subscription'; +$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()); +} + +$subDb = new Subscriptions(); + +$url = $res->args['url']; +$url = Helper::addSchema($url); +$urlObj = new \Net_URL2($url); +$topic = $urlObj->getNormalizedURL(); + + +$sub = $subDb->get($topic); +if ($sub === false) { + Log::error("No existing subscription for URL"); + exit(2); +} +if ($sub->sub_status === 'unsubscribed') { + Log::info('Already unsubscribed'); + exit(0); +} + +$subDb->unsubscribing($sub->sub_id); + +$callbackUrl = $GLOBALS['phinde']['baseurl'] . 'push-subscription.php' + . '?hub.topic=' . urlencode($topic) + . '&capkey=' . urlencode($sub->sub_capkey); +$req = new HttpRequest($sub->sub_hub, 'POST'); +$req->addPostParameter('hub.callback', $callbackUrl); +$req->addPostParameter('hub.mode', 'unsubscribe'); +$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('Unsubscription initiated'); + exit(0); +} + +Log::error( + 'Error: Unsubscription response status code was not 202 but ' + . $res->getStatus() +); +Log::error($res->getBody()); +?> |
