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