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