]> git.mxchange.org Git - friendica.git/blob - include/queue.php
Merge pull request #1000 from fabrixxm/master
[friendica.git] / include / queue.php
1 <?php
2 require_once("boot.php");
3 require_once('include/queue_fn.php');
4
5 function handle_pubsubhubbub() {
6         global $a, $db;
7
8         logger('queue [pubsubhubbub]: start');
9
10         // We'll push to each subscriber that has push > 0,
11         // i.e. there has been an update (set in notifier.php).
12
13         $r = q("SELECT * FROM `push_subscriber` WHERE `push` > 0");
14
15         foreach($r as $rr) {
16                 $params = get_feed_for($a, '', $rr['nickname'], $rr['last_update']);
17                 $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
18
19                 $headers = array("Content-type: application/atom+xml",
20                                                  sprintf("Link: <%s>;rel=hub," .
21                                                                  "<%s>;rel=self",
22                                                                  $a->get_baseurl() . '/pubsubhubbub',
23                                                                  $rr['topic']),
24                                                  "X-Hub-Signature: sha1=" . $hmac_sig);
25
26                 logger('queue [pubsubhubbub]: POST', $headers);
27
28                 post_url($rr['callback_url'], $params, $headers);
29                 $ret = $a->get_curl_code();
30
31                 if ($ret >= 200 && $ret <= 299) {
32                         logger('queue [pubsubhubbub]: successfully pushed to ' .
33                                    $rr['callback_url']);
34
35                         // set last_update to "now", and reset push=0
36                         $date_now = datetime_convert('UTC','UTC','now','Y-m-d H:i:s');
37                         q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' " .
38                           "WHERE id = %d",
39                           dbesc($date_now),
40                           intval($rr['id']));
41
42                 } else {
43                         logger('queue [pubsubhubbub]: error when pushing to ' .
44                                    $rr['callback_url'] . 'HTTP: ', $ret);
45
46                         // we use the push variable also as a counter, if we failed we
47                         // increment this until some upper limit where we give up
48                         $new_push = intval($rr['push']) + 1;
49
50                         if ($new_push > 30) // OK, let's give up
51                                 $new_push = 0;
52
53                         q("UPDATE `push_subscriber` SET `push` = %d, last_update = '%s' " .
54                           "WHERE id = %d",
55                           $new_push,
56                           dbesc($date_now),
57                           intval($rr['id']));
58                 }
59         }
60 }
61
62
63 function queue_run(&$argv, &$argc){
64         global $a, $db;
65
66         if(is_null($a)){
67                 $a = new App;
68         }
69
70         if(is_null($db)){
71                 @include(".htconfig.php");
72                 require_once("include/dba.php");
73                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
74                 unset($db_host, $db_user, $db_pass, $db_data);
75         };
76
77
78         require_once("include/session.php");
79         require_once("include/datetime.php");
80         require_once('include/items.php');
81         require_once('include/bbcode.php');
82         require_once('include/pidfile.php');
83
84         load_config('config');
85         load_config('system');
86
87         $lockpath = get_config('system','lockpath');
88         if ($lockpath != '') {
89                 $pidfile = new pidfile($lockpath, 'queue');
90                 if($pidfile->is_already_running()) {
91                         logger("queue: Already running");
92                         if ($pidfile->running_time() > 9*60) {
93                                 $pidfile->kill();
94                                 logger("queue: killed stale process");
95                                 // Calling a new instance
96                                 proc_run('php',"include/queue.php");
97                         }
98                         return;
99                 }
100         }
101
102         $a->set_baseurl(get_config('system','url'));
103
104         load_hooks();
105
106         if($argc > 1)
107                 $queue_id = intval($argv[1]);
108         else
109                 $queue_id = 0;
110
111         $deadguys = array();
112
113         logger('queue: start');
114
115         handle_pubsubhubbub();
116
117         $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
118
119         $r = q("select * from deliverq where 1");
120         if($r) {
121                 foreach($r as $rr) {
122                         logger('queue: deliverq');
123                         proc_run('php','include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
124                         if($interval)
125                                 @time_sleep_until(microtime(true) + (float) $interval);
126                 }
127         }
128
129         $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue` 
130                 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id` 
131                 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
132         if($r) {
133                 foreach($r as $rr) {
134                         logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
135                         logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
136                 }
137                 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
138         }
139
140         if($queue_id) {
141                 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
142                         intval($queue_id)
143                 );
144         }
145         else {
146
147                 // For the first 12 hours we'll try to deliver every 15 minutes
148                 // After that, we'll only attempt delivery once per hour. 
149
150                 $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 ))");
151         }
152         if(! $r){
153                 return;
154         }
155
156         if(! $queue_id)
157                 call_hooks('queue_predeliver', $a, $r);
158
159
160         // delivery loop
161
162         require_once('include/salmon.php');
163         require_once('include/diaspora.php');
164
165         foreach($r as $q_item) {
166
167                 // queue_predeliver hooks may have changed the queue db details, 
168                 // so check again if this entry still needs processing
169
170                 if($queue_id) {
171                         $qi = q("select * from queue where `id` = %d limit 1",
172                                 intval($queue_id)
173                         );
174                 }
175                 else {
176                         $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
177                                 intval($q_item['id'])
178                         );
179                 }
180                 if(! count($qi))
181                         continue;
182
183
184                 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
185                         intval($qi[0]['cid'])
186                 );
187                 if(! count($c)) {
188                         remove_queue_item($q_item['id']);
189                         continue;
190                 }
191                 if(in_array($c[0]['notify'],$deadguys)) {
192                                 logger('queue: skipping known dead url: ' . $c[0]['notify']);
193                                 update_queue_time($q_item['id']);
194                                 continue;
195                 }
196
197                 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey` 
198                         FROM `user` WHERE `uid` = %d LIMIT 1",
199                         intval($c[0]['uid'])
200                 );
201                 if(! count($u)) {
202                         remove_queue_item($q_item['id']);
203                         continue;
204                 }
205
206                 $data      = $qi[0]['content'];
207                 $public    = $qi[0]['batch'];
208                 $contact   = $c[0];
209                 $owner     = $u[0];
210
211                 $deliver_status = 0;
212
213                 switch($contact['network']) {
214                         case NETWORK_DFRN:
215                                 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
216                                 $deliver_status = dfrn_deliver($owner,$contact,$data);
217
218                                 if($deliver_status == (-1)) {
219                                         update_queue_time($q_item['id']);
220                                         $deadguys[] = $contact['notify'];
221                                 }
222                                 else {
223                                         remove_queue_item($q_item['id']);
224                                 }
225                                 break;
226                         case NETWORK_OSTATUS:
227                                 if($contact['notify']) {
228                                         logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
229                                         $deliver_status = slapper($owner,$contact['notify'],$data);
230
231                                         if($deliver_status == (-1))
232                                                 update_queue_time($q_item['id']);
233                                         else
234                                                 remove_queue_item($q_item['id']);
235                                 }
236                                 break;
237                         case NETWORK_DIASPORA:
238                                 if($contact['notify']) {
239                                         logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
240                                         $deliver_status = diaspora_transmit($owner,$contact,$data,$public,true);
241
242                                         if($deliver_status == (-1))
243                                                 update_queue_time($q_item['id']);
244                                         else
245                                                 remove_queue_item($q_item['id']);
246                                 }
247                                 break;
248
249                         default:
250                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
251                                 call_hooks('queue_deliver', $a, $params);
252
253                                 if($params['result'])
254                                                 remove_queue_item($q_item['id']);
255                                 else
256                                                 update_queue_time($q_item['id']);
257
258                                 break;
259
260                 }
261         }
262
263         return;
264
265 }
266
267 if (array_search(__file__,get_included_files())===0){
268   queue_run($argv,$argc);
269   killme();
270 }