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\PConfig;
13 function showmore_install() {
14 Addon::registerHook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
15 Addon::registerHook('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
16 Addon::registerHook('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
19 function showmore_uninstall() {
20 Addon::unregisterHook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
21 Addon::unregisterHook('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
22 Addon::unregisterHook('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
25 function showmore_addon_settings(&$a,&$s) {
30 /* Add our stylesheet to the page so we can make our settings look nice */
32 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
34 $enable_checked = (intval(PConfig::get(local_user(),'showmore','disable')) ? '' : ' checked="checked"');
35 $chars = PConfig::get(local_user(),'showmore','chars');
39 $s .= '<span id="settings_showmore_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
40 $s .= '<h3>' . t('"Show more" Settings').'</h3>';
42 $s .= '<div id="settings_showmore_expanded" class="settings-block" style="display: none;">';
43 $s .= '<span class="fakelink" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
44 $s .= '<h3>' . t('"Show more" Settings').'</h3>';
47 $s .= '<div id="showmore-wrapper">';
49 $s .= '<label id="showmore-enable-label" for="showmore-enable">'.t('Enable Show More').'</label>';
50 $s .= '<input id="showmore-enable" type="checkbox" name="showmore-enable" value="1"'.$enable_checked.' />';
51 $s .= '<div class="clear"></div>';
52 $s .= '<label id="showmore-label" for="showmore-chars">'.t('Cutting posts after how much characters').' </label>';
53 $s .= '<input id="showmore-words" type="text" name="showmore-chars" value="'.$chars.'" />';
54 $s .= '</div><div class="clear"></div>';
56 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="showmore-submit" name="showmore-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
57 // $s .= '<div class="showmore-desc">' . t('Use /expression/ to provide regular expressions') . '</div>';
63 function showmore_addon_settings_post(&$a,&$b) {
68 if($_POST['showmore-submit']) {
69 PConfig::set(local_user(),'showmore','chars',trim($_POST['showmore-chars']));
70 $enable = ((x($_POST,'showmore-enable')) ? intval($_POST['showmore-enable']) : 0);
72 PConfig::set(local_user(),'showmore','disable', $disable);
73 info( t('Show More Settings saved.') . EOL);
77 function get_body_length($body) {
78 $string = trim($body);
80 // DomDocument doesn't like empty strings
81 if(! strlen($string)) {
85 // We need to get rid of hidden tags (display: none)
87 // Get rid of the warning. It would be better to have some valid html as input
88 $dom = @DomDocument::loadHTML($body);
89 $xpath = new DOMXPath($dom);
92 * Checking any possible syntax of the style attribute with xpath is impossible
93 * So we just get any element with a style attribute, and check them with a regexp
95 $xr = $xpath->query('//*[@style]');
96 foreach($xr as $node) {
97 if(preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
98 // Hidden, remove it from its parent
99 $node->parentNode->removeChild($node);
102 // Now we can get the body of our HTML DomDocument, it contains only what is visible
103 $string = $dom->saveHTML();
105 $string = strip_tags($string);
106 return strlen($string);
109 function showmore_prepare_body(&$a,&$b) {
112 if(PConfig::get(local_user(),'showmore','disable'))
115 $chars = (int)PConfig::get(local_user(),'showmore','chars');
119 if (get_body_length($b['html']) > $chars) {
121 $shortened = trim(showmore_cutitem($b['html'], $chars))."...";
125 $rnd = random_string(8);
126 $b['html'] = '<span id="showmore-teaser-'.$rnd.'" class="showmore-teaser" style="display: block;">'.$shortened." ".
127 '<span id="showmore-wrap-'.$rnd.'" style="white-space:nowrap;" class="showmore-wrap fakelink" onclick="openClose(\'showmore-'.$rnd.'\'); openClose(\'showmore-teaser-'.$rnd.'\');" >'.sprintf(t('show more')).'</span></span>'.
128 '<div id="showmore-'.$rnd.'" class="showmore-content" style="display: none;">'.$b['html'].'</div>';
132 function showmore_cutitem($text, $limit) {
135 $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
137 $text = substr($text, 0, $limit);
139 $pos1 = strrpos($text, "<");
140 $pos2 = strrpos($text, ">");
141 $pos3 = strrpos($text, "&");
142 $pos4 = strrpos($text, ";");
146 $text = substr($text, 0, $pos1);
149 $text = substr($text, 0, $pos3);
152 $doc = new DOMDocument();
153 $doc->preserveWhiteSpace = false;
155 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
156 @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
158 $text = $doc->saveHTML();
159 $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);