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