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