5 * Database table containing information about Pubsubhubbub subscriptions
11 public function __construct()
14 $GLOBALS['phinde']['db_dsn'],
15 $GLOBALS['phinde']['db_user'],
16 $GLOBALS['phinde']['db_pass']
18 $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
24 * @param string $topic Topic URL
26 * @return false|object False if the row does not exist
28 public function get($topic)
30 $stmt = $this->db->prepare(
31 'SELECT * FROM subscriptions'
32 . ' WHERE sub_topic = :topic'
34 $stmt->execute([':topic' => $topic]);
36 //fetchObject() itself returns FALSE on failure
37 return $stmt->fetchObject();
41 * Create a new subscription entry in database.
42 * Automatically generates secret, capkey and lease seconds.
44 * This method does NOT:
45 * - check for duplicates (do it yourself)
46 * - return the object (fetch it yourself)
47 * - send subscription requests to the hub
49 * @param string $topic URL to subscribe to
53 public function create($topic)
55 $stmt = $this->db->prepare(
56 'INSERT INTO subscriptions'
57 . ' (sub_topic, sub_status, sub_lease_seconds, sub_expires'
58 . ', sub_secret, sub_capkey, sub_created, sub_updated'
59 . ', sub_pings, sub_lastping, sub_statusmessage)'
61 . ' (:topic, "subscribing", :lease_seconds, "0000-00-00 00:00:00"'
62 . ', :secret, :capkey, NOW(), NOW()'
63 . ', 0, "0000-00-00 00:00:00", "")'
68 ':lease_seconds' => 86400 * 30,
69 ':secret' => bin2hex(openssl_random_pseudo_bytes(16)),
70 ':capkey' => bin2hex(openssl_random_pseudo_bytes(16)),
76 * A subscription has been confirmed by the hub - mark it as active.
78 * @param integer $subId Subscription ID
79 * @param integer $leaseSeconds Number of seconds until subscription expires
83 public function subscribed($subId, $leaseSeconds)
86 'UPDATE subscriptions'
87 . ' SET sub_status = "active"'
88 . ' , sub_lease_seconds = :leaseSeconds'
89 . ' , sub_expires = :expires'
90 . ' , sub_updated = NOW()'
91 . ' WHERE sub_id = :id'
94 ':leaseSeconds' => $leaseSeconds,
95 ':expires' => gmdate('Y-m-d H:i:s', time() + $leaseSeconds),
102 * Mark a subscription as "unsubscribed"
104 * @param integer $subId Subscription ID
108 public function unsubscribed($subId)
111 'UPDATE subscriptions'
112 . ' SET sub_status = "unsubscribed"'
113 . ' , sub_updated = NOW()'
114 . ' WHERE sub_id = :id'
115 )->execute([':id' => $subId]);
118 public function denied($subId, $reason)
121 'UPDATE subscriptions'
122 . ' SET sub_status = "denied"'
123 . ' , sub_statusmessage = :reason'
124 . ' , sub_updated = NOW()'
125 . ' WHERE sub_id = :id'
126 )->execute([':id' => $subId, ':reason' => $reason]);
129 public function pinged($subId)
132 'UPDATE subscriptions'
133 . ' SET sub_pings = sub_pings + 1'
134 . ' , sub_lastping = NOW()'
135 . ' , sub_updated = NOW()'
136 . ' WHERE sub_id = :id'
137 )->execute([':id' => $subId]);
141 * Detect the hub for the given topic URL
143 * @param string $url Topic URL
145 * @return array Topic URL and hub URL. Hub URL is NULL if there is none.
147 public function detectHub($url)
149 $hue = new HubUrlExtractor();
150 $hue->setRequestTemplate(new HttpRequest());
151 $urls = $hue->getUrls($url);
152 //we violate the spec by not requiring a self URL
153 $topicUrl = isset($urls['self']) ? $urls['self'] : $url;
154 $hubUrl = isset($urls['hub']) ? $urls['hub'] : null;
156 return array($topicUrl, $hubUrl);