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