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