4 * Description: Collapse posts
6 * Author: Michael Vogel <ike@piratenpartei.de>
7 * based upon NSFW from Mike Macgirvin <http://macgirvin.com/profile/mike>
10 use Friendica\Core\Hook;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13 use Friendica\Util\Strings;
15 function showmore_install()
17 Hook::register('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
18 Hook::register('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
19 Hook::register('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
22 function showmore_uninstall()
24 Hook::unregister('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
25 Hook::unregister('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
26 Hook::unregister('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
29 function showmore_addon_settings(&$a, &$s)
35 /* Add our stylesheet to the page so we can make our settings look nice */
37 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->getBaseURL().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
39 $enable_checked = (intval(PConfig::get(local_user(), 'showmore', 'disable')) ? '' : ' checked="checked"');
40 $chars = PConfig::get(local_user(), 'showmore', 'chars', 1100);
42 $s .= '<span id="settings_showmore_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
43 $s .= '<h3>' . L10n::t('"Show more" Settings').'</h3>';
45 $s .= '<div id="settings_showmore_expanded" class="settings-block" style="display: none;">';
46 $s .= '<span class="fakelink" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
47 $s .= '<h3>' . L10n::t('"Show more" Settings').'</h3>';
50 $s .= '<div id="showmore-wrapper">';
52 $s .= '<label id="showmore-enable-label" for="showmore-enable">'.L10n::t('Enable Show More').'</label>';
53 $s .= '<input id="showmore-enable" type="checkbox" name="showmore-enable" value="1"'.$enable_checked.' />';
54 $s .= '<div class="clear"></div>';
55 $s .= '<label id="showmore-label" for="showmore-chars">'.L10n::t('Cutting posts after how much characters').' </label>';
56 $s .= '<input id="showmore-words" type="text" name="showmore-chars" value="'.$chars.'" />';
57 $s .= '</div><div class="clear"></div>';
59 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="showmore-submit" name="showmore-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
60 // $s .= '<div class="showmore-desc">' . L10n::t('Use /expression/ to provide regular expressions') . '</div>';
66 function showmore_addon_settings_post(&$a, &$b)
72 if (!empty($_POST['showmore-submit'])) {
73 PConfig::set(local_user(), 'showmore', 'chars', trim($_POST['showmore-chars']));
74 $enable = (!empty($_POST['showmore-enable']) ? intval($_POST['showmore-enable']) : 0);
76 PConfig::set(local_user(), 'showmore', 'disable', $disable);
77 info(L10n::t('Show More Settings saved.') . EOL);
81 function get_body_length($body)
83 $string = trim($body);
85 // DomDocument doesn't like empty strings
86 if (!strlen($string)) {
90 // We need to get rid of hidden tags (display: none)
92 // Get rid of the warning. It would be better to have some valid html as input
93 $dom = @DomDocument::loadHTML($body);
94 $xpath = new DOMXPath($dom);
97 * Checking any possible syntax of the style attribute with xpath is impossible
98 * So we just get any element with a style attribute, and check them with a regexp
100 $xr = $xpath->query('//*[@style]');
101 foreach ($xr as $node) {
102 if (preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
103 // Hidden, remove it from its parent
104 $node->parentNode->removeChild($node);
107 // Now we can get the body of our HTML DomDocument, it contains only what is visible
108 $string = $dom->saveHTML();
110 $string = strip_tags($string);
111 return strlen($string);
114 function showmore_prepare_body(\Friendica\App $a, &$hook_data)
116 // No combination with content filters
117 if (!empty($hook_data['filter_reasons'])) {
121 if (PConfig::get(local_user(), 'showmore', 'disable')) {
125 $chars = (int) PConfig::get(local_user(), 'showmore', 'chars', 1100);
127 if (get_body_length($hook_data['html']) > $chars) {
129 $shortened = trim(showmore_cutitem($hook_data['html'], $chars)) . "...";
135 $rnd = Strings::getRandomHex(8);
136 $hook_data['html'] = '<span id="showmore-teaser-' . $rnd . '" class="showmore-teaser" style="display: block;">' . $shortened . " " .
137 '<span id="showmore-wrap-' . $rnd . '" style="white-space:nowrap;" class="showmore-wrap fakelink" onclick="openClose(\'showmore-' . $rnd . '\'); openClose(\'showmore-teaser-' . $rnd . '\');" >' . L10n::t('show more') . '</span></span>' .
138 '<div id="showmore-' . $rnd . '" class="showmore-content" style="display: none;">' . $hook_data['html'] . '</div>';
142 function showmore_cutitem($text, $limit)
146 $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
148 $text = substr($text, 0, $limit);
150 $pos1 = strrpos($text, "<");
151 $pos2 = strrpos($text, ">");
152 $pos3 = strrpos($text, "&");
153 $pos4 = strrpos($text, ";");
157 $text = substr($text, 0, $pos1);
160 $text = substr($text, 0, $pos3);
163 $doc = new DOMDocument();
164 $doc->preserveWhiteSpace = false;
166 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
167 @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
169 $text = $doc->saveHTML();
170 $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);