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