2016-05-29 09:28:31 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name: Frio Hovercard
|
|
|
|
* Description: Hovercard addon for the frio theme
|
|
|
|
* Version: 0.1
|
|
|
|
* Author: Rabuzarus <https://github.com/rabuzarus>
|
|
|
|
* License: GNU AFFERO GENERAL PUBLIC LICENSE (Version 3)
|
|
|
|
*/
|
2018-07-19 22:15:21 -04:00
|
|
|
|
2017-04-30 00:07:00 -04:00
|
|
|
use Friendica\App;
|
2017-11-06 21:22:52 -05:00
|
|
|
use Friendica\Core\Config;
|
2018-10-31 10:35:50 -04:00
|
|
|
use Friendica\Core\Renderer;
|
2018-01-27 11:59:10 -05:00
|
|
|
use Friendica\Core\System;
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2017-12-07 09:04:24 -05:00
|
|
|
use Friendica\Model\Contact;
|
2017-12-07 09:09:28 -05:00
|
|
|
use Friendica\Model\GContact;
|
2018-07-30 22:06:22 -04:00
|
|
|
use Friendica\Util\Proxy as ProxyUtils;
|
2018-11-08 10:24:47 -05:00
|
|
|
use Friendica\Util\Strings;
|
2016-05-29 09:28:31 -04:00
|
|
|
|
2018-01-01 16:27:01 -05:00
|
|
|
function hovercard_init(App $a)
|
|
|
|
{
|
2016-05-29 09:28:31 -04:00
|
|
|
// Just for testing purposes
|
2018-01-01 16:27:01 -05:00
|
|
|
$_GET['mode'] = 'minimal';
|
2016-05-29 09:28:31 -04:00
|
|
|
}
|
2017-04-30 00:01:26 -04:00
|
|
|
|
2018-01-01 16:27:01 -05:00
|
|
|
function hovercard_content()
|
|
|
|
{
|
2019-10-15 09:01:17 -04:00
|
|
|
$profileurl = $_REQUEST['profileurl'] ?? '';
|
|
|
|
$datatype = ($_REQUEST['datatype'] ?? '') ?: 'json';
|
2016-05-29 09:28:31 -04:00
|
|
|
|
|
|
|
// Get out if the system doesn't have public access allowed
|
2018-01-01 16:27:01 -05:00
|
|
|
if (intval(Config::get('system', 'block_public'))) {
|
2019-05-01 23:16:10 -04:00
|
|
|
throw new \Friendica\Network\HTTPException\ForbiddenException();
|
2018-01-01 16:27:01 -05:00
|
|
|
}
|
2016-05-29 09:28:31 -04:00
|
|
|
|
|
|
|
// Return the raw content of the template. We use this to make templates usable for js functions.
|
|
|
|
// Look at hovercard.js (function getHoverCardTemplate()).
|
2018-01-01 16:27:01 -05:00
|
|
|
// This part should be moved in its own module. Maybe we could make more templates accessible.
|
|
|
|
// (We need to discuss possible security leaks before doing this)
|
|
|
|
if ($datatype == 'tpl') {
|
|
|
|
$templatecontent = get_template_content('hovercard.tpl');
|
2016-05-29 09:28:31 -04:00
|
|
|
echo $templatecontent;
|
2018-12-26 00:40:12 -05:00
|
|
|
exit();
|
2016-05-29 09:28:31 -04:00
|
|
|
}
|
|
|
|
|
2018-01-01 16:27:01 -05:00
|
|
|
// If a contact is connected the url is internally changed to 'redir/CID'. We need the pure url to search for
|
2016-05-29 09:28:31 -04:00
|
|
|
// the contact. So we strip out the contact id from the internal url and look in the contact table for
|
|
|
|
// the real url (nurl)
|
2018-06-21 16:46:10 -04:00
|
|
|
if (strpos($profileurl, 'redir/') === 0) {
|
2016-05-29 09:28:31 -04:00
|
|
|
$cid = intval(substr($profileurl, 6));
|
2018-07-20 08:19:26 -04:00
|
|
|
$remote_contact = DBA::selectFirst('contact', ['nurl'], ['id' => $cid]);
|
2019-10-15 09:01:17 -04:00
|
|
|
$profileurl = $remote_contact['nurl'] ?? '';
|
2016-05-29 09:28:31 -04:00
|
|
|
}
|
|
|
|
|
2018-01-01 16:27:01 -05:00
|
|
|
$contact = [];
|
2016-05-29 09:28:31 -04:00
|
|
|
// if it's the url containing https it should be converted to http
|
2018-11-08 11:28:29 -05:00
|
|
|
$nurl = Strings::normaliseLink(GContact::cleanContactUrl($profileurl));
|
2018-07-30 01:06:32 -04:00
|
|
|
if (!$nurl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for contact data
|
|
|
|
// Look if the local user has got the contact
|
|
|
|
if (local_user()) {
|
|
|
|
$contact = Contact::getDetailsByURL($nurl, local_user());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not then check the global user
|
|
|
|
if (!count($contact)) {
|
2017-11-19 17:03:39 -05:00
|
|
|
$contact = Contact::getDetailsByURL($nurl);
|
2016-05-29 09:28:31 -04:00
|
|
|
}
|
2018-07-30 01:06:32 -04:00
|
|
|
|
|
|
|
// Feeds url could have been destroyed through "cleanContactUrl", so we now use the original url
|
|
|
|
if (!count($contact) && local_user()) {
|
2018-11-08 11:28:29 -05:00
|
|
|
$nurl = Strings::normaliseLink($profileurl);
|
2018-07-30 01:06:32 -04:00
|
|
|
$contact = Contact::getDetailsByURL($nurl, local_user());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count($contact)) {
|
2018-11-08 11:28:29 -05:00
|
|
|
$nurl = Strings::normaliseLink($profileurl);
|
2018-07-30 01:06:32 -04:00
|
|
|
$contact = Contact::getDetailsByURL($nurl);
|
|
|
|
}
|
|
|
|
|
2018-01-01 16:27:01 -05:00
|
|
|
if (!count($contact)) {
|
2016-05-29 09:28:31 -04:00
|
|
|
return;
|
2018-01-01 16:27:01 -05:00
|
|
|
}
|
2016-05-29 09:28:31 -04:00
|
|
|
|
|
|
|
// Get the photo_menu - the menu if possible contact actions
|
2018-01-01 16:27:01 -05:00
|
|
|
if (local_user()) {
|
2017-11-19 17:03:39 -05:00
|
|
|
$actions = Contact::photoMenu($contact);
|
2018-07-23 07:43:18 -04:00
|
|
|
} else {
|
|
|
|
$actions = [];
|
2018-01-01 16:27:01 -05:00
|
|
|
}
|
2016-05-29 09:28:31 -04:00
|
|
|
|
|
|
|
// Move the contact data to the profile array so we can deliver it to
|
2018-01-15 08:05:12 -05:00
|
|
|
$profile = [
|
2018-12-26 11:03:41 -05:00
|
|
|
'name' => $contact['name'],
|
|
|
|
'nick' => $contact['nick'],
|
2019-10-15 09:01:17 -04:00
|
|
|
'addr' => ($contact['addr'] ?? '') ?: $contact['url'],
|
2018-12-26 11:03:41 -05:00
|
|
|
'thumb' => ProxyUtils::proxifyUrl($contact['thumb'], false, ProxyUtils::SIZE_THUMB),
|
|
|
|
'url' => Contact::magicLink($contact['url']),
|
|
|
|
'nurl' => $contact['nurl'], // We additionally store the nurl as identifier
|
|
|
|
'location' => $contact['location'],
|
|
|
|
'gender' => $contact['gender'],
|
|
|
|
'about' => $contact['about'],
|
|
|
|
'network_link' => Strings::formatNetworkName($contact['network'], $contact['url']),
|
|
|
|
'tags' => $contact['keywords'],
|
|
|
|
'bd' => $contact['birthday'] <= DBA::NULL_DATE ? '' : $contact['birthday'],
|
2017-11-19 17:03:39 -05:00
|
|
|
'account_type' => Contact::getAccountType($contact),
|
2018-12-26 11:03:41 -05:00
|
|
|
'actions' => $actions,
|
2018-01-15 08:05:12 -05:00
|
|
|
];
|
2018-01-01 16:27:01 -05:00
|
|
|
if ($datatype == 'html') {
|
2018-10-31 10:44:06 -04:00
|
|
|
$tpl = Renderer::getMarkupTemplate('hovercard.tpl');
|
2018-10-31 10:35:50 -04:00
|
|
|
$o = Renderer::replaceMacros($tpl, [
|
2016-05-29 09:28:31 -04:00
|
|
|
'$profile' => $profile,
|
2018-01-15 08:05:12 -05:00
|
|
|
]);
|
2016-05-29 09:28:31 -04:00
|
|
|
|
|
|
|
return $o;
|
|
|
|
} else {
|
2018-01-27 11:59:10 -05:00
|
|
|
System::jsonExit($profile);
|
2016-05-29 09:28:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the raw content of a template file
|
2017-01-09 07:14:25 -05:00
|
|
|
*
|
2016-05-29 09:28:31 -04:00
|
|
|
* @param string $template The name of the template
|
2019-01-07 01:07:42 -05:00
|
|
|
* @param string $root Directory of the template
|
2017-01-09 07:14:25 -05:00
|
|
|
*
|
2016-05-29 09:28:31 -04:00
|
|
|
* @return string|bool Output the raw content if existent, otherwise false
|
2019-01-07 01:07:42 -05:00
|
|
|
* @throws Exception
|
2016-05-29 09:28:31 -04:00
|
|
|
*/
|
2018-01-01 16:27:01 -05:00
|
|
|
function get_template_content($template, $root = '')
|
|
|
|
{
|
2016-05-29 09:28:31 -04:00
|
|
|
// We load the whole template system to get the filename.
|
|
|
|
// Maybe we can do it a little bit smarter if I get time.
|
2019-01-07 13:24:53 -05:00
|
|
|
$templateEngine = Renderer::getTemplateEngine();
|
|
|
|
$template = $templateEngine->getTemplateFile($template, $root);
|
|
|
|
|
|
|
|
$filename = $template->filename;
|
2016-05-29 09:28:31 -04:00
|
|
|
|
|
|
|
// Get the content of the template file
|
2018-01-01 16:27:01 -05:00
|
|
|
if (file_exists($filename)) {
|
2016-05-29 09:28:31 -04:00
|
|
|
$content = file_get_contents($filename);
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|