]> git.mxchange.org Git - friendica.git/blob - mod/item.php
f0c2cc644d01b28b93fb01655b5bd4a4e8e4a915
[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
85         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
86                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
87                         intval($_SESSION['uid'])
88                 );
89         }
90         else {
91                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
92                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
93                                 intval($_SESSION['visitor_id'])
94                         );
95                 }
96         }
97
98         if(count($r)) {
99                 $author = $r[0];
100                 $contact_id = $author['id'];
101         }
102
103         // get contact info for owner
104         
105         if($profile_uid == $_SESSION['uid']) {
106                 $contact_record = $author;
107         }
108         else {
109                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
110                         intval($profile_uid)
111                 );
112                 if(count($r))
113                         $contact_record = $r[0];
114         }
115
116         $post_type = notags(trim($_POST['type']));
117
118         if($post_type === 'net-comment') {
119                 if($parent_item !== null) {
120                         if($parent_item['type'] === 'remote') {
121                                 $post_type = 'remote-comment';
122                         } 
123                         else {          
124                                 $post_type = 'wall-comment';
125                         }
126                 }
127         }
128
129         $str_tags = '';
130         $inform   = '';
131
132         $tags = get_tags($body);
133
134
135         if(count($tags)) {
136                 foreach($tags as $tag) {
137                         if(strpos($tag,'#') === 0) {
138                                 $basetag = str_replace('_',' ',substr($tag,1));
139                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
140                                 if(strlen($str_tags))
141                                         $str_tags .= ',';
142                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
143                                 continue;
144                         }
145                         if(strpos($tag,'@') === 0) {
146                                 $name = substr($tag,1);
147                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
148                                         $newname = $name;
149                                         $links = @lrdd($name);
150                                         if(count($links)) {
151                                                 foreach($links as $link) {
152                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
153                                         $profile = $link['@attributes']['href'];
154                                                         if($link['@attributes']['rel'] === 'salmon') {
155                                                                 if(strlen($inform))
156                                                                         $inform .= ',';
157                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
158                                                         }
159                                                 }
160                                         }
161                                 }
162                                 else {
163                                         $newname = $name;
164                                         if(strstr($name,'_')) {
165                                                 $newname = str_replace('_',' ',$name);
166                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
167                                                         dbesc($newname),
168                                                         intval($profile_uid)
169                                                 );
170                                         }
171                                         else {
172                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
173                                                         dbesc($name),
174                                                         intval($profile_uid)
175                                                 );
176                                         }
177                                         if(count($r)) {
178                                                 $profile = $r[0]['url'];
179                                                 $newname = $r[0]['name'];
180                                                 if(strlen($inform))
181                                                         $inform .= ',';
182                                                 $inform .= 'cid:' . $r[0]['id'];
183                                         }
184                                 }
185                                 if($profile) {
186                                         $body = str_replace($name,'[url=' . $profile . ']' . $newname   . '[/url]', $body);
187                                         $profile = str_replace(',','%2c',$profile);
188                                         if(strlen($str_tags))
189                                                 $str_tags .= ',';
190                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
191                                 }
192                         }
193                 }
194         }
195
196         $wall = 0;
197         if($post_type === 'wall' || $post_type === 'wall-comment')
198                 $wall = 1;
199
200         if(! strlen($verb))
201                 $verb = ACTIVITY_POST ;
202
203         $gravity = (($parent) ? 6 : 0 );
204  
205         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
206
207         $uri = item_new_uri($a->get_hostname(),$profile_uid);
208
209         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
210                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
211                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
212                 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 )",
213                 intval($profile_uid),
214                 dbesc($post_type),
215                 intval($wall),
216                 intval($gravity),
217                 intval($contact_id),
218                 dbesc($contact_record['name']),
219                 dbesc($contact_record['url']),
220                 dbesc($contact_record['thumb']),
221                 dbesc($author['name']),
222                 dbesc($author['url']),
223                 dbesc($author['thumb']),
224                 dbesc(datetime_convert()),
225                 dbesc(datetime_convert()),
226                 dbesc(datetime_convert()),
227                 dbesc($uri),
228                 dbesc($title),
229                 dbesc($body),
230                 dbesc($location),
231                 dbesc($coord),
232                 dbesc($str_tags),
233                 dbesc($inform),
234                 dbesc($verb),
235                 dbesc($str_contact_allow),
236                 dbesc($str_group_allow),
237                 dbesc($str_contact_deny),
238                 dbesc($str_group_deny),
239                 intval($private)
240         );
241         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
242                 dbesc($uri));
243         if(count($r)) {
244                 $post_id = $r[0]['id'];
245                 logger('mod_item: saved item ' . $post_id);
246
247                 if($parent) {
248
249                         // This item is the last leaf and gets the comment box, clear any ancestors
250                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
251                                 dbesc(datetime_convert()),
252                                 intval($parent)
253                         );
254
255                         // Inherit ACL's from the parent item.
256
257                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
258                                 WHERE `id` = %d LIMIT 1",
259                                 dbesc($parent_item['allow_cid']),
260                                 dbesc($parent_item['allow_gid']),
261                                 dbesc($parent_item['deny_cid']),
262                                 dbesc($parent_item['deny_gid']),
263                                 intval($parent_item['private']),
264                                 intval($post_id)
265                         );
266
267                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
268                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
269                                 require_once('bbcode.php');
270                                 $from = $author['name'];
271                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
272                                 $email_tpl = replace_macros($tpl, array(
273                                         '$sitename' => $a->config['sitename'],
274                                         '$siteurl' =>  $a->get_baseurl(),
275                                         '$username' => $user['username'],
276                                         '$email' => $user['email'],
277                                         '$from' => $from,
278                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
279                                         '$body' => strip_tags(bbcode($body))
280                                 ));
281
282                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
283                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
284                         }
285                 }
286                 else {
287                         $parent = $post_id;
288
289                         // let me know if somebody did a wall-to-wall post on my profile
290
291                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
292                                 require_once('bbcode.php');
293                                 $from = $author['name'];
294                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
295                                 $email_tpl = replace_macros($tpl, array(
296                                         '$sitename' => $a->config['sitename'],
297                                         '$siteurl' =>  $a->get_baseurl(),
298                                         '$username' => $user['username'],
299                                         '$email' => $user['email'],
300                                         '$from' => $from,
301                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
302                                         '$body' => strip_tags(bbcode($body))
303                                 ));
304
305                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
306                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
307                         }
308                 }
309
310                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
311                         WHERE `id` = %d LIMIT 1",
312                         intval($parent),
313                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
314                         dbesc(datetime_convert()),
315                         intval($post_id)
316                 );
317
318                 // photo comments turn the corresponding item visible to the profile wall
319                 // This way we don't see every picture in your new photo album posted to your wall at once.
320                 // They will show up as people comment on them.
321
322                 if(! $parent_item['visible']) {
323                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
324                                 intval($parent_item['id'])
325                         );
326                 }
327         }
328
329         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
330
331         logger('mod_item: notifier invoked: ' . "\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &");
332
333         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"$notify_type\" \"$post_id\" &",
334                 array(),$foo));
335
336         /**
337          * Post to Facebook stream
338          */
339
340         if((local_user()) && (local_user() == $profile_uid) && (! $private)) {
341                 $appid  = get_config('system', 'facebook_appid'  );
342                 $secret = get_config('system', 'facebook_secret' );
343                 if($appid && $secret) {
344                         $fb_post = get_pconfig($local_user(),'facebook','post');
345                         if($fb_post) {
346                                 require_once('library/facebook.php');
347                                 require_once('include/bbcode.php');     
348
349                                 $facebook = new Facebook(array(
350                                         'appId'  => $appid,
351                                         'secret' => $secret,
352                                         'cookie' => true
353                                 ));                     
354                                 try {
355                                         $statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> bbcode($body), 'cb' => ''));
356                                 } 
357                                 catch (FacebookApiException $e) {
358                                         notice( t('Facebook status update failed.') . EOL);
359                                 }
360                         }
361                 }
362         }
363
364         goaway($a->get_baseurl() . "/" . $_POST['return'] );
365         return; // NOTREACHED
366 }
367
368
369
370
371
372 function item_content(&$a) {
373
374         if((! local_user()) && (! remote_user()))
375                 return;
376
377         require_once('include/security.php');
378
379         $uid = $_SESSION['uid'];
380
381         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
382
383                 // locate item to be deleted
384
385                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
386                         intval($a->argv[2])
387                 );
388
389                 if(! count($r)) {
390                         notice( t('Item not found.') . EOL);
391                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
392                 }
393                 $item = $r[0];
394
395                 // check if logged in user is either the author or owner of this item
396
397                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
398
399                         // delete the item
400
401                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
402                                 dbesc(datetime_convert()),
403                                 dbesc(datetime_convert()),
404                                 intval($item['id'])
405                         );
406
407                         // If item is a link to a photo resource, nuke all the associated photos 
408                         // (visitors will not have photo resources)
409                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
410                         // generate a resource-id and therefore aren't intimately linked to the item. 
411
412                         if(strlen($item['resource-id'])) {
413                                 $q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
414                                         dbesc($item['resource-id']),
415                                         intval($item['uid'])
416                                 );
417                                 // ignore the result
418                         }
419
420                         // If it's the parent of a comment thread, kill all the kids
421
422                         if($item['uri'] == $item['parent-uri']) {
423                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
424                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
425                                         dbesc(datetime_convert()),
426                                         dbesc(datetime_convert()),
427                                         dbesc($item['parent-uri']),
428                                         intval($item['uid'])
429                                 );
430                                 // ignore the result
431                         }
432                         else {
433                                 // ensure that last-child is set in case the comment that had it just got wiped.
434                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
435                                         dbesc(datetime_convert()),
436                                         dbesc($item['parent-uri']),
437                                         intval($item['uid'])
438                                 );
439                                 // who is the last child now? 
440                                 $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",
441                                         dbesc($item['parent-uri']),
442                                         intval($item['uid'])
443                                 );
444                                 if(count($r)) {
445                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
446                                                 intval($r[0]['id'])
447                                         );
448                                 }       
449                         }
450                         $drop_id = intval($item['id']);
451                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
452                         
453                         // send the notification upstream/downstream as the case may be
454
455                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" &",
456                                 array(), $foo));
457
458                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
459                         return; //NOTREACHED
460                 }
461                 else {
462                         notice( t('Permission denied.') . EOL);
463                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
464                         //NOTREACHED
465                 }
466         }
467 }