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