]> git.mxchange.org Git - friendica.git/blob - include/items.php
pass vars via javascript, not html. Also fix broken pause img on IE
[friendica.git] / include / items.php
1 <?php
2
3 require_once('bbcode.php');
4
5 function get_feed_for(&$a, $dfrn_id, $owner_id, $last_update, $direction = 0) {
6
7
8         // default permissions - anonymous user
9
10         $sql_extra = " 
11                 AND `allow_cid` = '' 
12                 AND `allow_gid` = '' 
13                 AND `deny_cid`  = '' 
14                 AND `deny_gid`  = '' 
15         ";
16
17         if(strlen($owner_id) && ! intval($owner_id)) {
18                 $r = q("SELECT `uid`, `nickname` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
19                         dbesc($owner_id)
20                 );
21                 if(count($r)) {
22                         $owner_id = $r[0]['uid'];
23                         $owner_nick = $r[0]['nickname'];
24                 }
25         }
26
27         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
28                 intval($owner_id)
29         );
30         if(count($r))
31                 $owner = $r[0];
32         else
33                 killme();
34
35         if($dfrn_id && $dfrn_id != '*') {
36
37                 $sql_extra = '';
38                 switch($direction) {
39                         case (-1):
40                                 $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
41                                 $my_id = $dfrn_id;
42                                 break;
43                         case 0:
44                                 $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
45                                 $my_id = '1:' . $dfrn_id;
46                                 break;
47                         case 1:
48                                 $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
49                                 $my_id = '0:' . $dfrn_id;
50                                 break;
51                         default:
52                                 return false;
53                                 break; // NOTREACHED
54                 }
55
56                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `contact`.`uid` = %d $sql_extra LIMIT 1",
57                         intval($owner_id)
58                 );
59
60                 if(! count($r))
61                         return false;
62
63                 $contact = $r[0];
64                 $groups = init_groups_visitor($contact['id']);
65
66                 if(count($groups)) {
67                         for($x = 0; $x < count($groups); $x ++) 
68                                 $groups[$x] = '<' . intval($groups[$x]) . '>' ;
69                         $gs = implode('|', $groups);
70                 }
71                 else
72                         $gs = '<<>>' ; // Impossible to match 
73
74                 $sql_extra = sprintf(" 
75                         AND ( `allow_cid` = '' OR     `allow_cid` REGEXP '<%d>' ) 
76                         AND ( `deny_cid`  = '' OR NOT `deny_cid`  REGEXP '<%d>' ) 
77                         AND ( `allow_gid` = '' OR     `allow_gid` REGEXP '%s' )
78                         AND ( `deny_gid`  = '' OR NOT `deny_gid`  REGEXP '%s') 
79                 ",
80                         intval($contact['id']),
81                         intval($contact['id']),
82                         dbesc($gs),
83                         dbesc($gs)
84                 );
85         }
86
87         if($dfrn_id == '' || $dfrn_id == '*')
88                 $sort = 'DESC';
89         else
90                 $sort = 'ASC';
91
92         if(! strlen($last_update))
93                 $last_update = 'now - 30 days';
94
95         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
96
97         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
98                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
99                 `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`,
100                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
101                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
102                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
103                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
104                 AND `item`.`wall` = 1 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
105                 AND ( `item`.`edited` > '%s' OR `item`.`changed` > '%s' )
106                 $sql_extra
107                 ORDER BY `parent` %s, `created` ASC LIMIT 0, 300",
108                 intval($owner_id),
109                 dbesc($check_date),
110                 dbesc($check_date),
111                 dbesc($sort)
112         );
113
114         // Will check further below if this actually returned results.
115         // We will provide an empty feed in any case.
116
117         $items = $r;
118
119         $feed_template = file_get_contents('view/atom_feed.tpl');
120         $tomb_template = file_get_contents('view/atom_tomb.tpl');
121         $item_template = file_get_contents('view/atom_item.tpl');
122         $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
123
124         $atom = '';
125
126
127         $atom .= replace_macros($feed_template, array(
128                         '$feed_id'      => xmlify($a->get_baseurl() . '/profile/' . $owner_nick),
129                         '$feed_title'   => xmlify($owner['name']),
130                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , ATOM_TIME)) ,
131                         '$name'         => xmlify($owner['name']),
132                         '$profile_page' => xmlify($owner['url']),
133                         '$photo'        => xmlify($owner['photo']),
134                         '$thumb'        => xmlify($owner['thumb']),
135                         '$picdate'      => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , ATOM_TIME)) ,
136                         '$uridate'      => xmlify(datetime_convert('UTC','UTC',$owner['uri-date']    . '+00:00' , ATOM_TIME)) ,
137                         '$namdate'      => xmlify(datetime_convert('UTC','UTC',$owner['name-date']   . '+00:00' , ATOM_TIME)) 
138         ));
139
140
141         if(! count($items)) {
142                 $atom .= '</feed>' . "\r\n";
143                 return $atom;
144         }
145
146         foreach($items as $item) {
147
148                 // public feeds get html, our own nodes use bbcode
149
150                 if($dfrn_id == '*') {
151                         $item['body'] = bbcode($item['body']);
152                         $type = 'html';
153                 }
154                 else {
155                         $type = 'text';
156                 }
157
158                 if($item['deleted']) {
159                         $atom .= replace_macros($tomb_template, array(
160                                 '$id'      => xmlify($item['uri']),
161                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , ATOM_TIME))
162                         ));
163                 }
164                 else {
165                         $verb = construct_verb($item);
166                         $actobj = construct_activity($item);
167
168                         if($item['parent'] == $item['id']) {
169                                 $atom .= replace_macros($item_template, array(
170                                         '$name'               => xmlify($item['name']),
171                                         '$profile_page'       => xmlify($item['url']),
172                                         '$thumb'              => xmlify($item['thumb']),
173                                         '$owner_name'         => xmlify($item['owner-name']),
174                                         '$owner_profile_page' => xmlify($item['owner-link']),
175                                         '$owner_thumb'        => xmlify($item['owner-avatar']),
176                                         '$item_id'            => xmlify($item['uri']),
177                                         '$title'              => xmlify($item['title']),
178                                         '$published'          => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
179                                         '$updated'            => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
180                                         '$location'           => xmlify($item['location']),
181                                         '$type'               => $type,
182                                         '$content'            => xmlify($item['body']),
183                                         '$verb'               => xmlify($verb),
184                                         '$actobj'             => $actobj,  // do not xmlify
185                                         '$comment_allow'      => ((($item['last-child']) && ($contact['rel']) && ($contact['rel'] != REL_FAN)) ? 1 : 0)
186                                 ));
187                         }
188                         else {
189                                 $atom .= replace_macros($cmnt_template, array(
190                                         '$name'          => xmlify($item['name']),
191                                         '$profile_page'  => xmlify($item['url']),
192                                         '$thumb'         => xmlify($item['thumb']),
193                                         '$item_id'       => xmlify($item['uri']),
194                                         '$title'         => xmlify($item['title']),
195                                         '$published'     => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME)),
196                                         '$updated'       => xmlify(datetime_convert('UTC', 'UTC', $item['edited']  . '+00:00' , ATOM_TIME)),
197                                         '$type'          => $type,
198                                         '$content'       => xmlify($item['body']),
199                                         '$verb'          => xmlify($verb),
200                                         '$actobj'        => $actobj, // do not xmlify
201                                         '$parent_id'     => xmlify($item['parent-uri']),
202                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
203                                 ));
204                         }
205                 }
206         }
207
208         $atom .= '</feed>' . "\r\n";
209         return $atom;
210 }
211
212
213 function construct_verb($item) {
214         if($item['verb'])
215                 return $item['verb'];
216         return ACTIVITY_POST;
217 }
218
219 function construct_activity($item) {
220
221         if($item['object']) {
222                 $o = '<as:object>' . "\r\n";
223                 $r = @simplexml_load_string($item['object']);
224                 if($r->type)
225                         $o .= '<as:object-type>' . $r->type . '</as:object-type>' . "\r\n";
226                 if($r->id)
227                         $o .= '<id>' . $r->id . '</id>' . "\r\n";
228                 if($r->link)
229                         $o .= '<link rel="alternate" type="text/html" href="' . $r->link . '" />' . "\r\n";
230                 if($r->title)
231                         $o .= '<title>' . $r->title . '</title>' . "\r\n";
232                 if($r->content)
233                         $o .= '<content type="html" >' . bbcode($r->content) . '</content>' . "\r\n";
234                 $o .= '</as:object>' . "\r\n";
235                 return $o;
236         }
237
238         return '';
239
240
241
242
243
244 function get_atom_elements($item) {
245
246         require_once('library/HTMLPurifier.auto.php');
247         require_once('include/html2bbcode.php');
248
249         $res = array();
250
251         $raw_author = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'author');
252         if($raw_author) {
253                 if($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
254                 $res['author-avatar'] = unxmlify($raw_author[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
255         }
256
257         $author = $item->get_author();
258         $res['author-name'] = unxmlify($author->get_name());
259         $res['author-link'] = unxmlify($author->get_link());
260         if(! $res['author-avatar'])
261                 $res['author-avatar'] = unxmlify($author->get_avatar());
262         $res['uri'] = unxmlify($item->get_id());
263         $res['title'] = unxmlify($item->get_title());
264         $res['body'] = unxmlify($item->get_content());
265
266         $maxlen = get_max_import_size();
267         if($maxlen && (strlen($res['body']) > $maxlen))
268                 $res['body'] = substr($res['body'],0, $maxlen);
269
270         // It isn't certain at this point whether our content is plaintext or html and we'd be foolish to trust 
271         // the content type. Our own network only emits text normally, though it might have been converted to 
272         // html if we used a pubsubhubbub transport. But if we see even one html open tag in our text, we will
273         // have to assume it is all html and needs to be purified.
274
275         // It doesn't matter all that much security wise - because before this content is used anywhere, we are 
276         // going to escape any tags we find regardless, but this lets us import a limited subset of html from 
277         // the wild, by sanitising it and converting supported tags to bbcode before we rip out any remaining 
278         // html.
279
280
281         if(strpos($res['body'],'<')) {
282
283                 $res['body'] = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
284                         '[youtube]$1[/youtube]', $res['body']);
285
286                 $config = HTMLPurifier_Config::createDefault();
287                 $config->set('Core.DefinitionCache', null);
288
289                 // we shouldn't need a whitelist, because the bbcode converter
290                 // will strip out any unsupported tags.
291                 // $config->set('HTML.Allowed', 'p,b,a[href],i'); 
292
293                 $purifier = new HTMLPurifier($config);
294                 $res['body'] = $purifier->purify($res['body']);
295         }
296
297         
298         $res['body'] = html2bbcode($res['body']);
299
300
301         $allow = $item->get_item_tags(NAMESPACE_DFRN,'comment-allow');
302         if($allow && $allow[0]['data'] == 1)
303                 $res['last-child'] = 1;
304         else
305                 $res['last-child'] = 0;
306
307         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
308         if($rawcreated)
309                 $res['created'] = unxmlify($rawcreated[0]['data']);
310
311         $rawlocation = $item->get_item_tags(NAMESPACE_DFRN, 'location');
312         if($rawlocation)
313                 $res['location'] = unxmlify($rawlocation[0]['data']);
314
315
316         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
317         if($rawedited)
318                 $res['edited'] = unxmlify($rawcreated[0]['data']);
319
320         $rawowner = $item->get_item_tags(NAMESPACE_DFRN, 'owner');
321         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data'])
322                 $res['owner-name'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['name'][0]['data']);
323         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
324                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
325         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data'])
326                 $res['owner-link'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['uri'][0]['data']);
327         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
328                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
329
330         if($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'photo')
331                 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href']);
332         elseif($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
333                 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
334
335         $rawverb = $item->get_item_tags(NAMESPACE_ACTIVITY, 'verb');
336         // select between supported verbs
337         if($rawverb)
338                 $res['verb'] = unxmlify($rawverb[0]['data']);
339
340         $rawobj = $item->get_item_tags(NAMESPACE_ACTIVITY, 'object');
341
342
343         if($rawobj) {
344                 $res['object'] = '<object>' . "\n";
345                 if($rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data']) {
346                         $res['object-type'] = $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'];
347                         $res['object'] .= '<type>' . $rawobj[0]['child'][NAMESPACE_ACTIVITY]['object-type'][0]['data'] . '</type>' . "\n";
348                 }       
349                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'])
350                         $res['object'] .= '<id>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['id'][0]['data'] . '</id>' . "\n";
351                 
352                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['rel'] == 'alternate')
353                         $res['object'] .= '<link>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'][0]['attribs']['']['href'] . '</link>' . "\n";
354                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'])
355                         $res['object'] .= '<title>' . $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['title'][0]['data'] . '</title>' . "\n";
356                 if($rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data']) {
357                         $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['content'][0]['data'];
358                         if(! $body)
359                                 $body = $rawobj[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['summary'][0]['data'];
360                         if(strpos($body,'<')) {
361
362                                 $body = preg_replace('#<object[^>]+>.+?' . 'http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+).+?</object>#s',
363                                         '[youtube]$1[/youtube]', $body);
364
365                                 $config = HTMLPurifier_Config::createDefault();
366                                 $config->set('Core.DefinitionCache', null);
367
368                                 $purifier = new HTMLPurifier($config);
369                                 $body = $purifier->purify($body);
370                         }
371
372                         $body = html2bbcode($body);
373                         $res['object'] .= '<content>' . $body . '</content>' . "\n";
374                 }
375
376                 $res['object'] .= '</object>' . "\n";
377         }
378
379         return $res;
380 }
381
382 function item_store($arr) {
383
384 //print_r($arr);
385
386
387         if($arr['gravity'])
388                 $arr['gravity'] = intval($arr['gravity']);
389         elseif($arr['parent-uri'] == $arr['uri'])
390                 $arr['gravity'] = 0;
391         elseif($arr['verb'] == ACTIVITY_POST)
392                 $arr['gravity'] = 6;
393
394         if(! x($arr,'type'))
395                 $arr['type'] = 'remote';
396         $arr['wall'] = ((intval($arr['wall'])) ? 1 : 0);
397         $arr['uri'] = notags(trim($arr['uri']));
398         $arr['author-name'] = notags(trim($arr['author-name']));
399         $arr['author-link'] = notags(trim($arr['author-link']));
400         $arr['author-avatar'] = notags(trim($arr['author-avatar']));
401         $arr['owner-name'] = notags(trim($arr['owner-name']));
402         $arr['owner-link'] = notags(trim($arr['owner-link']));
403         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
404         $arr['created'] = ((x($arr,'created') !== false) ? datetime_convert('UTC','UTC',$arr['created']) : datetime_convert());
405         $arr['edited']  = ((x($arr,'edited')  !== false) ? datetime_convert('UTC','UTC',$arr['edited'])  : datetime_convert());
406         $arr['changed'] = datetime_convert();
407         $arr['title'] = notags(trim($arr['title']));
408         $arr['location'] = notags(trim($arr['location']));
409         $arr['body'] = escape_tags(trim($arr['body']));
410         $arr['last-child'] = intval($arr['last-child']);
411         $arr['visible'] = ((x($arr,'visible') !== false) ? intval($arr['visible']) : 1);
412         $arr['deleted'] = 0;
413         $arr['parent-uri'] = notags(trim($arr['parent-uri']));
414         $arr['verb'] = notags(trim($arr['verb']));
415         $arr['object-type'] = notags(trim($arr['object-type']));
416         $arr['object'] = trim($arr['object']);
417
418         $parent_id = 0;
419         $parent_missing = false;
420
421         dbesc_array($arr);
422
423         $r = q("INSERT INTO `item` (`" 
424                         . implode("`, `", array_keys($arr)) 
425                         . "`) VALUES ('" 
426                         . implode("', '", array_values($arr)) 
427                         . "')" );
428
429         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
430                 dbesc($arr['parent-uri']),
431                 intval($arr['uid'])
432         );
433
434         if(count($r))
435                 $parent_id = $r[0]['id'];
436         else {
437                 $parent_missing = true;
438         }
439
440         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
441                 $arr['uri'],           // already dbesc'd
442                 intval($arr['uid'])
443         );
444         if(count($r))
445                 $current_post = $r[0]['id'];
446         else
447                 return 0;
448
449         if($parent_missing) {
450
451                 // perhaps the parent was deleted, but in any case, this thread is dead
452                 // and unfortunately our brand new item now has to be destroyed
453
454                 q("DELETE FROM `item` WHERE `id` = %d LIMIT 1",
455                         intval($current_post)
456                 );
457                 return 0;
458         }
459
460         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
461                 intval($parent_id),
462                 intval($current_post)
463         );
464
465         return $current_post;
466 }