]> git.mxchange.org Git - friendica.git/blob - include/queue.php
rev update
[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         }
69         else {
70
71                 // For the first 12 hours we'll try to deliver every 15 minutes
72                 // After that, we'll only attempt delivery once per hour. 
73
74                 $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 ))");
75         }
76         if(! count($r)){
77                 return;
78         }
79
80         if(! $queue_id)
81                 call_hooks('queue_predeliver', $a, $r);
82
83
84         // delivery loop
85
86         require_once('include/salmon.php');
87         require_once('include/diaspora.php');
88
89         foreach($r as $q_item) {
90
91                 // queue_predeliver hooks may have changed the queue db details, 
92                 // so check again if this entry still needs processing
93
94                 if($queue_id) {
95                         $qi = q("select * from queue where `id` = %d limit 1",
96                                 intval($queue_id)
97                         );
98                 }
99                 else {
100                         $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
101                                 intval($q_item['id'])
102                         );
103                 }
104                 if(! count($qi))
105                         continue;
106
107
108                 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
109                         intval($qi[0]['cid'])
110                 );
111                 if(! count($c)) {
112                         remove_queue_item($q_item['id']);
113                         continue;
114                 }
115                 if(in_array($c[0]['notify'],$deadguys)) {
116                                 logger('queue: skipping known dead url: ' . $c[0]['notify']);
117                                 update_queue_time($q_item['id']);
118                                 continue;
119                 }
120
121                 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey` 
122                         FROM `user` WHERE `uid` = %d LIMIT 1",
123                         intval($c[0]['uid'])
124                 );
125                 if(! count($u)) {
126                         remove_queue_item($q_item['id']);
127                         continue;
128                 }
129
130                 $data      = $qi[0]['content'];
131                 $public    = $qi[0]['batch'];
132                 $contact   = $c[0];
133                 $owner     = $u[0];
134
135                 $deliver_status = 0;
136
137                 switch($contact['network']) {
138                         case NETWORK_DFRN:
139                                 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
140                                 $deliver_status = dfrn_deliver($owner,$contact,$data);
141
142                                 if($deliver_status == (-1)) {
143                                         update_queue_time($q_item['id']);
144                                         $deadguys[] = $contact['notify'];
145                                 }
146                                 else {
147                                         remove_queue_item($q_item['id']);
148                                 }
149                                 break;
150                         case NETWORK_OSTATUS:
151                                 if($contact['notify']) {
152                                         logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
153                                         $deliver_status = slapper($owner,$contact['notify'],$data);
154
155                                         if($deliver_status == (-1))
156                                                 update_queue_time($q_item['id']);
157                                         else
158                                                 remove_queue_item($q_item['id']);
159                                 }
160                                 break;
161                         case NETWORK_DIASPORA:
162                                 if($contact['notify']) {
163                                         logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
164                                         $deliver_status = diaspora_transmit($owner,$contact,$data,$public);
165
166                                         if($deliver_status == (-1))
167                                                 update_queue_time($q_item['id']);
168                                         else
169                                                 remove_queue_item($q_item['id']);
170                                 }
171                                 break;
172
173                         default:
174                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
175                                 call_hooks('queue_deliver', $a, $params);
176                 
177                                 if($params['result'])
178                                                 remove_queue_item($q_item['id']);
179                                 else
180                                                 update_queue_time($q_item['id']);
181         
182                                 break;
183
184                 }
185         }
186                 
187         return;
188
189 }
190
191 if (array_search(__file__,get_included_files())===0){
192   queue_run($argv,$argc);
193   killme();
194 }