]> git.mxchange.org Git - friendica.git/blob - src/Worker/PubSubPublish.php
API: fix sender/recipient of PMs: check api_user before get user info.
[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\Database\DBM;
11 use Friendica\Protocol\OStatus;
12 use Friendica\Util\Network;
13 use Friendica\Model\PushSubscriber;
14 use dba;
15
16 require_once 'include/items.php';
17
18 class PubSubPublish {
19         public static function execute($pubsubpublish_id = 0)
20         {
21                 if ($pubsubpublish_id == 0) {
22                         return;
23                 }
24
25                 self::publish($pubsubpublish_id);
26         }
27
28         private static function publish($id) {
29                 global $a;
30
31                 $subscriber = dba::selectFirst('push_subscriber', [], ['id' => $id]);
32                 if (!DBM::is_result($subscriber)) {
33                         return;
34                 }
35
36                 /// @todo Check server status with PortableContact::checkServer()
37                 // Before this can be done we need a way to safely detect the server url.
38
39                 logger("Generate feed of user " . $subscriber['nickname']. " to " . $subscriber['callback_url']. " - last updated " . $subscriber['last_update'], LOGGER_DEBUG);
40
41                 $last_update = $subscriber['last_update'];
42                 $params = OStatus::feed($subscriber['nickname'], $last_update);
43
44                 if (!$params) {
45                         return;
46                 }
47
48                 $hmac_sig = hash_hmac("sha1", $params, $subscriber['secret']);
49
50                 $headers = ["Content-type: application/atom+xml",
51                                 sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
52                                         System::baseUrl() . '/pubsubhubbub/' . $subscriber['nickname'],
53                                         $subscriber['topic']),
54                                 "X-Hub-Signature: sha1=" . $hmac_sig];
55
56                 logger('POST ' . print_r($headers, true) . "\n" . $params, LOGGER_DATA);
57
58                 Network::post($subscriber['callback_url'], $params, $headers);
59                 $ret = $a->get_curl_code();
60
61                 $condition = ['id' => $subscriber['id']];
62
63                 if ($ret >= 200 && $ret <= 299) {
64                         logger('Successfully pushed to ' . $subscriber['callback_url']);
65
66                         PushSubscriber::reset($subscriber['id'], $last_update);
67                 } else {
68                         logger('Delivery error when pushing to ' . $subscriber['callback_url'] . ' HTTP: ' . $ret);
69
70                         PushSubscriber::delay($subscriber['id']);
71                 }
72         }
73 }