]> git.mxchange.org Git - friendica.git/blob - include/items.php
lotsoflittlestuff
[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         if(strlen($owner_id) && ! intval($owner_id)) {
11                 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
12                         dbesc($owner_id)
13                 );
14                 if(count($r))
15                         $owner_id = $r[0]['uid'];
16         }
17
18         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
19                 intval($owner_id)
20         );
21         if(count($r))
22                 $owner = $r[0];
23         else
24                 killme();
25
26         if($dfrn_id != '*') {
27
28                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
29                         dbesc($dfrn_id)
30                 );
31                 if(! count($r))
32                         return false;
33
34                 $contact = $r[0];
35                 $groups = init_groups_visitor($contact['id']);
36
37
38                 $gs = '<<>>'; // should be impossible to match
39                 if(count($groups)) {
40                         foreach($groups as $g)
41                                 $gs .= '|<' . intval($g) . '>';
42                 } 
43                 $sql_extra = sprintf(
44                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
45                         AND ( `deny_cid` = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
46                         AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
47                         AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
48
49                         intval($contact['id']),
50                         intval($contact['id']),
51                         dbesc($gs),
52                         dbesc($gs)
53                 );
54         }
55
56         if(! strlen($last_update))
57                 $last_update = 'now - 30 days';
58         $check_date = datetime_convert('UTC','UTC',$last_update,'Y-m-d H:i:s');
59
60         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
61                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
62                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
63                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
64                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
65                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
66                 AND `item`.`type` != 'remote' AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
67                 AND `item`.`edited` > '%s'
68                 $sql_extra
69                 ORDER BY `parent` ASC, `created` ASC LIMIT 0, 300",
70                 intval($owner_id),
71                 dbesc($check_date)
72         );
73         if(! count($r))
74                 killme();
75
76         $items = $r;
77
78         $feed_template = file_get_contents('view/atom_feed.tpl');
79         $tomb_template = file_get_contents('view/atom_tomb.tpl');
80         $item_template = file_get_contents('view/atom_item.tpl');
81         $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
82
83         $atom = '';
84
85
86         $atom .= replace_macros($feed_template, array(
87                         '$feed_id' => xmlify($a->get_baseurl()),
88                         '$feed_title' => xmlify($owner['name']),
89                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
90                         '$name' => xmlify($owner['name']),
91                         '$profile_page' => xmlify($owner['url']),
92                         '$photo' => xmlify($owner['photo'])
93         ));
94
95         foreach($items as $item) {
96                 if($item['deleted']) {
97                         $atom .= replace_macros($tomb_template, array(
98                                 '$id' => xmlify($item['uri']),
99                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
100                         ));
101                 }
102                 else {
103
104                         if($item['parent'] == $item['id']) {
105                                 $atom .= replace_macros($item_template, array(
106                                         '$name' => xmlify($item['name']),
107                                         '$profile_page' => xmlify($item['url']),
108                                         '$thumb' => xmlify($item['thumb']),
109                                         '$owner_name' => xmlify($item['owner-name']),
110                                         '$owner_profile_page' => xmlify($item['owner-link']),
111                                         '$owner_thumb' => xmlify($item['owner-avatar']),
112                                         '$item_id' => xmlify($item['uri']),
113                                         '$title' => xmlify($item['name']),
114                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
115                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
116                                         '$content' =>xmlify($item['body']),
117                                         '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0)
118                                 ));
119                         }
120                         else {
121                                 $atom .= replace_macros($cmnt_template, array(
122                                         '$name' => xmlify($item['name']),
123                                         '$profile_page' => xmlify($item['url']),
124                                         '$thumb' => xmlify($item['thumb']),
125                                         '$item_id' => xmlify($item['uri']),
126                                         '$title' => xmlify($item['title']),
127                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
128                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
129                                         '$content' =>xmlify($item['body']),
130                                         '$parent_id' => xmlify($item['parent-uri']),
131                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
132                                 ));
133                         }
134                 }
135         }
136
137         $atom .= "</feed>\r\n";
138
139         return $atom;
140
141
142
143
144
145 function get_atom_elements($item) {
146
147         $res = array();
148
149         $author = $item->get_author();
150         $res['author-name'] = unxmlify($author->get_name());
151         $res['author-link'] = unxmlify($author->get_link());
152         $res['author-avatar'] = unxmlify($author->get_avatar());
153         $res['uri'] = unxmlify($item->get_id());
154         $res['title'] = unxmlify($item->get_title());
155         $res['body'] = unxmlify($item->get_content());
156
157         $maxlen = get_max_import_size();
158         if($maxlen && (strlen($res['body']) > $maxlen))
159                 $res['body'] = substr($res['body'],0, $maxlen);
160
161         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
162         if($allow && $allow[0]['data'] == 1)
163                 $res['last-child'] = 1;
164         else
165                 $res['last-child'] = 0;
166
167         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
168         if($rawcreated)
169                 $res['created'] = unxmlify($rawcreated[0]['data']);
170
171         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
172         if($rawedited)
173                 $res['edited'] = unxmlify($rawcreated[0]['data']);
174
175         $rawowner = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0', 'owner');
176         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data'])
177                 $res['owner-name'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data']);
178         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data'])
179                 $res['owner-link'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data']);
180         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data'])
181                 $res['owner-avatar'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data']);
182
183         return $res;
184 }
185
186 function post_remote($a,$arr) {
187
188
189         if(! x($arr,'type'))
190                 $arr['type'] = 'remote';
191         $arr['uri'] = notags(trim($arr['uri']));
192         $arr['author-name'] = notags(trim($arr['author-name']));
193         $arr['author-link'] = notags(trim($arr['author-link']));
194         $arr['author-avatar'] = notags(trim($arr['author-avatar']));
195         $arr['owner-name'] = notags(trim($arr['owner-name']));
196         $arr['owner-link'] = notags(trim($arr['owner-link']));
197         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
198         $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
199         $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
200         $arr['title'] = notags(trim($arr['title']));
201         $arr['body'] = escape_tags(trim($arr['body']));
202         $arr['last-child'] = intval($arr['last-child']);
203         $arr['visible'] = 1;
204         $arr['deleted'] = 0;
205         $arr['parent-uri'] = notags(trim($arr['parent-uri']));
206
207         $parent_id = 0;
208
209         dbesc_array($arr);
210 //dbg(3);
211         $r = q("INSERT INTO `item` (`" 
212                         . implode("`, `", array_keys($arr)) 
213                         . "`) VALUES ('" 
214                         . implode("', '", array_values($arr)) 
215                         . "')" );
216
217         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
218                 dbesc($arr['parent-uri']),
219                 intval($arr['uid'])
220         );
221
222         if(count($r))
223                 $parent_id = $r[0]['id'];
224         else {
225                 // if parent is missing, what do we do?
226         }
227
228         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
229                 $arr['uri'],
230                 intval($arr['uid'])
231         );
232         if(count($r))
233                 $current_post = $r[0]['id'];
234
235         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
236                 intval($parent_id),
237                 intval($current_post)
238         );
239
240         return $current_post;
241 }