]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
set utf-8 on all emails
[friendica.git] / include / notifier.php
1 <?php
2 require_once("boot.php");
3
4 function notifier_run($argv, $argc){
5         global $a, $db;
6
7         if(is_null($a)){
8                 $a = new App;
9         }
10   
11         if(is_null($db)) {
12                 @include(".htconfig.php");
13                 require_once("dba.php");
14                 $db = new dba($db_host, $db_user, $db_pass, $db_data);
15                         unset($db_host, $db_user, $db_pass, $db_data);
16         }
17
18         require_once("session.php");
19         require_once("datetime.php");
20         require_once('include/items.php');
21         require_once('include/bbcode.php');
22
23         if($argc < 3)
24                 return;
25
26         $a->set_baseurl(get_config('system','url'));
27
28         logger('notifier: invoked: ' . print_r($argv,true));
29
30         $cmd = $argv[1];
31
32         switch($cmd) {
33
34                 case 'mail':
35                 default:
36                         $item_id = intval($argv[2]);
37                         if(! $item_id){
38                                 return;
39                         }
40                         break;
41         }
42
43         $expire = false;
44         $top_level = false;
45         $recipients = array();
46         $url_recipients = array();
47
48         if($cmd === 'mail') {
49
50                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
51                                 intval($item_id)
52                 );
53                 if(! count($message)){
54                         return;
55                 }
56                 $uid = $message[0]['uid'];
57                 $recipients[] = $message[0]['contact-id'];
58                 $item = $message[0];
59
60         }
61         elseif($cmd === 'expire') {
62                 $expire = true;
63                 $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1 
64                         AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP - INTERVAL 10 MINUTE",
65                         intval($item_id)
66                 );
67                 $uid = $item_id;
68                 $item_id = 0;
69                 if(! count($items))
70                         return;
71         }
72         else {
73
74                 // find ancestors
75                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
76                         intval($item_id)
77                 );
78
79                 if((! count($r)) || (! intval($r[0]['parent']))) {
80                         return;
81                 }
82
83                 $parent_id = intval($r[0]['parent']);
84                 $uid = $r[0]['uid'];
85                 $updated = $r[0]['edited'];
86
87                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
88                         intval($parent_id)
89                 );
90
91                 if(! count($items)) {
92                         return;
93                 }
94
95                 // avoid race condition with deleting entries
96
97                 if($items[0]['deleted']) {
98                         foreach($items as $item)
99                                 $item['deleted'] = 1;
100                 }
101
102                 if(count($items) == 1 && $items[0]['uri'] === $items[0]['parent-uri'])
103                         $top_level = true;
104         }
105
106         $r = q("SELECT `contact`.*, `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`, `user`.`page-flags` 
107                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
108                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
109                 intval($uid)
110         );
111
112         if(! count($r))
113                 return;
114
115         $owner = $r[0];
116
117         $hub = get_config('system','huburl');
118
119         // If this is a public conversation, notify the feed hub
120         $notify_hub = true;
121
122         // fill this in with a single salmon slap if applicable
123         $slap = '';
124
125         if($cmd != 'mail') {
126
127                 require_once('include/group.php');
128
129                 $parent = $items[0];
130
131                 if($parent['type'] === 'remote' && (! $expire)) {
132                         // local followup to remote post
133                         $followup = true;
134                         $notify_hub = false; // not public
135                         $conversant_str = dbesc($parent['contact-id']);
136                 }
137                 else {
138                         $followup = false;
139
140                         if((strlen($parent['allow_cid'])) 
141                                 || (strlen($parent['allow_gid'])) 
142                                 || (strlen($parent['deny_cid'])) 
143                                 || (strlen($parent['deny_gid']))) {
144                                 $notify_hub = false; // private recipients, not public
145                         }
146
147                         $allow_people = expand_acl($parent['allow_cid']);
148                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
149                         $deny_people  = expand_acl($parent['deny_cid']);
150                         $deny_groups  = expand_groups(expand_acl($parent['deny_gid']));
151
152                         $conversants = array();
153
154                         foreach($items as $item) {
155                                 $recipients[] = $item['contact-id'];
156                                 $conversants[] = $item['contact-id'];
157                                 // pull out additional tagged people to notify (if public message)
158                                 if($notify_hub && strlen($item['inform'])) {
159                                         $people = explode(',',$item['inform']);
160                                         foreach($people as $person) {
161                                                 if(substr($person,0,4) === 'cid:') {
162                                                         $recipients[] = intval(substr($person,4));
163                                                         $conversants[] = intval(substr($person,4));
164                                                 }
165                                                 else {
166                                                         $url_recipients[] = substr($person,4);
167                                                 }
168                                         }
169                                 }
170                         }
171
172                         logger('notifier: url_recipients' . print_r($url_recipients,true));
173
174                         $conversants = array_unique($conversants);
175
176
177                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
178                         $deny = array_unique(array_merge($deny_people,$deny_groups));
179                         $recipients = array_diff($recipients,$deny);
180
181                         $conversant_str = dbesc(implode(', ',$conversants));
182                 }
183
184                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
185
186
187                 if(count($r))
188                         $contacts = $r;
189         }
190
191         $feed_template = load_view_file('view/atom_feed.tpl');
192         $mail_template = load_view_file('view/atom_mail.tpl');
193
194         $atom = '';
195         $slaps = array();
196
197         $hubxml = feed_hublinks();
198
199         $birthday = feed_birthday($owner['uid'],$owner['timezone']);
200
201         if(strlen($birthday))
202                 $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
203
204         $atom .= replace_macros($feed_template, array(
205                         '$version'      => xmlify(FRIENDIKA_VERSION),
206                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
207                         '$feed_title'   => xmlify($owner['name']),
208                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
209                         '$hub'          => $hubxml,
210                         '$salmon'       => '',  // private feed, we don't use salmon here
211                         '$name'         => xmlify($owner['name']),
212                         '$profile_page' => xmlify($owner['url']),
213                         '$photo'        => xmlify($owner['photo']),
214                         '$thumb'        => xmlify($owner['thumb']),
215                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
216                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
217                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) ,
218                         '$birthday'     => $birthday
219         ));
220
221         if($cmd === 'mail') {
222                 $notify_hub = false;  // mail is  not public
223
224                 $atom .= replace_macros($mail_template, array(
225                         '$name'         => xmlify($owner['name']),
226                         '$profile_page' => xmlify($owner['url']),
227                         '$thumb'        => xmlify($owner['thumb']),
228                         '$item_id'      => xmlify($item['uri']),
229                         '$subject'      => xmlify($item['title']),
230                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
231                         '$content'      => xmlify($item['body']),
232                         '$parent_id'    => xmlify($item['parent-uri'])
233                 ));
234         }
235         else {
236                 if($followup) {
237                         foreach($items as $item) {  // there is only one item
238                                 if(! $item['parent'])
239                                         continue;
240                                 if($item['id'] == $item_id) {
241                                         logger('notifier: followup: item: ' . print_r($item,true), LOGGER_DATA);
242                                         $slap  = atom_entry($item,'html',$owner,$owner,false);
243                                         $atom .= atom_entry($item,'text',$owner,$owner,false);
244                                 }
245                         }
246                 }
247                 else {
248                         foreach($items as $item) {
249
250                                 if(! $item['parent'])
251                                         continue;
252
253                                 $contact = get_item_contact($item,$contacts);
254                                 if(! $contact)
255                                         continue;
256
257                                 $atom .= atom_entry($item,'text',$contact,$owner,true);
258
259                                 if(($top_level) && ($notify_hub) && ($item['author-link'] === $item['owner-link']) && (! $expire)) 
260                                         $slaps[] = atom_entry($item,'html',$contact,$owner,true);
261                         }
262                 }
263         }
264         $atom .= '</feed>' . "\r\n";
265
266         logger('notifier: ' . $atom, LOGGER_DATA);
267
268         logger('notifier: slaps: ' . print_r($slaps,true), LOGGER_DATA);
269
270         if($followup)
271                 $recip_str = $parent['contact-id'];
272         else
273                 $recip_str = implode(', ', $recipients);
274
275
276         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ",
277                 dbesc($recip_str)
278         );
279
280         // delivery loop
281
282         require_once('include/salmon.php');
283
284         if(count($r)) {
285                 foreach($r as $contact) {
286                         if($contact['self'])
287                                 continue;
288
289                         $deliver_status = 0;
290
291                         switch($contact['network']) {
292                                 case 'dfrn':
293                                         logger('notifier: dfrndelivery: ' . $contact['name']);
294                                         $deliver_status = dfrn_deliver($owner,$contact,$atom);
295
296                                         logger('notifier: dfrn_delivery returns ' . $deliver_status);
297         
298                                         if($deliver_status == (-1)) {
299                                                 logger('notifier: delivery failed: queuing message');
300                                                 // queue message for redelivery
301                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
302                                                         VALUES ( %d, '%s', '%s', '%s') ",
303                                                         intval($contact['id']),
304                                                         dbesc(datetime_convert()),
305                                                         dbesc(datetime_convert()),
306                                                         dbesc($atom)
307                                                 );
308                                         }
309                                         break;
310                                 case 'stat':
311                                         if($followup && $contact['notify']) {
312                                                 logger('notifier: slapdelivery: ' . $contact['name']);
313                                                 $deliver_status = slapper($owner,$contact['notify'],$slap);
314
315                                                 if($deliver_status == (-1)) {
316                                                         // queue message for redelivery
317                                                         q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
318                                                                 VALUES ( %d, '%s', '%s', '%s') ",
319                                                                 intval($contact['id']),
320                                                                 dbesc(datetime_convert()),
321                                                                 dbesc(datetime_convert()),
322                                                                 dbesc($slap)
323                                                         );
324
325                                                 }
326         
327
328                                         }
329                                         else {
330
331                                                 // only send salmon if public - e.g. if it's ok to notify
332                                                 // a public hub, it's ok to send a salmon
333
334                                                 if((count($slaps)) && ($notify_hub) && (! $expire)) {
335                                                         logger('notifier: slapdelivery: ' . $contact['name']);
336                                                         foreach($slaps as $slappy) {
337                                                                 if($contact['notify']) {
338                                                                         $deliver_status = slapper($owner,$contact['notify'],$slappy);
339                                                                         if($deliver_status == (-1)) {
340                                                                                 // queue message for redelivery
341                                                                                 q("INSERT INTO `queue` ( `cid`, `created`, `last`, `content`)
342                                                                                         VALUES ( %d, '%s', '%s', '%s') ",
343                                                                                         intval($contact['id']),
344                                                                                         dbesc(datetime_convert()),
345                                                                                         dbesc(datetime_convert()),
346                                                                                         dbesc($slappy)
347                                                                                 );                                                              
348                                                                         }
349                                                                 }
350                                                         }
351                                                 }
352                                         }
353                                         break;
354                                 case 'mail':
355                                 case 'dspr':
356                                 case 'feed':
357                                 case 'face':
358                                 default:
359                                         break;
360                         }
361                 }
362         }
363                 
364         // send additional slaps to mentioned remote tags (@foo@example.com)
365
366         if($slap && count($url_recipients) && $followup && $notify_hub && (! $expire)) {
367                 foreach($url_recipients as $url) {
368                         if($url) {
369                                 logger('notifier: urldelivery: ' . $url);
370                                 $deliver_status = slapper($owner,$url,$slap);
371                                 // TODO: redeliver/queue these items on failure, though there is no contact record
372                         }
373                 }
374         }
375
376         if((strlen($hub)) && ($notify_hub)) {
377                 $hubs = explode(',', $hub);
378                 if(count($hubs)) {
379                         foreach($hubs as $h) {
380                                 $h = trim($h);
381                                 if(! strlen($h))
382                                         continue;
383                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
384                                 post_url($h,$params);
385                                 logger('pubsub: publish: ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
386                                 if(count($hubs) > 1)
387                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
388                         }
389                 }
390         }
391
392         if($notify_hub) {
393
394                 /**
395                  *
396                  * If you have less than 150 dfrn friends and it's a public message,
397                  * we'll just go ahead and push them out securely with dfrn/rino.
398                  * If you've got more than that, you'll have to rely on PuSH delivery.
399                  *
400                  */
401
402                 $max_allowed = ((get_config('system','maxpubdeliver') === false) ? 150 : intval(get_config('system','maxpubdeliver')));
403                                 
404                 /**
405                  *
406                  * Only get the bare essentials and go back for the full record. 
407                  * If you've got a lot of friends and we grab all the details at once it could exhaust memory. 
408                  *
409                  */
410
411                 $r = q("SELECT `id`, `name` FROM `contact` 
412                         WHERE `network` = 'dfrn' AND `uid` = %d AND `blocked` = 0 AND `pending` = 0
413                         AND `rel` != %d ",
414                         intval($owner['uid']),
415                         intval(REL_FAN)
416                 );
417
418                 if((count($r)) && (($max_allowed == 0) || (count($r) < $max_allowed))) {
419
420                         foreach($r as $rr) {
421
422                                 /* Don't deliver to folks who have already been delivered to */
423
424                                 if(! in_array($rr['id'], $conversants)) {
425                                         $n = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
426                                                         intval($rr['id'])
427                                         );
428
429                                         if(count($n)) {
430                                         
431                                                 logger('notifier: dfrnpubdelivery: ' . $n[0]['name']);
432                                                 $deliver_status = dfrn_deliver($owner,$n[0],$atom);
433                                         }
434                                 }
435                                 else
436                                         logger('notifier: dfrnpubdelivery: ignoring ' . $rr['name']);
437                         }
438                 }
439         }
440
441         return;
442 }
443
444 if (array_search(__file__,get_included_files())===0){
445   echo "run!";
446   notifier_run($argv,$argc);
447   killme();
448 }