]> git.mxchange.org Git - friendica.git/blob - mod/item.php
ad32985921fd71dac5f976aec71554b186dbea1e
[friendica.git] / mod / item.php
1 <?php
2
3 /**
4  *
5  * This is the POST destination for most all locally posted
6  * text stuff. This function handles status, wall-to-wall status, 
7  * local comments, and remote coments - that are posted on this site 
8  * (as opposed to being delivered in a feed).
9  * All of these become an "item" which is our basic unit of 
10  * information.
11  * Posts that originate externally or do not fall into the above 
12  * posting categories go through item_store() instead of this function. 
13  *
14  */  
15
16 function item_post(&$a) {
17
18         if((! local_user()) && (! remote_user()))
19                 return;
20
21         require_once('include/security.php');
22
23         $uid = local_user();
24
25         call_hooks('post_local_start', $_POST);
26
27         $parent = ((x($_POST,'parent')) ? intval($_POST['parent']) : 0);
28
29         $parent_item = null;
30         $parent_contact = null;
31
32         if($parent) {
33                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
34                         intval($parent)
35                 );
36                 if(! count($r)) {
37                         notice( t('Unable to locate original post.') . EOL);
38                         if(x($_POST,'return')) 
39                                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
40                         killme();
41                 }
42                 $parent_item = $r[0];
43                 if($parent_item['contact-id'] && $uid) {
44                         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
45                                 intval($parent_item['contact-id']),
46                                 intval($uid)
47                         );
48                         if(count($r))
49                                 $parent_contact = $r[0];
50                 }
51         }
52
53         $profile_uid = ((x($_POST,'profile_uid')) ? intval($_POST['profile_uid']) : 0);
54
55
56         if(! can_write_wall($a,$profile_uid)) {
57                 notice( t('Permission denied.') . EOL) ;
58                 if(x($_POST,'return')) 
59                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
60                 killme();
61         }
62
63         $user = null;
64
65         $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
66                 intval($profile_uid)
67         );
68         if(count($r))
69                 $user = $r[0];
70         
71
72         $str_group_allow   = perms2str($_POST['group_allow']);
73         $str_contact_allow = perms2str($_POST['contact_allow']);
74         $str_group_deny    = perms2str($_POST['group_deny']);
75         $str_contact_deny  = perms2str($_POST['contact_deny']);
76
77         $private = ((strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny)) ? 1 : 0);
78
79         if(($parent_item) && 
80                 (($parent_item['private']) 
81                         || strlen($parent_item['allow_cid']) 
82                         || strlen($parent_item['allow_gid']) 
83                         || strlen($parent_item['deny_cid']) 
84                         || strlen($parent_item['deny_gid'])
85                 )
86         ) {
87                 $private = 1;
88         }
89
90         $title             = notags(trim($_POST['title']));
91         $body              = escape_tags(trim($_POST['body']));
92         $location          = notags(trim($_POST['location']));
93         $coord             = notags(trim($_POST['coord']));
94         $verb              = notags(trim($_POST['verb']));
95         $emailcc           = notags(trim($_POST['emailcc']));
96
97         if(! strlen($body)) {
98                 notice( t('Empty post discarded.') . EOL );
99                 if(x($_POST,'return')) 
100                         goaway($a->get_baseurl() . "/" . $_POST['return'] );
101                 killme();
102         }
103
104         // get contact info for poster
105
106         $author = null;
107         $self   = false;
108
109         if(($_SESSION['uid']) && ($_SESSION['uid'] == $profile_uid)) {
110                 $self = true;
111                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
112                         intval($_SESSION['uid'])
113                 );
114         }
115         else {
116                 if((x($_SESSION,'visitor_id')) && (intval($_SESSION['visitor_id']))) {
117                         $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
118                                 intval($_SESSION['visitor_id'])
119                         );
120                 }
121         }
122
123         if(count($r)) {
124                 $author = $r[0];
125                 $contact_id = $author['id'];
126         }
127
128         // get contact info for owner
129         
130         if($profile_uid == $_SESSION['uid']) {
131                 $contact_record = $author;
132         }
133         else {
134                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
135                         intval($profile_uid)
136                 );
137                 if(count($r))
138                         $contact_record = $r[0];
139         }
140
141         $post_type = notags(trim($_POST['type']));
142
143         if($post_type === 'net-comment') {
144                 if($parent_item !== null) {
145                         if($parent_item['type'] === 'remote') {
146                                 $post_type = 'remote-comment';
147                         } 
148                         else {          
149                                 $post_type = 'wall-comment';
150                         }
151                 }
152         }
153
154
155         /**
156          *
157          * When a photo was uploaded into the message using the (profile wall) ajax 
158          * uploader, The permissions are initially set to disallow anybody but the
159          * owner from seeing it. This is because the permissions may not yet have been
160          * set for the post. If it's private, the photo permissions should be set
161          * appropriately. But we didn't know the final permissions on the post until
162          * now. So now we'll look for links of uploaded messages that are in the
163          * post and set them to the same permissions as the post itself.
164          *
165          */
166
167         $match = null;
168
169         if(preg_match_all("/\[img\](.+?)\[\/img\]/",$body,$match)) {
170                 $images = $match[1];
171                 if(count($images)) {
172                         foreach($images as $image) {
173                                 if(! stristr($image,$a->get_baseurl() . '/photo/'))
174                                         continue;
175                                 $image_uri = substr($image,strrpos($image,'/') + 1);
176                                 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
177                                 $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'
178                                         WHERE `resource-id` = '%s' AND `album` = '%s' ",
179                                         dbesc($str_contact_allow),
180                                         dbesc($str_group_allow),
181                                         dbesc($str_contact_deny),
182                                         dbesc($str_group_deny),
183                                         dbesc($image_uri),
184                                         dbesc( t('Wall Photos'))
185                                 );
186  
187                         }
188                 }
189         }
190
191         /**
192          * Fold multi-line [code] sequences
193          */
194
195         $body = preg_replace('/\[\/code\]\s*\[code\]/m',"\n",$body); 
196
197         /**
198          * Look for any tags and linkify them
199          */
200
201         $str_tags = '';
202         $inform   = '';
203
204
205         $tags = get_tags($body);
206
207         if(($parent_contact) && ($parent_contact['network'] === 'stat') && ($parent_contact['nick']) && (! in_array('@' . $parent_contact['nick'],$tags))) {
208                 $body = '@' . $parent_contact['nick'] . ' ' . $body;
209                 $tags[] = '@' . $parent_contact['nick'];
210         }               
211
212         if(count($tags)) {
213                 foreach($tags as $tag) {
214                         if(strpos($tag,'#') === 0) {
215                                 $basetag = str_replace('_',' ',substr($tag,1));
216                                 $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);
217                                 if(strlen($str_tags))
218                                         $str_tags .= ',';
219                                 $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
220                                 continue;
221                         }
222                         if(strpos($tag,'@') === 0) {
223                                 $stat = false;
224                                 $name = substr($tag,1);
225                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
226                                         $newname = $name;
227                                         $links = @lrdd($name);
228                                         if(count($links)) {
229                                                 foreach($links as $link) {
230                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
231                                         $profile = $link['@attributes']['href'];
232                                                         if($link['@attributes']['rel'] === 'salmon') {
233                                                                 if(strlen($inform))
234                                                                         $inform .= ',';
235                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
236                                                         }
237                                                 }
238                                         }
239                                 }
240                                 else {
241                                         $newname = $name;
242                                         $alias = '';
243                                         if(strstr($name,'_')) {
244                                                 $newname = str_replace('_',' ',$name);
245                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
246                                                         dbesc($newname),
247                                                         intval($profile_uid)
248                                                 );
249                                         }
250                                         else {
251                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
252                                                         dbesc($name),
253                                                         intval($profile_uid)
254                                                 );
255                                         }
256                                         if(count($r)) {
257                                                 $profile = $r[0]['url'];
258                                                 if($r[0]['network'] === 'stat') {
259                                                         $newname = $r[0]['nick'];
260                                                         $stat = true;
261                                                         if($r[0]['alias'])
262                                                                 $alias = $r[0]['alias'];
263                                                 }
264                                                 else
265                                                         $newname = $r[0]['name'];
266                                                 if(strlen($inform))
267                                                         $inform .= ',';
268                                                 $inform .= 'cid:' . $r[0]['id'];
269                                         }
270                                 }
271                                 if($profile) {
272                                         $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);
273                                         $profile = str_replace(',','%2c',$profile);
274                                         if(strlen($str_tags))
275                                                 $str_tags .= ',';
276                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
277
278                                         // Status.Net seems to require the numeric ID URL in a mention if the person isn't 
279                                         // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both. 
280
281                                         if(strlen($alias)) {
282                                                 if(strlen($str_tags))
283                                                         $str_tags .= ',';
284                                                 $str_tags .= '@[url=' . $alias . ']' . $newname . '[/url]';
285                                         }
286
287                                 }
288                         }
289                 }
290         }
291
292
293
294         $wall = 0;
295         if($post_type === 'wall' || $post_type === 'wall-comment')
296                 $wall = 1;
297
298         if(! strlen($verb))
299                 $verb = ACTIVITY_POST ;
300
301         $gravity = (($parent) ? 6 : 0 );
302  
303         $notify_type = (($parent) ? 'comment-new' : 'wall-new' );
304
305         $uri = item_new_uri($a->get_hostname(),$profile_uid);
306
307         $datarray = array();
308         $datarray['uid']           = $profile_uid;
309         $datarray['type']          = $post_type;
310         $datarray['wall']          = $wall;
311         $datarray['gravity']       = $gravity;
312         $datarray['contact-id']    = $contact_id;
313         $datarray['owner-name']    = $contact_record['name'];
314         $datarray['owner-link']    = $contact_record['url'];
315         $datarray['owner-avatar']  = $contact_record['thumb'];
316         $datarray['author-name']   = $author['name'];
317         $datarray['author-link']   = $author['url'];
318         $datarray['author-avatar'] = $author['thumb'];
319         $datarray['created']       = datetime_convert();
320         $datarray['edited']        = datetime_convert();
321         $datarray['changed']       = datetime_convert();
322         $datarray['uri']           = $uri;
323         $datarray['title']         = $title;
324         $datarray['body']          = $body;
325         $datarray['location']      = $location;
326         $datarray['coord']         = $coord;
327         $datarray['tag']           = $str_tags;
328         $datarray['inform']        = $inform;
329         $datarray['verb']          = $verb;
330         $datarray['allow_cid']     = $str_contact_allow;
331         $datarray['allow_gid']     = $str_group_allow;
332         $datarray['deny_cid']      = $str_contact_deny;
333         $datarray['deny_gid']      = $str_group_deny;
334         $datarray['private']       = $private;
335
336         /**
337          * These fields are for the convenience of plugins...
338          * 'self' if true indicates the owner is posting on their own wall
339          * If parent is 0 it is a top-level post.
340          */
341
342         $datarray['parent']        = $parent;
343         $datarray['self']          = $self;
344
345
346         call_hooks('post_local',$datarray);
347
348         $r = q("INSERT INTO `item` (`uid`,`type`,`wall`,`gravity`,`contact-id`,`owner-name`,`owner-link`,`owner-avatar`, 
349                 `author-name`, `author-link`, `author-avatar`, `created`, `edited`, `changed`, `uri`, `title`, `body`, `location`, `coord`, 
350                 `tag`, `inform`, `verb`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private` )
351                 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 )",
352                 intval($datarray['uid']),
353                 dbesc($datarray['type']),
354                 intval($datarray['wall']),
355                 intval($datarray['gravity']),
356                 intval($datarray['contact-id']),
357                 dbesc($datarray['owner-name']),
358                 dbesc($datarray['owner-link']),
359                 dbesc($datarray['owner-avatar']),
360                 dbesc($datarray['author-name']),
361                 dbesc($datarray['author-link']),
362                 dbesc($datarray['author-avatar']),
363                 dbesc($datarray['created']),
364                 dbesc($datarray['edited']),
365                 dbesc($datarray['changed']),
366                 dbesc($datarray['uri']),
367                 dbesc($datarray['title']),
368                 dbesc($datarray['body']),
369                 dbesc($datarray['location']),
370                 dbesc($datarray['coord']),
371                 dbesc($datarray['tag']),
372                 dbesc($datarray['inform']),
373                 dbesc($datarray['verb']),
374                 dbesc($datarray['allow_cid']),
375                 dbesc($datarray['allow_gid']),
376                 dbesc($datarray['deny_cid']),
377                 dbesc($datarray['deny_gid']),
378                 intval($datarray['private'])
379         );
380
381         $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
382                 dbesc($datarray['uri']));
383         if(count($r)) {
384                 $post_id = $r[0]['id'];
385                 logger('mod_item: saved item ' . $post_id);
386
387                 if($parent) {
388
389                         // This item is the last leaf and gets the comment box, clear any ancestors
390                         $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ",
391                                 dbesc(datetime_convert()),
392                                 intval($parent)
393                         );
394
395                         // Inherit ACL's from the parent item.
396
397                         $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d
398                                 WHERE `id` = %d LIMIT 1",
399                                 dbesc($parent_item['allow_cid']),
400                                 dbesc($parent_item['allow_gid']),
401                                 dbesc($parent_item['deny_cid']),
402                                 dbesc($parent_item['deny_gid']),
403                                 intval($parent_item['private']),
404                                 intval($post_id)
405                         );
406
407                         // Send a notification email to the conversation owner, unless the owner is me and I wrote this item
408                         if(($user['notify-flags'] & NOTIFY_COMMENT) && ($contact_record != $author)) {
409                                 require_once('bbcode.php');
410                                 $from = $author['name'];
411                                 /*
412                                 $tpl = load_view_file('view/cmnt_received_eml.tpl');                    
413                                 $email_tpl = replace_macros($tpl, array(
414                                         '$sitename' => $a->config['sitename'],
415                                         '$siteurl' =>  $a->get_baseurl(),
416                                         '$username' => $user['username'],
417                                         '$email' => $user['email'],
418                                         '$from' => $from,
419                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
420                                         '$body' => strip_tags(bbcode($datarray['body']))
421                                 ));
422
423                                 $res = mail($user['email'], $from . t(" commented on your item at ") . $a->config['sitename'],
424                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );    
425                                 */
426                                 // name of the automated email sender
427                                 $msg['notificationfromname']    = stripslashes($datarray['author-name']);;
428                                 // noreply address to send from
429                                 $msg['notificationfromemail']   = t('noreply') . '@' . $a->get_hostname();                              
430
431                                 // text version
432                                 // process the message body to display properly in text mode
433                                 $msg['textversion']
434                                         = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
435                                 
436                                 // html version
437                                 // process the message body to display properly in text mode
438                                 $msg['htmlversion']     
439                                         = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
440
441                                 // load the template for private message notifications
442                                 $tpl = load_view_file('view/cmnt_received_html_body_eml.tpl');
443                                 $email_html_body_tpl = replace_macros($tpl,array(
444                                         '$sitename'             => $a->config['sitename'],                              // name of this site
445                                         '$siteurl'              => $a->get_baseurl(),                                   // descriptive url of this site
446                                         '$thumb'                => $conv['author-avatar'],                              // thumbnail url for sender icon
447                                         '$email'                => $importer['email'],                                  // email address to send to
448                                         '$url'                  => $conv['author-link'],                                // full url for the site
449                                         '$from'                 => $from,                                                               // name of the person sending the message
450                                         '$body'                 => $msg['htmlversion'],                                 // html version of the message
451                                         '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
452                                 ));
453                         
454                                 // load the template for private message notifications
455                                 $tpl = load_view_file('view/cmnt_received_text_body_eml.tpl');
456                                 $email_text_body_tpl = replace_macros($tpl,array(
457                                         '$sitename'             => $a->config['sitename'],                              // name of this site
458                                         '$siteurl'              => $a->get_baseurl(),                                   // descriptive url of this site
459                                         '$thumb'                => $conv['author-avatar'],                              // thumbnail url for sender icon
460                                         '$email'                => $importer['email'],                                  // email address to send to
461                                         '$url'                  => $conv['author-link'],                                // full url for the site
462                                         '$from'                 => $from,                                                               // name of the person sending the message
463                                         '$body'                 => $msg['textversion'],                                 // text version of the message
464                                         '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
465                                 ));
466
467                                 // use the EmailNotification library to send the message
468                                 require_once("include/EmailNotification.php");
469                                 EmailNotification::sendTextHtmlEmail(
470                                         $msg['notificationfromname'],
471                                         t("Administrator@") . $a->get_hostname(),
472                                         t("noreply") . '@' . $a->get_hostname(),
473                                         $user['email'],
474                                         $from . t(" commented on an item at ") . $a->config['sitename'],
475                                         $email_html_body_tpl,
476                                         $email_text_body_tpl
477                                 );
478                         }
479                 }
480                 else {
481                         $parent = $post_id;
482
483                         // let me know if somebody did a wall-to-wall post on my profile
484
485                         if(($user['notify-flags'] & NOTIFY_WALL) && ($contact_record != $author)) {
486                                 require_once('bbcode.php');
487                                 $from = $author['name'];
488                                 /*
489                                 $tpl = load_view_file('view/wall_received_eml.tpl');                    
490                                 $email_tpl = replace_macros($tpl, array(
491                                         '$sitename' => $a->config['sitename'],
492                                         '$siteurl' =>  $a->get_baseurl(),
493                                         '$username' => $user['username'],
494                                         '$email' => $user['email'],
495                                         '$from' => $from,
496                                         '$display' => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id,
497                                         '$body' => strip_tags(bbcode($datarray['body']))
498                                 ));
499
500                                 $res = mail($user['email'], $from . t(" posted on your profile wall at ") . $a->config['sitename'],
501                                         $email_tpl,t("From: Administrator@") . $a->get_hostname() );
502                                 */
503                                                                                                 
504                                                         
505                                 // name of the automated email sender
506                                 $msg['notificationfromname']    = $from;
507                                 // noreply address to send from
508                                 $msg['notificationfromemail']   = t('noreply') . '@' . $a->get_hostname();                              
509
510                                 // text version
511                                 // process the message body to display properly in text mode
512                                 $msg['textversion']
513                                         = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
514                                 
515                                 // html version
516                                 // process the message body to display properly in text mode
517                                 $msg['htmlversion']     
518                                         = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r","\\n\\n" ,"\\n"), "<br />\n",$datarray['body']))));
519
520                                 // load the template for private message notifications
521                                 $tpl = load_view_file('view/wall_received_html_body_eml.tpl');
522                                 $email_html_body_tpl = replace_macros($tpl,array(
523                                         '$sitename'             => $a->config['sitename'],                              // name of this site
524                                         '$siteurl'              => $a->get_baseurl(),                                   // descriptive url of this site
525                                         '$thumb'                => $conv['author-avatar'],                              // thumbnail url for sender icon
526                                         '$url'                  => $conv['author-link'],                                // full url for the site
527                                         '$from'                 => $from,                                                               // name of the person sending the message
528                                         '$body'                 => $msg['htmlversion'],                                 // html version of the message
529                                         '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
530                                 ));
531                         
532                                 // load the template for private message notifications
533                                 $tpl = load_view_file('view/wall_received_text_body_eml.tpl');
534                                 $email_text_body_tpl = replace_macros($tpl,array(
535                                         '$sitename'             => $a->config['sitename'],                              // name of this site
536                                         '$siteurl'              => $a->get_baseurl(),                                   // descriptive url of this site
537                                         '$thumb'                => $conv['author-avatar'],                              // thumbnail url for sender icon
538                                         '$url'                  => $conv['author-link'],                                // full url for the site
539                                         '$from'                 => $from,                                                               // name of the person sending the message
540                                         '$body'                 => $msg['textversion'],                                 // text version of the message
541                                         '$display'              => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $r,
542                                 ));
543
544                                 // use the EmailNotification library to send the message
545                                 require_once("include/EmailNotification.php");
546                                 EmailNotification::sendTextHtmlEmail(
547                                         $msg['notificationfromname'],
548                                         t("Administrator@") . $a->get_hostname(),
549                                         t("noreply") . '@' . $a->get_hostname(),
550                                         $user['email'],
551                                         $from . t(" commented on an item at ") . $a->config['sitename'],
552                                         $email_html_body_tpl,
553                                         $email_text_body_tpl
554                                 );
555                         }
556                 }
557
558                 $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1
559                         WHERE `id` = %d LIMIT 1",
560                         intval($parent),
561                         dbesc(($parent == $post_id) ? $uri : $parent_item['uri']),
562                         dbesc($a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $post_id),
563                         dbesc(datetime_convert()),
564                         intval($post_id)
565                 );
566
567                 // photo comments turn the corresponding item visible to the profile wall
568                 // This way we don't see every picture in your new photo album posted to your wall at once.
569                 // They will show up as people comment on them.
570
571                 if(! $parent_item['visible']) {
572                         $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d LIMIT 1",
573                                 intval($parent_item['id'])
574                         );
575                 }
576         }
577         else {
578                 logger('mod_item: unable to retrieve post that was just stored.');
579                 notify( t('System error. Post not saved.'));
580                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
581                 // NOTREACHED
582         }
583
584         proc_run('php', "include/notifier.php", $notify_type, "$post_id");
585
586         $datarray['id'] = $post_id;
587
588         call_hooks('post_local_end', $datarray);
589
590         if(strlen($emailcc) && $profile_uid == local_user()) {
591                 $erecips = explode(',', $emailcc);
592                 if(count($erecips)) {
593                         foreach($erecips as $recip) {
594                                 $addr = trim($recip);
595                                 if(! strlen($addr))
596                                         continue;
597                                 $disclaimer = '<hr />' . t('This message was sent to you by ') . $a->user['username'] 
598                                         . t(', a member of the Friendika social network.') . '<br />';
599                                 $disclaimer .= t('You may visit them online at') . ' ' 
600                                         . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '<br />';
601                                 $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . '<br />'; 
602
603                                 $subject  = '[Friendika]' . ' ' . $a->user['username'] . ' ' . t('posted an update.');
604                                 $headers  = 'From: ' . $a->user['username'] . ' <' . $a->user['email'] . '>' . "\n";
605                                 $headers .= 'MIME-Version: 1.0' . "\n";
606                                 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\n";
607                                 $headers .= 'Content-Transfer-Encoding: 8bit' . "\n\n";
608                                 $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
609                                 $html    = prepare_body($datarray);
610                                 $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
611                                 @mail($addr, $subject, $message, $headers);
612                         }
613                 }
614         }
615
616         logger('post_complete');
617         if((x($_POST,'return')) && strlen($_POST['return'])) {
618                 logger('return: ' . $_POST['return']);
619                 goaway($a->get_baseurl() . "/" . $_POST['return'] );
620         }
621         $json = array('success' => 1);
622         if(x($_POST,'jsreload') && strlen($_POST['jsreload']))
623                 $json['reload'] = $a->get_baseurl() . '/' . $_POST['jsreload'];
624
625         logger('post_json: ' . print_r($json,true), LOGGER_DEBUG);
626
627         echo json_encode($json);
628         killme();
629         // NOTREACHED
630 }
631
632
633
634
635
636 function item_content(&$a) {
637
638         if((! local_user()) && (! remote_user()))
639                 return;
640
641         require_once('include/security.php');
642
643         $uid = $_SESSION['uid'];
644
645         if(($a->argc == 3) && ($a->argv[1] === 'drop') && intval($a->argv[2])) {
646
647                 // locate item to be deleted
648
649                 $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
650                         intval($a->argv[2])
651                 );
652
653                 if(! count($r)) {
654                         notice( t('Item not found.') . EOL);
655                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
656                 }
657                 $item = $r[0];
658
659                 // check if logged in user is either the author or owner of this item
660
661                 if(($_SESSION['visitor_id'] == $item['contact-id']) || ($_SESSION['uid'] == $item['uid'])) {
662
663                         // delete the item
664
665                         $r = q("UPDATE `item` SET `deleted` = 1, `body` = '', `edited` = '%s', `changed` = '%s' WHERE `id` = %d LIMIT 1",
666                                 dbesc(datetime_convert()),
667                                 dbesc(datetime_convert()),
668                                 intval($item['id'])
669                         );
670
671                         // If item is a link to a photo resource, nuke all the associated photos 
672                         // (visitors will not have photo resources)
673                         // This only applies to photos uploaded from the photos page. Photos inserted into a post do not
674                         // generate a resource-id and therefore aren't intimately linked to the item. 
675
676                         if(strlen($item['resource-id'])) {
677                                 q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ",
678                                         dbesc($item['resource-id']),
679                                         intval($item['uid'])
680                                 );
681                                 // ignore the result
682                         }
683
684                         // If it's the parent of a comment thread, kill all the kids
685
686                         if($item['uri'] == $item['parent-uri']) {
687                                 $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '' 
688                                         WHERE `parent-uri` = '%s' AND `uid` = %d ",
689                                         dbesc(datetime_convert()),
690                                         dbesc(datetime_convert()),
691                                         dbesc($item['parent-uri']),
692                                         intval($item['uid'])
693                                 );
694                                 // ignore the result
695                         }
696                         else {
697                                 // ensure that last-child is set in case the comment that had it just got wiped.
698                                 q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ",
699                                         dbesc(datetime_convert()),
700                                         dbesc($item['parent-uri']),
701                                         intval($item['uid'])
702                                 );
703                                 // who is the last child now? 
704                                 $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",
705                                         dbesc($item['parent-uri']),
706                                         intval($item['uid'])
707                                 );
708                                 if(count($r)) {
709                                         q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1",
710                                                 intval($r[0]['id'])
711                                         );
712                                 }       
713                         }
714                         $drop_id = intval($item['id']);
715                         
716                         // send the notification upstream/downstream as the case may be
717
718                         proc_run('php',"include/notifier.php","drop","$drop_id");
719
720                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
721                         //NOTREACHED
722                 }
723                 else {
724                         notice( t('Permission denied.') . EOL);
725                         goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
726                         //NOTREACHED
727                 }
728         }
729 }