]> git.mxchange.org Git - friendica.git/blob - include/items.php
poller
[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 
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(((strlen($item['remote-id'])) ? $item['remote-id'] : "urn:X-dfrn:" . $a->get_baseurl() . ":{$owner['uid']}:{$item['hash']}")),
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(((strlen($item['remote-id'])) ? $item['remote-id'] : "urn:X-dfrn:" . $a->get_baseurl() . ":{$owner['uid']}:{$item['hash']}")),
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'] && (! $contact['blocked']))) ? 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(((strlen($item['remote-id'])) ? $item['remote-id'] : "urn:X-dfrn:" . $a->get_baseurl() . ":{$owner['uid']}:{$item['hash']}")),
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
131                                                                                                                 //      ......this is wrong!!!!
132                                         '$parent_id' => xmlify("urn:X-dfrn:" . $a->get_baseurl() . ":{$owner['uid']}:{$items[0]['hash']}"),
133                                         '$comment_allow' => (($item['last-child']) ? 1 : 0)
134                                 ));
135                         }
136                 }
137         }
138
139         $atom .= "</feed>\r\n";
140
141         return $atom;
142
143
144
145
146
147 function get_atom_elements($item) {
148
149         $res = array();
150
151         $author = $item->get_author();
152         $res['remote-name'] = unxmlify($author->get_name());
153         $res['remote-link'] = unxmlify($author->get_link());
154         $res['remote-avatar'] = unxmlify($author->get_avatar());
155         $res['remote-id'] = unxmlify($item->get_id());
156         $res['title'] = unxmlify($item->get_title());
157         $res['body'] = unxmlify($item->get_content());
158
159         $maxlen = get_max_import_size();
160         if($maxlen && (strlen($res['body']) > $maxlen))
161                 $res['body'] = substr($res['body'],0, $maxlen);
162
163         $allow = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0','comment-allow');
164         if($allow && $allow[0]['data'] == 1)
165                 $res['last-child'] = 1;
166         else
167                 $res['last-child'] = 0;
168
169         $rawcreated = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'published');
170         if($rawcreated)
171                 $res['created'] = unxmlify($rawcreated[0]['data']);
172
173         $rawedited = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'updated');
174         if($rawedited)
175                 $res['edited'] = unxmlify($rawcreated[0]['data']);
176
177         $rawowner = $item->get_item_tags('http://purl.org/macgirvin/dfrn/1.0', 'owner');
178         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data'])
179                 $res['owner-name'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['name'][0]['data']);
180         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data'])
181                 $res['owner-link'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['uri'][0]['data']);
182         if($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data'])
183                 $res['owner-avatar'] = unxmlify($rawowner[0]['child']['http://purl.org/macgirvin/dfrn/1.0']['avatar'][0]['data']);
184
185         return $res;
186 }
187
188 function post_remote($a,$arr) {
189
190         $arr['hash'] = random_string();
191         if(! x($arr,'type'))
192                 $arr['type'] = 'remote';
193         $arr['remote-name'] = notags(trim($arr['remote-name']));
194         $arr['remote-link'] = notags(trim($arr['remote-link']));
195         $arr['remote-avatar'] = notags(trim($arr['remote-avatar']));
196         $arr['owner-name'] = notags(trim($arr['owner-name']));
197         $arr['owner-link'] = notags(trim($arr['owner-link']));
198         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
199         if(! strlen($arr['remote-avatar']))
200                 $arr['remote-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
201         if(! strlen($arr['owner-avatar']))
202                 $arr['owner-avatar'] = $a->get_baseurl() . '/images/default-profile-sm.jpg';
203         $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
204         $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
205         $arr['title'] = notags(trim($arr['title']));
206         $arr['body'] = escape_tags(trim($arr['body']));
207         $arr['last-child'] = intval($arr['last-child']);
208         $arr['visible'] = 1;
209         $arr['deleted'] = 0;
210
211         $local_parent = false;
212
213         if(isset($arr['parent_hash'])) {
214                 $local_parent = true;
215                 $parent = $arr['parent_hash'];
216                 unset($arr['parent_hash']);
217         }
218         else {
219                 $parent = $arr['parent_urn'];
220                 unset($arr['parent_urn']);
221         }
222
223         $parent_id = 0;
224
225         dbesc_array($arr);
226 dbg(3);
227         $r = q("INSERT INTO `item` (`" 
228                         . implode("`, `", array_keys($arr)) 
229                         . "`) VALUES ('" 
230                         . implode("', '", array_values($arr)) 
231                         . "')" );
232
233         if($local_parent) {
234                 $r = q("SELECT `id` FROM `item` WHERE `hash` = '%s' AND `uid` = %d LIMIT 1",
235                         dbesc($parent),
236                         intval($arr['uid'])
237                 );
238         }
239         else {
240                 $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
241                         dbesc($parent),
242                         intval($arr['uid'])
243                 );
244         }
245         if(count($r))
246                 $parent_id = $r[0]['id'];
247         
248
249         $r = q("SELECT `id` FROM `item` WHERE `remote-id` = '%s' AND `uid` = %d LIMIT 1",
250                 $arr['remote-id'],
251                 intval($arr['uid'])
252         );
253         if(count($r))
254                 $current_post = $r[0]['id'];
255
256         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
257                 intval($parent_id),
258                 intval($current_post)
259         );
260
261         return $current_post;
262 }