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