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