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