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