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