]> git.mxchange.org Git - friendica.git/blob - include/queue.php
Merge pull request #3284 from annando/issue-3278
[friendica.git] / include / queue.php
1 <?php
2
3 use \Friendica\Core\Config;
4
5 require_once('include/queue_fn.php');
6 require_once('include/dfrn.php');
7 require_once("include/datetime.php");
8 require_once('include/items.php');
9 require_once('include/bbcode.php');
10 require_once('include/socgraph.php');
11 require_once('include/cache.php');
12
13 function queue_run(&$argv, &$argc){
14         global $a;
15
16         if ($argc > 1) {
17                 $queue_id = intval($argv[1]);
18         } else {
19                 $queue_id = 0;
20         }
21
22         $cachekey_deadguy = 'queue_run:deadguy:';
23         $cachekey_server = 'queue_run:server:';
24
25         if (!$queue_id) {
26
27                 logger('queue: start');
28
29                 // Handling the pubsubhubbub requests
30                 proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
31
32                 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
33                         INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
34                         WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
35                 if ($r) {
36                         foreach ($r as $rr) {
37                                 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
38                                 logger('Expired queue data: ' . $rr['content'], LOGGER_DATA);
39                         }
40                         q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
41                 }
42
43                 // For the first 12 hours we'll try to deliver every 15 minutes
44                 // After that, we'll only attempt delivery once per hour.
45
46                 $r = q("SELECT `id` FROM `queue` WHERE ((`created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR && `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE) OR (`last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR)) ORDER BY `cid`, `created`");
47
48                 call_hooks('queue_predeliver', $a, $r);
49
50                 if (dbm::is_result($r)) {
51                         foreach ($r as $q_item) {
52                                 logger('Call queue for id '.$q_item['id']);
53                                 proc_run(PRIORITY_LOW, "include/queue.php", $q_item['id']);
54                         }
55                 }
56                 return;
57         }
58
59
60         // delivering
61
62         require_once('include/salmon.php');
63         require_once('include/diaspora.php');
64
65         $r = q("SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
66                 intval($queue_id));
67
68         if (!dbm::is_result($r)) {
69                 return;
70         }
71
72         $q_item = $r[0];
73
74         $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
75                 intval($q_item['cid'])
76         );
77
78         if (!dbm::is_result($c)) {
79                 remove_queue_item($q_item['id']);
80                 return;
81         }
82
83         $dead = Cache::get($cachekey_deadguy.$c[0]['notify']);
84
85         if (!is_null($dead) AND $dead) {
86                 logger('queue: skipping known dead url: '.$c[0]['notify']);
87                 update_queue_time($q_item['id']);
88                 return;
89         }
90
91         $server = poco_detect_server($c[0]['url']);
92
93         if ($server != "") {
94                 $vital = Cache::get($cachekey_server.$server);
95
96                 if (is_null($vital)) {
97                         logger("Check server ".$server." (".$c[0]["network"].")");
98
99                         $vital = poco_check_server($server, $c[0]["network"], true);
100                         Cache::set($cachekey_server.$server, $vital, CACHE_QUARTER_HOUR);
101                 }
102
103                 if (!is_null($vital) AND !$vital) {
104                         logger('queue: skipping dead server: '.$server);
105                         update_queue_time($q_item['id']);
106                         return;
107                 }
108         }
109
110         $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
111                 FROM `user` WHERE `uid` = %d LIMIT 1",
112                 intval($c[0]['uid'])
113         );
114         if (!dbm::is_result($u)) {
115                 remove_queue_item($q_item['id']);
116                 return;
117         }
118
119         $data      = $q_item['content'];
120         $public    = $q_item['batch'];
121         $contact   = $c[0];
122         $owner     = $u[0];
123
124         $deliver_status = 0;
125
126         switch ($contact['network']) {
127                 case NETWORK_DFRN:
128                         logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
129                         $deliver_status = dfrn::deliver($owner, $contact, $data);
130
131                         if ($deliver_status == (-1)) {
132                                 update_queue_time($q_item['id']);
133                                 Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
134                         } else {
135                                 remove_queue_item($q_item['id']);
136                         }
137                         break;
138                 case NETWORK_OSTATUS:
139                         if ($contact['notify']) {
140                                 logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
141                                 $deliver_status = slapper($owner, $contact['notify'], $data);
142
143                                 if ($deliver_status == (-1)) {
144                                         update_queue_time($q_item['id']);
145                                         Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
146                                 } else {
147                                         remove_queue_item($q_item['id']);
148                                 }
149                         }
150                         break;
151                 case NETWORK_DIASPORA:
152                         if ($contact['notify']) {
153                                 logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
154                                 $deliver_status = Diaspora::transmit($owner, $contact, $data, $public, true);
155
156                                 if ($deliver_status == (-1)) {
157                                         update_queue_time($q_item['id']);
158                                         Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
159                                 } else {
160                                         remove_queue_item($q_item['id']);
161                                 }
162                         }
163                         break;
164
165                 default:
166                         $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
167                         call_hooks('queue_deliver', $a, $params);
168
169                         if ($params['result']) {
170                                 remove_queue_item($q_item['id']);
171                         } else {
172                                 update_queue_time($q_item['id']);
173                         }
174                         break;
175
176         }
177         logger('Deliver status '.(int)$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
178
179         return;
180 }