public function createSubscription($topic, PushHub $hub)
 {
     $subscription = ['topic' => $topic, 'hub' => $hub->getUrl()];
     $existingSubscription = $this->db->prepare('SELECT * FROM ' . $this->prefix . 'subscriptions WHERE topic = :topic AND hub = :hub;');
     $existingSubscription->execute($subscription);
     if ($existingSubscription->rowCount() !== 0) {
         $subscription = $existingSubscription->fetch();
         if ($subscription['mode'] !== 'subscribe') {
             if ($this->db->prepare("UPDATE {$this->prefix}subscriptions SET mode='subscribe' WHERE id = :id;")->execute($subscription) === false) {
                 return [null, new Exception('Failed updating subscription: ' . print_r($this->db->errorInfo(), true))];
             }
         }
     } else {
         // Create a sufficiently random ID for the subscription.
         // Not relying on database autoincrement as this ID is used in public endpoints. In the case of unsigned ping requests
         // this provides minimal security by obscurity.
         $subscription['id'] = uniqid(time(), true);
         $insertStatement = $this->db->prepare('INSERT INTO ' . $this->prefix . 'subscriptions (id, topic, hub) VALUES (:id, :topic, :hub);');
         if ($insertStatement->execute($subscription) === false) {
             return [null, new Exception('Failed saving subscription: ' . print_r($insertStatement->errorInfo(), true))];
         }
         $existingSubscription->execute(['topic' => $subscription['topic'], 'hub' => $subscription['hub']]);
         $subscription = $existingSubscription->fetch();
         if ($subscription === false) {
             return [null, new Exception('Failed retrieving subscription: ' . print_r($existingSubscription->errorInfo(), true))];
         }
     }
     return [$subscription, null];
 }
示例#2
0
 public function subscribe($url, $callback)
 {
     if (parse_url($url, PHP_URL_FRAGMENT) === null and $this->fragment !== null) {
         $url = rtrim($url, '#');
         $url = "{$url}#{$this->fragment}";
     }
     return parent::subscribe($url, $callback);
 }
示例#3
0
 public function testCanCreateHub()
 {
     $url = 'http://pubsubhubbub.example.com';
     $hub = new PushHub($url);
     $this->assertEquals($url, $hub->getUrl());
 }
示例#4
0
                    return $source['topic'];
                }, $column['sources'])));
            }
        }
    }
    $output->writeln("All topics in use:");
    foreach ($allTopics as $topic) {
        $output->writeln(" -> {$topic}");
    }
    $subscriptions = $app['subscriptions.storage']->getSubscriptions();
    foreach ($subscriptions as $subscription) {
        if (!in_array($subscription['topic'], $allTopics)) {
            if ($subscription['hub'] == $dh->getUrl()) {
                $unsubhub = $dh;
            } else {
                $unsubhub = new Subscriptions\PushHub($subscription['hub']);
            }
            $callback = $app['url_generator']->generate('subscriptions.id.ping', ['id' => $subscription['id']], true);
            $unsubhub->unsubscribe($subscription['topic'], $callback);
            $output->writeln("Unsubscribed from {$subscription['topic']} at {$unsubhub}");
        }
    }
});
$console->register('poll')->setDescription('Polls any feeds subscribed to with a URL of inert-hub')->setCode(function (InputInterface $input, OutputInterface $output) use($app) {
    /** @var Subscriptions\SubscriptionStorage */
    $storage = $app['subscriptions.storage'];
    /** @var Subscriptions\PushHub $defaultHub */
    $defaultHub = $app['subscriptions.defaulthub'];
    foreach ($storage->getSubscriptionsForHub($defaultHub->getUrl()) as $subscription) {
        $output->writeln("Fetching {$subscription['topic']}");
        list($context, $err) = Subscriptions\manualFetch($app, $subscription['topic'], $app['http.client']);