3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Contact;
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;
40 * Asynchronous HTML fragment provider for frio contact hovercards
42 class Hovercard extends BaseModule
44 /** @var IManageConfigValues */
46 /** @var IHandleUserSessions */
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 = [])
51 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
53 $this->config = $config;
54 $this->userSession = $userSession;
57 protected function rawContent(array $request = [])
59 $contact_url = $request['url'] ?? '';
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();
66 /* Possible formats for relative URLs that need to be converted to the absolute contact URL:
67 * - contact/redir/123456
68 * - contact/123456/conversations
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'] ?? '';
76 throw new HTTPException\BadRequestException();
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());
84 $contact = Contact::getByURL($contact_url, false);
87 if (!count($contact)) {
88 throw new HTTPException\NotFoundException();
91 // Get the photo_menu - the menu if possible contact actions
92 if ($this->userSession->isAuthenticated()) {
93 $actions = Contact::photoMenu($contact, $this->userSession->getLocalUserId());
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, [
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,
118 System::httpExit($o);