]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
a few more fsw tweaks
[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`.`sprvkey`, `user`.`spubkey`, `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         // fill this in with a salmon slap if applicable
91         $slap = '';
92
93         if($cmd != 'mail') {
94
95                 require_once('include/group.php');
96
97                 $parent = $items[0];
98
99                 if($parent['type'] === 'remote') {
100                         // local followup to remote post
101                         $followup = true;
102                         $notify_hub = false; // not public
103                         $conversant_str = dbesc($parent['contact-id']);
104                 }
105                 else {
106                         $followup = false;
107
108                         if((strlen($parent['allow_cid'])) 
109                                 || (strlen($parent['allow_gid'])) 
110                                 || (strlen($parent['deny_cid'])) 
111                                 || (strlen($parent['deny_gid'])))
112                                 $notify_hub = false; // private recipients, not public
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                         }
125
126                         $conversants = array_unique($conversants,SORT_NUMERIC);
127
128
129                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
130                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
131                         $recipients = array_diff($recipients,$deny);
132
133                         $conversant_str = dbesc(implode(', ',$conversants));
134                 }
135
136                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
137
138                 if( ! count($r))
139                         killme();
140
141                 $contacts = $r;
142
143                 $tomb_template = load_view_file('view/atom_tomb.tpl');
144                 $item_template = load_view_file('view/atom_item.tpl');
145                 $cmnt_template = load_view_file('view/atom_cmnt.tpl');
146         }
147
148         $feed_template = load_view_file('view/atom_feed.tpl');
149         $mail_template = load_view_file('view/atom_mail.tpl');
150
151         $atom = '';
152
153         $hubxml = '';
154         if(strlen($hub)) {
155                 $hubs = explode(',', $hub);
156                 if(count($hubs)) {
157                         foreach($hubs as $h) {
158                                 $h = trim($h);
159                                 if(! strlen($h))
160                                         continue;
161                                 $hubxml .= '<link rel="hub" href="' . xmlify($h) . '" />' . "\n" ;
162                         }
163                 }
164         }
165
166         $atom .= replace_macros($feed_template, array(
167                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
168                         '$feed_title'   => xmlify($owner['name']),
169                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
170                         '$hub'          => $hubxml,
171                         '$salmon'       => '',   // private feed, we don't use salmon here
172                         '$name'         => xmlify($owner['name']),
173                         '$profile_page' => xmlify($owner['url']),
174                         '$photo'        => xmlify($owner['photo']),
175                         '$thumb'        => xmlify($owner['thumb']),
176                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
177                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
178                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME))
179         ));
180
181         if($cmd === 'mail') {
182                 $notify_hub = false;  // mail is  not public
183                 $atom .= replace_macros($mail_template, array(
184                         '$name'         => xmlify($owner['name']),
185                         '$profile_page' => xmlify($owner['url']),
186                         '$thumb'        => xmlify($owner['thumb']),
187                         '$item_id'      => xmlify($item['uri']),
188                         '$subject'      => xmlify($item['title']),
189                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
190                         '$content'      => xmlify($item['body']),
191                         '$parent_id'    => xmlify($item['parent-uri'])
192                 ));
193         }
194         else {
195
196                 if($followup) {
197                         foreach($items as $item) {  // there is only one item
198
199                                 $verb = construct_verb($item);
200                                 $actobj = construct_activity($item);
201
202                                 if($item['id'] == $item_id) {
203                                         $slap = replace_macros($cmnt_template, array(
204                                                 '$name'               => xmlify($owner['name']),
205                                                 '$profile_page'       => xmlify($owner['url']),
206                                                 '$thumb'              => xmlify($owner['thumb']),
207                                                 '$owner_name'         => xmlify($item['owner-name']),
208                                                 '$owner_profile_page' => xmlify($item['owner-link']),
209                                                 '$owner_thumb'        => xmlify($item['owner-avatar']),
210                                                 '$item_id'            => xmlify($item['uri']),
211                                                 '$title'              => xmlify($item['title']),
212                                                 '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
213                                                 '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
214                                                 '$location'           => xmlify($item['location']),
215                                                 '$coord'              => xmlify($item['coord']),
216                                                 '$type'               => 'text',
217                                                 '$verb'               => xmlify($verb),
218                                                 '$actobj'             => $actobj,
219                                                 '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
220                                                 '$content'            => xmlify($item['body']),
221                                                 '$parent_id'          => xmlify($item['parent-uri']),
222                                                 '$comment_allow'      => 0
223                                         ));
224                                 }
225                         }
226                         $atom .= $slap;
227                 }
228                 else {
229                         foreach($items as $item) {
230                                 if($item['deleted']) {
231                                         $atom .= replace_macros($tomb_template, array(
232                                                 '$id' => xmlify($item['uri']),
233                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
234                                         ));
235                                 }
236                                 else {
237                                         $contact = get_item_contact($item,$contacts);
238                                         if(! $contact)
239                                                 continue;
240
241                                         $verb = construct_verb($item);
242                                         $actobj = construct_activity($item);
243
244                                         if($item['parent'] == $item['id']) {
245                                                 $atom .= replace_macros($item_template, array(
246                                                         '$name'               => xmlify($contact['name']),
247                                                         '$profile_page'       => xmlify($contact['url']),
248                                                         '$thumb'              => xmlify($contact['thumb']),
249                                                         '$owner_name'         => xmlify($item['owner-name']),
250                                                         '$owner_profile_page' => xmlify($item['owner-link']),
251                                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
252                                                         '$item_id'            => xmlify($item['uri']),
253                                                         '$title'              => xmlify($item['title']),
254                                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
255                                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
256                                                         '$location'           => xmlify($item['location']),
257                                                         '$coord'              => xmlify($item['coord']),
258                                                         '$type'               => 'text',
259                                                         '$verb'               => xmlify($verb),
260                                                         '$actobj'             => $actobj,
261                                                         '$content'            => xmlify($item['body']),
262                                                         '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
263                                                         '$comment_allow'      => (($item['last-child']) ? 1 : 0)
264                                                 ));
265                                         }
266                                         else {
267                                                 $atom .= replace_macros($cmnt_template, array(
268                                                         '$name'          => xmlify($contact['name']),
269                                                         '$profile_page'  => xmlify($contact['url']),
270                                                         '$thumb'         => xmlify($contact['thumb']),
271                                                         '$item_id'       => xmlify($item['uri']),
272                                                         '$title'         => xmlify($item['title']),
273                                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
274                                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
275                                                         '$content'       => xmlify($item['body']),
276                                                         '$alt'           => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
277                                                         '$location'      => xmlify($item['location']),
278                                                         '$coord'         => xmlify($item['coord']),
279                                                         '$type'          => 'text',
280                                                         '$verb'          => xmlify($verb),
281                                                         '$actobj'        => $actobj,
282                                                         '$parent_id'     => xmlify($item['parent-uri']),
283                                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
284                                                 ));
285                                         }
286                                 }
287                         }
288                 }
289         }
290         $atom .= '</feed>' . "\r\n";
291
292         if($debugging)
293                 echo $atom;
294
295
296         if($followup)
297                 $recip_str = $parent['contact-id'];
298         else
299                 $recip_str = implode(', ', $recipients);
300
301
302         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
303                 dbesc($recip_str)
304         );
305         if(! count($r))
306                 killme();
307
308         // delivery loop
309
310
311
312         foreach($r as $contact) {
313                 if($contact['self'])
314                         continue;
315
316                 $deliver_status = 0;
317
318                 switch($contact['network']) {
319                         case 'dfrn':
320                                 $deliver_status = dfrn_deliver($owner,$contact,$atom,$debugging);
321                                 break;
322                         default:
323                                 if($followup) {
324                                         require_once('include/salmon.php');
325                                         slapper($owner,$contact,$slap);
326                                 }
327                                 break;
328                 }
329
330                 if(($cmd === 'mail') && ($deliver_status == 0)) {
331                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
332                                 intval($item_id)
333                         );
334                 }
335         }
336
337         if((strlen($hub)) && ($notify_hub)) {
338                 $hubs = explode(',', $hub);
339                 if(count($hubs)) {
340                         foreach($hubs as $h) {
341                                 $h = trim($h);
342                                 if(! strlen($h))
343                                         continue;
344                                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
345                                 post_url($h,$params);
346                                 if($debugging) {
347                                         file_put_contents('pubsub.out', "\n\n" . "Pinged hub " . $h . ' at ' 
348                                                 . datetime_convert() . "\n" . "Hub returned " . $a->get_curl_code() . "\n\n" , FILE_APPEND);
349                                 }
350                                 if(count($hubs) > 1)
351                                         sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
352                         }
353                 }
354         }
355
356         killme();
357