]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Hovercard.php
Merge pull request #11531 from annando/display-polls
[friendica.git] / src / Module / Contact / Hovercard.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
27 use Friendica\Core\System;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Network\HTTPException;
32 use Friendica\Util\Strings;
33
34 /**
35  * Asynchronous HTML fragment provider for frio contact hovercards
36  */
37 class Hovercard extends BaseModule
38 {
39         protected function rawContent(array $request = [])
40         {
41                 $contact_url = $_REQUEST['url'] ?? '';
42
43                 // Get out if the system doesn't have public access allowed
44                 if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
45                         throw new HTTPException\ForbiddenException();
46                 }
47
48                 // If a contact is connected the url is internally changed to 'redir/CID'. We need the pure url to search for
49                 // the contact. So we strip out the contact id from the internal url and look in the contact table for
50                 // the real url (nurl)
51                 if (strpos($contact_url, 'redir/') === 0) {
52                         $cid = intval(substr($contact_url, 6));
53                 }
54
55                 if (strpos($contact_url, 'contact/') === 0) {
56                         $cid = intval(substr($contact_url, 8));
57                 }
58
59                 if (!empty($cid)) {                     
60                         $remote_contact = Contact::selectFirst(['nurl'], ['id' => $cid]);
61                         $contact_url = $remote_contact['nurl'] ?? '';
62                 }
63
64                 $contact = [];
65
66                 // if it's the url containing https it should be converted to http
67                 if (!$contact_url) {
68                         throw new HTTPException\BadRequestException();
69                 }
70
71                 // Search for contact data
72                 // Look if the local user has got the contact
73                 if (Session::isAuthenticated()) {
74                         $contact = Contact::getByURLForUser($contact_url, local_user());
75                 } else {
76                         $contact = Contact::getByURL($contact_url, false);
77                 }
78
79                 if (!count($contact)) {
80                         throw new HTTPException\NotFoundException();
81                 }
82
83                 // Get the photo_menu - the menu if possible contact actions
84                 if (Session::isAuthenticated()) {
85                         $actions = Contact::photoMenu($contact);
86                 } else {
87                         $actions = [];
88                 }
89
90                 // Move the contact data to the profile array so we can deliver it to
91                 $tpl = Renderer::getMarkupTemplate('hovercard.tpl');
92                 $o = Renderer::replaceMacros($tpl, [
93                         '$profile' => [
94                                 'name'         => $contact['name'],
95                                 'nick'         => $contact['nick'],
96                                 'addr'         => $contact['addr'] ?: $contact['url'],
97                                 'thumb'        => Contact::getThumb($contact),
98                                 'url'          => Contact::magicLinkByContact($contact),
99                                 'nurl'         => $contact['nurl'],
100                                 'location'     => $contact['location'],
101                                 'about'        => $contact['about'],
102                                 'network_link' => Strings::formatNetworkName($contact['network'], $contact['url']),
103                                 'tags'         => $contact['keywords'],
104                                 'bd'           => $contact['bd'] <= DBA::NULL_DATE ? '' : $contact['bd'],
105                                 'account_type' => Contact::getAccountType($contact['contact-type']),
106                                 'actions'      => $actions,
107                         ],
108                 ]);
109
110                 echo $o;
111                 System::exit();
112         }
113 }