summaryrefslogtreecommitdiff
path: root/plugins/OStatus/classes/HubSub.php
blob: a81de68e69daee2abe9cd01eb069dfac2150a471 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/*
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2010, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * PuSH feed subscription record
 * @package Hub
 * @author Brion Vibber <brion@status.net>
 */
class HubSub extends Memcached_DataObject
{
    public $__table = 'hubsub';

    public $hashkey; // sha1(topic . '|' . $callback); (topic, callback) key is too long for myisam in utf8
    public $topic;
    public $callback;
    public $secret;
    public $challenge;
    public $lease;
    public $sub_start;
    public $sub_end;
    public $created;

    public /*static*/ function staticGet($topic, $callback)
    {
        return parent::staticGet(__CLASS__, 'hashkey', self::hashkey($topic, $callback));
    }

    protected static function hashkey($topic, $callback)
    {
        return sha1($topic . '|' . $callback);
    }

    /**
     * return table definition for DB_DataObject
     *
     * DB_DataObject needs to know something about the table to manipulate
     * instances. This method provides all the DB_DataObject needs to know.
     *
     * @return array array of column definitions
     */

    function table()
    {
        return array('hashkey' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
                     'topic' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
                     'callback' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
                     'secret' => DB_DATAOBJECT_STR,
                     'challenge' => DB_DATAOBJECT_STR,
                     'lease' =>  DB_DATAOBJECT_INT,
                     'sub_start' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
                     'sub_end' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME,
                     'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
    }

    static function schemaDef()
    {
        return array(new ColumnDef('hashkey', 'char',
                                   /*size*/40,
                                   /*nullable*/false,
                                   /*key*/'PRI'),
                     new ColumnDef('topic', 'varchar',
                                   /*size*/255,
                                   /*nullable*/false,
                                   /*key*/'KEY'),
                     new ColumnDef('callback', 'varchar',
                                   255, false),
                     new ColumnDef('secret', 'text',
                                   null, true),
                     new ColumnDef('challenge', 'varchar',
                                   32, true),
                     new ColumnDef('lease', 'int',
                                   null, true),
                     new ColumnDef('sub_start', 'datetime',
                                   null, true),
                     new ColumnDef('sub_end', 'datetime',
                                   null, true),
                     new ColumnDef('created', 'datetime',
                                   null, false));
    }

    function keys()
    {
        return array_keys($this->keyTypes());
    }

    function sequenceKeys()
    {
        return array(false, false, false);
    }

    /**
     * return key definitions for DB_DataObject
     *
     * DB_DataObject needs to know about keys that the table has; this function
     * defines them.
     *
     * @return array key definitions
     */

    function keyTypes()
    {
        return array('hashkey' => 'K');
    }

    /**
     * Validates a requested lease length, sets length plus
     * subscription start & end dates.
     *
     * Does not save to database -- use before insert() or update().
     *
     * @param int $length in seconds
     */
    function setLease($length)
    {
        assert(is_int($length));

        $min = 86400;
        $max = 86400 * 30;

        if ($length == 0) {
            // We want to garbage collect dead subscriptions!
            $length = $max;
        } elseif( $length < $min) {
            $length = $min;
        } else if ($length > $max) {
            $length = $max;
        }

        $this->lease = $length;
        $this->start_sub = common_sql_now();
        $this->end_sub = common_sql_date(time() + $length);
    }

    /**
     * Send a verification ping to subscriber
     * @param string $mode 'subscribe' or 'unsubscribe'
     * @param string $token hub.verify_token value, if provided by client
     */
    function verify($mode, $token=null)
    {
        assert($mode == 'subscribe' || $mode == 'unsubscribe');

        // Is this needed? data object fun...
        $clone = clone($this);
        $clone->challenge = common_good_rand(16);
        $clone->update($this);
        $this->challenge = $clone->challenge;
        unset($clone);

        $params = array('hub.mode' => $mode,
                        'hub.topic' => $this->topic,
                        'hub.challenge' => $this->challenge);
        if ($mode == 'subscribe') {
            $params['hub.lease_seconds'] = $this->lease;
        }
        if ($token !== null) {
            $params['hub.verify_token'] = $token;
        }
        $url = $this->callback . '?' . http_build_query($params, '', '&'); // @fixme ugly urls

        try {
            $request = new HTTPClient();
            $response = $request->get($url);
            $status = $response->getStatus();

            if ($status >= 200 && $status < 300) {
                $fail = false;
            } else {
                // @fixme how can we schedule a second attempt?
                // Or should we?
                $fail = "Returned HTTP $status";
            }
        } catch (Exception $e) {
            $fail = $e->getMessage();
        }
        if ($fail) {
            // @fixme how can we schedule a second attempt?
            // or save a fail count?
            // Or should we?
            common_log(LOG_ERR, "Failed to verify $mode for $this->topic at $this->callback: $fail");
            return false;
        } else {
            if ($mode == 'subscribe') {
                // Establish or renew the subscription!
                // This seems unnecessary... dataobject fun!
                $clone = clone($this);
                $clone->challenge = null;
                $clone->setLease($this->lease);
                $clone->update($this);
                unset($clone);

                $this->challenge = null;
                $this->setLease($this->lease);
                common_log(LOG_ERR, "Verified $mode of $this->callback:$this->topic for $this->lease seconds");
            } else if ($mode == 'unsubscribe') {
                common_log(LOG_ERR, "Verified $mode of $this->callback:$this->topic");
                $this->delete();
            }
            return true;
        }
    }

    /**
     * Insert wrapper; transparently set the hash key from topic and callback columns.
     * @return boolean success
     */
    function insert()
    {
        $this->hashkey = self::hashkey($this->topic, $this->callback);
        return parent::insert();
    }

    /**
     * Schedule delivery of a 'fat ping' to the subscriber's callback
     * endpoint. If queues are disabled, this will run immediately.
     *
     * @param string $atom well-formed Atom feed
     * @param int $retries optional count of retries if POST fails; defaults to hub_retries from config or 0 if unset
     */
    function distribute($atom, $retries=null)
    {
        if ($retries === null) {
            $retries = intval(common_config('ostatus', 'hub_retries'));
        }

        $data = array('sub' => clone($this),
                      'atom' => $atom,
                      'retries' => $retries);
        $qm = QueueManager::get();
        $qm->enqueue($data, 'hubout');
    }

    /**
     * Send a 'fat ping' to the subscriber's callback endpoint
     * containing the given Atom feed chunk.
     *
     * Determination of which items to send should be done at
     * a higher level; don't just shove in a complete feed!
     *
     * @param string $atom well-formed Atom feed
     * @throws Exception (HTTP or general)
     */
    function push($atom)
    {
        $headers = array('Content-Type: application/atom+xml');
        if ($this->secret) {
            $hmac = hash_hmac('sha1', $atom, $this->secret);
            $headers[] = "X-Hub-Signature: sha1=$hmac";
        } else {
            $hmac = '(none)';
        }
        common_log(LOG_INFO, "About to push feed to $this->callback for $this->topic, HMAC $hmac");

        $request = new HTTPClient();
        $request->setBody($atom);
        $response = $request->post($this->callback, $headers);

        if ($response->isOk()) {
            return true;
        } else {
            throw new Exception("Callback returned status: " .
                                $response->getStatus() .
                                "; body: " .
                                trim($response->getBody()));
        }
    }
}