]> git.mxchange.org Git - friendica.git/blob - mod/item.php
add rel=mentioned links for tagged persons
[friendica.git] / mod / item.php
1 <?php
2
3 // This is the POST destination for most all locally posted
4 // text stuff. This function handles status, wall-to-wall status, 
5 // local comments, and remote coments - that are posted on this site 
6 // (as opposed to being delivered in a feed).
7 // All of these become an "item" which is our basic unit of 
8 // information. 
9
10 function item_post(&$a) {
11
12         if((! local_user()) && (! remote_user()))
13                 return;
14
15         require_once('include/security.php');
16
17         $uid = local_user();
18
19         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
20
21         $parent_item = null;
22
23         if($parent) {
24                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
25                         intval($parent)
26                 );
27                 if(! count($r)) {
28                         notice( t('Unable to locate original post.') . EOL);
29                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
30                 }
31                 $parent_item = $r[0];
32         }
33
34         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
35
36
37         if(! can_write_wall($a,$profile_uid)) {
38                 notice( t('Permission denied.') . EOL) ;
39                 return;
40         }
41
42         $user = null;
43
44         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
45                 intval($profile_uid)
46         );
47         if(count($r))
48                 $user = $r[0];
49         
50
51         $str_group_allow   = perms2str($_POST['group_allow']);
52         $str_contact_allow = perms2str($_POST['contact_allow']);
53         $str_group_deny    = perms2str($_POST['group_deny']);
54         $str_contact_deny  = perms2str($_POST['contact_deny']);
55
56         $title             = notags(trim($_POST['title']));
57         $body              = escape_tags(trim($_POST['body']));
58         $location          = notags(trim($_POST['location']));
59         $coord             = notags(trim($_POST['coord']));
60         $verb              = notags(trim($_POST['verb']));
61
62         if(! strlen($body)) {
63                 notice( t('Empty post discarded.') . EOL );
64                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
65
66         }
67
68         // get contact info for poster
69
70         $author = null;
71
72         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
73                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
74                         intval($_SESSION['uid'])
75                 );
76         }
77         else {
78                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
79                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
80                                 intval($_SESSION['visitor_id'])
81                         );
82                 }
83         }
84
85         if(count($r)) {
86                 $author = $r[0];
87                 $contact_id = $author['id'];
88         }
89
90         // get contact info for owner
91         
92         if($profile_uid == $_SESSION['uid']) {
93                 $contact_record = $author;
94         }
95         else {
96                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
97                         intval($profile_uid)
98                 );
99                 if(count($r))
100                         $contact_record = $r[0];
101         }
102
103         $post_type = notags(trim($_POST['type']));
104
105         if($post_type === 'net-comment') {
106                 if($parent_item !== null) {
107                         if($parent_item['type'] === 'remote') {
108                                 $post_type = 'remote-comment';
109                         } 
110                         else {          
111                                 $post_type = 'wall-comment';
112                         }
113                 }
114         }
115
116         $str_tags = '';
117         $inform   = '';
118
119         $tags = get_tags($body);
120
121
122         if($tags) {
123                 foreach($tags as $tag) {
124                         if(strpos($tag,'@') === 0) {
125                                 $name = substr($tag,1);
126                                 if(strpos($name,'@')) {
127                                         $newname = $name;
128                                         $links = @webfinger($name);
129                                         if(count($links)) {
130                                                 foreach($links as $link) {
131                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
132                                         $profile = $link['@attributes']['href'];
133                                                         if($link['@attributes']['rel'] === 'salmon') {
134                                                                 if(strlen($inform))
135                                                                         $inform .= ',';
136                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
137                                                         }
138                                                 }
139                                         }
140                                 }
141                                 else {
142                                         $newname = $name;
143                                         if(strstr($name,'_')) {
144                                                 $newname = str_replace('_',' ',$name);
145                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
146                                                         dbesc($newname),
147                                                         intval($profile_uid)
148                                                 );
149                                         }
150                                         else {
151                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
152                                                         dbesc($name),
153                                                         intval($profile_uid)
154                                                 );
155                                         }
156                                         if(count($r)) {
157                                                 $profile = $r[0]['url'];
158                                                 if(strlen($inform))
159                                                         $inform .= ',';
160                                                 $inform .= 'cid:' . $r[0]['id'];
161                                         }
162                                 }
163                                 if($profile) {
164                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
165                                         if(strlen($str_tags))
166                                                 $str_tags .= ',';
167                                         $profile = str_replace(',','%2c',$profile);
168                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
169                                 }
170                         }
171                 }
172         }
173
174         $wall = 0;
175         if($post_type === 'wall' || $post_type === 'wall-comment')
176                 $wall = 1;
177
178         if(! strlen($verb))
179                 $verb = ACTIVITY_POST ;
180
181         $gravity = (($parent) ? 6 : 0 );
182  
183         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
184
185         $uri = item_new_uri($a->get_hostname(),$profile_uid);
186
187         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
188                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
189                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
190                 VALUES( %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
191                 intval($profile_uid),
192                 dbesc($post_type),
193                 intval($wall),
194                 intval($gravity),
195                 intval($contact_id),
196                 dbesc($contact_record['name']),
197                 dbesc($contact_record['url']),
198                 dbesc($contact_record['thumb']),
199                 dbesc($author['name']),
200                 dbesc($author['url']),
201                 dbesc($author['thumb']),
202                 dbesc(datetime_convert()),
203                 dbesc(datetime_convert()),
204                 dbesc(datetime_convert()),
205                 dbesc($uri),
206                 dbesc($title),
207                 dbesc($body),
208                 dbesc($location),
209                 dbesc($coord),
210                 dbesc($str_tags),
211                 dbesc($inform),
212                 dbesc($verb),
213                 dbesc($str_contact_allow),
214                 dbesc($str_group_allow),
215                 dbesc($str_contact_deny),
216                 dbesc($str_group_deny)
217         );
218         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
219                 dbesc($uri));
220         if(count($r)) {
221                 $post_id = $r[0]['id'];
222
223                 if($parent) {
224
225                         // This item is the last leaf and gets the comment box, clear any ancestors
226                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
227                                 dbesc(datetime_convert()),
228                                 intval($parent)
229                         );
230
231                         // Inherit ACL's from the parent item.
232                         // TODO merge with subsequent UPDATE operation and save a db write 
233
234                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
235                                 WHERE `id` = %d LIMIT 1",
236                                 dbesc($parent_item['allow_cid']),
237                                 dbesc($parent_item['allow_gid']),
238                                 dbesc($parent_item['deny_cid']),
239                                 dbesc($parent_item['deny_gid']),
240                                 intval($post_id)
241                         );
242
243                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
244                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
245                                 require_once('bbcode.php');
246                                 $from = $author['name'];
247                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
248                                 $email_tpl = replace_macros($tpl, array(
249                                         '$sitename' => $a->config['sitename'],
250                                         '$siteurl' =>  $a->get_baseurl(),
251                                         '$username' => $user['username'],
252                                         '$email' => $user['email'],
253                                         '$from' => $from,
254                                         '$body' => strip_tags(bbcode($body))
255                                 ));
256
257                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
258                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
259                         }
260                 }
261                 else {
262                         $parent = $post_id;
263
264                         // let me know if somebody did a wall-to-wall post on my profile
265
266                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
267                                 require_once('bbcode.php');
268                                 $from = $author['name'];
269                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
270                                 $email_tpl = replace_macros($tpl, array(
271                                         '$sitename' => $a->config['sitename'],
272                                         '$siteurl' =>  $a->get_baseurl(),
273                                         '$username' => $user['username'],
274                                         '$email' => $user['email'],
275                                         '$from' => $from,
276                                         '$body' => strip_tags(bbcode($body))
277                                 ));
278
279                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
280                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
281                         }
282                 }
283
284                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
285                         WHERE `id` = %d LIMIT 1",
286                         intval($parent),
287                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
288                         dbesc(datetime_convert()),
289                         intval($post_id)
290                 );
291
292                 // photo comments turn the corresponding item visible to the profile wall
293                 // This way we don't see every picture in your new photo album posted to your wall at once.
294                 // They will show up as people comment on them.
295
296                 if(! $parent_item['visible']) {
297                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
298                                 intval($parent_item['id'])
299                         );
300                 }
301         }
302
303         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
304         $proc_debug = get_config('system','proc_debug');
305
306         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" $proc_debug &",
307                 array(),$foo));
308
309         goaway($a->get_baseurl() . "/" . $_POST['return'] );
310         return; // NOTREACHED
311 }
312
313 function item_content(&$a) {
314
315         if((! local_user()) && (! remote_user()))
316                 return;
317
318         require_once('include/security.php');
319
320         $uid = $_SESSION['uid'];
321
322         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
323
324                 // locate item to be deleted
325
326                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
327                         intval($a->argv[2])
328                 );
329
330                 if(! count($r)) {
331                         notice( t('Item not found.') . EOL);
332                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
333                 }
334                 $item = $r[0];
335
336                 // check if logged in user is either the author or owner of this item
337
338                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
339
340                         // delete the item
341
342                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
343                                 dbesc(datetime_convert()),
344                                 dbesc(datetime_convert()),
345                                 intval($item['id'])
346                         );
347
348                         // If item is a link to a photo resource, nuke all the associated photos 
349                         // (visitors will not have photo resources)
350                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
351                         // generate a resource-id and therefore aren't intimately linked to the item. 
352
353                         if(strlen($item['resource-id'])) {
354                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
355                                         dbesc($item['resource-id']),
356                                         intval($item['uid'])
357                                 );
358                                 // ignore the result
359                         }
360
361                         // If it's the parent of a comment thread, kill all the kids
362
363                         if($item['uri'] == $item['parent-uri']) {
364                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
365                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
366                                         dbesc(datetime_convert()),
367                                         dbesc(datetime_convert()),
368                                         dbesc($item['parent-uri']),
369                                         intval($item['uid'])
370                                 );
371                                 // ignore the result
372                         }
373                         else {
374                                 // ensure that last-child is set in case the comment that had it just got wiped.
375                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
376                                         dbesc(datetime_convert()),
377                                         dbesc($item['parent-uri']),
378                                         intval($item['uid'])
379                                 );
380                                 // who is the last child now? 
381                                 $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d ORDER BY `edited` DESC LIMIT 1",
382                                         dbesc($item['parent-uri']),
383                                         intval($item['uid'])
384                                 );
385                                 if(count($r)) {
386                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
387                                                 intval($r[0]['id'])
388                                         );
389                                 }       
390                         }
391                         $drop_id = intval($item['id']);
392                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
393                         $proc_debug = get_config('system','proc_debug');
394
395                         
396                         // send the notification upstream/downstream as the case may be
397
398                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug &",
399                                 array(), $foo));
400
401                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
402                         return; //NOTREACHED
403                 }
404                 else {
405                         notice( t('Permission denied.') . EOL);
406                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
407                         //NOTREACHED
408                 }
409         }
410 }