]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Profile.php
Merge pull request #7250 from MrPetovan/bug/6410-normalize-message-button
[friendica.git] / src / Model / Profile.php
index 9d2600b3daab16156c825ff4f458cd352ad1bf6a..f78edb5bd071408b9836e9b45d1eeac422c7d65f 100644 (file)
@@ -18,6 +18,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
+use Friendica\Core\Session;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
@@ -117,7 +118,7 @@ class Profile
 
                if (count($profiledata) > 0) {
                        // Add profile data to sidebar
-                       $a->page['aside'] .= self::sidebar($profiledata, true, $show_connect);
+                       $a->page['aside'] .= self::sidebar($a, $profiledata, true, $show_connect);
 
                        if (!DBA::isResult($user)) {
                                return;
@@ -188,7 +189,7 @@ class Profile
                 * But: When this profile was on the same server, then we could display the contacts
                 */
                if (!$profiledata) {
-                       $a->page['aside'] .= self::sidebar($a->profile, $block, $show_connect);
+                       $a->page['aside'] .= self::sidebar($a, $a->profile, $block, $show_connect);
                }
 
                return;
@@ -280,10 +281,8 @@ class Profile
         * @hooks 'profile_sidebar'
         *      array $arr
         */
-       private static function sidebar($profile, $block = 0, $show_connect = true)
+       private static function sidebar(App $a, $profile, $block = 0, $show_connect = true)
        {
-               $a = \get_app();
-
                $o = '';
                $location = false;
 
@@ -304,127 +303,102 @@ class Profile
 
                Hook::callAll('profile_sidebar_enter', $profile);
 
-
-               // don't show connect link to yourself
-               $connect = $profile['uid'] != local_user() ? L10n::t('Connect') : false;
-
-               // don't show connect link to authenticated visitors either
-               if (remote_user() && !empty($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $visitor) {
-                               if ($visitor['uid'] == $profile['uid']) {
-                                       $connect = false;
-                                       break;
-                               }
-                       }
-               }
-
-               if (!$show_connect) {
-                       $connect = false;
+               if (isset($profile['url'])) {
+                       $profile_url = $profile['url'];
+               } else {
+                       $profile_url = $a->getBaseURL() . '/profile/' . $profile['nickname'];
                }
 
-               $profile_url = '';
+               $follow_link = null;
+               $unfollow_link = null;
+               $subscribe_feed_link = null;
+               $wallmessage_link = null;
 
-               // Is the local user already connected to that user?
-               if ($connect && local_user()) {
-                       if (isset($profile['url'])) {
-                               $profile_url = Strings::normaliseLink($profile['url']);
-                       } else {
-                               $profile_url = Strings::normaliseLink(System::baseUrl() . '/profile/' . $profile['nickname']);
-                       }
 
-                       if (DBA::exists('contact', ['pending' => false, 'uid' => local_user(), 'nurl' => $profile_url])) {
-                               $connect = false;
-                       }
-               }
 
-               // Is the remote user already connected to that user?
-               if ($connect && Contact::isFollower(remote_user(), $profile['uid'])) {
-                       $connect = false;
+               $visitor_contact = [];
+               if ($profile['uid'] && self::getMyURL()) {
+                       $visitor_contact = Contact::selectFirst(['rel'], ['uid' => $profile['uid'], 'nurl' => Strings::normaliseLink(self::getMyURL())]);
                }
 
-               if ($connect && ($profile['network'] != Protocol::DFRN) && !isset($profile['remoteconnect'])) {
-                       $connect = false;
+               $profile_contact = [];
+               if ($profile['cid'] && self::getMyURL()) {
+                       $profile_contact = Contact::selectFirst(['rel'], ['id' => $profile['cid']]);
                }
 
-               $remoteconnect = null;
-               if (isset($profile['remoteconnect'])) {
-                       $remoteconnect = $profile['remoteconnect'];
-               }
+               $profile_is_dfrn = $profile['network'] == Protocol::DFRN;
+               $profile_is_native = in_array($profile['network'], Protocol::NATIVE_SUPPORT);
+               $local_user_is_self = local_user() && local_user() == ($profile['profile_uid'] ?? 0);
+               $visitor_is_authenticated = (bool)self::getMyURL();
+               $visitor_is_following =
+                       in_array($visitor_contact['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND])
+                       || in_array($profile_contact['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND]);
+               $visitor_is_followed =
+                       in_array($visitor_contact['rel'] ?? 0, [Contact::SHARING, Contact::FRIEND])
+                       || in_array($profile_contact['rel'] ?? 0, [Contact::FOLLOWER, Contact::FRIEND]);
+               $visitor_base_path = self::getMyURL() ? preg_replace('=/profile/(.*)=ism', '', self::getMyURL()) : '';
 
-               if ($connect && ($profile['network'] == Protocol::DFRN) && !isset($remoteconnect)) {
-                       $subscribe_feed = L10n::t('Atom feed');
-               } else {
-                       $subscribe_feed = false;
-               }
-
-               $wallmessage = false;
-               $wallmessage_link = false;
-
-               // See issue https://github.com/friendica/friendica/issues/3838
-               // Either we remove the message link for remote users or we enable creating messages from remote users
-               if (remote_user() || (self::getMyURL() && !empty($profile['unkmail']) && ($profile['uid'] != local_user()))) {
-                       $wallmessage = L10n::t('Message');
+               if (!$local_user_is_self) {
+                       if (!$visitor_is_authenticated) {
+                               $follow_link = 'dfrn_request/' . $profile['nickname'];
+                       } elseif ($profile_is_native) {
+                               if ($visitor_is_following) {
+                                       $unfollow_link = $visitor_base_path . '/unfollow?url=' . urlencode($profile_url);
+                               } else {
+                                       $follow_link =  $visitor_base_path .'/follow?url=' . urlencode($profile_url);
+                               }
+                       }
 
-                       if (remote_user()) {
-                               $r = q(
-                                       "SELECT `url` FROM `contact` WHERE `uid` = %d AND `id` = '%s' AND `rel` = %d",
-                                       intval($profile['uid']),
-                                       intval(remote_user()),
-                                       intval(Contact::FRIEND)
-                               );
-                       } else {
-                               $r = q(
-                                       "SELECT `url` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `rel` = %d",
-                                       intval($profile['uid']),
-                                       DBA::escape(Strings::normaliseLink(self::getMyURL())),
-                                       intval(Contact::FRIEND)
-                               );
+                       if ($profile_is_dfrn) {
+                               $subscribe_feed_link = 'dfrn_poll/' . $profile['nickname'];
                        }
-                       if ($r) {
-                               $remote_url = $r[0]['url'];
-                               $message_path = preg_replace('=(.*)/profile/(.*)=ism', '$1/message/new/', $remote_url);
-                               $wallmessage_link = $message_path . base64_encode(defaults($profile, 'addr', ''));
-                       } else if (!empty($profile['nickname'])) {
-                               $wallmessage_link = 'wallmessage/' . $profile['nickname'];
+
+                       if (Contact::canReceivePrivateMessages($profile)) {
+                               if ($visitor_is_followed || $visitor_is_following) {
+                                       $wallmessage_link = $visitor_base_path . '/message/new/' . base64_encode(defaults($profile, 'addr', ''));
+                               } elseif ($visitor_is_authenticated && !empty($profile['unkmail'])) {
+                                       $wallmessage_link = 'wallmessage/' . $profile['nickname'];
+                               }
                        }
                }
 
                // show edit profile to yourself
-               if (!$is_contact && $profile['uid'] == local_user() && Feature::isEnabled(local_user(), 'multi_profiles')) {
-                       $profile['edit'] = [System::baseUrl() . '/profiles', L10n::t('Profiles'), '', L10n::t('Manage/edit profiles')];
-                       $r = q(
-                               "SELECT * FROM `profile` WHERE `uid` = %d",
-                               local_user()
-                       );
+               if (!$is_contact && $local_user_is_self) {
+                       if (Feature::isEnabled(local_user(), 'multi_profiles')) {
+                               $profile['edit'] = [System::baseUrl() . '/profiles', L10n::t('Profiles'), '', L10n::t('Manage/edit profiles')];
+                               $r = q(
+                                       "SELECT * FROM `profile` WHERE `uid` = %d",
+                                       local_user()
+                               );
 
-                       $profile['menu'] = [
-                               'chg_photo' => L10n::t('Change profile photo'),
-                               'cr_new' => L10n::t('Create New Profile'),
-                               'entries' => [],
-                       ];
+                               $profile['menu'] = [
+                                       'chg_photo' => L10n::t('Change profile photo'),
+                                       'cr_new' => L10n::t('Create New Profile'),
+                                       'entries' => [],
+                               ];
 
-                       if (DBA::isResult($r)) {
-                               foreach ($r as $rr) {
-                                       $profile['menu']['entries'][] = [
-                                               'photo' => $rr['thumb'],
-                                               'id' => $rr['id'],
-                                               'alt' => L10n::t('Profile Image'),
-                                               'profile_name' => $rr['profile-name'],
-                                               'isdefault' => $rr['is-default'],
-                                               'visibile_to_everybody' => L10n::t('visible to everybody'),
-                                               'edit_visibility' => L10n::t('Edit visibility'),
-                                       ];
+                               if (DBA::isResult($r)) {
+                                       foreach ($r as $rr) {
+                                               $profile['menu']['entries'][] = [
+                                                       'photo' => $rr['thumb'],
+                                                       'id' => $rr['id'],
+                                                       'alt' => L10n::t('Profile Image'),
+                                                       'profile_name' => $rr['profile-name'],
+                                                       'isdefault' => $rr['is-default'],
+                                                       'visibile_to_everybody' => L10n::t('visible to everybody'),
+                                                       'edit_visibility' => L10n::t('Edit visibility'),
+                                               ];
+                                       }
                                }
+                       } else {
+                               $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
+                               $profile['menu'] = [
+                                       'chg_photo' => L10n::t('Change profile photo'),
+                                       'cr_new' => null,
+                                       'entries' => [],
+                               ];
                        }
                }
-               if (!$is_contact && $profile['uid'] == local_user() && !Feature::isEnabled(local_user(), 'multi_profiles')) {
-                       $profile['edit'] = [System::baseUrl() . '/profiles/' . $profile['id'], L10n::t('Edit profile'), '', L10n::t('Edit profile')];
-                       $profile['menu'] = [
-                               'chg_photo' => L10n::t('Change profile photo'),
-                               'cr_new' => null,
-                               'entries' => [],
-                       ];
-               }
 
                // Fetch the account type
                $account_type = Contact::getAccountType($profile);
@@ -525,10 +499,13 @@ class Profile
                $o .= Renderer::replaceMacros($tpl, [
                        '$profile' => $p,
                        '$xmpp' => $xmpp,
-                       '$connect' => $connect,
-                       '$remoteconnect' => $remoteconnect,
-                       '$subscribe_feed' => $subscribe_feed,
-                       '$wallmessage' => $wallmessage,
+                       '$follow' => L10n::t('Follow'),
+                       '$follow_link' => $follow_link,
+                       '$unfollow' => L10n::t('Unfollow'),
+                       '$unfollow_link' => $unfollow_link,
+                       '$subscribe_feed' => L10n::t('Atom feed'),
+                       '$subscribe_feed_link' => $subscribe_feed_link,
+                       '$wallmessage' => L10n::t('Message'),
                        '$wallmessage_link' => $wallmessage_link,
                        '$account_type' => $account_type,
                        '$location' => $location,
@@ -877,32 +854,35 @@ class Profile
                return '';
        }
 
-       public static function getTabs($a, $is_owner = false, $nickname = null)
+    /**
+     * @param App    $a
+     * @param string $current
+     * @param bool   $is_owner
+     * @param string $nickname
+     * @return string
+     * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+     */
+       public static function getTabs(App $a, string $current, bool $is_owner, string $nickname = null)
        {
                if (is_null($nickname)) {
                        $nickname = $a->user['nickname'];
                }
 
-               $tab = false;
-               if (!empty($_GET['tab'])) {
-                       $tab = Strings::escapeTags(trim($_GET['tab']));
-               }
-
-               $url = System::baseUrl() . '/profile/' . $nickname;
+               $baseProfileUrl = System::baseUrl() . '/profile/' . $nickname;
 
                $tabs = [
                        [
                                'label' => L10n::t('Status'),
-                               'url'   => $url,
-                               'sel'   => !$tab && $a->argv[0] == 'profile' ? 'active' : '',
+                               'url'   => $baseProfileUrl,
+                               'sel'   => !$current ? 'active' : '',
                                'title' => L10n::t('Status Messages and Posts'),
                                'id'    => 'status-tab',
                                'accesskey' => 'm',
                        ],
                        [
                                'label' => L10n::t('Profile'),
-                               'url'   => $url . '/?tab=profile',
-                               'sel'   => $tab == 'profile' ? 'active' : '',
+                               'url'   => $baseProfileUrl . '/?tab=profile',
+                               'sel'   => $current == 'profile' ? 'active' : '',
                                'title' => L10n::t('Profile Details'),
                                'id'    => 'profile-tab',
                                'accesskey' => 'r',
@@ -910,7 +890,7 @@ class Profile
                        [
                                'label' => L10n::t('Photos'),
                                'url'   => System::baseUrl() . '/photos/' . $nickname,
-                               'sel'   => !$tab && $a->argv[0] == 'photos' ? 'active' : '',
+                               'sel'   => $current == 'photos' ? 'active' : '',
                                'title' => L10n::t('Photo Albums'),
                                'id'    => 'photo-tab',
                                'accesskey' => 'h',
@@ -918,7 +898,7 @@ class Profile
                        [
                                'label' => L10n::t('Videos'),
                                'url'   => System::baseUrl() . '/videos/' . $nickname,
-                               'sel'   => !$tab && $a->argv[0] == 'videos' ? 'active' : '',
+                               'sel'   => $current == 'videos' ? 'active' : '',
                                'title' => L10n::t('Videos'),
                                'id'    => 'video-tab',
                                'accesskey' => 'v',
@@ -930,7 +910,7 @@ class Profile
                        $tabs[] = [
                                'label' => L10n::t('Events'),
                                'url'   => System::baseUrl() . '/events',
-                               'sel'   => !$tab && $a->argv[0] == 'events' ? 'active' : '',
+                               'sel'   => $current == 'events' ? 'active' : '',
                                'title' => L10n::t('Events and Calendar'),
                                'id'    => 'events-tab',
                                'accesskey' => 'e',
@@ -941,7 +921,7 @@ class Profile
                        $tabs[] = [
                                'label' => L10n::t('Events'),
                                'url'   => System::baseUrl() . '/cal/' . $nickname,
-                               'sel'   => !$tab && $a->argv[0] == 'cal' ? 'active' : '',
+                               'sel'   => $current == 'cal' ? 'active' : '',
                                'title' => L10n::t('Events and Calendar'),
                                'id'    => 'events-tab',
                                'accesskey' => 'e',
@@ -952,7 +932,7 @@ class Profile
                        $tabs[] = [
                                'label' => L10n::t('Personal Notes'),
                                'url'   => System::baseUrl() . '/notes',
-                               'sel'   => !$tab && $a->argv[0] == 'notes' ? 'active' : '',
+                               'sel'   => $current == 'notes' ? 'active' : '',
                                'title' => L10n::t('Only You Can See This'),
                                'id'    => 'notes-tab',
                                'accesskey' => 't',
@@ -969,18 +949,18 @@ class Profile
                        ];
                }
 
-               if (!$is_owner && empty($a->profile['hide-friends'])) {
+               if ($is_owner || empty($a->profile['hide-friends'])) {
                        $tabs[] = [
                                'label' => L10n::t('Contacts'),
-                               'url'   => System::baseUrl() . '/viewcontacts/' . $nickname,
-                               'sel'   => !$tab && $a->argv[0] == 'viewcontacts' ? 'active' : '',
+                               'url'   => $baseProfileUrl . '/contacts',
+                               'sel'   => $current == 'contacts' ? 'active' : '',
                                'title' => L10n::t('Contacts'),
                                'id'    => 'viewcontacts-tab',
                                'accesskey' => 'k',
                        ];
                }
 
-               $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $tab, 'tabs' => $tabs];
+               $arr = ['is_owner' => $is_owner, 'nickname' => $nickname, 'tab' => $current, 'tabs' => $tabs];
                Hook::callAll('profile_tabs', $arr);
 
                $tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
@@ -995,10 +975,7 @@ class Profile
         */
        public static function getMyURL()
        {
-               if (!empty($_SESSION['my_url'])) {
-                       return $_SESSION['my_url'];
-               }
-               return null;
+               return Session::get('my_url');
        }
 
        /**
@@ -1249,56 +1226,96 @@ class Profile
         */
        public static function searchProfiles($start = 0, $count = 100, $search = null)
        {
-               if ($search) {
-                       $search = DBA::escape($search);
-
-                       $sql_extra = " AND ((`profile`.`name` LIKE '%$search%') OR
-                               (`user`.`nickname` LIKE '%$search%') OR
-                               (`profile`.`pdesc` LIKE '%$search%') OR
-                               (`profile`.`locality` LIKE '%$search%') OR
-                               (`profile`.`region` LIKE '%$search%') OR
-                               (`profile`.`country-name` LIKE '%$search%') OR
-                               (`profile`.`gender` LIKE '%$search%') OR
-                               (`profile`.`marital` LIKE '%$search%') OR
-                               (`profile`.`sexual` LIKE '%$search%') OR
-                               (`profile`.`about` LIKE '%$search%') OR
-                               (`profile`.`romance` LIKE '%$search%') OR
-                               (`profile`.`work` LIKE '%$search%') OR
-                               (`profile`.`education` LIKE '%$search%') OR
-                               (`profile`.`pub_keywords` LIKE '%$search%') OR
-                               (`profile`.`prv_keywords` LIKE '%$search%'))";
-               } else {
-                       $sql_extra = '';
-               }
-
                $publish = (Config::get('system', 'publish_all') ? '' : " AND `publish` = 1 ");
-
                $total = 0;
-               $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
+
+               if (!empty($search)) {
+                       $searchTerm = '%' . $search . '%';
+                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
                                FROM `profile`
                                LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
-                               WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` $sql_extra");
+                               WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
+                               AND ((`profile`.`name` LIKE ?) OR
+                               (`user`.`nickname` LIKE ?) OR
+                               (`profile`.`pdesc` LIKE ?) OR
+                               (`profile`.`locality` LIKE ?) OR
+                               (`profile`.`region` LIKE ?) OR
+                               (`profile`.`country-name` LIKE ?) OR
+                               (`profile`.`gender` LIKE ?) OR
+                               (`profile`.`marital` LIKE ?) OR
+                               (`profile`.`sexual` LIKE ?) OR
+                               (`profile`.`about` LIKE ?) OR
+                               (`profile`.`romance` LIKE ?) OR
+                               (`profile`.`work` LIKE ?) OR
+                               (`profile`.`education` LIKE ?) OR
+                               (`profile`.`pub_keywords` LIKE ?) OR
+                               (`profile`.`prv_keywords` LIKE ?))",
+                               $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
+                               $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm);
+               } else {
+                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
+                               FROM `profile`
+                               LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
+                               WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed`");
+               }
+
                if (DBA::isResult($cnt)) {
                        $total = $cnt['total'];
                }
 
                $order = " ORDER BY `name` ASC ";
-               $limit = $start . ',' . $count;
+               $profiles = [];
 
-               $profiles = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
+               // If nothing found, don't try to select details
+               if ($total > 0) {
+                       if (!empty($search)) {
+                               $searchTerm = '%' . $search . '%';
+
+                               $profiles = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
+                       `contact`.`addr`, `contact`.`url` AS `profile_url`
+                       FROM `profile`
+                       LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
+                       LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
+                       WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
+                       AND ((`profile`.`name` LIKE ?) OR
+                               (`user`.`nickname` LIKE ?) OR
+                               (`profile`.`pdesc` LIKE ?) OR
+                               (`profile`.`locality` LIKE ?) OR
+                               (`profile`.`region` LIKE ?) OR
+                               (`profile`.`country-name` LIKE ?) OR
+                               (`profile`.`gender` LIKE ?) OR
+                               (`profile`.`marital` LIKE ?) OR
+                               (`profile`.`sexual` LIKE ?) OR
+                               (`profile`.`about` LIKE ?) OR
+                               (`profile`.`romance` LIKE ?) OR
+                               (`profile`.`work` LIKE ?) OR
+                               (`profile`.`education` LIKE ?) OR
+                               (`profile`.`pub_keywords` LIKE ?) OR
+                               (`profile`.`prv_keywords` LIKE ?))
+                       $order LIMIT ?,?",
+                                       $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
+                                       $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
+                                       $start, $count
+                               );
+                       } else {
+                               $profiles = DBA::p("SELECT `profile`.*, `profile`.`uid` AS `profile_uid`, `user`.`nickname`, `user`.`timezone` , `user`.`page-flags`,
                        `contact`.`addr`, `contact`.`url` AS `profile_url`
                        FROM `profile`
                        LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
                        LEFT JOIN `contact` ON `contact`.`uid` = `user`.`uid`
                        WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed` AND `contact`.`self`
-                       $sql_extra $order LIMIT $limit"
-               );
+                       $order LIMIT ?,?",
+                                       $start, $count
+                               );
+                       }
+               }
 
-               if (DBA::isResult($profiles)) {
+               if (DBA::isResult($profiles) && $total > 0) {
                        return [
                                'total'   => $total,
                                'entries' => DBA::toArray($profiles),
                        ];
+
                } else {
                        return [
                                'total'   => $total,