]> git.mxchange.org Git - friendica.git/blob - mod/item.php
much better solution to privacy on ajax wall photos
[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         $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
57
58         if(($parent_item) && 
59                 (($parent_item['private']) 
60                         || strlen($parent_item['allow_cid']) 
61                         || strlen($parent_item['allow_gid']) 
62                         || strlen($parent_item['deny_cid']) 
63                         || strlen($parent_item['deny_gid'])
64                 )
65         ) {
66                 $private = 1;
67         }
68
69         $title             = notags(trim($_POST['title']));
70         $body              = escape_tags(trim($_POST['body']));
71         $location          = notags(trim($_POST['location']));
72         $coord             = notags(trim($_POST['coord']));
73         $verb              = notags(trim($_POST['verb']));
74
75         if(! strlen($body)) {
76                 notice( t('Empty post discarded.') . EOL );
77                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
78
79         }
80
81         // get contact info for poster
82
83         $author = null;
84         $self   = false;
85
86         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
87                 $self = true;
88                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
89                         intval($_SESSION['uid'])
90                 );
91         }
92         else {
93                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
94                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
95                                 intval($_SESSION['visitor_id'])
96                         );
97                 }
98         }
99
100         if(count($r)) {
101                 $author = $r[0];
102                 $contact_id = $author['id'];
103         }
104
105         // get contact info for owner
106         
107         if($profile_uid == $_SESSION['uid']) {
108                 $contact_record = $author;
109         }
110         else {
111                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
112                         intval($profile_uid)
113                 );
114                 if(count($r))
115                         $contact_record = $r[0];
116         }
117
118         $post_type = notags(trim($_POST['type']));
119
120         if($post_type === 'net-comment') {
121                 if($parent_item !== null) {
122                         if($parent_item['type'] === 'remote') {
123                                 $post_type = 'remote-comment';
124                         } 
125                         else {          
126                                 $post_type = 'wall-comment';
127                         }
128                 }
129         }
130
131
132         /**
133          *
134          * When a photo was uploaded into the message using the (profile wall) ajax 
135          * uploader, The permissions are initially set to disallow anybody but the
136          * owner from seeing it. This is because the permissions may not yet have been
137          * set for the post. If it's private, the photo permissions should be set
138          * appropriately. But we didn't know the final permissions on the post until
139          * now. So now we'll look for links of uploaded messages that are in the
140          * post and set them to the same permissions as the post itself.
141          *
142          */
143
144         $match = null;
145
146         if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
147                 $images = $match[1];
148                 if(count($images)) {
149                         foreach($images as $image) {
150                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
151                                         continue;
152                                 $image_uri = substr($image,strrpos($image,'/') + 1);
153                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
154                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
155                                         WHERE `resource-id` = '%s' AND `album` = '%s' ",
156                                         dbesc($str_contact_allow),
157                                         dbesc($str_group_allow),
158                                         dbesc($str_contact_deny),
159                                         dbesc($str_group_deny),
160                                         dbesc($image_uri),
161                                         dbesc( t('Wall Photos'))
162                                 );
163  
164                         }
165                 }
166         }
167
168
169
170         /**
171          * Look for any tags and linkify them
172          */
173
174         $str_tags = '';
175         $inform   = '';
176
177         $tags = get_tags($body);
178
179
180         if(count($tags)) {
181                 foreach($tags as $tag) {
182                         if(strpos($tag,'#') === 0) {
183                                 $basetag = str_replace('_',' ',substr($tag,1));
184                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
185                                 if(strlen($str_tags))
186                                         $str_tags .= ',';
187                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
188                                 continue;
189                         }
190                         if(strpos($tag,'@') === 0) {
191                                 $name = substr($tag,1);
192                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
193                                         $newname = $name;
194                                         $links = @lrdd($name);
195                                         if(count($links)) {
196                                                 foreach($links as $link) {
197                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
198                                         $profile = $link['@attributes']['href'];
199                                                         if($link['@attributes']['rel'] === 'salmon') {
200                                                                 if(strlen($inform))
201                                                                         $inform .= ',';
202                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
203                                                         }
204                                                 }
205                                         }
206                                 }
207                                 else {
208                                         $newname = $name;
209                                         if(strstr($name,'_')) {
210                                                 $newname = str_replace('_',' ',$name);
211                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
212                                                         dbesc($newname),
213                                                         intval($profile_uid)
214                                                 );
215                                         }
216                                         else {
217                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
218                                                         dbesc($name),
219                                                         intval($profile_uid)
220                                                 );
221                                         }
222                                         if(count($r)) {
223                                                 $profile = $r[0]['url'];
224                                                 $newname = $r[0]['name'];
225                                                 if(strlen($inform))
226                                                         $inform .= ',';
227                                                 $inform .= 'cid:' . $r[0]['id'];
228                                         }
229                                 }
230                                 if($profile) {
231                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
232                                         $profile = str_replace(',','%2c',$profile);
233                                         if(strlen($str_tags))
234                                                 $str_tags .= ',';
235                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
236                                 }
237                         }
238                 }
239         }
240
241         $wall = 0;
242         if($post_type === 'wall' || $post_type === 'wall-comment')
243                 $wall = 1;
244
245         if(! strlen($verb))
246                 $verb = ACTIVITY_POST ;
247
248         $gravity = (($parent) ? 6 : 0 );
249  
250         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
251
252         $uri = item_new_uri($a->get_hostname(),$profile_uid);
253
254         $datarray = array();
255         $datarray['uid']           = $profile_uid;
256         $datarray['type']          = $post_type;
257         $datarray['wall']          = $wall;
258         $datarray['gravity']       = $gravity;
259         $datarray['contact-id']    = $contact_id;
260         $datarray['owner-name']    = $contact_record['name'];
261         $datarray['owner-link']    = $contact_record['url'];
262         $datarray['owner-avatar']  = $contact_record['thumb'];
263         $datarray['author-name']   = $author['name'];
264         $datarray['author-link']   = $author['url'];
265         $datarray['author-avatar'] = $author['thumb'];
266         $datarray['created']       = datetime_convert();
267         $datarray['edited']        = datetime_convert();
268         $datarray['changed']       = datetime_convert();
269         $datarray['uri']           = $uri;
270         $datarray['title']         = $title;
271         $datarray['body']          = $body;
272         $datarray['location']      = $location;
273         $datarray['coord']         = $coord;
274         $datarray['tag']           = $str_tags;
275         $datarray['inform']        = $inform;
276         $datarray['verb']          = $verb;
277         $datarray['allow_cid']     = $str_contact_allow;
278         $datarray['allow_gid']     = $str_group_allow;
279         $datarray['deny_cid']      = $str_contact_deny;
280         $datarray['deny_gid']      = $str_group_deny;
281         $datarray['private']       = $private;
282
283         /**
284          * These fields are for the convenience of plugins...
285          * 'self' if true indicates the owner is posting on their own wall
286          * If parent is 0 it is a top-level post.
287          */
288
289         $datarray['parent']        = $parent;
290         $datarray['self']          = $self;
291
292
293         call_hooks('post_local',$datarray);
294
295         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
296                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
297                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
298                 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', %d )",
299                 intval($datarray['uid']),
300                 dbesc($datarray['type']),
301                 intval($datarray['wall']),
302                 intval($datarray['gravity']),
303                 intval($datarray['contact-id']),
304                 dbesc($datarray['owner-name']),
305                 dbesc($datarray['owner-link']),
306                 dbesc($datarray['owner-avatar']),
307                 dbesc($datarray['author-name']),
308                 dbesc($datarray['author-link']),
309                 dbesc($datarray['author-avatar']),
310                 dbesc($datarray['created']),
311                 dbesc($datarray['edited']),
312                 dbesc($datarray['changed']),
313                 dbesc($datarray['uri']),
314                 dbesc($datarray['title']),
315                 dbesc($datarray['body']),
316                 dbesc($datarray['location']),
317                 dbesc($datarray['coord']),
318                 dbesc($datarray['tag']),
319                 dbesc($datarray['inform']),
320                 dbesc($datarray['verb']),
321                 dbesc($datarray['allow_cid']),
322                 dbesc($datarray['allow_gid']),
323                 dbesc($datarray['deny_cid']),
324                 dbesc($datarray['deny_gid']),
325                 intval($datarray['private'])
326         );
327
328         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
329                 dbesc($datarray['uri']));
330         if(count($r)) {
331                 $post_id = $r[0]['id'];
332                 logger('mod_item: saved item ' . $post_id);
333
334                 if($parent) {
335
336                         // This item is the last leaf and gets the comment box, clear any ancestors
337                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
338                                 dbesc(datetime_convert()),
339                                 intval($parent)
340                         );
341
342                         // Inherit ACL's from the parent item.
343
344                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
345                                 WHERE `id` = %d LIMIT 1",
346                                 dbesc($parent_item['allow_cid']),
347                                 dbesc($parent_item['allow_gid']),
348                                 dbesc($parent_item['deny_cid']),
349                                 dbesc($parent_item['deny_gid']),
350                                 intval($parent_item['private']),
351                                 intval($post_id)
352                         );
353
354                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
355                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
356                                 require_once('bbcode.php');
357                                 $from = $author['name'];
358                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
359                                 $email_tpl = replace_macros($tpl, array(
360                                         '$sitename' => $a->config['sitename'],
361                                         '$siteurl' =>  $a->get_baseurl(),
362                                         '$username' => $user['username'],
363                                         '$email' => $user['email'],
364                                         '$from' => $from,
365                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
366                                         '$body' => strip_tags(bbcode($datarray['body']))
367                                 ));
368
369                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
370                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
371                         }
372                 }
373                 else {
374                         $parent = $post_id;
375
376                         // let me know if somebody did a wall-to-wall post on my profile
377
378                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
379                                 require_once('bbcode.php');
380                                 $from = $author['name'];
381                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
382                                 $email_tpl = replace_macros($tpl, array(
383                                         '$sitename' => $a->config['sitename'],
384                                         '$siteurl' =>  $a->get_baseurl(),
385                                         '$username' => $user['username'],
386                                         '$email' => $user['email'],
387                                         '$from' => $from,
388                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
389                                         '$body' => strip_tags(bbcode($datarray['body']))
390                                 ));
391
392                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
393                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
394                         }
395                 }
396
397                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
398                         WHERE `id` = %d LIMIT 1",
399                         intval($parent),
400                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
401                         dbesc(datetime_convert()),
402                         intval($post_id)
403                 );
404
405                 // photo comments turn the corresponding item visible to the profile wall
406                 // This way we don't see every picture in your new photo album posted to your wall at once.
407                 // They will show up as people comment on them.
408
409                 if(! $parent_item['visible']) {
410                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
411                                 intval($parent_item['id'])
412                         );
413                 }
414         }
415
416         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
417
418         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
419
420         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
421                 array(),$foo));
422
423         $datarray['id'] = $post_id;
424
425         call_hooks('post_local_end', $datarray);
426  
427
428         goaway($a->get_baseurl() . "/" . $_POST['return'] );
429         return; // NOTREACHED
430 }
431
432
433
434
435
436 function item_content(&$a) {
437
438         if((! local_user()) && (! remote_user()))
439                 return;
440
441         require_once('include/security.php');
442
443         $uid = $_SESSION['uid'];
444
445         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
446
447                 // locate item to be deleted
448
449                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
450                         intval($a->argv[2])
451                 );
452
453                 if(! count($r)) {
454                         notice( t('Item not found.') . EOL);
455                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
456                 }
457                 $item = $r[0];
458
459                 // check if logged in user is either the author or owner of this item
460
461                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
462
463                         // delete the item
464
465                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
466                                 dbesc(datetime_convert()),
467                                 dbesc(datetime_convert()),
468                                 intval($item['id'])
469                         );
470
471                         // If item is a link to a photo resource, nuke all the associated photos 
472                         // (visitors will not have photo resources)
473                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
474                         // generate a resource-id and therefore aren't intimately linked to the item. 
475
476                         if(strlen($item['resource-id'])) {
477                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
478                                         dbesc($item['resource-id']),
479                                         intval($item['uid'])
480                                 );
481                                 // ignore the result
482                         }
483
484                         // If it's the parent of a comment thread, kill all the kids
485
486                         if($item['uri'] == $item['parent-uri']) {
487                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
488                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
489                                         dbesc(datetime_convert()),
490                                         dbesc(datetime_convert()),
491                                         dbesc($item['parent-uri']),
492                                         intval($item['uid'])
493                                 );
494                                 // ignore the result
495                         }
496                         else {
497                                 // ensure that last-child is set in case the comment that had it just got wiped.
498                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
499                                         dbesc(datetime_convert()),
500                                         dbesc($item['parent-uri']),
501                                         intval($item['uid'])
502                                 );
503                                 // who is the last child now? 
504                                 $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",
505                                         dbesc($item['parent-uri']),
506                                         intval($item['uid'])
507                                 );
508                                 if(count($r)) {
509                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
510                                                 intval($r[0]['id'])
511                                         );
512                                 }       
513                         }
514                         $drop_id = intval($item['id']);
515                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
516                         
517                         // send the notification upstream/downstream as the case may be
518
519                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
520                                 array(), $foo));
521
522                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
523                         return; //NOTREACHED
524                 }
525                 else {
526                         notice( t('Permission denied.') . EOL);
527                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
528                         //NOTREACHED
529                 }
530         }
531 }