]> git.mxchange.org Git - friendica.git/blob - include/queue.php
da5028aee5e59697ce4c43092257e3ed2c81d44e
[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         $deadguys = array();
33
34         logger('queue: start');
35
36         $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue` 
37                 LEFT JOIN `contact` ON `queue`.`cid` = `contact`.`id` 
38                 WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
39         if(count($r)) {
40                 foreach($r as $rr) {
41                         logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
42                         logger('Expired queue data :' . $rr['content'], LOGGER_DATA);
43                 }
44                 q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
45         }
46                 
47         $r = q("SELECT `id` FROM `queue` WHERE `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ");
48
49         if(! count($r)){
50                 return;
51         }
52
53         call_hooks('queue_predeliver', $a, $r);
54
55
56         // delivery loop
57
58         require_once('include/salmon.php');
59         require_once('include/diaspora.php');
60
61         foreach($r as $q_item) {
62
63                 // queue_predeliver hooks may have changed the queue db details, 
64                 // so check again if this entry still needs processing
65
66                 $qi = q("SELECT * FROM `queue` WHERE `id` = %d AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE ",
67                         intval($q_item['id'])
68                 );
69                 if(! count($qi))
70                         continue;
71
72
73                 $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
74                         intval($qi[0]['cid'])
75                 );
76                 if(! count($c)) {
77                         remove_queue_item($q_item['id']);
78                         continue;
79                 }
80                 if(in_array($c[0]['notify'],$deadguys)) {
81                                 logger('queue: skipping known dead url: ' . $c[0]['notify']);
82                                 update_queue_time($q_item['id']);
83                                 continue;
84                 }
85
86                 $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
87                         intval($c[0]['uid'])
88                 );
89                 if(! count($u)) {
90                         remove_queue_item($q_item['id']);
91                         continue;
92                 }
93
94                 $data      = $qi[0]['content'];
95                 $contact   = $c[0];
96                 $owner     = $u[0];
97
98                 $deliver_status = 0;
99
100                 switch($contact['network']) {
101                         case NETWORK_DFRN:
102                                 logger('queue: dfrndelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
103                                 $deliver_status = dfrn_deliver($owner,$contact,$data);
104
105                                 if($deliver_status == (-1)) {
106                                         update_queue_time($q_item['id']);
107                                         $deadguys[] = $contact['notify'];
108                                 }
109                                 else {
110                                         remove_queue_item($q_item['id']);
111                                 }
112                                 break;
113                         case NETWORK_OSTATUS:
114                                 if($contact['notify']) {
115                                         logger('queue: slapdelivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
116                                         $deliver_status = slapper($owner,$contact['notify'],$data);
117
118                                         if($deliver_status == (-1))
119                                                 update_queue_time($q_item['id']);
120                                         else
121                                                 remove_queue_item($q_item['id']);
122                                 }
123                                 break;
124                         case NETWORK_DIASPORA:
125                                 if($contact['notify']) {
126                                         logger('queue: diaspora_delivery: item ' . $q_item['id'] . ' for ' . $contact['name']);
127                                         $deliver_status = diaspora_transmit($owner,$contact['notify'],$data);
128
129                                         if($deliver_status == (-1))
130                                                 update_queue_time($q_item['id']);
131                                         else
132                                                 remove_queue_item($q_item['id']);
133                                 }
134                                 break;
135
136                         default:
137                                 $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
138                                 call_hooks('queue_deliver', $a, $params);
139                 
140                                 if($params['result'])
141                                                 remove_queue_item($q_item['id']);
142                                 else
143                                                 update_queue_time($q_item['id']);
144         
145                                 break;
146
147                 }
148         }
149                 
150         return;
151
152 }
153
154 if (array_search(__file__,get_included_files())===0){
155   queue_run($argv,$argc);
156   killme();
157 }