]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
9866c02325d15b01d2283e48e732bf5702902452
[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 = ((strlen($hub)) ? '<link rel="hub" href="' . xmlify($hub) . '" />' . "\n" : '');
153         
154
155         $atom .= replace_macros($feed_template, array(
156                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
157                         '$feed_title'   => xmlify($owner['name']),
158                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
159                         '$hub'          => $hubxml,
160                         '$salmon'       => '',   // private feed, we don't use salmon here
161                         '$name'         => xmlify($owner['name']),
162                         '$profile_page' => xmlify($owner['url']),
163                         '$photo'        => xmlify($owner['photo']),
164                         '$thumb'        => xmlify($owner['thumb']),
165                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
166                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
167                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME))
168         ));
169
170         if($cmd === 'mail') {
171                 $notify_hub = false;  // mail is  not public
172                 $atom .= replace_macros($mail_template, array(
173                         '$name'         => xmlify($owner['name']),
174                         '$profile_page' => xmlify($owner['url']),
175                         '$thumb'        => xmlify($owner['thumb']),
176                         '$item_id'      => xmlify($item['uri']),
177                         '$subject'      => xmlify($item['title']),
178                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
179                         '$content'      => xmlify($item['body']),
180                         '$parent_id'    => xmlify($item['parent-uri'])
181                 ));
182         }
183         else {
184
185                 if($followup) {
186                         foreach($items as $item) {  // there is only one item
187
188                                 $verb = construct_verb($item);
189                                 $actobj = construct_activity($item);
190
191                                 if($item['id'] == $item_id) {
192                                         $atom .= replace_macros($cmnt_template, array(
193                                                 '$name'               => xmlify($owner['name']),
194                                                 '$profile_page'       => xmlify($owner['url']),
195                                                 '$thumb'              => xmlify($owner['thumb']),
196                                                 '$owner_name'         => xmlify($item['owner-name']),
197                                                 '$owner_profile_page' => xmlify($item['owner-link']),
198                                                 '$owner_thumb'        => xmlify($item['owner-avatar']),
199                                                 '$item_id'            => xmlify($item['uri']),
200                                                 '$title'              => xmlify($item['title']),
201                                                 '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
202                                                 '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
203                                                 '$location'           => xmlify($item['location']),
204                                                 '$type'               => 'text',
205                                                 '$verb'               => xmlify($verb),
206                                                 '$actobj'             => $actobj,
207                                                 '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
208                                                 '$content'            => xmlify($item['body']),
209                                                 '$parent_id'          => xmlify($item['parent-uri']),
210                                                 '$comment_allow'      => 0
211                                         ));
212                                 }
213                         }
214                 }
215                 else {
216                         foreach($items as $item) {
217                                 if($item['deleted']) {
218                                         $atom .= replace_macros($tomb_template, array(
219                                                 '$id' => xmlify($item['uri']),
220                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
221                                         ));
222                                 }
223                                 else {
224                                         $contact = get_item_contact($item,$contacts);
225                                         if(! $contact)
226                                                 continue;
227
228                                         $verb = construct_verb($item);
229                                         $actobj = construct_activity($item);
230
231                                         if($item['parent'] == $item['id']) {
232                                                 $atom .= replace_macros($item_template, array(
233                                                         '$name'               => xmlify($contact['name']),
234                                                         '$profile_page'       => xmlify($contact['url']),
235                                                         '$thumb'              => xmlify($contact['thumb']),
236                                                         '$owner_name'         => xmlify($item['owner-name']),
237                                                         '$owner_profile_page' => xmlify($item['owner-link']),
238                                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
239                                                         '$item_id'            => xmlify($item['uri']),
240                                                         '$title'              => xmlify($item['title']),
241                                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
242                                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
243                                                         '$location'           => xmlify($item['location']),
244                                                         '$type'               => 'text',
245                                                         '$verb'               => xmlify($verb),
246                                                         '$actobj'             => $actobj,
247                                                         '$content'            => xmlify($item['body']),
248                                                         '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
249                                                         '$comment_allow'      => (($item['last-child']) ? 1 : 0)
250                                                 ));
251                                         }
252                                         else {
253                                                 $atom .= replace_macros($cmnt_template, array(
254                                                         '$name'          => xmlify($contact['name']),
255                                                         '$profile_page'  => xmlify($contact['url']),
256                                                         '$thumb'         => xmlify($contact['thumb']),
257                                                         '$item_id'       => xmlify($item['uri']),
258                                                         '$title'         => xmlify($item['title']),
259                                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
260                                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
261                                                         '$content'       => xmlify($item['body']),
262                                                         '$alt'           => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
263                                                         '$location'      => xmlify($item['location']),
264                                                         '$type'          => 'text',
265                                                         '$verb'          => xmlify($verb),
266                                                         '$actobj'        => $actobj,
267                                                         '$parent_id'     => xmlify($item['parent-uri']),
268                                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
269                                                 ));
270                                         }
271                                 }
272                         }
273                 }
274         }
275         $atom .= '</feed>' . "\r\n";
276
277         if($debugging)
278                 echo $atom;
279
280
281         if($followup)
282                 $recip_str = $parent['contact-id'];
283         else
284                 $recip_str = implode(', ', $recipients);
285
286
287         $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ",
288                 dbesc($recip_str)
289         );
290         if(! count($r))
291                 killme();
292
293         // delivery loop
294
295         foreach($r as $contact) {
296                 if($contact['self'])
297                         continue;
298
299                 $deliver_status = 0;
300
301                 switch($contact['network']) {
302                         case 'dfrn':
303                                 $deliver_status = dfrn_deliver($contact,$atom,$debugging);
304                                 break;
305                         default:
306                                 break;
307                 }
308
309                 if(($cmd === 'mail') && ($deliver_status == 0)) {
310                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
311                                 intval($item_id)
312                         );
313                 }
314         }
315
316         if((strlen($hub)) && ($notify_hub)) {
317                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
318                 post_url($hub,$params);
319                 if($debugging) {
320                         file_put_contents('pubsub.out', "\n\n" . "Pinged hub at " . datetime_convert() . "\n" . "Hub returned " . $a->get_curl_code() . "\n\n" , FILE_APPEND);
321                 }
322         }
323
324         killme();
325