4 * Description: Collapse posts
6 * Author: Michael Vogel <ike@piratenpartei.de>
7 * based upon NSFW from Mike Macgirvin <http://macgirvin.com/profile/mike>
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
15 use Friendica\Util\Strings;
17 function showmore_install()
19 Hook::register('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
20 Hook::register('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
21 Hook::register('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
24 function showmore_addon_settings(App &$a, array &$data)
26 if (!DI::userSession()->getLocalUserId()) {
30 DI::page()->registerStylesheet(__DIR__ . '/showmore.css', 'all');
32 $enabled = !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'disable');
33 $chars = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'chars', 1100);
35 $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/showmore/');
36 $html = Renderer::replaceMacros($t, [
37 '$enabled' => ['showmore-enable', DI::l10n()->t('Enable Show More'), $enabled],
38 '$chars' => ['showmore-chars', DI::l10n()->t('Cutting posts after how many characters'), $chars],
42 'addon' => 'showmore',
43 'title' => DI::l10n()->t('"Show more" Settings'),
48 function showmore_addon_settings_post(App $a, array &$b)
50 if (!DI::userSession()->getLocalUserId()) {
54 if (!empty($_POST['showmore-submit'])) {
55 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'showmore', 'chars', trim($_POST['showmore-chars']));
56 $enable = (!empty($_POST['showmore-enable']) ? intval($_POST['showmore-enable']) : 0);
58 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'showmore', 'disable', $disable);
62 function get_body_length($body)
64 $string = trim($body);
66 // DomDocument doesn't like empty strings
67 if (!strlen($string)) {
71 // We need to get rid of hidden tags (display: none)
73 // Get rid of the warning. It would be better to have some valid html as input
74 $doc = new DOMDocument();
75 @$doc->loadHTML($body);
76 $xpath = new DOMXPath($doc);
79 * Checking any possible syntax of the style attribute with xpath is impossible
80 * So we just get any element with a style attribute, and check them with a regexp
82 $xr = $xpath->query('//*[@style]');
83 foreach ($xr as $node) {
84 if (preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
85 // Hidden, remove it from its parent
86 $node->parentNode->removeChild($node);
89 // Now we can get the body of our HTML DomDocument, it contains only what is visible
90 $string = $doc->saveHTML();
92 $string = strip_tags($string);
93 return strlen($string);
96 function showmore_prepare_body(App $a, &$hook_data)
98 // No combination with content filters
99 if (!empty($hook_data['filter_reasons'])) {
103 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'disable')) {
107 $chars = (int) DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'chars', 1100);
109 if (get_body_length($hook_data['html']) > $chars) {
111 $shortened = trim(showmore_cutitem($hook_data['html'], $chars)) . "...";
117 $rnd = Strings::getRandomHex(8);
118 $hook_data['html'] = '<span id="showmore-teaser-' . $rnd . '" class="showmore-teaser" style="display: block;" aria-hidden="true" dir="auto">' . $shortened . " " .
119 '<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>' .
120 '<div id="showmore-' . $rnd . '" class="showmore-content" style="display: none;" aria-hidden="false" dir="auto">' . $hook_data['html'] . '</div>';
124 function showmore_cutitem($text, $limit)
128 $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
130 $text = substr($text, 0, $limit);
132 $pos1 = strrpos($text, "<");
133 $pos2 = strrpos($text, ">");
134 $pos3 = strrpos($text, "&");
135 $pos4 = strrpos($text, ";");
139 $text = substr($text, 0, $pos1);
142 $text = substr($text, 0, $pos3);
145 $doc = new DOMDocument();
146 $doc->preserveWhiteSpace = false;
148 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
149 @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
151 $text = $doc->saveHTML();
152 $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);