]> git.mxchange.org Git - friendica.git/blob - include/notifier.php
trialing pubsubhubbub - operational 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 = $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)
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
88         if($cmd != 'mail') {
89
90                 require_once('include/group.php');
91
92                 $parent = $items[0];
93
94                 if($parent['type'] === 'remote') {
95                         // local followup to remote post
96                         $followup = true;
97                         $conversant_str = dbesc($parent['contact-id']);
98                 }
99                 else {
100                         $followup = false;
101
102                         $allow_people = expand_acl($parent['allow_cid']);
103                         $allow_groups = expand_groups(expand_acl($parent['allow_gid']));
104                         $deny_people = expand_acl($parent['deny_cid']);
105                         $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
106
107                         $conversants = array();
108
109                         foreach($items as $item) {
110                                 $recipients[] = $item['contact-id'];
111                                 $conversants[] = $item['contact-id'];
112                         }
113
114                         $conversants = array_unique($conversants,SORT_NUMERIC);
115
116
117                         $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC);
118                         $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC);
119                         $recipients = array_diff($recipients,$deny);
120
121                         $conversant_str = dbesc(implode(', ',$conversants));
122
123
124                 }
125
126                 $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0");
127
128                 if( ! count($r))
129                         killme();
130
131                 $contacts = $r;
132
133                 $tomb_template = load_view_file('view/atom_tomb.tpl');
134                 $item_template = load_view_file('view/atom_item.tpl');
135                 $cmnt_template = load_view_file('view/atom_cmnt.tpl');
136         }
137
138         $feed_template = load_view_file('view/atom_feed.tpl');
139         $mail_template = load_view_file('view/atom_mail.tpl');
140
141         $atom = '';
142
143         $hubxml = ((strlen($hub)) ? '<link rel="hub" href="' . xmlify($hub) . '" />' . "\n" : '');
144         
145
146         $atom .= replace_macros($feed_template, array(
147                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname'] ),
148                         '$feed_title'   => xmlify($owner['name']),
149                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
150                         '$hub'          => $hubxml,
151                         '$name'         => xmlify($owner['name']),
152                         '$profile_page' => xmlify($owner['url']),
153                         '$photo'        => xmlify($owner['photo']),
154                         '$thumb'        => xmlify($owner['thumb']),
155                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
156                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
157                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME))
158         ));
159
160         if($cmd === 'mail') {
161                 $atom .= replace_macros($mail_template, array(
162                         '$name'         => xmlify($owner['name']),
163                         '$profile_page' => xmlify($owner['url']),
164                         '$thumb'        => xmlify($owner['thumb']),
165                         '$item_id'      => xmlify($item['uri']),
166                         '$subject'      => xmlify($item['title']),
167                         '$created'      => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
168                         '$content'      => xmlify($item['body']),
169                         '$parent_id'    => xmlify($item['parent-uri'])
170                 ));
171         }
172         else {
173
174                 if($followup) {
175                         foreach($items as $item) {  // there is only one item
176
177                                 $verb = construct_verb($item);
178                                 $actobj = construct_activity($item);
179
180                                 if($item['id'] == $item_id) {
181                                         $atom .= replace_macros($cmnt_template, array(
182                                                 '$name'               => xmlify($owner['name']),
183                                                 '$profile_page'       => xmlify($owner['url']),
184                                                 '$thumb'              => xmlify($owner['thumb']),
185                                                 '$owner_name'         => xmlify($item['owner-name']),
186                                                 '$owner_profile_page' => xmlify($item['owner-link']),
187                                                 '$owner_thumb'        => xmlify($item['owner-avatar']),
188                                                 '$item_id'            => xmlify($item['uri']),
189                                                 '$title'              => xmlify($item['title']),
190                                                 '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
191                                                 '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
192                                                 '$location'           => xmlify($item['location']),
193                                                 '$type'               => 'text',
194                                                 '$verb'               => xmlify($verb),
195                                                 '$actobj'             => $actobj,
196                                                 '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
197                                                 '$content'            => xmlify($item['body']),
198                                                 '$parent_id'          => xmlify($item['parent-uri']),
199                                                 '$comment_allow'      => 0
200                                         ));
201                                 }
202                         }
203                 }
204                 else {
205                         foreach($items as $item) {
206                                 if($item['deleted']) {
207                                         $atom .= replace_macros($tomb_template, array(
208                                                 '$id' => xmlify($item['uri']),
209                                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
210                                         ));
211                                 }
212                                 else {
213                                         $contact = get_item_contact($item,$contacts);
214                                         if(! $contact)
215                                                 continue;
216
217                                         $verb = construct_verb($item);
218                                         $actobj = construct_activity($item);
219
220                                         if($item['parent'] == $item['id']) {
221                                                 $atom .= replace_macros($item_template, array(
222                                                         '$name'               => xmlify($contact['name']),
223                                                         '$profile_page'       => xmlify($contact['url']),
224                                                         '$thumb'              => xmlify($contact['thumb']),
225                                                         '$owner_name'         => xmlify($item['owner-name']),
226                                                         '$owner_profile_page' => xmlify($item['owner-link']),
227                                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
228                                                         '$item_id'            => xmlify($item['uri']),
229                                                         '$title'              => xmlify($item['title']),
230                                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
231                                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
232                                                         '$location'           => xmlify($item['location']),
233                                                         '$type'               => 'text',
234                                                         '$verb'               => xmlify($verb),
235                                                         '$actobj'             => $actobj,
236                                                         '$content'            => xmlify($item['body']),
237                                                         '$alt'                => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
238                                                         '$comment_allow'      => (($item['last-child']) ? 1 : 0)
239                                                 ));
240                                         }
241                                         else {
242                                                 $atom .= replace_macros($cmnt_template, array(
243                                                         '$name'          => xmlify($contact['name']),
244                                                         '$profile_page'  => xmlify($contact['url']),
245                                                         '$thumb'         => xmlify($contact['thumb']),
246                                                         '$item_id'       => xmlify($item['uri']),
247                                                         '$title'         => xmlify($item['title']),
248                                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
249                                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
250                                                         '$content'       => xmlify($item['body']),
251                                                         '$alt'           => xmlify($a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id']),
252                                                         '$location'      => xmlify($item['location']),
253                                                         '$type'          => 'text',
254                                                         '$verb'          => xmlify($verb),
255                                                         '$actobj'        => $actobj,
256                                                         '$parent_id'     => xmlify($item['parent-uri']),
257                                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
258                                                 ));
259                                         }
260                                 }
261                         }
262                 }
263         }
264         $atom .= '</feed>' . "\r\n";
265
266         if($debugging)
267                 echo $atom;
268
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 ) ",
277                 dbesc($recip_str)
278         );
279         if(! count($r))
280                 killme();
281
282         // delivery loop
283
284         foreach($r as $contact) {
285                 if($contact['self'])
286                         continue;
287
288                 $deliver_status = 0;
289
290                 switch($contact['network']) {
291                         case 'dfrn':
292                                 $deliver_status = dfrn_deliver($contact,$atom,$debugging);
293                                 break;
294                         default:
295                                 break;
296                 }
297
298                 if(($cmd === 'mail') && ($deliver_status == 0)) {
299                         $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1",
300                                 intval($item_id)
301                         );
302                 }
303         }
304
305         if((strlen($hub)) && ($cmd !== 'mail') && ($followup == false)) {
306                 $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
307                 post_url($hub,$params);
308         }
309
310         killme();
311