]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
more bugs
[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
16 if($argc < 3)
17         exit;
18 dbg(3);
19         $baseurl = $argv[1];
20         $a->set_baseurl($argv[1]);
21
22         $cmd = $argv[2];
23
24         switch($cmd) {
25
26                 default:
27                         $item_id = intval($argv[3]);
28                         if(! $item_id)
29                                 killme();
30                         break;
31         }
32
33
34         $recipients = array();
35
36         // find ancestors
37
38         $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
39                 intval($item_id)
40         );
41         if(! count($r))
42                 killme();
43
44         $parent = $r[0]['parent'];
45         $uid = $r[0]['uid'];
46         $updated = $r[0]['edited'];
47
48         $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
49                 intval($parent)
50         );
51
52         if(! count($items))
53                 killme();
54
55         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
56                 intval($uid)
57         );
58
59         if(count($r))
60                 $owner = $r[0];
61         else
62                 killme();
63
64
65         require_once('include/group.php');
66
67         $parent = $items[0];
68
69         if($parent['type'] == 'remote') {
70                 // local followup to remote post
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 ) AND `blocked` = 0 AND `pending` = 0");
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($a->get_baseurl()),
117                         '$feed_title' => xmlify($owner['name']),
118                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', 
119                                 $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
120                         '$name' => xmlify($owner['name']),
121                         '$profile_page' => xmlify($owner['url']),
122                         '$photo' => xmlify($owner['photo'])
123         ));
124
125         if($followup) {
126                 foreach($items as $item) {
127                         if($item['id'] == $item_id) {
128                                 $atom .= replace_macros($cmnt_template, array(
129                                         '$name' => xmlify($owner['name']),
130                                         '$profile_page' => xmlify($owner['url']),
131                                         '$thumb' => xmlify($owner['thumb']),
132                                         '$item_id' => xmlify($item['uri']),
133                                         '$title' => xmlify($item['title']),
134                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', 
135                                                 $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
136                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
137                                                 $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
138                                         '$content' =>xmlify($item['body']),
139                                         '$parent_id' => xmlify($item['parent-uri']),
140                                         '$comment_allow' => 0
141                                 ));
142                         }
143                 }
144         }
145         else {
146                 foreach($items as $item) {
147                         if($item['deleted']) {
148                                 $atom .= replace_macros($tomb_template, array(
149                                         '$id' => xmlify($item['uri']),
150                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
151                                                 $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
152                                 ));
153                         }
154                         else {
155                                 foreach($contacts as $contact) {
156                                         if($item['contact-id'] == $contact['id']) {
157                                                 if($item['parent'] == $item['id']) {
158                                                         $atom .= replace_macros($item_template, array(
159                                                                 '$name' => xmlify($contact['name']),
160                                                                 '$profile_page' => xmlify($contact['url']),
161                                                                 '$thumb' => xmlify($contact['thumb']),
162                                                                 '$owner_name' => xmlify($item['owner-name']),
163                                                                 '$owner_profile_page' => xmlify($item['owner-link']),
164                                                                 '$owner_thumb' => xmlify($item['owner-avatar']),
165                                                                 '$item_id' => xmlify($item['uri']),
166                                                                 '$title' => xmlify($contact['name']),
167                                                                 '$published' => xmlify(datetime_convert('UTC', 'UTC', 
168                                                                         $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
169                                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
170                                                                         $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
171                                                                 '$content' =>xmlify($item['body']),
172                                                                 '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0)
173                                                         ));
174                                                 }
175                                                 else {
176                                                         $atom .= replace_macros($cmnt_template, array(
177                                                                 '$name' => xmlify($contact['name']),
178                                                                 '$profile_page' => xmlify($contact['url']),
179                                                                 '$thumb' => xmlify($contact['thumb']),
180                                                                 '$item_id' => xmlify($item['uri']),
181                                                                 '$title' => xmlify($item['title']),
182                                                                 '$published' => xmlify(datetime_convert('UTC', 'UTC', 
183                                                                         $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
184                                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', 
185                                                                         $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
186                                                                 '$content' =>xmlify($item['body']),
187                                                                 '$parent_id' => xmlify($item['parent-uri']),
188                                                                 '$comment_allow' => (($item['last-child']) ? 1 : 0)
189                                                         ));
190                                                 }
191                                         }
192                                 }
193                         }
194                 }
195         }
196         $atom .= "</feed>\r\n";
197
198         // create a clone of this feed but with comments disabled to send to those who can't respond. 
199
200         $atom_nowrite = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
201
202
203         if($followup)
204                 $recip_str = $parent['contact-id'];
205         else
206                 $recip_str = implode(', ', $recipients);
207
208
209         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
210                 dbesc($recip_str)
211         );
212         if(! count($r))
213                 killme();
214
215         // delivery loop
216
217         foreach($r as $rr) {
218
219                 if($rr['self'])
220                         continue;
221
222                 if(! strlen($rr['dfrn-id']))
223                         continue;
224                 $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id'];
225
226                 $xml = fetch_url($url);
227 echo $xml;
228                 if(! $xml)
229                         continue;
230
231                 $res = simplexml_load_string($xml);
232
233                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || ($res->dfrn_id != $rr['dfrn-id']))
234                         continue;
235
236                 $postvars = array();
237
238                 $postvars['dfrn_id'] = $rr['dfrn-id'];
239                 $challenge = hex2bin($res->challenge);
240
241                 openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
242
243                 if(strlen($rr['dfrn-id']) && (! $rr['blocked']))
244                         $postvars['data'] = $atom;
245                 else
246                         $postvars['data'] = $atom_nowrite;
247
248                 $xml = post_url($rr['notify'],$postvars);
249 echo $xml;
250         }
251
252         killme();
253