]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
add owner's birthday information to feed (if allowed to)
[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);
10                 unset($db_host, $db_user, $db_pass, $db_data);
11
12
13         require_once("session.php");
14         require_once("datetime.php");
15         require_once('include/items.php');
16         require_once('include/bbcode.php');
17
18         if($argc < 3)
19                 exit;
20
21         $a->set_baseurl(get_config('system','url'));
22
23         logger('notifier: invoked: ' . print_r($argv,true));
24
25         $cmd = $argv[1];
26
27         switch($cmd) {
28
29                 case 'mail':
30                 default:
31                         $item_id = intval($argv[2]);
32                         if(! $item_id)
33                                 killme();
34                         break;
35         }
36
37         $recipients = array();
38         $url_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_id = $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_id)
67                 );
68
69                 if(! count($items))
70                         killme();
71         }
72
73         $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags` 
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         $hub = get_config('system','huburl');
85
86         // If this is a public conversation, notify the feed hub
87         $notify_hub = true;
88
89         // fill this in with a single salmon slap if applicable
90         $slap = '';
91
92         if($cmd != 'mail') {
93
94                 require_once('include/group.php');
95
96                 $parent = $items[0];
97
98                 if($parent['type'] === 'remote') {
99                         // local followup to remote post
100                         $followup = true;
101                         $notify_hub = false; // not public
102                         $conversant_str = dbesc($parent['contact-id']);
103                 }
104                 else {
105                         $followup = false;
106
107                         if((strlen($parent['allow_cid'])) 
108                                 || (strlen($parent['allow_gid'])) 
109                                 || (strlen($parent['deny_cid'])) 
110                                 || (strlen($parent['deny_gid']))) {
111                                 $notify_hub = false; // private recipients, not public
112                         }
113
114                         $allow_people = expand_acl($parent['allow_cid']);
115                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
116                         $deny_people = expand_acl($parent['deny_cid']);
117                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
118
119                         $conversants = array();
120
121                         foreach($items as $item) {
122                                 $recipients[] = $item['contact-id'];
123                                 $conversants[] = $item['contact-id'];
124                                 // pull out additional tagged people to notify (if public message)
125                                 if($notify_hub && strlen($item['inform'])) {
126                                         $people = explode(',',$item['inform']);
127                                         foreach($people as $person) {
128                                                 if(substr($person,0,4) === 'cid:') {
129                                                         $recipients[] = intval(substr($person,4));
130                                                         $conversants[] = intval(substr($person,4));
131                                                 }
132                                                 else {
133                                                         $url_recipients[] = substr($person,4);
134                                                 }
135                                         }
136                                 }
137                         }
138
139                         logger('notifier: url_recipients' . print_r($url_recipients,true));
140
141                         $conversants = array_unique($conversants);
142
143
144                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
145                         $deny = array_unique(array_merge($deny_people,$deny_groups));
146                         $recipients = array_diff($recipients,$deny);
147
148                         $conversant_str = dbesc(implode(', ',$conversants));
149                 }
150
151                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
152
153                 if( ! count($r))
154                         killme();
155
156                 $contacts = $r;
157         }
158
159         $feed_template = load_view_file('view/atom_feed.tpl');
160         $mail_template = load_view_file('view/atom_mail.tpl');
161
162         $atom = '';
163         $hubxml = '';
164         $slaps = array();
165
166         if(strlen($hub)) {
167                 $hubs = explode(',', $hub);
168                 if(count($hubs)) {
169                         foreach($hubs as $h) {
170                                 $h = trim($h);
171                                 if(! strlen($h))
172                                         continue;
173                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
174                         }
175                 }
176         }
177
178         $atom .= replace_macros($feed_template, array(
179                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
180                         '$feed_title'   => xmlify($owner['name']),
181                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
182                         '$hub'          => $hubxml,
183                         '$salmon'       => '',   // private feed, we don't use salmon here
184                         '$name'         => xmlify($owner['name']),
185                         '$profile_page' => xmlify($owner['url']),
186                         '$photo'        => xmlify($owner['photo']),
187                         '$thumb'        => xmlify($owner['thumb']),
188                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
189                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
190                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
191                         '$birthday'     => ''
192         ));
193
194         if($cmd === 'mail') {
195                 $notify_hub = false;  // mail is  not public
196
197                 $atom .= replace_macros($mail_template, array(
198                         '$name'         => xmlify($owner['name']),
199                         '$profile_page' => xmlify($owner['url']),
200                         '$thumb'        => xmlify($owner['thumb']),
201                         '$item_id'      => xmlify($item['uri']),
202                         '$subject'      => xmlify($item['title']),
203                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
204                         '$content'      => xmlify($item['body']),
205                         '$parent_id'    => xmlify($item['parent-uri'])
206                 ));
207         }
208         else {
209
210                 if($followup) {
211                         foreach($items as $item) {  // there is only one item
212                                 if($item['id'] == $item_id) {
213                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
214                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
215                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
216                                 }
217                         }
218                 }
219                 else {
220                         foreach($items as $item) {
221                                 $contact = get_item_contact($item,$contacts);
222                                 if(! $contact)
223                                         continue;
224
225                                 $atom   .= atom_entry($item,'text',$contact,$owner,true);
226                                 $slaps[] = atom_entry($item,'html',$contact,$owner,true);
227                         }
228                 }
229         }
230         $atom .= '</feed>' . "\r\n";
231
232         logger('notifier: ' . $atom, LOGGER_DATA);
233
234         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
235
236         if($followup)
237                 $recip_str = $parent['contact-id'];
238         else
239                 $recip_str = implode(', ', $recipients);
240
241
242         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 ",
243                 dbesc($recip_str)
244         );
245         if(! count($r))
246                 killme();
247
248         // delivery loop
249
250         require_once('include/salmon.php');
251
252         foreach($r as $contact) {
253                 if($contact['self'])
254                         continue;
255
256                 $deliver_status = 0;
257
258                 switch($contact['network']) {
259                         case 'dfrn':
260                                 logger('notifier: dfrndelivery: ' . $contact['name']);
261                                 $deliver_status = dfrn_deliver($owner,$contact,$atom);
262
263                                 logger('notifier: dfrn_delivery returns ' . $deliver_status);
264
265                                 if($deliver_status == (-1)) {
266                                         logger('notifier: delivery failed: queuing message');
267                                         // queue message for redelivery
268                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
269                                                 VALUES ( %d, '%s', '%s', '%s') ",
270                                                 intval($contact['id']),
271                                                 dbesc(datetime_convert()),
272                                                 dbesc(datetime_convert()),
273                                                 dbesc($atom)
274                                         );
275                                 }
276                                 break;
277                         default:
278                                 if($followup && $contact['notify']) {
279                                         logger('notifier: slapdelivery: ' . $contact['name']);
280                                         $deliver_status = slapper($owner,$contact['notify'],$slap);
281
282                                         if($deliver_status == (-1)) {
283                                                 // queue message for redelivery
284                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
285                                                         VALUES ( %d, '%s', '%s', '%s') ",
286                                                         intval($contact['id']),
287                                                         dbesc(datetime_convert()),
288                                                         dbesc(datetime_convert()),
289                                                         dbesc($slap)
290                                                 );
291
292                                         }
293
294
295                                 }
296                                 else {
297
298                                         // only send salmon if public - e.g. if it's ok to notify
299                                         // a public hub, it's ok to send a salmon
300
301                                         if(count($slaps) && $notify_hub) {
302                                                 logger('notifier: slapdelivery: ' . $contact['name']);
303                                                 foreach($slaps as $slappy) {
304                                                         if($contact['notify']) {
305                                                                 $deliver_status = slapper($owner,$contact['notify'],$slappy);
306                                                                 if($deliver_status == (-1)) {
307                                                                         // queue message for redelivery
308                                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
309                                                                                 VALUES ( %d, '%s', '%s', '%s') ",
310                                                                                 intval($contact['id']),
311                                                                                 dbesc(datetime_convert()),
312                                                                                 dbesc(datetime_convert()),
313                                                                                 dbesc($slappy)
314                                                                         );                                                              
315                                                                 }
316                                                         }
317                                                 }
318                                         }
319                                 }
320                                 break;
321                 }
322         }
323                 
324         // send additional slaps to mentioned remote tags (@foo@example.com)
325
326         if(count($slaps) && count($url_recipients) && $notify_hub) {
327                 foreach($url_recipients as $url) {
328                         logger('notifier: urldelivery: ' . $url);
329                         foreach($slaps as $slappy) {
330                                 if($url) {
331                                         $deliver_status = slapper($owner,$url,$slappy);
332                                         // TODO: redeliver/queue these items on failure, though there is no contact record
333                                 }
334                         }
335                 }
336         }
337
338         if((strlen($hub)) && ($notify_hub)) {
339                 $hubs = explode(',', $hub);
340                 if(count($hubs)) {
341                         foreach($hubs as $h) {
342                                 $h = trim($h);
343                                 if(! strlen($h))
344                                         continue;
345                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
346                                 post_url($h,$params);
347                                 logger('pubsub: publish: ' . $h . ' returned ' . $a->get_curl_code());
348                                 if(count($hubs) > 1)
349                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
350                         }
351                 }
352         }
353
354         killme();
355