]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
like, dislike, activity streams, etc.
[friendica.git] / include / notifier.php
1 <?php
2
3         $debugging = true;
4
5         require_once("boot.php");
6
7         $a = new App;
8
9         @include(".htconfig.php");
10         require_once("dba.php");
11         $db = new dba($db_host, $db_user, $db_pass, $db_data);
12                 unset($db_host, $db_user, $db_pass, $db_data);
13
14         require_once("session.php");
15         require_once("datetime.php");
16         require_once('include/items.php');
17
18         if($argc < 3)
19                 exit;
20
21         $a->set_baseurl(get_config('system','url'));
22
23         $cmd = $argv[1];
24
25         switch($cmd) {
26
27                 case 'mail':
28                 default:
29                         $item_id = intval($argv[2]);
30                         if(! $item_id)
31                                 killme();
32                         break;
33         }
34
35         if($debugging)
36                 dbg(3);
37
38         $recipients = array();
39
40         if($cmd == 'mail') {
41
42                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
43                                 intval($item_id)
44                 );
45                 if(! count($message))
46                         killme();
47                 $uid = $message[0]['uid'];
48                 $recipients[] = $message[0]['contact-id'];
49                 $item = $message[0];
50
51         }
52         else {
53                 // find ancestors
54
55                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
56                         intval($item_id)
57                 );
58                 if(! count($r))
59                         killme();
60
61                 $parent = $r[0]['parent'];
62                 $uid = $r[0]['uid'];
63                 $updated = $r[0]['edited'];
64
65                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
66                         intval($parent)
67                 );
68
69                 if(! count($items))
70                         killme();
71         }
72
73         $r = q("SELECT `contact`.*, `user`.`nickname` 
74                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
75                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
76                 intval($uid)
77         );
78
79         if(count($r))
80                 $owner = $r[0];
81         else
82                 killme();
83
84         if($cmd != 'mail') {
85
86                 require_once('include/group.php');
87
88                 $parent = $items[0];
89
90                 if($parent['type'] == 'remote') {
91                         // local followup to remote post
92                         $followup = true;
93                         $conversant_str = dbesc($parent['contact-id']);
94                 }
95                 else {
96                         $followup = false;
97
98                         $allow_people = expand_acl($parent['allow_cid']);
99                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
100                         $deny_people = expand_acl($parent['deny_cid']);
101                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
102
103                         $conversants = array();
104
105                         foreach($items as $item) {
106                                 $recipients[] = $item['contact-id'];
107                                 $conversants[] = $item['contact-id'];
108                         }
109
110                         $conversants = array_unique($conversants,SORT_NUMERIC);
111
112
113                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
114                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
115                         $recipients = array_diff($recipients,$deny);
116         
117                         $conversant_str = dbesc(implode(', ',$conversants));
118
119
120                 }
121
122                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
123
124                 if( ! count($r))
125                         killme();
126
127                 $contacts = $r;
128
129                 $tomb_template = file_get_contents('view/atom_tomb.tpl');
130                 $item_template = file_get_contents('view/atom_item.tpl');
131                 $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
132         }
133
134         $feed_template = file_get_contents('view/atom_feed.tpl');
135         $mail_template = file_get_contents('view/atom_mail.tpl');
136
137         $atom = '';
138
139
140         $atom .= replace_macros($feed_template, array(
141                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
142                         '$feed_title'   => xmlify($owner['name']),
143                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
144                         '$name'         => xmlify($owner['name']),
145                         '$profile_page' => xmlify($owner['url']),
146                         '$photo'        => xmlify($owner['photo']),
147                         '$thumb'        => xmlify($owner['thumb']),
148                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
149                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
150                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME))
151         ));
152
153         if($cmd == 'mail') {
154                 $atom .= replace_macros($mail_template, array(
155                         '$name'         => xmlify($owner['name']),
156                         '$profile_page' => xmlify($owner['url']),
157                         '$thumb'        => xmlify($owner['thumb']),
158                         '$item_id'      => xmlify($item['uri']),
159                         '$subject'      => xmlify($item['title']),
160                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
161                         '$content'      => xmlify($item['body']),
162                         '$parent_id'    => xmlify($item['parent-uri'])
163                 ));
164         }
165         else {
166
167
168                 if($followup) {
169                         foreach($items as $item) {
170
171                                 $verb = construct_verb($item);
172                                 $actobj = construct_activity($item);
173
174                                 if($item['id'] == $item_id) {
175                                         $atom .= replace_macros($cmnt_template, array(
176                                                 '$name'               => xmlify($owner['name']),
177                                                 '$profile_page'       => xmlify($owner['url']),
178                                                 '$thumb'              => xmlify($owner['thumb']),
179                                                 '$owner_name'         => xmlify($item['owner-name']),
180                                                 '$owner_profile_page' => xmlify($item['owner-link']),
181                                                 '$owner_thumb'        => xmlify($item['owner-avatar']),
182                                                 '$item_id'            => xmlify($item['uri']),
183                                                 '$title'              => xmlify($item['title']),
184                                                 '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
185                                                 '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
186                                                 '$location'           => xmlify($item['location']),
187                                                 '$type'               => 'text',
188                                                 '$verb'               => xmlify($verb),
189                                                 '$actobj'             => $actobj,
190                                                 '$content'            => xmlify($item['body']),
191                                                 '$parent_id'          => xmlify($item['parent-uri']),
192                                                 '$comment_allow'      => 0
193                                         ));
194                                 }
195                         }
196                 }
197                 else {
198                         foreach($items as $item) {
199                                 if($item['deleted']) {
200                                         $atom .= replace_macros($tomb_template, array(
201                                                 '$id' => xmlify($item['uri']),
202                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
203                                         ));
204                                 }
205                                 else {
206                                         foreach($contacts as $contact) {
207                                                 if($item['contact-id'] == $contact['id']) {
208
209                                                         $verb = construct_verb($item);
210                                                         $actobj = construct_activity($item);
211
212                                                         if($item['parent'] == $item['id']) {
213                                                                 $atom .= replace_macros($item_template, array(
214                                                                         '$name'               => xmlify($contact['name']),
215                                                                         '$profile_page'       => xmlify($contact['url']),
216                                                                         '$thumb'              => xmlify($contact['thumb']),
217                                                                         '$owner_name'         => xmlify($item['owner-name']),
218                                                                         '$owner_profile_page' => xmlify($item['owner-link']),
219                                                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
220                                                                         '$item_id'            => xmlify($item['uri']),
221                                                                         '$title'              => xmlify($item['title']),
222                                                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
223                                                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
224                                                                         '$location'           => xmlify($item['location']),
225                                                                         '$type'               => 'text',
226                                                                         '$verb'               => xmlify($verb),
227                                                                         '$actobj'             => $actobj,
228                                                                         '$content'            => xmlify($item['body']),
229                                                                         '$comment_allow'      => (($item['last-child']) ? 1 : 0)
230                                                                 ));
231                                                         }
232                                                         else {
233                                                                 $atom .= replace_macros($cmnt_template, array(
234                                                                         '$name'          => xmlify($contact['name']),
235                                                                         '$profile_page'  => xmlify($contact['url']),
236                                                                         '$thumb'         => xmlify($contact['thumb']),
237                                                                         '$item_id'       => xmlify($item['uri']),
238                                                                         '$title'         => xmlify($item['title']),
239                                                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
240                                                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME)),
241                                                                         '$content'       => xmlify($item['body']),
242                                                                         '$location'      => xmlify($item['location']),
243                                                                         '$type'          => 'text',
244                                                                         '$verb'          => xmlify($verb),
245                                                                         '$actobj'        => $actobj,
246                                                                         '$parent_id'     => xmlify($item['parent-uri']),
247                                                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
248                                                                 ));
249                                                         }
250                                                 }
251                                         }
252                                 }
253                         }
254                 }
255         }
256         $atom .= "</feed>\r\n";
257
258         if($debugging)
259                 echo $atom;
260
261         // create a clone of this feed but with comments disabled to send to those who can't respond. 
262
263         $atom_nowrite = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
264
265
266         if($followup)
267                 $recip_str = $parent['contact-id'];
268         else
269                 $recip_str = implode(', ', $recipients);
270
271
272         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
273                 dbesc($recip_str)
274         );
275         if(! count($r))
276                 killme();
277
278         // delivery loop
279
280         foreach($r as $rr) {
281                 if($rr['self'])
282                         continue;
283
284                 if((! strlen($rr['dfrn-id'])) && (! $rr['duplex']))
285                         continue;
286
287
288                 $idtosend = $orig_id = (($rr['dfrn-id']) ? $rr['dfrn-id'] : $rr['issued-id']);
289
290                 if($rr['duplex'] && $rr['dfrn-id'])
291                         $idtosend = '0:' . $orig_id;
292                 if($rr['duplex'] && $rr['issued-id'])
293                         $idtosend = '1:' . $orig_id;            
294
295                 $url = $rr['notify'] . '?dfrn_id=' . $idtosend;
296
297                 if($debugging)
298                         echo "URL: $url";
299
300                 $xml = fetch_url($url);
301
302                 if($debugging)
303                         echo $xml;
304
305                 if(! $xml)
306                         continue;
307
308                 $res = simplexml_load_string($xml);
309
310                 if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
311                         continue;
312
313                 $postvars     = array();
314
315                 $sent_dfrn_id = hex2bin($res->dfrn_id);
316                 $challenge    = hex2bin($res->challenge);
317
318                 $final_dfrn_id = '';
319
320                 if($rr['duplex'] && strlen($rr['prvkey'])) {
321                         openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['prvkey']);
322                         openssl_private_decrypt($challenge,$postvars['challenge'],$rr['prvkey']);
323                 }
324                 else {
325                         openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['pubkey']);
326                         openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']);
327                 }
328
329                 $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
330
331                 if(strpos($final_dfrn_id,':') == 1)
332                         $final_dfrn_id = substr($final_dfrn_id,2);
333
334                 if($final_dfrn_id != $orig_id) {
335                         // did not decode properly - cannot trust this site 
336                         continue;
337                 }
338
339                 $postvars['dfrn_id'] = $idtosend;
340
341                 if(($rr['rel']) && ($rr['rel'] != REL_FAN) && (! $rr['blocked']) && (! $rr['readonly'])) {
342                         $postvars['data'] = $atom;
343                 }
344                 else {
345                         $postvars['data'] = $atom_nowrite;
346                 }
347
348                 $xml = post_url($rr['notify'],$postvars);
349
350                 if($debugging)
351                         echo $xml;
352
353                 $res = simplexml_load_string($xml);
354
355                 // Currently there is no retry attempt for failed mail delivery.
356                 // We need to handle this in the UI, report the non-deliverables and try again
357  
358                 if(($cmd == 'mail') && (intval($res->status) == 0)) {
359
360                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
361                                 intval($item_id)
362                         );
363                 }
364         }
365
366         killme();
367