]> git.mxchange.org Git - friendica.git/blob - include/items.php
fixup image on wall
[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`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
65                 `contact`.`id` AS `contact-id`, `contact`.`uid` AS `contact-uid`
66                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
67                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
68                 AND NOT `item`.`type` IN ( 'remote', 'net-comment' ) AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
69                 AND `item`.`edited` > '%s'
70                 $sql_extra
71                 ORDER BY `parent` ASC, `created` ASC LIMIT 0, 300",
72                 intval($owner_id),
73                 dbesc($check_date)
74         );
75         if(! count($r))
76                 killme();
77
78         $items = $r;
79
80         $feed_template = file_get_contents('view/atom_feed.tpl');
81         $tomb_template = file_get_contents('view/atom_tomb.tpl');
82         $item_template = file_get_contents('view/atom_item.tpl');
83         $cmnt_template = file_get_contents('view/atom_cmnt.tpl');
84
85         $atom = '';
86
87
88         $atom .= replace_macros($feed_template, array(
89                         '$feed_id' => xmlify($a->get_baseurl()),
90                         '$feed_title' => xmlify($owner['name']),
91                         '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) ,
92                         '$name' => xmlify($owner['name']),
93                         '$profile_page' => xmlify($owner['url']),
94                         '$photo' => xmlify($owner['photo'])
95         ));
96
97         foreach($items as $item) {
98                 if($item['deleted']) {
99                         $atom .= replace_macros($tomb_template, array(
100                                 '$id' => xmlify($item['uri']),
101                                 '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z'))
102                         ));
103                 }
104                 else {
105
106                         if($item['parent'] == $item['id']) {
107                                 $atom .= replace_macros($item_template, array(
108                                         '$name' => xmlify($item['name']),
109                                         '$profile_page' => xmlify($item['url']),
110                                         '$thumb' => xmlify($item['thumb']),
111                                         '$owner_name' => xmlify($item['owner-name']),
112                                         '$owner_profile_page' => xmlify($item['owner-link']),
113                                         '$owner_thumb' => xmlify($item['owner-avatar']),
114                                         '$item_id' => xmlify($item['uri']),
115                                         '$title' => xmlify($item['title']),
116                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
117                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
118                                         '$content' =>xmlify($item['body']),
119                                         '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0)
120                                 ));
121                         }
122                         else {
123                                 $atom .= replace_macros($cmnt_template, array(
124                                         '$name' => xmlify($item['name']),
125                                         '$profile_page' => xmlify($item['url']),
126                                         '$thumb' => xmlify($item['thumb']),
127                                         '$item_id' => xmlify($item['uri']),
128                                         '$title' => xmlify($item['title']),
129                                         '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
130                                         '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')),
131                                         '$content' =>xmlify($item['body']),
132                                         '$parent_id' => xmlify($item['parent-uri']),
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['author-name'] = unxmlify($author->get_name());
153         $res['author-link'] = unxmlify($author->get_link());
154         $res['author-avatar'] = unxmlify($author->get_avatar());
155         $res['uri'] = 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(NAMESPACE_DFRN,'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(NAMESPACE_DFRN, 'owner');
178         if($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data'])
179                 $res['owner-name'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['name'][0]['data']);
180         if($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data'])
181                 $res['owner-link'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['uri'][0]['data']);
182         if($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data'])
183                 $res['owner-avatar'] = unxmlify($rawowner[0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']);
184
185         return $res;
186 }
187
188 function post_remote($a,$arr) {
189
190
191         if(! x($arr,'type'))
192                 $arr['type'] = 'remote';
193         $arr['uri'] = notags(trim($arr['uri']));
194         $arr['author-name'] = notags(trim($arr['author-name']));
195         $arr['author-link'] = notags(trim($arr['author-link']));
196         $arr['author-avatar'] = notags(trim($arr['author-avatar']));
197         $arr['owner-name'] = notags(trim($arr['owner-name']));
198         $arr['owner-link'] = notags(trim($arr['owner-link']));
199         $arr['owner-avatar'] = notags(trim($arr['owner-avatar']));
200         $arr['created'] = datetime_convert('UTC','UTC',$arr['created'],'Y-m-d H:i:s');
201         $arr['edited'] = datetime_convert('UTC','UTC',$arr['edited'],'Y-m-d H:i:s');
202         $arr['title'] = notags(trim($arr['title']));
203         $arr['body'] = escape_tags(trim($arr['body']));
204         $arr['last-child'] = intval($arr['last-child']);
205         $arr['visible'] = 1;
206         $arr['deleted'] = 0;
207         $arr['parent-uri'] = notags(trim($arr['parent-uri']));
208
209         $parent_id = 0;
210
211         dbesc_array($arr);
212 //dbg(3);
213         $r = q("INSERT INTO `item` (`" 
214                         . implode("`, `", array_keys($arr)) 
215                         . "`) VALUES ('" 
216                         . implode("', '", array_values($arr)) 
217                         . "')" );
218
219         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
220                 dbesc($arr['parent-uri']),
221                 intval($arr['uid'])
222         );
223
224         if(count($r))
225                 $parent_id = $r[0]['id'];
226         else {
227                 // if parent is missing, what do we do?
228         }
229
230         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
231                 $arr['uri'],
232                 intval($arr['uid'])
233         );
234         if(count($r))
235                 $current_post = $r[0]['id'];
236
237         $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d LIMIT 1",
238                 intval($parent_id),
239                 intval($current_post)
240         );
241
242         return $current_post;
243 }