]> git.mxchange.org Git - friendica.git/blob - mod/hovercard.php
1c9766d49614c8a69bbe6d76c61e939ee81bf6f9
[friendica.git] / mod / hovercard.php
1 <?php
2
3 /**
4  * Name: Frio Hovercard
5  * Description: Hovercard addon for the frio theme
6  * Version: 0.1
7  * Author: Rabuzarus <https://github.com/rabuzarus>
8  * License: GNU AFFERO GENERAL PUBLIC LICENSE (Version 3)
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Config;
13 use Friendica\Core\Renderer;
14 use Friendica\Core\System;
15 use Friendica\Database\DBA;
16 use Friendica\Model\Contact;
17 use Friendica\Model\GContact;
18 use Friendica\Util\Proxy as ProxyUtils;
19
20 function hovercard_init(App $a)
21 {
22         // Just for testing purposes
23         $_GET['mode'] = 'minimal';
24 }
25
26 function hovercard_content()
27 {
28         $profileurl = defaults($_REQUEST, 'profileurl', '');
29         $datatype   = defaults($_REQUEST, 'datatype'  , 'json');
30
31         // Get out if the system doesn't have public access allowed
32         if (intval(Config::get('system', 'block_public'))) {
33                 System::httpExit(401);
34         }
35
36         // Return the raw content of the template. We use this to make templates usable for js functions.
37         // Look at hovercard.js (function getHoverCardTemplate()).
38         // This part should be moved in its own module. Maybe we could make more templates accessible.
39         // (We need to discuss possible security leaks before doing this)
40         if ($datatype == 'tpl') {
41                 $templatecontent = get_template_content('hovercard.tpl');
42                 echo $templatecontent;
43                 killme();
44         }
45
46         // If a contact is connected the url is internally changed to 'redir/CID'. We need the pure url to search for
47         // the contact. So we strip out the contact id from the internal url and look in the contact table for
48         // the real url (nurl)
49         $cid = 0;
50         if (strpos($profileurl, 'redir/') === 0) {
51                 $cid = intval(substr($profileurl, 6));
52                 $remote_contact = DBA::selectFirst('contact', ['nurl'], ['id' => $cid]);
53                 $profileurl = defaults($remote_contact, 'nurl', '');
54         }
55
56         $contact = [];
57         // if it's the url containing https it should be converted to http
58         $nurl = normalise_link(GContact::cleanContactUrl($profileurl));
59         if (!$nurl) {
60                 return;
61         }
62
63         // Search for contact data
64         // Look if the local user has got the contact
65         if (local_user()) {
66                 $contact = Contact::getDetailsByURL($nurl, local_user());
67         }
68
69         // If not then check the global user
70         if (!count($contact)) {
71                 $contact = Contact::getDetailsByURL($nurl);
72         }
73
74         // Feeds url could have been destroyed through "cleanContactUrl", so we now use the original url
75         if (!count($contact) && local_user()) {
76                 $nurl = normalise_link($profileurl);
77                 $contact = Contact::getDetailsByURL($nurl, local_user());
78         }
79
80         if (!count($contact)) {
81                 $nurl = normalise_link($profileurl);
82                 $contact = Contact::getDetailsByURL($nurl);
83         }
84
85         if (!count($contact)) {
86                 return;
87         }
88
89         // Get the photo_menu - the menu if possible contact actions
90         if (local_user()) {
91                 $actions = Contact::photoMenu($contact);
92         } else {
93                 $actions = [];
94         }
95
96         // Move the contact data to the profile array so we can deliver it to
97         $profile = [
98                 'name'     => $contact['name'],
99                 'nick'     => $contact['nick'],
100                 'addr'     => defaults($contact, 'addr', $contact['url']),
101                 'thumb'    => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
102                 'url'      => Contact::magicLink($contact['url']),
103                 'nurl'     => $contact['nurl'], // We additionally store the nurl as identifier
104                 'location' => $contact['location'],
105                 'gender'   => $contact['gender'],
106                 'about'    => $contact['about'],
107                 'network'  => format_network_name($contact['network'], $contact['url']),
108                 'tags'     => $contact['keywords'],
109                 'bd'       => $contact['birthday'] <= '0001-01-01' ? '' : $contact['birthday'],
110                 'account_type' => Contact::getAccountType($contact),
111                 'actions'  => $actions,
112         ];
113         if ($datatype == 'html') {
114                 $tpl = get_markup_template('hovercard.tpl');
115                 $o = Renderer::replaceMacros($tpl, [
116                         '$profile' => $profile,
117                 ]);
118
119                 return $o;
120         } else {
121                 System::jsonExit($profile);
122         }
123 }
124
125 /**
126  * @brief Get the raw content of a template file
127  *
128  * @param string $template The name of the template
129  * @param string $root Directory of the template
130  *
131  * @return string|bool Output the raw content if existent, otherwise false
132  */
133 function get_template_content($template, $root = '')
134 {
135         // We load the whole template system to get the filename.
136         // Maybe we can do it a little bit smarter if I get time.
137         $t = get_markup_template($template, $root);
138         $filename = $t->filename;
139
140         // Get the content of the template file
141         if (file_exists($filename)) {
142                 $content = file_get_contents($filename);
143
144                 return $content;
145         }
146
147         return false;
148 }