]> git.mxchange.org Git - friendica.git/blob - include/items.php
feed generator
[friendica.git] / include / items.php
1 <?php
2
3
4 function get_feed_for(&$a,$dfrn_id,$owner_id,$last_update) {
5
6         // default permissions - anonymous user
7
8         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
9
10         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
11                 intval($owner_id)
12         );
13         if(count($r))
14                 $owner = $r[0];
15         else
16                 killme();
17
18         if($dfrn_id != '*') {
19
20                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
21                         dbesc($dfrn_id)
22                 );
23                 if(! count($r))
24                         return false;
25
26                 $contact = $r[0];
27                 $groups = init_groups_visitor($contact['id']);
28
29
30                 $gs = '<<>>'; // should be impossible to match
31                 if(count($groups)) {
32                         foreach($groups as $g)
33                                 $gs .= '|<' . intval($g) . '>';
34                 } 
35                 $sql_extra = sprintf(
36                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
37                         AND ( `deny_cid` = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
38                         AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
39                         AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
40
41                         intval($_SESSION['visitor_id']),
42                         intval($_SESSION['visitor_id']),
43                         dbesc($gs),
44                         dbesc($gs)
45                 );
46         }
47
48         if(! strlen($last_update))
49                 $last_update = 'now - 30 days';
50         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
51
52         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
53                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
54                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
55                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
56                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
57                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
58                 AND `item`.`type` != 'remote' AND `contact`.`blocked` = 0 
59                 AND `item`.`edited` > '%s'
60                 $sql_extra
61                 ORDER BY `parent` DESC, `id` ASC LIMIT 0, 300",
62                 intval($owner_id),
63                 dbesc($check_date)
64         );
65         if(! count($r))
66                 killme();
67
68         $items = $r;
69
70         $feed_template = file_get_contents('view/atom_feed.tpl');
71         $tomb_template = file_get_contents('view/atom_tomb.tpl');
72         $item_template = file_get_contents('view/atom_item.tpl');
73         $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
74
75         $atom = '';
76
77
78         $atom .= replace_macros($feed_template, array(
79                         '$feed_id' => xmlify($a->get_baseurl()),
80                         '$feed_title' => xmlify($owner['name']),
81                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
82                         '$name' => xmlify($owner['name']),
83                         '$profile_page' => xmlify($owner['url']),
84                         '$photo' => xmlify($owner['photo'])
85         ));
86
87         foreach($items as $item) {
88                 if($item['deleted']) {
89                         $atom .= replace_macros($tomb_template, array(
90                                 '$id' => xmlify(((strlen($item['remote-id'])) ? $item['remote-id'] : "urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}")),
91                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
92                         ));
93                 }
94                 else {
95
96                         if($item['parent'] == $item['id']) {
97                                 $atom .= replace_macros($item_template, array(
98                                         '$name' => xmlify($item['name']),
99                                         '$profile_page' => xmlify($item['url']),
100                                         '$thumb' => xmlify($item['thumb']),
101                                         '$owner_name' => xmlify($item['owner-name']),
102                                         '$owner_profile_page' => xmlify($item['owner-link']),
103                                         '$owner_thumb' => xmlify($item['owner-avatar']),
104                                         '$item_id' => xmlify(((strlen($item['remote-id'])) ? $item['remote-id'] : "urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}")),
105                                         '$title' => xmlify($item['name']),
106                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
107                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
108                                         '$content' =>xmlify($item['body']),
109                                         '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'] && (! $contact['blocked']))) ? 1 : 0)
110                                 ));
111                         }
112                         else {
113                                 $atom .= replace_macros($cmnt_template, array(
114                                         '$name' => xmlify($item['name']),
115                                         '$profile_page' => xmlify($item['url']),
116                                         '$thumb' => xmlify($item['thumb']),
117                                         '$item_id' => xmlify(((strlen($item['remote-id'])) ? $item['remote-id'] : "urn:X-dfrn:$baseurl:{$owner['uid']}:{$item['hash']}")),
118                                         '$title' => xmlify($item['title']),
119                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
120                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
121                                         '$content' =>xmlify($item['body']),
122                                         '$parent_id' => xmlify("urn:X-dfrn:$baseurl:{$owner['uid']}:{$items[0]['hash']}"),
123                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
124                                 ));
125                         }
126                 }
127         }
128
129
130         $atom .= "</feed>\r\n";
131
132         return $atom;
133