]> git.mxchange.org Git - friendica.git/blob - include/pubsubpublish.php
Merge branch 'develop' into 1703-worker-splitting
[friendica.git] / include / pubsubpublish.php
1 <?php
2 use \Friendica\Core\Config;
3
4 require_once('include/items.php');
5 require_once('include/ostatus.php');
6
7 function pubsubpublish_run(&$argv, &$argc){
8
9         if ($argc > 1) {
10                 $pubsubpublish_id = intval($argv[1]);
11         } else {
12                 // We'll push to each subscriber that has push > 0,
13                 // i.e. there has been an update (set in notifier.php).
14                 $r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0");
15
16                 foreach ($r as $rr) {
17                         logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
18                         proc_run(PRIORITY_HIGH, 'include/pubsubpublish.php', $rr["id"]);
19                 }
20         }
21
22         handle_pubsubhubbub($pubsubpublish_id);
23
24         return;
25 }
26
27 function handle_pubsubhubbub($id) {
28         global $a;
29
30         $r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
31
32         if (!dbm::is_result($r)) {
33                 return;
34         }
35
36         $rr = $r[0];
37
38         logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
39
40         $params = ostatus::feed($a, $rr['nickname'], $rr['last_update']);
41         $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
42
43         $headers = array("Content-type: application/atom+xml",
44                         sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
45                                 App::get_baseurl().'/pubsubhubbub',
46                                 $rr['topic']),
47                         "X-Hub-Signature: sha1=".$hmac_sig);
48
49         logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
50
51         post_url($rr['callback_url'], $params, $headers);
52         $ret = $a->get_curl_code();
53
54         if ($ret >= 200 && $ret <= 299) {
55                 logger('successfully pushed to '.$rr['callback_url']);
56
57                 // set last_update to "now", and reset push=0
58                 $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
59                 q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
60                         dbesc($date_now),
61                         intval($rr['id']));
62
63         } else {
64                 logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
65
66                 // we use the push variable also as a counter, if we failed we
67                 // increment this until some upper limit where we give up
68                 $new_push = intval($rr['push']) + 1;
69
70                 if ($new_push > 30) {
71                         // OK, let's give up
72                         $new_push = 0;
73                 }
74
75                 q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
76                         $new_push,
77                         intval($rr['id']));
78         }
79 }