]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Hovercard.php
Issue 13052: The limit parameter now behaves like the Mastodon counterpart
[friendica.git] / src / Module / Contact / Hovercard.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Contact;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Core\System;
31 use Friendica\Database\DBA;
32 use Friendica\Model\Contact;
33 use Friendica\Module\Response;
34 use Friendica\Network\HTTPException;
35 use Friendica\Util\Profiler;
36 use Friendica\Util\Strings;
37 use Psr\Log\LoggerInterface;
38
39 /**
40  * Asynchronous HTML fragment provider for frio contact hovercards
41  */
42 class Hovercard extends BaseModule
43 {
44         /** @var IManageConfigValues */
45         private $config;
46         /** @var IHandleUserSessions */
47         private $userSession;
48
49         public function __construct(IHandleUserSessions $userSession, IManageConfigValues $config, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
50         {
51                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52
53                 $this->config      = $config;
54                 $this->userSession = $userSession;
55         }
56
57         protected function rawContent(array $request = [])
58         {
59                 $contact_url = $request['url'] ?? '';
60
61                 // Get out if the system doesn't have public access allowed
62                 if ($this->config->get('system', 'block_public') && !$this->userSession->isAuthenticated()) {
63                         throw new HTTPException\ForbiddenException();
64                 }
65
66                 /* Possible formats for relative URLs that need to be converted to the absolute contact URL:
67                  * - contact/redir/123456
68                  * - contact/123456/conversations
69                  */
70                 if (strpos($contact_url, 'contact/') === 0 && preg_match('/(\d+)/', $contact_url, $matches)) {
71                         $remote_contact = Contact::selectFirst(['nurl'], ['id' => $matches[1]]);
72                         $contact_url    = $remote_contact['nurl'] ?? '';
73                 }
74
75                 if (!$contact_url) {
76                         throw new HTTPException\BadRequestException();
77                 }
78
79                 // Search for contact data
80                 // Look if the local user has got the contact
81                 if ($this->userSession->isAuthenticated()) {
82                         $contact = Contact::getByURLForUser($contact_url, $this->userSession->getLocalUserId());
83                 } else {
84                         $contact = Contact::getByURL($contact_url, false);
85                 }
86
87                 if (!count($contact)) {
88                         throw new HTTPException\NotFoundException();
89                 }
90
91                 // Get the photo_menu - the menu if possible contact actions
92                 if ($this->userSession->isAuthenticated()) {
93                         $actions = Contact::photoMenu($contact, $this->userSession->getLocalUserId());
94                 } else {
95                         $actions = [];
96                 }
97
98                 // Move the contact data to the profile array so we can deliver it to
99                 $tpl = Renderer::getMarkupTemplate('hovercard.tpl');
100                 $o   = Renderer::replaceMacros($tpl, [
101                         '$profile' => [
102                                 'name'         => $contact['name'],
103                                 'nick'         => $contact['nick'],
104                                 'addr'         => $contact['addr'] ?: $contact['url'],
105                                 'thumb'        => Contact::getThumb($contact),
106                                 'url'          => Contact::magicLinkByContact($contact),
107                                 'nurl'         => $contact['nurl'],
108                                 'location'     => $contact['location'],
109                                 'about'        => $contact['about'],
110                                 'network_link' => Strings::formatNetworkName($contact['network'], $contact['url']),
111                                 'tags'         => $contact['keywords'],
112                                 'bd'           => $contact['bd'] <= DBA::NULL_DATE ? '' : $contact['bd'],
113                                 'account_type' => Contact::getAccountType($contact['contact-type']),
114                                 'actions'      => $actions,
115                         ],
116                 ]);
117
118                 System::httpExit($o);
119         }
120 }