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 * Count number of subscriptions
43 * @return array Array of keys with different status, number as value
45 public function count()
47 $stmt = $this->db->prepare(
48 'SELECT COUNT(*) as count, sub_status FROM subscriptions'
49 . ' GROUP BY sub_status'
50 . ' ORDER BY sub_status'
55 foreach ($stmt as $row) {
56 $res[$row['sub_status']] = $row['count'];
63 * Create a new subscription entry in database.
64 * Automatically generates secret, capkey and lease seconds.
66 * This method does NOT:
67 * - check for duplicates (do it yourself)
68 * - return the object (fetch it yourself)
69 * - send subscription requests to the hub
71 * @param string $topic URL to subscribe to
75 public function create($topic)
77 $stmt = $this->db->prepare(
78 'INSERT INTO subscriptions'
79 . ' (sub_topic, sub_status, sub_lease_seconds, sub_expires'
80 . ', sub_secret, sub_capkey, sub_created, sub_updated'
81 . ', sub_pings, sub_lastping, sub_statusmessage)'
83 . ' (:topic, "subscribing", :lease_seconds, "0000-00-00 00:00:00"'
84 . ', :secret, :capkey, NOW(), NOW()'
85 . ', 0, "0000-00-00 00:00:00", "")'
90 ':lease_seconds' => 86400 * 30,
91 ':secret' => bin2hex(openssl_random_pseudo_bytes(16)),
92 ':capkey' => bin2hex(openssl_random_pseudo_bytes(16)),
98 * A subscription has been confirmed by the hub - mark it as active.
100 * @param integer $subId Subscription ID
101 * @param integer $leaseSeconds Number of seconds until subscription expires
105 public function subscribed($subId, $leaseSeconds)
108 'UPDATE subscriptions'
109 . ' SET sub_status = "active"'
110 . ' , sub_lease_seconds = :leaseSeconds'
111 . ' , sub_expires = :expires'
112 . ' , sub_updated = NOW()'
113 . ' WHERE sub_id = :id'
116 ':leaseSeconds' => $leaseSeconds,
117 ':expires' => gmdate('Y-m-d H:i:s', time() + $leaseSeconds),
124 * Mark a subscription as "unsubscribed"
126 * @param integer $subId Subscription ID
130 public function unsubscribed($subId)
133 'UPDATE subscriptions'
134 . ' SET sub_status = "unsubscribed"'
135 . ' , sub_updated = NOW()'
136 . ' WHERE sub_id = :id'
137 )->execute([':id' => $subId]);
140 public function denied($subId, $reason)
143 'UPDATE subscriptions'
144 . ' SET sub_status = "denied"'
145 . ' , sub_statusmessage = :reason'
146 . ' , sub_updated = NOW()'
147 . ' WHERE sub_id = :id'
148 )->execute([':id' => $subId, ':reason' => $reason]);
151 public function pinged($subId)
154 'UPDATE subscriptions'
155 . ' SET sub_pings = sub_pings + 1'
156 . ' , sub_lastping = NOW()'
157 . ' , sub_updated = NOW()'
158 . ' WHERE sub_id = :id'
159 )->execute([':id' => $subId]);
163 * Detect the hub for the given topic URL
165 * @param string $url Topic URL
167 * @return array Topic URL and hub URL. Hub URL is NULL if there is none.
169 public function detectHub($url)
171 $hue = new HubUrlExtractor();
172 $hue->setRequestTemplate(new HttpRequest());
173 $urls = $hue->getUrls($url);
174 //we violate the spec by not requiring a self URL
175 $topicUrl = isset($urls['self']) ? $urls['self'] : $url;
176 $hubUrl = isset($urls['hub']) ? $urls['hub'] : null;
178 return array($topicUrl, $hubUrl);