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