]> git.mxchange.org Git - friendica-addons.git/blob - frio_hovercard.php
hovercard: output template as clean text
[friendica-addons.git] / frio_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 require_once("include/socgraph.php");
12 require_once("include/Contact.php");
13
14 function frio_hovercard_install() {
15         logger("installed frio-hovercard");
16 }
17
18 function frio_hovercard_uninstall() {
19         logger("uninstalled frio-hovercard");
20 }
21 function frio_hovercard_module() {
22         return;
23 }
24 function frio_hovercard_content() {
25         $profileurl     =       (x($_REQUEST,'profileurl')      ? $_REQUEST['profileurl']       : "");
26         $datatype       =       (x($_REQUEST,'datatype')        ?$_REQUEST['datatype']          : "json");
27
28         // Get out if the system doesn't have public access allowed
29         if(intval(get_config('system','block_public')))
30                 http_status_exit(401);
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 it's own module. Maybe we could make more templates accessabel.
35         // (We need to discuss possible security lacks before doing this)
36         if ($datatype == "tpl") {
37                 $templatecontent = get_template_content("hovercard.tpl", "addon/frio_hovercard/");
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         if(local_user() && strpos($profileurl, "redir/") === 0) {
46                 $cid = intval(substr($profileurl, 6));
47                 $r = q("SELECT `nurl`, `self`  FROM `contact` WHERE `id` = '%d' LIMIT 1", intval($cid));
48                 $profileurl = ($r[0]["nurl"] ? $r[0]["nurl"] : "");
49                 $self = ($r[0]["self"] ? $r[0]["self"] : "");
50         }
51
52         // if it's the url containing https it should be converted to http
53         $nurl = normalise_link(clean_contact_url($profileurl));
54         if($nurl) {
55                 // Search for contact data
56                 $contact = get_contact_details_by_url($nurl);
57
58                 // Get_contact_details_by_url() doesn't provide the nurl but we 
59                 // need it for the photo_menu, so we copy it to the contact array
60                 if (!isset($contact["nurl"]))
61                         $contact["nurl"] = $nurl;
62         }
63
64         if(!is_array($contact))
65                 return;
66
67         // Get the photo_menu - the menu if possible contact actions
68         $actions = contact_photo_menu($contact);
69
70
71         // Move the contact data to the profile array so we can deliver it to
72         // 
73         $profile = array(
74                 'name' => $contact["name"],
75                 'nick'  => $contact["nick"],
76                 'addr'  => (($contact["addr"] != "") ? $contact["addr"] : $contact["url"]),
77                 'thumb' => proxy_url($contact["photo"], false, PROXY_SIZE_THUMB),
78                 'url' => ($cid ? ("redir/".$cid) : zrl($contact["url"])),
79                 'nurl' => $contact["nurl"], // We additionaly strore the nurl as identifier
80 //              'alias' => $contact["alias"],
81                 'location' => $contact["location"],
82                 'gender' => $contact["gender"],
83                 'about' => $contact["about"],
84                 'network' => format_network_name($contact["network"]),
85                 'tags' => intval($contact["keywords"]),
86 //              'nsfw' => intval($contact["nsfw"]),
87 //              'server_url' => $contact["server_url"],
88                 'bd' => (($contact["birthday"] == "0000-00-00") ? "" : $contact["birthday"]),
89 //              'generation' => $contact["generation"],
90                 'account_type' => ($contact['community'] ? t("Forum") : ""),
91                 'actions' => $actions,
92         );
93
94         if($datatype == "html") {
95                 $t = get_markup_template("hovercard.tpl", "addon/frio_hovercard/");
96
97                 $o = replace_macros($t, array(
98                         '$profile' => $profile,
99                 ));
100
101                 return $o;
102
103         } else {
104                 json_return_and_die($profile);
105         }
106 }
107
108 /**
109  * @brief Get the raw content of a template file
110  * 
111  * @param string $template The name of the template
112  * @param string $root Directory of the template
113  * 
114  * @return string|bool Output the raw content if existent, otherwise false
115  */
116 function get_template_content($template, $root = "") {
117         // We load the whole template system to get the filename.
118         // Maybe we can do it a little bit smarter if I get time.
119         $t = get_markup_template($template, $root);
120         $filename = $t->filename;
121
122         // Get the content of the template file
123         if(file_exists($filename)) {
124                 $content = file_get_contents($filename);
125
126                 return $content;
127         }
128
129         return false;
130 }