]> git.mxchange.org Git - friendica.git/blob - include/queue.php
Merge pull request #169 from michal-s/master
[friendica.git] / include / queue.php
1 <?php
2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4
5 function queue_run($argv, $argc){
6         global $a, $db;
7
8         if(is_null($a)){
9                 $a = new App;
10         }
11   
12         if(is_null($db)){
13                 @include(".htconfig.php");
14                 require_once("dba.php");
15                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
16                 unset($db_host, $db_user, $db_pass, $db_data);
17         };
18
19
20         require_once("session.php");
21         require_once("datetime.php");
22         require_once('include/items.php');
23         require_once('include/bbcode.php');
24
25         load_config('config');
26         load_config('system');
27
28         $a->set_baseurl(get_config('system','url'));
29
30         load_hooks();
31
32         if($argc > 1)
33                 $queue_id = intval($argv[1]);
34         else
35                 $queue_id = 0;
36
37         $deadguys = array();
38
39         logger('queue: start');
40
41         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
42
43         $r = q("select * from deliverq where 1");
44         if(count($r)) {
45                 foreach($r as $rr) {
46                         logger('queue: deliverq');
47                         proc_run('php','include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
48                         if($interval)
49                                 @time_sleep_until(microtime(true) + (float) $interval);
50                 }
51         }
52
53         $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue` 
54                 LEFT JOIN `contact` ON `queue`.`cid` = `contact`.`id` 
55                 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
56         if(count($r)) {
57                 foreach($r as $rr) {
58                         logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
59                         logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
60                 }
61                 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
62         }
63                 
64         if($queue_id)
65                 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
66                         intval($queue_id)
67                 );
68         else
69                 $r = q("SELECT `id` FROM `queue` WHERE `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ");
70
71         if(! count($r)){
72                 return;
73         }
74
75         if(! $queue_id)
76                 call_hooks('queue_predeliver', $a, $r);
77
78
79         // delivery loop
80
81         require_once('include/salmon.php');
82         require_once('include/diaspora.php');
83
84         foreach($r as $q_item) {
85
86                 // queue_predeliver hooks may have changed the queue db details, 
87                 // so check again if this entry still needs processing
88
89                 if($queue_id) {
90                         $qi = q("select * from queue where `id` = %d limit 1",
91                                 intval($queue_id)
92                         );
93                 }
94                 else {
95                         $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
96                                 intval($q_item['id'])
97                         );
98                 }
99                 if(! count($qi))
100                         continue;
101
102
103                 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
104                         intval($qi[0]['cid'])
105                 );
106                 if(! count($c)) {
107                         remove_queue_item($q_item['id']);
108                         continue;
109                 }
110                 if(in_array($c[0]['notify'],$deadguys)) {
111                                 logger('queue: skipping known dead url: ' . $c[0]['notify']);
112                                 update_queue_time($q_item['id']);
113                                 continue;
114                 }
115
116                 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey` 
117                         FROM `user` WHERE `uid` = %d LIMIT 1",
118                         intval($c[0]['uid'])
119                 );
120                 if(! count($u)) {
121                         remove_queue_item($q_item['id']);
122                         continue;
123                 }
124
125                 $data      = $qi[0]['content'];
126                 $contact   = $c[0];
127                 $owner     = $u[0];
128
129                 $deliver_status = 0;
130
131                 switch($contact['network']) {
132                         case NETWORK_DFRN:
133                                 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
134                                 $deliver_status = dfrn_deliver($owner,$contact,$data);
135
136                                 if($deliver_status == (-1)) {
137                                         update_queue_time($q_item['id']);
138                                         $deadguys[] = $contact['notify'];
139                                 }
140                                 else {
141                                         remove_queue_item($q_item['id']);
142                                 }
143                                 break;
144                         case NETWORK_OSTATUS:
145                                 if($contact['notify']) {
146                                         logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
147                                         $deliver_status = slapper($owner,$contact['notify'],$data);
148
149                                         if($deliver_status == (-1))
150                                                 update_queue_time($q_item['id']);
151                                         else
152                                                 remove_queue_item($q_item['id']);
153                                 }
154                                 break;
155                         case NETWORK_DIASPORA:
156                                 if($contact['notify']) {
157                                         logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
158                                         $deliver_status = diaspora_transmit($owner,$contact,$data);
159
160                                         if($deliver_status == (-1))
161                                                 update_queue_time($q_item['id']);
162                                         else
163                                                 remove_queue_item($q_item['id']);
164                                 }
165                                 break;
166
167                         default:
168                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
169                                 call_hooks('queue_deliver', $a, $params);
170                 
171                                 if($params['result'])
172                                                 remove_queue_item($q_item['id']);
173                                 else
174                                                 update_queue_time($q_item['id']);
175         
176                                 break;
177
178                 }
179         }
180                 
181         return;
182
183 }
184
185 if (array_search(__file__,get_included_files())===0){
186   queue_run($argv,$argc);
187   killme();
188 }