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\Addon;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
14 function showmore_install() {
15 Addon::registerHook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
16 Addon::registerHook('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
17 Addon::registerHook('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
20 function showmore_uninstall() {
21 Addon::unregisterHook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
22 Addon::unregisterHook('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
23 Addon::unregisterHook('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
26 function showmore_addon_settings(&$a,&$s) {
31 /* Add our stylesheet to the page so we can make our settings look nice */
33 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
35 $enable_checked = (intval(PConfig::get(local_user(),'showmore','disable')) ? '' : ' checked="checked"');
36 $chars = PConfig::get(local_user(),'showmore','chars');
40 $s .= '<span id="settings_showmore_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
41 $s .= '<h3>' . L10n::t('"Show more" Settings').'</h3>';
43 $s .= '<div id="settings_showmore_expanded" class="settings-block" style="display: none;">';
44 $s .= '<span class="fakelink" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
45 $s .= '<h3>' . L10n::t('"Show more" Settings').'</h3>';
48 $s .= '<div id="showmore-wrapper">';
50 $s .= '<label id="showmore-enable-label" for="showmore-enable">'.L10n::t('Enable Show More').'</label>';
51 $s .= '<input id="showmore-enable" type="checkbox" name="showmore-enable" value="1"'.$enable_checked.' />';
52 $s .= '<div class="clear"></div>';
53 $s .= '<label id="showmore-label" for="showmore-chars">'.L10n::t('Cutting posts after how much characters').' </label>';
54 $s .= '<input id="showmore-words" type="text" name="showmore-chars" value="'.$chars.'" />';
55 $s .= '</div><div class="clear"></div>';
57 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="showmore-submit" name="showmore-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
58 // $s .= '<div class="showmore-desc">' . L10n::t('Use /expression/ to provide regular expressions') . '</div>';
64 function showmore_addon_settings_post(&$a,&$b) {
69 if($_POST['showmore-submit']) {
70 PConfig::set(local_user(),'showmore','chars',trim($_POST['showmore-chars']));
71 $enable = ((x($_POST,'showmore-enable')) ? intval($_POST['showmore-enable']) : 0);
73 PConfig::set(local_user(),'showmore','disable', $disable);
74 info(L10n::t('Show More Settings saved.') . EOL);
78 function get_body_length($body) {
79 $string = trim($body);
81 // DomDocument doesn't like empty strings
82 if(! strlen($string)) {
86 // We need to get rid of hidden tags (display: none)
88 // Get rid of the warning. It would be better to have some valid html as input
89 $dom = @DomDocument::loadHTML($body);
90 $xpath = new DOMXPath($dom);
93 * Checking any possible syntax of the style attribute with xpath is impossible
94 * So we just get any element with a style attribute, and check them with a regexp
96 $xr = $xpath->query('//*[@style]');
97 foreach($xr as $node) {
98 if(preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
99 // Hidden, remove it from its parent
100 $node->parentNode->removeChild($node);
103 // Now we can get the body of our HTML DomDocument, it contains only what is visible
104 $string = $dom->saveHTML();
106 $string = strip_tags($string);
107 return strlen($string);
110 function showmore_prepare_body(&$a,&$b) {
113 if(PConfig::get(local_user(),'showmore','disable'))
116 $chars = (int)PConfig::get(local_user(),'showmore','chars');
120 if (get_body_length($b['html']) > $chars) {
122 $shortened = trim(showmore_cutitem($b['html'], $chars))."...";
126 $rnd = random_string(8);
127 $b['html'] = '<span id="showmore-teaser-'.$rnd.'" class="showmore-teaser" style="display: block;">'.$shortened." ".
128 '<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>'.
129 '<div id="showmore-'.$rnd.'" class="showmore-content" style="display: none;">'.$b['html'].'</div>';
133 function showmore_cutitem($text, $limit) {
136 $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
138 $text = substr($text, 0, $limit);
140 $pos1 = strrpos($text, "<");
141 $pos2 = strrpos($text, ">");
142 $pos3 = strrpos($text, "&");
143 $pos4 = strrpos($text, ";");
147 $text = substr($text, 0, $pos1);
150 $text = substr($text, 0, $pos3);
153 $doc = new DOMDocument();
154 $doc->preserveWhiteSpace = false;
156 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
157 @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
159 $text = $doc->saveHTML();
160 $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);