2 require_once("boot.php");
3 require_once('include/queue_fn.php');
5 function handle_pubsubhubbub() {
8 logger('queue [pubsubhubbub]: start');
10 // We'll push to each subscriber that has push > 0,
11 // i.e. there has been an update (set in notifier.php).
13 $r = q("SELECT * FROM `push_subscriber` WHERE `push` > 0");
16 $params = get_feed_for($a, '', $rr['nickname'], $rr['last_update']);
17 $hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
19 $headers = array("Content-type: application/atom+xml",
20 sprintf("Link: <%s>;rel=hub," .
22 $a->get_baseurl() . '/pubsubhubbub',
24 "X-Hub-Signature: sha1=" . $hmac_sig);
26 logger('queue [pubsubhubbub]: POST', $headers);
28 post_url($rr['callback_url'], $params, $headers);
29 $ret = $a->get_curl_code();
31 if ($ret >= 200 && $ret <= 299) {
32 logger('queue [pubsubhubbub]: successfully pushed to ' .
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' " .
43 logger('queue [pubsubhubbub]: error when pushing to ' .
44 $rr['callback_url'] . 'HTTP: ', $ret);
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;
50 if ($new_push > 30) // OK, let's give up
53 q("UPDATE `push_subscriber` SET `push` = %d, last_update = '%s' " .
63 function queue_run(&$argv, &$argc){
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);
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');
84 load_config('config');
85 load_config('system');
87 $lockpath = get_config('system','lockpath');
88 if ($lockpath != '') {
89 $pidfile = new pidfile($lockpath, 'queue.lck');
90 if($pidfile->is_already_running()) {
91 logger("queue: Already running");
96 $a->set_baseurl(get_config('system','url'));
101 $queue_id = intval($argv[1]);
107 logger('queue: start');
109 handle_pubsubhubbub();
111 $interval = ((get_config('system','delivery_interval') === false) ? 2 : intval(get_config('system','delivery_interval')));
113 $r = q("select * from deliverq where 1");
116 logger('queue: deliverq');
117 proc_run('php','include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']);
119 @time_sleep_until(microtime(true) + (float) $interval);
123 $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
124 INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
125 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
128 logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
129 logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
131 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
135 $r = q("SELECT `id` FROM `queue` WHERE `id` = %d LIMIT 1",
141 // For the first 12 hours we'll try to deliver every 15 minutes
142 // After that, we'll only attempt delivery once per hour.
144 $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 call_hooks('queue_predeliver', $a, $r);
156 require_once('include/salmon.php');
157 require_once('include/diaspora.php');
159 foreach($r as $q_item) {
161 // queue_predeliver hooks may have changed the queue db details,
162 // so check again if this entry still needs processing
165 $qi = q("select * from queue where `id` = %d limit 1",
170 $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
171 intval($q_item['id'])
178 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
179 intval($qi[0]['cid'])
182 remove_queue_item($q_item['id']);
185 if(in_array($c[0]['notify'],$deadguys)) {
186 logger('queue: skipping known dead url: ' . $c[0]['notify']);
187 update_queue_time($q_item['id']);
191 $u = q("SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
192 FROM `user` WHERE `uid` = %d LIMIT 1",
196 remove_queue_item($q_item['id']);
200 $data = $qi[0]['content'];
201 $public = $qi[0]['batch'];
207 switch($contact['network']) {
209 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
210 $deliver_status = dfrn_deliver($owner,$contact,$data);
212 if($deliver_status == (-1)) {
213 update_queue_time($q_item['id']);
214 $deadguys[] = $contact['notify'];
217 remove_queue_item($q_item['id']);
220 case NETWORK_OSTATUS:
221 if($contact['notify']) {
222 logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
223 $deliver_status = slapper($owner,$contact['notify'],$data);
225 if($deliver_status == (-1))
226 update_queue_time($q_item['id']);
228 remove_queue_item($q_item['id']);
231 case NETWORK_DIASPORA:
232 if($contact['notify']) {
233 logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
234 $deliver_status = diaspora_transmit($owner,$contact,$data,$public,true);
236 if($deliver_status == (-1))
237 update_queue_time($q_item['id']);
239 remove_queue_item($q_item['id']);
244 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
245 call_hooks('queue_deliver', $a, $params);
247 if($params['result'])
248 remove_queue_item($q_item['id']);
250 update_queue_time($q_item['id']);
261 if (array_search(__file__,get_included_files())===0){
262 queue_run($argv,$argc);