]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Profile.php
Replace System::httpExit() by HTTPException throwing
[friendica.git] / src / Model / Profile.php
index b5e54474b819e3c9daca9d7c8f9f178052a80d7b..13156a93285531786bcfad5488f93e153226b674 100644 (file)
@@ -9,6 +9,7 @@ use Friendica\Content\Feature;
 use Friendica\Content\ForumManager;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
+use Friendica\Content\Widget\ContactBlock;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\Hook;
@@ -111,8 +112,6 @@ class Profile
 
                if (!DBA::isResult($user) && empty($profiledata)) {
                        Logger::log('profile error: ' . $a->query_string, Logger::DEBUG);
-                       notice(L10n::t('Requested account is not available.') . EOL);
-                       $a->error = 404;
                        return;
                }
 
@@ -129,8 +128,6 @@ class Profile
 
                if (empty($pdata) && empty($profiledata)) {
                        Logger::log('profile error: ' . $a->query_string, Logger::DEBUG);
-                       notice(L10n::t('Requested profile is not available.') . EOL);
-                       $a->error = 404;
                        return;
                }
 
@@ -475,9 +472,9 @@ class Profile
 
                $contact_block = '';
                $updated = '';
-               $contacts = 0;
+               $contact_count = 0;
                if (!$block) {
-                       $contact_block = HTML::contactBlock();
+                       $contact_block = ContactBlock::getHTML($a->profile);
 
                        if (is_array($a->profile) && !$a->profile['hide-friends']) {
                                $r = q(
@@ -488,20 +485,15 @@ class Profile
                                        $updated = date('c', strtotime($r[0]['updated']));
                                }
 
-                               $r = q(
-                                       "SELECT COUNT(*) AS `total` FROM `contact`
-                                       WHERE `uid` = %d
-                                               AND NOT `self` AND NOT `blocked` AND NOT `pending`
-                                               AND NOT `hidden` AND NOT `archive`
-                                               AND `network` IN ('%s', '%s', '%s', '')",
-                                       intval($profile['uid']),
-                                       DBA::escape(Protocol::DFRN),
-                                       DBA::escape(Protocol::DIASPORA),
-                                       DBA::escape(Protocol::OSTATUS)
-                               );
-                               if (DBA::isResult($r)) {
-                                       $contacts = intval($r[0]['total']);
-                               }
+                               $contact_count = DBA::count('contact', [
+                                       'uid' => $profile['uid'],
+                                       'self' => false,
+                                       'blocked' => false,
+                                       'pending' => false,
+                                       'hidden' => false,
+                                       'archive' => false,
+                                       'network' => [Protocol::DFRN, Protocol::ACTIVITYPUB, Protocol::OSTATUS, Protocol::DIASPORA],
+                               ]);
                        }
                }
 
@@ -545,7 +537,7 @@ class Profile
                        '$homepage' => $homepage,
                        '$about' => $about,
                        '$network' => L10n::t('Network:'),
-                       '$contacts' => $contacts,
+                       '$contacts' => $contact_count,
                        '$updated' => $updated,
                        '$diaspora' => $diaspora,
                        '$contact_block' => $contact_block,
@@ -791,7 +783,7 @@ class Profile
                                $profile['marital']['with'] = $a->profile['with'];
                        }
 
-                       if (strlen($a->profile['howlong']) && $a->profile['howlong'] >= DBA::NULL_DATETIME) {
+                       if (strlen($a->profile['howlong']) && $a->profile['howlong'] > DBA::NULL_DATETIME) {
                                $profile['howlong'] = Temporal::getRelativeDate($a->profile['howlong'], L10n::t('for %1$d %2$s'));
                        }
 
@@ -1087,34 +1079,20 @@ class Profile
        }
 
        /**
-        * OpenWebAuth authentication.
-        *
-        * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/zid.php
+        * Set the visitor cookies (see remote_user()) for the given handle
         *
-        * @param string $token
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        * @throws \ImagickException
+        * @param string $handle Visitor handle
+        * @return array Visitor contact array
         */
-       public static function openWebAuthInit($token)
+       public static function addVisitorCookieForHandle($handle)
        {
                $a = \get_app();
 
-               // Clean old OpenWebAuthToken entries.
-               OpenWebAuthToken::purge('owt', '3 MINUTE');
-
-               // Check if the token we got is the same one
-               // we have stored in the database.
-               $visitor_handle = OpenWebAuthToken::getMeta('owt', 0, $token);
-
-               if($visitor_handle === false) {
-                       return;
-               }
-
                // Try to find the public contact entry of the visitor.
-               $cid = Contact::getIdForURL($visitor_handle);
-               if(!$cid) {
-                       Logger::log('owt: unable to finger ' . $visitor_handle, Logger::DEBUG);
-                       return;
+               $cid = Contact::getIdForURL($handle);
+               if (!$cid) {
+                       Logger::log('unable to finger ' . $handle, Logger::DEBUG);
+                       return [];
                }
 
                $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
@@ -1137,6 +1115,43 @@ class Profile
 
                        $_SESSION['remote'][] = ['cid' => $contact['id'], 'uid' => $contact['uid'], 'url' => $visitor['url']];
                }
+
+               $a->contact = $visitor;
+
+               Logger::info('Authenticated visitor', ['url' => $visitor['url']]);
+
+               return $visitor;
+       }
+
+       /**
+        * OpenWebAuth authentication.
+        *
+        * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/zid.php
+        *
+        * @param string $token
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
+        */
+       public static function openWebAuthInit($token)
+       {
+               $a = \get_app();
+
+               // Clean old OpenWebAuthToken entries.
+               OpenWebAuthToken::purge('owt', '3 MINUTE');
+
+               // Check if the token we got is the same one
+               // we have stored in the database.
+               $visitor_handle = OpenWebAuthToken::getMeta('owt', 0, $token);
+
+               if ($visitor_handle === false) {
+                       return;
+               }
+
+               $visitor = self::addVisitorCookieForHandle($visitor_handle);
+               if (empty($visitor)) {
+                       return;
+               }
+
                $arr = [
                        'visitor' => $visitor,
                        'url' => $a->query_string