]> git.mxchange.org Git - friendica.git/blob - include/pubsubpublish.php
Break out list of acceptable protocols in "src" attribute into separate variable...
[friendica.git] / include / pubsubpublish.php
1 <?php
2 require_once("boot.php");
3 require_once("include/ostatus.php");
4
5 use \Friendica\Core\Config;
6 use \Friendica\Core\PConfig;
7
8 function handle_pubsubhubbub($id) {
9         global $a, $db;
10
11         $r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
12         if (!$r)
13                 return;
14         else
15                 $rr = $r[0];
16
17         logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
18
19         $params = ostatus::feed($a, $rr['nickname'], $rr['last_update']);
20         $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
21
22         $headers = array("Content-type: application/atom+xml",
23                         sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
24                                 App::get_baseurl().'/pubsubhubbub',
25                                 $rr['topic']),
26                         "X-Hub-Signature: sha1=".$hmac_sig);
27
28         logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
29
30         post_url($rr['callback_url'], $params, $headers);
31         $ret = $a->get_curl_code();
32
33         if ($ret >= 200 && $ret <= 299) {
34                 logger('successfully pushed to '.$rr['callback_url']);
35
36                 // set last_update to "now", and reset push=0
37                 $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
38                 q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
39                         dbesc($date_now),
40                         intval($rr['id']));
41
42         } else {
43                 logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
44
45                 // we use the push variable also as a counter, if we failed we
46                 // increment this until some upper limit where we give up
47                 $new_push = intval($rr['push']) + 1;
48
49                 if ($new_push > 30) // OK, let's give up
50                         $new_push = 0;
51
52                 q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
53                         $new_push,
54                         intval($rr['id']));
55         }
56 }
57
58
59 function pubsubpublish_run(&$argv, &$argc){
60         global $a, $db;
61
62         if(is_null($a)){
63                 $a = new App;
64         }
65
66         if(is_null($db)){
67                 @include(".htconfig.php");
68                 require_once("include/dba.php");
69                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
70                 unset($db_host, $db_user, $db_pass, $db_data);
71         };
72
73         require_once('include/items.php');
74
75         Config::load();
76
77         // Don't check this stuff if the function is called by the poller
78         if (App::callstack() != "poller_run") {
79                 if (App::is_already_running("pubsubpublish", "include/pubsubpublish.php", 540)) {
80                         return;
81                 }
82         }
83
84         $a->set_baseurl(get_config('system','url'));
85
86         load_hooks();
87
88         if ($argc > 1) {
89                 $pubsubpublish_id = intval($argv[1]);
90         }
91         else {
92                 // We'll push to each subscriber that has push > 0,
93                 // i.e. there has been an update (set in notifier.php).
94                 $r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0");
95
96                 // Use the delivery interval that is also used for the notifier
97                 $interval = Config::get("system", "delivery_interval", 2);
98
99                 // If we are using the worker we don't need a delivery interval
100                 if (get_config("system", "worker")) {
101                         $interval = false;
102                 }
103
104                 foreach ($r as $rr) {
105                         logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
106                         proc_run(PRIORITY_HIGH, 'include/pubsubpublish.php', $rr["id"]);
107
108                         if($interval)
109                                 @time_sleep_until(microtime(true) + (float) $interval);
110                 }
111         }
112
113         handle_pubsubhubbub($pubsubpublish_id);
114
115         return;
116
117 }
118
119 if (array_search(__file__,get_included_files())===0){
120   pubsubpublish_run($_SERVER["argv"],$_SERVER["argc"]);
121   killme();
122 }
123