]> git.mxchange.org Git - friendica.git/blob - src/Worker/PubSubPublish.php
Merge pull request #5077 from annando/fix-blocking-ostatus
[friendica.git] / src / Worker / PubSubPublish.php
1 <?php
2 /**
3  * @file src/Worker/PubSubPublish.php
4  */
5
6 namespace Friendica\Worker;
7
8 use Friendica\App;
9 use Friendica\Core\System;
10 use Friendica\Core\Config;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBM;
13 use Friendica\Protocol\OStatus;
14 use Friendica\Util\Network;
15 use Friendica\Util\DateTimeFormat;
16 use dba;
17
18 require_once 'include/items.php';
19
20 class PubSubPublish {
21         public static function execute($pubsubpublish_id = 0)
22         {
23                 if ($pubsubpublish_id == 0) {
24                         return;
25                 }
26
27                 self::publish($pubsubpublish_id);
28         }
29
30         private static function publish($id) {
31                 global $a;
32
33                 $subscriber = dba::selectFirst('push_subscriber', [], ['id' => $id]);
34                 if (!DBM::is_result($subscriber)) {
35                         return;
36                 }
37
38                 /// @todo Check server status with PortableContact::checkServer()
39                 // Before this can be done we need a way to safely detect the server url.
40
41                 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
42
43                 $last_update = $subscriber['last_update'];
44                 $params = OStatus::feed($subscriber['nickname'], $last_update);
45
46                 if (!$params) {
47                         return;
48                 }
49
50                 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
51
52                 $headers = ["Content-type: application/atom+xml",
53                                 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
54                                         System::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
55                                         $subscriber['topic']),
56                                 "X-Hub-Signature: sha1=" . $hmac_sig];
57
58                 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
59
60                 Network::post($subscriber['callback_url'], $params, $headers);
61                 $ret = $a->get_curl_code();
62
63                 $condition = ['id' => $subscriber['id']];
64
65                 if ($ret >= 200 && $ret <= 299) {
66                         logger('Successfully pushed to ' . $subscriber['callback_url']);
67
68                         // set last_update to the "created" date of the last item, and reset push=0
69                         $fields = ['push' => 0, 'next_try' => NULL_DATE, 'last_update' => $last_update];
70                         dba::update('push_subscriber', $fields, $condition);
71
72                 } else {
73                         logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
74
75                         // we use the push variable also as a counter, if we failed we
76                         // increment this until some upper limit where we give up
77                         $retrial = $subscriber['push'];
78
79                         if ($retrial > 14) {
80                                 dba::update('push_subscriber', ['push' => 0, 'next_try' => NULL_DATE], $condition);
81                                 logger('Delivery error: Giving up for ' . $subscriber['callback_url'], LOGGER_DEBUG);
82                         } else {
83                                 // Calculate the delay until the next trial
84                                 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
85                                 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
86
87                                 $retrial = $retrial + 1;
88
89                                 dba::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], $condition);
90                                 logger('Delivery error: Next try (' . $retrial . ') for ' . $subscriber['callback_url'] . ' at ' . $next, LOGGER_DEBUG);
91                         }
92                 }
93         }
94 }