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