]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
99b3859960da5dbd4bc0797800550410e27cb9e7
[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         $debugging = get_config('system','debugging');
14
15         require_once("session.php");
16         require_once("datetime.php");
17         require_once('include/items.php');
18
19
20         if($argc < 3)
21                 exit;
22
23         $a->set_baseurl(get_config('system','url'));
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         if($debugging)
38                 dbg(3);
39
40         $recipients = array();
41
42         if($cmd === 'mail') {
43
44                 $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
45                                 intval($item_id)
46                 );
47                 if(! count($message))
48                         killme();
49                 $uid = $message[0]['uid'];
50                 $recipients[] = $message[0]['contact-id'];
51                 $item = $message[0];
52
53         }
54         else {
55                 // find ancestors
56
57                 $r = q("SELECT `parent`, `uid`, `edited` FROM `item` WHERE `id` = %d LIMIT 1",
58                         intval($item_id)
59                 );
60                 if(! count($r))
61                         killme();
62
63                 $parent_id = $r[0]['parent'];
64                 $uid = $r[0]['uid'];
65                 $updated = $r[0]['edited'];
66
67                 $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC",
68                         intval($parent_id)
69                 );
70
71                 if(! count($items))
72                         killme();
73         }
74
75         $r = q("SELECT `contact`.*, `user`.`nickname`, `user`.`page-flags` 
76                 FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
77                 WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
78                 intval($uid)
79         );
80
81         if(count($r))
82                 $owner = $r[0];
83         else
84                 killme();
85
86         $hub = get_config('system','huburl');
87         // If this is a public conversation, notify the feed hub
88         $notify_hub = true;
89
90         if($cmd != 'mail') {
91
92                 require_once('include/group.php');
93
94                 $parent = $items[0];
95
96                 if($parent['type'] === 'remote') {
97                         // local followup to remote post
98                         $followup = true;
99                         $notify_hub = false; // not public
100                         $conversant_str = dbesc($parent['contact-id']);
101                 }
102                 else {
103                         $followup = false;
104
105                         if((strlen($parent['allow_cid'])) 
106                                 || (strlen($parent['allow_gid'])) 
107                                 || (strlen($parent['deny_cid'])) 
108                                 || (strlen($parent['deny_gid'])))
109                                 $notify_hub = false; // private recipients, not public
110
111                         $allow_people = expand_acl($parent['allow_cid']);
112                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
113                         $deny_people = expand_acl($parent['deny_cid']);
114                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
115
116                         $conversants = array();
117
118                         foreach($items as $item) {
119                                 $recipients[] = $item['contact-id'];
120                                 $conversants[] = $item['contact-id'];
121                         }
122
123                         $conversants = array_unique($conversants,SORT_NUMERIC);
124
125
126                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
127                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
128                         $recipients = array_diff($recipients,$deny);
129
130                         $conversant_str = dbesc(implode(', ',$conversants));
131                 }
132
133                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
134
135                 if( ! count($r))
136                         killme();
137
138                 $contacts = $r;
139
140                 $tomb_template = load_view_file('view/atom_tomb.tpl');
141                 $item_template = load_view_file('view/atom_item.tpl');
142                 $cmnt_template = load_view_file('view/atom_cmnt.tpl');
143         }
144
145         $feed_template = load_view_file('view/atom_feed.tpl');
146         $mail_template = load_view_file('view/atom_mail.tpl');
147
148         $atom = '';
149
150         $hubxml = '';
151         if(strlen($hub)) {
152                 $hubs = explode(',', $hub);
153                 if(count($hubs)) {
154                         foreach($hubs as $h) {
155                                 $h = trim($h);
156                                 if(! strlen($h))
157                                         continue;
158                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
159                         }
160                 }
161         }
162
163         $atom .= replace_macros($feed_template, array(
164                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
165                         '$feed_title'   => xmlify($owner['name']),
166                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
167                         '$hub'          => $hubxml,
168                         '$salmon'       => '',   // private feed, we don't use salmon here
169                         '$name'         => xmlify($owner['name']),
170                         '$profile_page' => xmlify($owner['url']),
171                         '$photo'        => xmlify($owner['photo']),
172                         '$thumb'        => xmlify($owner['thumb']),
173                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
174                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
175                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME))
176         ));
177
178         if($cmd === 'mail') {
179                 $notify_hub = false;  // mail is  not public
180                 $atom .= replace_macros($mail_template, array(
181                         '$name'         => xmlify($owner['name']),
182                         '$profile_page' => xmlify($owner['url']),
183                         '$thumb'        => xmlify($owner['thumb']),
184                         '$item_id'      => xmlify($item['uri']),
185                         '$subject'      => xmlify($item['title']),
186                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
187                         '$content'      => xmlify($item['body']),
188                         '$parent_id'    => xmlify($item['parent-uri'])
189                 ));
190         }
191         else {
192
193                 if($followup) {
194                         foreach($items as $item) {  // there is only one item
195
196                                 $verb = construct_verb($item);
197                                 $actobj = construct_activity($item);
198
199                                 if($item['id'] == $item_id) {
200                                         $atom .= replace_macros($cmnt_template, array(
201                                                 '$name'               => xmlify($owner['name']),
202                                                 '$profile_page'       => xmlify($owner['url']),
203                                                 '$thumb'              => xmlify($owner['thumb']),
204                                                 '$owner_name'         => xmlify($item['owner-name']),
205                                                 '$owner_profile_page' => xmlify($item['owner-link']),
206                                                 '$owner_thumb'        => xmlify($item['owner-avatar']),
207                                                 '$item_id'            => xmlify($item['uri']),
208                                                 '$title'              => xmlify($item['title']),
209                                                 '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
210                                                 '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
211                                                 '$location'           => xmlify($item['location']),
212                                                 '$coord'              => xmlify($item['coord']),
213                                                 '$type'               => 'text',
214                                                 '$verb'               => xmlify($verb),
215                                                 '$actobj'             => $actobj,
216                                                 '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
217                                                 '$content'            => xmlify($item['body']),
218                                                 '$parent_id'          => xmlify($item['parent-uri']),
219                                                 '$comment_allow'      => 0
220                                         ));
221                                 }
222                         }
223                 }
224                 else {
225                         foreach($items as $item) {
226                                 if($item['deleted']) {
227                                         $atom .= replace_macros($tomb_template, array(
228                                                 '$id' => xmlify($item['uri']),
229                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
230                                         ));
231                                 }
232                                 else {
233                                         $contact = get_item_contact($item,$contacts);
234                                         if(! $contact)
235                                                 continue;
236
237                                         $verb = construct_verb($item);
238                                         $actobj = construct_activity($item);
239
240                                         if($item['parent'] == $item['id']) {
241                                                 $atom .= replace_macros($item_template, array(
242                                                         '$name'               => xmlify($contact['name']),
243                                                         '$profile_page'       => xmlify($contact['url']),
244                                                         '$thumb'              => xmlify($contact['thumb']),
245                                                         '$owner_name'         => xmlify($item['owner-name']),
246                                                         '$owner_profile_page' => xmlify($item['owner-link']),
247                                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
248                                                         '$item_id'            => xmlify($item['uri']),
249                                                         '$title'              => xmlify($item['title']),
250                                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
251                                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
252                                                         '$location'           => xmlify($item['location']),
253                                                         '$coord'              => xmlify($item['coord']),
254                                                         '$type'               => 'text',
255                                                         '$verb'               => xmlify($verb),
256                                                         '$actobj'             => $actobj,
257                                                         '$content'            => xmlify($item['body']),
258                                                         '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
259                                                         '$comment_allow'      => (($item['last-child']) ? 1 : 0)
260                                                 ));
261                                         }
262                                         else {
263                                                 $atom .= replace_macros($cmnt_template, array(
264                                                         '$name'          => xmlify($contact['name']),
265                                                         '$profile_page'  => xmlify($contact['url']),
266                                                         '$thumb'         => xmlify($contact['thumb']),
267                                                         '$item_id'       => xmlify($item['uri']),
268                                                         '$title'         => xmlify($item['title']),
269                                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
270                                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
271                                                         '$content'       => xmlify($item['body']),
272                                                         '$alt'           => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
273                                                         '$location'      => xmlify($item['location']),
274                                                         '$coord'         => xmlify($item['coord']),
275                                                         '$type'          => 'text',
276                                                         '$verb'          => xmlify($verb),
277                                                         '$actobj'        => $actobj,
278                                                         '$parent_id'     => xmlify($item['parent-uri']),
279                                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
280                                                 ));
281                                         }
282                                 }
283                         }
284                 }
285         }
286         $atom .= '</feed>' . "\r\n";
287
288         if($debugging)
289                 echo $atom;
290
291
292         if($followup)
293                 $recip_str = $parent['contact-id'];
294         else
295                 $recip_str = implode(', ', $recipients);
296
297
298         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
299                 dbesc($recip_str)
300         );
301         if(! count($r))
302                 killme();
303
304         // delivery loop
305
306         foreach($r as $contact) {
307                 if($contact['self'])
308                         continue;
309
310                 $deliver_status = 0;
311
312                 switch($contact['network']) {
313                         case 'dfrn':
314                                 $deliver_status = dfrn_deliver($owner,$contact,$atom,$debugging);
315                                 break;
316                         default:
317                                 break;
318                 }
319
320                 if(($cmd === 'mail') && ($deliver_status == 0)) {
321                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
322                                 intval($item_id)
323                         );
324                 }
325         }
326
327         if((strlen($hub)) && ($notify_hub)) {
328                 $hubs = explode(',', $hub);
329                 if(count($hubs)) {
330                         foreach($hubs as $h) {
331                                 $h = trim($h);
332                                 if(! strlen($h))
333                                         continue;
334                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
335                                 post_url($h,$params);
336                                 if($debugging) {
337                                         file_put_contents('pubsub.out', "\n\n" . "Pinged hub " . $h . ' at ' 
338                                                 . datetime_convert() . "\n" . "Hub returned " . $a->get_curl_code() . "\n\n" , FILE_APPEND);
339                                 }
340                                 if(count($hubs) > 1)
341                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
342                         }
343                 }
344         }
345
346         killme();
347