]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
4d114a4e3327ab96cb645fa4af5e6809c1d00ac5
[friendica.git] / include / notifier.php
1 <?php
2
3 require_once("boot.php");
4
5 $a = new App;
6
7 @include(".htconfig.php");
8 require_once("dba.php");
9 $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
10         unset($db_host, $db_user, $db_pass, $db_data);
11
12 require_once("session.php");
13 require_once("datetime.php");
14
15 // FIXME - generalise for other content, probably create a notify queue in 
16 // the db with type and recipient list
17
18 if($argc < 3)
19         exit;
20
21         $baseurl = trim(hex2bin($argv[1]));
22
23         $cmd = $argv[2];
24
25         switch($cmd) {
26
27                 default:
28                         $item_id = intval($argv[3]);
29                         if(! $item_id)
30                                 killme();
31                         break;
32         }
33
34
35         $recipients = array();
36
37         // find ancestors
38
39         $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
40                 intval($item_id)
41         );
42         if(! count($r))
43                 killme();
44
45         $parent = $r[0]['parent'];
46         $uid = $r[0]['uid'];
47         $updated = $r[0]['edited'];
48
49         $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
50                 intval($parent)
51         );
52
53         if(! count($items))
54                 killme();
55
56         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
57                 intval($uid)
58         );
59
60         if(count($r))
61                 $owner = $r[0];
62         else
63                 killme();
64
65
66         require_once('include/group.php');
67
68         $parent = $items[0];
69
70         if(strlen($parent['remote-id'])) {
71                 $followup = true;
72                 $conversant_str = dbesc($parent['contact-id']);
73         }
74         else {
75                 $followup = false;
76
77                 $allow_people = expand_acl($parent['allow_cid']);
78                 $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
79                 $deny_people = expand_acl($parent['deny_cid']);
80                 $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
81
82                 $conversants = array();
83
84                 foreach($items as $item) {
85                         $recipients[] = $item['contact-id'];
86                         $conversants[] = $item['contact-id'];
87                 }
88
89                 $conversants = array_unique($conversants,SORT_NUMERIC);
90
91
92                 $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
93                 $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
94                 $recipients = array_diff($recipients,$deny);
95         
96                 $conversant_str = dbesc(implode(', ',$conversants));
97         }
98
99         $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) ");
100
101         if( ! count($r))
102                 killme();
103
104         $contacts = $r;
105
106
107         $feed_template = file_get_contents('view/atom_feed.tpl');
108         $tomb_template = file_get_contents('view/atom_tomb.tpl');
109         $item_template = file_get_contents('view/atom_item.tpl');
110         $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
111
112         $atom = '';
113
114
115         $atom .= replace_macros($feed_template, array(
116                         '$feed_id' => xmlify($baseurl),
117                         '$feed_title' => xmlify($owner['name']),
118                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
119                         '$name' => xmlify($owner['name']),
120                         '$profile_page' => xmlify($owner['url']),
121                         '$photo' => xmlify($owner['photo'])
122         ));
123
124         if($followup) {
125                 $atom .= replace_macros($cmnt_template, array(
126                         '$name' => xmlify($contact['name']),
127                         '$profile_page' => xmlify($contact['url']),
128                         '$thumb' => xmlify($contact['thumb']),
129                         '$item_id' => xmlify("urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}"),
130                         '$title' => xmlify($item['title']),
131                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
132                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
133                         '$content' =>xmlify($item['body']),
134                         '$parent_id' => xmlify("{$items[0]['remote-id']}"),
135                         '$comment_allow' => 0
136                 ));
137         }
138         else {
139                 foreach($items as $item) {
140                         if($item['deleted']) {
141                                 $atom .= replace_macros($tomb_template, array(
142                                         '$id' => xmlify("urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}"),
143                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
144                                 ));
145                         }
146                         else {
147                                 foreach($contacts as $contact) {
148                                         if($item['contact-id'] == $contact['id']) {
149                                                 if($item['parent'] == $item['id']) {
150                                                         $atom .= replace_macros($item_template, array(
151                                                                 '$name' => xmlify($contact['name']),
152                                                                 '$profile_page' => xmlify($contact['url']),
153                                                                 '$thumb' => xmlify($contact['thumb']),
154                                                                 '$item_id' => xmlify("urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}"),
155                                                                 '$title' => xmlify($contact['name']),
156                                                                 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
157                                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
158                                                                 '$content' =>xmlify($item['body']),
159                                                                 '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'] && (! $contact['blocked']))) ? 1 : 0)
160                                                         ));
161                                                 }
162                                                 else {
163                                                         $atom .= replace_macros($cmnt_template, array(
164                                                                 '$name' => xmlify($contact['name']),
165                                                                 '$profile_page' => xmlify($contact['url']),
166                                                                 '$thumb' => xmlify($contact['thumb']),
167                                                                 '$item_id' => xmlify("urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}"),
168                                                                 '$title' => xmlify($item['title']),
169                                                                 '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
170
171                                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
172                                                                 '$content' =>xmlify($item['body']),
173                                                                 '$parent_id' => xmlify("urn:X-dfrn:$baseurl:{$owner['uid']}:{$items[0]['hash']}"),
174                                                                 '$comment_allow' => (($item['last-child']) ? 1 : 0)
175                                                         ));
176                                                 }
177                                         }
178                                 }
179                         }
180                 }
181         }
182         $atom .= "</feed>\r\n";
183
184         // create a separate feed with comments disabled and send to those who can't respond. 
185
186         $atom_nowrite = str_replace('<dfrn:comment-allow>1</dfrn:comment-allow>','<dfrn:comment-allow>0</dfrn:comment-allow>',$atom);
187
188 print_r($atom);
189
190
191 dbg(3);
192
193
194
195 print_r($recipients);
196
197         if($followup)
198                 $recip_str = $parent['contact-id'];
199         else
200                 $recip_str = implode(', ', $recipients);
201
202         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
203                 dbesc($recip_str)
204         );
205         if(! count($r))
206                 killme();
207
208         // delivery loop
209
210         foreach($r as $rr) {
211                 if($rr['self'])
212                         continue;
213
214                 if(! strlen($rr['dfrn-id']))
215                         continue;
216                 $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id'];
217 print_r($url);
218                 $xml = fetch_url($url);
219 echo $xml;
220
221 print_r($xml);
222                 if(! $xml)
223                         continue;
224
225                 $res = simplexml_load_string($xml);
226 print_r($res);
227 var_dump($res);
228
229                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $rr['dfrn-id']))
230                         continue;
231
232                 $postvars = array();
233
234                 $postvars['dfrn_id'] = $rr['dfrn-id'];
235                 $challenge = hex2bin($res->challenge);
236 echo "dfrn-id:" . $res->dfrn_id . "\r\n";
237 echo "challenge:" . $res->challenge . "\r\n";
238 echo "pubkey:" . $rr['pubkey'] . "\r\n";
239
240                 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
241
242                 if(strlen($rr['dfrn-id']) && (! $rr['blocked']))
243                         $postvars['data'] = $atom;
244                 else
245                         $postvars['data'] = $atom_nowrite;
246
247 print_r($postvars);
248                 $xml = post_url($url,$postvars);
249
250 print_r($xml);                          
251         }
252
253         killme();
254