]> git.mxchange.org Git - friendica.git/blob - include/queue.php
fix typo
[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                 $public    = $qi[0]['batch'];
127                 $contact   = $c[0];
128                 $owner     = $u[0];
129
130                 $deliver_status = 0;
131
132                 switch($contact['network']) {
133                         case NETWORK_DFRN:
134                                 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
135                                 $deliver_status = dfrn_deliver($owner,$contact,$data);
136
137                                 if($deliver_status == (-1)) {
138                                         update_queue_time($q_item['id']);
139                                         $deadguys[] = $contact['notify'];
140                                 }
141                                 else {
142                                         remove_queue_item($q_item['id']);
143                                 }
144                                 break;
145                         case NETWORK_OSTATUS:
146                                 if($contact['notify']) {
147                                         logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
148                                         $deliver_status = slapper($owner,$contact['notify'],$data);
149
150                                         if($deliver_status == (-1))
151                                                 update_queue_time($q_item['id']);
152                                         else
153                                                 remove_queue_item($q_item['id']);
154                                 }
155                                 break;
156                         case NETWORK_DIASPORA:
157                                 if($contact['notify']) {
158                                         logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
159                                         $deliver_status = diaspora_transmit($owner,$contact,$data,$public);
160
161                                         if($deliver_status == (-1))
162                                                 update_queue_time($q_item['id']);
163                                         else
164                                                 remove_queue_item($q_item['id']);
165                                 }
166                                 break;
167
168                         default:
169                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
170                                 call_hooks('queue_deliver', $a, $params);
171                 
172                                 if($params['result'])
173                                                 remove_queue_item($q_item['id']);
174                                 else
175                                                 update_queue_time($q_item['id']);
176         
177                                 break;
178
179                 }
180         }
181                 
182         return;
183
184 }
185
186 if (array_search(__file__,get_included_files())===0){
187   queue_run($argv,$argc);
188   killme();
189 }