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