2012-02-19 13:48:09 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Show More
|
|
|
|
* Description: Collapse posts
|
|
|
|
* Version: 1.0
|
|
|
|
* Author: Michael Vogel <ike@piratenpartei.de>
|
|
|
|
* based upon NSFW from Mike Macgirvin <http://macgirvin.com/profile/mike>
|
|
|
|
*
|
|
|
|
*/
|
2021-11-20 04:56:55 -05:00
|
|
|
|
|
|
|
use Friendica\App;
|
2018-12-26 02:28:16 -05:00
|
|
|
use Friendica\Core\Hook;
|
2021-11-20 04:56:55 -05:00
|
|
|
use Friendica\Core\Renderer;
|
2022-10-18 16:42:46 -04:00
|
|
|
use Friendica\Core\Session;
|
2019-12-29 21:55:10 -05:00
|
|
|
use Friendica\DI;
|
2018-11-08 11:45:19 -05:00
|
|
|
use Friendica\Util\Strings;
|
2017-11-06 18:55:24 -05:00
|
|
|
|
2018-08-08 02:24:47 -04:00
|
|
|
function showmore_install()
|
|
|
|
{
|
2018-12-26 02:28:16 -05:00
|
|
|
Hook::register('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
|
|
|
|
Hook::register('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
|
|
|
|
Hook::register('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
|
2012-02-19 13:48:09 -05:00
|
|
|
}
|
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
function showmore_addon_settings(App &$a, array &$data)
|
2018-08-08 02:24:47 -04:00
|
|
|
{
|
2022-10-18 16:42:46 -04:00
|
|
|
if (!Session::getLocalUser()) {
|
2012-02-19 13:48:09 -05:00
|
|
|
return;
|
2018-08-08 02:24:47 -04:00
|
|
|
}
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2021-11-20 04:29:09 -05:00
|
|
|
DI::page()->registerStylesheet(__DIR__ . '/showmore.css', 'all');
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2022-10-18 16:42:46 -04:00
|
|
|
$enabled = !DI::pConfig()->get(Session::getLocalUser(), 'showmore', 'disable');
|
|
|
|
$chars = DI::pConfig()->get(Session::getLocalUser(), 'showmore', 'chars', 1100);
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/showmore/');
|
|
|
|
$html = Renderer::replaceMacros($t, [
|
|
|
|
'$enabled' => ['showmore-enable', DI::l10n()->t('Enable Show More'), $enabled],
|
|
|
|
'$chars' => ['showmore-chars', DI::l10n()->t('Cutting posts after how many characters'), $chars],
|
|
|
|
]);
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
$data = [
|
|
|
|
'addon' => 'showmore',
|
|
|
|
'title' => DI::l10n()->t('"Show more" Settings'),
|
|
|
|
'html' => $html,
|
|
|
|
];
|
2012-02-19 13:48:09 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 01:16:22 -04:00
|
|
|
function showmore_addon_settings_post(App $a, array &$b)
|
2018-08-08 02:24:47 -04:00
|
|
|
{
|
2022-10-18 16:42:46 -04:00
|
|
|
if (!Session::getLocalUser()) {
|
2012-02-19 13:48:09 -05:00
|
|
|
return;
|
2018-08-08 02:24:47 -04:00
|
|
|
}
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2018-08-08 02:24:47 -04:00
|
|
|
if (!empty($_POST['showmore-submit'])) {
|
2022-10-18 16:42:46 -04:00
|
|
|
DI::pConfig()->set(Session::getLocalUser(), 'showmore', 'chars', trim($_POST['showmore-chars']));
|
2018-11-30 09:11:56 -05:00
|
|
|
$enable = (!empty($_POST['showmore-enable']) ? intval($_POST['showmore-enable']) : 0);
|
2012-02-19 13:48:09 -05:00
|
|
|
$disable = 1-$enable;
|
2022-10-18 16:42:46 -04:00
|
|
|
DI::pConfig()->set(Session::getLocalUser(), 'showmore', 'disable', $disable);
|
2012-02-19 13:48:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 02:24:47 -04:00
|
|
|
function get_body_length($body)
|
|
|
|
{
|
2012-07-23 14:07:15 -04:00
|
|
|
$string = trim($body);
|
|
|
|
|
2012-07-27 04:08:30 -04:00
|
|
|
// DomDocument doesn't like empty strings
|
2018-08-08 02:24:47 -04:00
|
|
|
if (!strlen($string)) {
|
2012-07-27 04:08:30 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-23 14:07:15 -04:00
|
|
|
// We need to get rid of hidden tags (display: none)
|
2012-07-23 15:22:20 -04:00
|
|
|
|
|
|
|
// Get rid of the warning. It would be better to have some valid html as input
|
2020-12-26 18:30:21 -05:00
|
|
|
$doc = new DOMDocument();
|
|
|
|
@$doc->loadHTML($body);
|
|
|
|
$xpath = new DOMXPath($doc);
|
2012-07-23 14:07:15 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Checking any possible syntax of the style attribute with xpath is impossible
|
|
|
|
* So we just get any element with a style attribute, and check them with a regexp
|
|
|
|
*/
|
|
|
|
$xr = $xpath->query('//*[@style]');
|
2018-08-08 02:24:47 -04:00
|
|
|
foreach ($xr as $node) {
|
|
|
|
if (preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
|
2012-07-23 14:07:15 -04:00
|
|
|
// Hidden, remove it from its parent
|
|
|
|
$node->parentNode->removeChild($node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now we can get the body of our HTML DomDocument, it contains only what is visible
|
2020-12-26 18:30:21 -05:00
|
|
|
$string = $doc->saveHTML();
|
2012-07-23 14:07:15 -04:00
|
|
|
|
|
|
|
$string = strip_tags($string);
|
|
|
|
return strlen($string);
|
|
|
|
}
|
|
|
|
|
2021-11-20 04:56:55 -05:00
|
|
|
function showmore_prepare_body(App $a, &$hook_data)
|
2018-04-01 02:32:58 -04:00
|
|
|
{
|
|
|
|
// No combination with content filters
|
|
|
|
if (!empty($hook_data['filter_reasons'])) {
|
|
|
|
return;
|
|
|
|
}
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2022-10-18 16:42:46 -04:00
|
|
|
if (DI::pConfig()->get(Session::getLocalUser(), 'showmore', 'disable')) {
|
2012-02-19 13:48:09 -05:00
|
|
|
return;
|
2018-04-01 02:32:58 -04:00
|
|
|
}
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2022-10-18 16:42:46 -04:00
|
|
|
$chars = (int) DI::pConfig()->get(Session::getLocalUser(), 'showmore', 'chars', 1100);
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2018-04-01 02:32:58 -04:00
|
|
|
if (get_body_length($hook_data['html']) > $chars) {
|
2012-02-19 13:48:09 -05:00
|
|
|
$found = true;
|
2018-04-01 02:32:58 -04:00
|
|
|
$shortened = trim(showmore_cutitem($hook_data['html'], $chars)) . "...";
|
2018-07-10 08:30:23 -04:00
|
|
|
} else {
|
|
|
|
$found = false;
|
2012-02-19 13:48:09 -05:00
|
|
|
}
|
|
|
|
|
2018-04-01 02:32:58 -04:00
|
|
|
if ($found) {
|
2018-11-08 11:45:19 -05:00
|
|
|
$rnd = Strings::getRandomHex(8);
|
2021-05-20 08:41:01 -04:00
|
|
|
$hook_data['html'] = '<span id="showmore-teaser-' . $rnd . '" class="showmore-teaser" style="display: block;" aria-hidden="true" dir="auto">' . $shortened . " " .
|
|
|
|
'<span id="showmore-wrap-' . $rnd . '" style="white-space:nowrap;" class="showmore-wrap fakelink" onclick="openClose(\'showmore-' . $rnd . '\'); openClose(\'showmore-teaser-' . $rnd . '\');">' . DI::l10n()->t('show more') . '</span></span>' .
|
|
|
|
'<div id="showmore-' . $rnd . '" class="showmore-content" style="display: none;" aria-hidden="false" dir="auto">' . $hook_data['html'] . '</div>';
|
2012-02-19 13:48:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 02:24:47 -04:00
|
|
|
function showmore_cutitem($text, $limit)
|
|
|
|
{
|
2012-02-19 13:48:09 -05:00
|
|
|
$text = trim($text);
|
|
|
|
|
|
|
|
$text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
|
|
|
|
|
|
|
|
$text = substr($text, 0, $limit);
|
|
|
|
|
|
|
|
$pos1 = strrpos($text, "<");
|
|
|
|
$pos2 = strrpos($text, ">");
|
|
|
|
$pos3 = strrpos($text, "&");
|
|
|
|
$pos4 = strrpos($text, ";");
|
|
|
|
|
|
|
|
if ($pos1 > $pos3) {
|
|
|
|
if ($pos1 > $pos2)
|
|
|
|
$text = substr($text, 0, $pos1);
|
|
|
|
} else {
|
|
|
|
if ($pos3 > $pos4)
|
|
|
|
$text = substr($text, 0, $pos3);
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->preserveWhiteSpace = false;
|
|
|
|
|
|
|
|
$doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
|
|
|
|
@$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
|
|
|
|
|
|
|
|
$text = $doc->saveHTML();
|
2018-01-15 08:15:33 -05:00
|
|
|
$text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);
|
2012-02-19 13:48:09 -05:00
|
|
|
|
2018-08-08 02:24:47 -04:00
|
|
|
return $text;
|
2012-02-19 13:48:09 -05:00
|
|
|
}
|