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