]> git.mxchange.org Git - friendica.git/blobdiff - mod/message.php
Merge pull request #3344 from tobiasd/20170412-doc
[friendica.git] / mod / message.php
index b6981cc88c6fbbadc458150fb22d8eeb33e86171..d3bd1558cc8d5f6116755677be3e0a54250e7180 100644 (file)
@@ -2,8 +2,9 @@
 
 require_once('include/acl_selectors.php');
 require_once('include/message.php');
+require_once('include/Smilies.php');
 
-function message_init(&$a) {
+function message_init(App $a) {
 
        $tabs = '';
 
@@ -23,25 +24,25 @@ function message_init(&$a) {
                '$tabs'=>$tabs,
                '$new'=>$new,
        ));
-       $base = $a->get_baseurl();
+       $base = App::get_baseurl();
 
        $head_tpl = get_markup_template('message-head.tpl');
        $a->page['htmlhead'] .= replace_macros($head_tpl,array(
-               '$baseurl' => $a->get_baseurl(true),
+               '$baseurl' => App::get_baseurl(true),
                '$base' => $base
        ));
 
        $end_tpl = get_markup_template('message-end.tpl');
        $a->page['end'] .= replace_macros($end_tpl,array(
-               '$baseurl' => $a->get_baseurl(true),
+               '$baseurl' => App::get_baseurl(true),
                '$base' => $base
        ));
 
 }
 
