]> git.mxchange.org Git - friendica.git/blob - include/pubsubpublish.php
Require statement
[friendica.git] / include / pubsubpublish.php
1 <?php
2 /**
3  * @file include/pubsubpublish.php
4  */
5 use Friendica\App;
6 use Friendica\Core\System;
7 use Friendica\Core\Config;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBM;
10 use Friendica\Protocol\OStatus;
11
12 require_once 'include/items.php';
13
14 function pubsubpublish_run(&$argv, &$argc)
15 {
16         global $a;
17
18         if ($argc > 1) {
19                 $pubsubpublish_id = intval($argv[1]);
20         } else {
21                 // We'll push to each subscriber that has push > 0,
22                 // i.e. there has been an update (set in notifier.php).
23                 $r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0 ORDER BY `last_update` DESC");
24
25                 foreach ($r as $rr) {
26                         logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
27                         Worker::add(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
28                                         'pubsubpublish', (int)$rr["id"]);
29                 }
30         }
31
32         handle_pubsubhubbub($pubsubpublish_id);
33
34         return;
35 }
36
37 function handle_pubsubhubbub($id) {
38         global $a;
39
40         $r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
41         if (!DBM::is_result($r)) {
42                 return;
43         }
44
45         $rr = $r[0];
46
47         /// @todo Check server status with PortableContact::checkServer()
48         // Before this can be done we need a way to safely detect the server url.
49
50         logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
51
52         $last_update = $rr['last_update'];
53         $params = OStatus::feed($a, $rr['nickname'], $last_update);
54
55         if (!$params) {
56                 return;
57         }
58
59         $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
60
61         $headers = array("Content-type: application/atom+xml",
62                         sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
63                                 System::baseUrl().'/pubsubhubbub/'.$rr['nickname'],
64                                 $rr['topic']),
65                         "X-Hub-Signature: sha1=".$hmac_sig);
66
67         logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
68
69         post_url($rr['callback_url'], $params, $headers);
70         $ret = $a->get_curl_code();
71
72         if ($ret >= 200 && $ret <= 299) {
73                 logger('successfully pushed to '.$rr['callback_url']);
74
75                 // set last_update to the "created" date of the last item, and reset push=0
76                 q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
77                         dbesc($last_update),
78                         intval($rr['id']));
79
80         } else {
81                 logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
82
83                 // we use the push variable also as a counter, if we failed we
84                 // increment this until some upper limit where we give up
85                 $new_push = intval($rr['push']) + 1;
86
87                 if ($new_push > 30) // OK, let's give up
88                         $new_push = 0;
89
90                 q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
91                         $new_push,
92                         intval($rr['id']));
93         }
94 }