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