-function message_post(&$a) {
+function message_post(App $a) {
 
-       if(! local_user()) {
+       if (! local_user()) {
                notice( t('Permission denied.') . EOL);
                return;
        }
@@ -51,17 +52,6 @@ function message_post(&$a) {
        $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
        $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
 
-       // Work around doubled linefeeds in Tinymce 3.5b2
-
-/*     $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
-       if(! $plaintext) {
-               $body = fix_mce_lf($body);
-       }*/
-       $plaintext = intval(!feature_enabled(local_user(),'richtext'));
-       if(! $plaintext) {
-               $body = fix_mce_lf($body);
-       }
-
        $ret = send_message($recipient, $body, $subject, $replyto);
        $norecip = false;
 
@@ -159,7 +149,7 @@ function item_redir_and_replace_images($body, $images, $cid) {
        $newbody = $newbody . $origbody;
 
        $cnt = 0;
-       foreach($images as $image) {
+       foreach ($images as $image) {
                // We're depending on the property of 'foreach' (specified on the PHP website) that
                // it loops over the array starting from the first element and going sequentially
                // to the last element
@@ -172,17 +162,17 @@ function item_redir_and_replace_images($body, $images, $cid) {
 
 
 
-function message_content(&$a) {
+function message_content(App $a) {
 
        $o = '';
        nav_set_selected('messages');
 
-       if(! local_user()) {
+       if (! local_user()) {
                notice( t('Permission denied.') . EOL);
                return;
        }
 
-       $myprofile = 'profile/' . $a->user['nickname'];
+       $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
 
        $tpl = get_markup_template('mail_head.tpl');
        $header = replace_macros($tpl, array(
@@ -230,10 +220,10 @@ function message_content(&$a) {
                                intval($a->argv[2]),
                                intval(local_user())
                        );
-                       if($r) {
+                       if ($r) {
                                info( t('Message deleted.') . EOL );
                        }
-                       //goaway($a->get_baseurl(true) . '/message' );
+                       //goaway(App::get_baseurl(true) . '/message' );
                        goaway($_SESSION['return_url']);
                }
                else {
@@ -241,7 +231,7 @@ function message_content(&$a) {
                                intval($a->argv[2]),
                                intval(local_user())
                        );
-                       if(count($r)) {
+                       if (dbm::is_result($r)) {
                                $parent = $r[0]['parent-uri'];
                                $convid = $r[0]['convid'];
 
@@ -264,7 +254,7 @@ function message_content(&$a) {
                                if($r)
                                        info( t('Conversation removed.') . EOL );
                        }
-                       //goaway($a->get_baseurl(true) . '/message' );
+                       //goaway(App::get_baseurl(true) . '/message' );
                        goaway($_SESSION['return_url']);
                }
 
@@ -274,26 +264,16 @@ function message_content(&$a) {
 
                $o .= $header;
 
-/*             $plaintext = false;
-               if(intval(get_pconfig(local_user(),'system','plaintext')))
-                       $plaintext = true;*/
-               $plaintext = true;
-               if( local_user() && feature_enabled(local_user(),'richtext') )
-                       $plaintext = false;
-
-
                $tpl = get_markup_template('msg-header.tpl');
                $a->page['htmlhead'] .= replace_macros($tpl, array(
-                       '$baseurl' => $a->get_baseurl(true),
-                       '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
+                       '$baseurl' => App::get_baseurl(true),
                        '$nickname' => $a->user['nickname'],
                        '$linkurl' => t('Please enter a link URL:')
                ));
 
                $tpl = get_markup_template('msg-end.tpl');
                $a->page['end'] .= replace_macros($tpl, array(
-                       '$baseurl' => $a->get_baseurl(true),
-                       '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
+                       '$baseurl' => App::get_baseurl(true),
                        '$nickname' => $a->user['nickname'],
                        '$linkurl' => t('Please enter a link URL:')
                ));
@@ -304,15 +284,31 @@ function message_content(&$a) {
                $prename = $preurl = $preid = '';
 
                if($preselect) {
-                       $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
+                       $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
                                intval(local_user()),
                                intval($a->argv[2])
                        );
-                       if(count($r)) {
+                       if (!dbm::is_result($r)) {
+                               $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
+                                       intval(local_user()),
+                                       dbesc(normalise_link(base64_decode($a->argv[2])))
+                               );
+                       }
+
+                       if (!dbm::is_result($r)) {
+                               $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
+                                       intval(local_user()),
+                                       dbesc(base64_decode($a->argv[2]))
+                               );
+                       }
+
+                       if (dbm::is_result($r)) {
                                $prename = $r[0]['name'];
                                $preurl = $r[0]['url'];
                                $preid = $r[0]['id'];
-                       }
+                               $preselect = array($preid);
+                       } else
+                               $preselect = false;
                }
 
                $prefill = (($preselect) ? $prename  : '');
@@ -341,32 +337,30 @@ function message_content(&$a) {
                        '$wait' => t('Please wait'),
                        '$submit' => t('Submit')
                ));
-
                return $o;
        }
 
 
        $_SESSION['return_url'] = $a->query_string;
 
-       if($a->argc == 1) {
+       if ($a->argc == 1) {
 
                // List messages
 
                $o .= $header;
 
-               $r = q("SELECT count(*) AS `total` FROM `mail`
+               $r = q("SELECT count(*) AS `total` FROM `mail`, ANY_VALUE(`created`) AS `created`
                        WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
-                       intval(local_user()),
-                       dbesc($myprofile)
+                       intval(local_user())
                );
 
-               if (dba::is_result($r)) {
+               if (dbm::is_result($r)) {
                        $a->set_pager_total($r[0]['total']);
                }
 
                $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
 
-               if(! dba::is_result($r)) {
+               if (! dbm::is_result($r)) {
                        info( t('No messages.') . EOL);
                        return $o;
                }
@@ -382,17 +376,13 @@ function message_content(&$a) {
 
                $o .= $header;
 
-               $plaintext = true;
-               if( local_user() && feature_enabled(local_user(),'richtext') )
-                       $plaintext = false;
-
                $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
                        FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
                        WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
                        intval(local_user()),
                        intval($a->argv[1])
                );
-               if(count($r)) {
+               if (dbm::is_result($r)) {
                        $contact_id = $r[0]['contact-id'];
                        $convid = $r[0]['convid'];
 
@@ -423,21 +413,18 @@ function message_content(&$a) {
 
                $tpl = get_markup_template('msg-header.tpl');
                $a->page['htmlhead'] .= replace_macros($tpl, array(
-                       '$baseurl' => $a->get_baseurl(true),
-                       '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
+                       '$baseurl' => App::get_baseurl(true),
                        '$nickname' => $a->user['nickname'],
                        '$linkurl' => t('Please enter a link URL:')
                ));
 
                $tpl = get_markup_template('msg-end.tpl');
                $a->page['end'] .= replace_macros($tpl, array(
-                       '$baseurl' => $a->get_baseurl(true),
-                       '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
+                       '$baseurl' => App::get_baseurl(true),
                        '$nickname' => $a->user['nickname'],
                        '$linkurl' => t('Please enter a link URL:')
                ));
 
-
                $mails = array();
                $seen = 0;
                $unknown = false;
@@ -448,9 +435,11 @@ function message_content(&$a) {
                        if($message['from-url'] == $myprofile) {
                                $from_url = $myprofile;
                                $sparkle = '';
-                       }
-                       else {
-                               $from_url = 'redir/' . $message['contact-id'];
+                       } elseif ($message['contact-id'] != 0) {
+                               $from_url = 'redir/'.$message['contact-id'];
+                               $sparkle = ' sparkle';
+                       } else {
+                               $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
                                $sparkle = ' sparkle';
                        }
 
@@ -462,22 +451,27 @@ function message_content(&$a) {
                        if($a->theme['template_engine'] === 'internal') {
                                $from_name_e = template_escape($message['from-name']);
                                $subject_e = template_escape($message['title']);
-                               $body_e = template_escape(smilies(bbcode($message['body'])));
+                               $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
                                $to_name_e = template_escape($message['name']);
-                       }
-                       else {
+                       } else {
                                $from_name_e = $message['from-name'];
                                $subject_e = $message['title'];
-                               $body_e = smilies(bbcode($message['body']));
+                               $body_e = Smilies::replace(bbcode($message['body']));
                                $to_name_e = $message['name'];
                        }
 
+                       $contact = get_contact_details_by_url($message['from-url']);
+                       if (isset($contact["thumb"]))
+                               $from_photo = $contact["thumb"];
+                       else
+                               $from_photo = $message['from-photo'];
+
                        $mails[] = array(
                                'id' => $message['id'],
                                'from_name' => $from_name_e,
                                'from_url' => $from_url,
                                'sparkle' => $sparkle,
-                               'from_photo' => $message['from-photo'],
+                               'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
                                'subject' => $subject_e,
                                'body' => $body_e,
                                'delete' => t('Delete message'),
@@ -534,9 +528,14 @@ function message_content(&$a) {
 }
 
 function get_messages($user, $lstart, $lend) {
-
+       //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
        return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
-               `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
+               ANY_VALUE(`mail`.`id`), ANY_VALUE(`mail`.`uid`), ANY_VALUE(`mail`.`guid`), ANY_VALUE(`mail`.`from-name`),
+               ANY_VALUE(`mail`.`from-photo`), ANY_VALUE(`mail`.`from-url`), ANY_VALUE(`mail`.`contact-id`),
+               ANY_VALUE(`mail`.`convid`), ANY_VALUE(`mail`.`title`), ANY_VALUE(`mail`.`body`), ANY_VALUE(`mail`.`seen`),
+               ANY_VALUE(`mail`.`reply`), ANY_VALUE(`mail`.`replied`), ANY_VALUE(`mail`.`unknown`),
+               ANY_VALUE(`mail`.`uri`), ANY_VALUE(`mail`.`parent-uri`), ANY_VALUE(`mail`.`created`),
+               ANY_VALUE(`contact`.`name`), ANY_VALUE(`contact`.`url`), ANY_VALUE(`contact`.`thumb`), ANY_VALUE(`contact`.`network`),
                count( * ) as count
                FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
                WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
@@ -551,19 +550,16 @@ function render_messages(array $msg, $t) {
        $tpl = get_markup_template($t);
        $rslt = '';
 
-       $myprofile = 'profile/' . $a->user['nickname'];
+       $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
 
        foreach($msg as $rr) {
 
-               if($rr['unknown']) {
+               if($rr['unknown'])
                        $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
-               }
-               elseif (link_compare($rr['from-url'], $myprofile)){
+               elseif (link_compare($rr['from-url'], $myprofile))
                        $participants = sprintf( t("You and %s"), $rr['name']);
-               }
-               else {
-                       $participants = sprintf( t("%s and You"), $rr['from-name']);
-               }
+               else
+                       $participants = sprintf(t("%s and You"), $rr['from-name']);
 
                if($a->theme['template_engine'] === 'internal') {
                        $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
@@ -576,12 +572,18 @@ function render_messages(array $msg, $t) {
                        $to_name_e = $rr['name'];
                }
 
+               $contact = get_contact_details_by_url($rr['url']);
+               if (isset($contact["thumb"]))
+                       $from_photo = $contact["thumb"];
+               else
+                       $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
+
                $rslt .= replace_macros($tpl, array(
                        '$id' => $rr['id'],
                        '$from_name' => $participants,
                        '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
                        '$sparkle' => ' sparkle',
-                       '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
+                       '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
                        '$subject' => $subject_e,
                        '$delete' => t('Delete conversation'),
                        '$body' => $body_e,