4 * Description: Collapse posts
6 * Author: Michael Vogel <ike@piratenpartei.de>
7 * based upon NSFW from Mike Macgirvin <http://macgirvin.com/profile/mike>
11 function showmore_install() {
12 register_hook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
13 register_hook('plugin_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
14 register_hook('plugin_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
17 function showmore_uninstall() {
18 unregister_hook('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
19 unregister_hook('plugin_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
20 unregister_hook('plugin_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
23 function showmore_addon_settings(&$a,&$s) {
28 /* Add our stylesheet to the page so we can make our settings look nice */
30 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
32 $enable_checked = (intval(get_pconfig(local_user(),'showmore','disable')) ? '' : ' checked="checked"');
33 $chars = get_pconfig(local_user(),'showmore','chars');
37 $s .= '<span id="settings_showmore_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
38 $s .= '<h3>' . t('"Show more" Settings').'</h3>';
40 $s .= '<div id="settings_showmore_expanded" class="settings-block" style="display: none;">';
41 $s .= '<span class="fakelink" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
42 $s .= '<h3>' . t('"Show more" Settings').'</h3>';
45 $s .= '<div id="showmore-wrapper">';
47 $s .= '<label id="showmore-enable-label" for="showmore-enable">'.t('Enable Show More').'</label>';
48 $s .= '<input id="showmore-enable" type="checkbox" name="showmore-enable" value="1"'.$enable_checked.' />';
49 $s .= '<div class="clear"></div>';
50 $s .= '<label id="showmore-label" for="showmore-chars">'.t('Cutting posts after how much characters').' </label>';
51 $s .= '<input id="showmore-words" type="text" name="showmore-chars" value="'.$chars.'" />';
52 $s .= '</div><div class="clear"></div>';
54 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="showmore-submit" name="showmore-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
55 // $s .= '<div class="showmore-desc">' . t('Use /expression/ to provide regular expressions') . '</div>';
61 function showmore_addon_settings_post(&$a,&$b) {
66 if($_POST['showmore-submit']) {
67 set_pconfig(local_user(),'showmore','chars',trim($_POST['showmore-chars']));
68 $enable = ((x($_POST,'showmore-enable')) ? intval($_POST['showmore-enable']) : 0);
70 set_pconfig(local_user(),'showmore','disable', $disable);
71 info( t('Show More Settings saved.') . EOL);
75 function get_body_length($body) {
76 $string = trim($body);
78 // DomDocument doesn't like empty strings
79 if(! strlen($string)) {
83 // We need to get rid of hidden tags (display: none)
85 // Get rid of the warning. It would be better to have some valid html as input
86 $dom = @DomDocument::loadHTML($body);
87 $xpath = new DOMXPath($dom);
90 * Checking any possible syntax of the style attribute with xpath is impossible
91 * So we just get any element with a style attribute, and check them with a regexp
93 $xr = $xpath->query('//*[@style]');
94 foreach($xr as $node) {
95 if(preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
96 // Hidden, remove it from its parent
97 $node->parentNode->removeChild($node);
100 // Now we can get the body of our HTML DomDocument, it contains only what is visible
101 $string = $dom->saveHTML();
103 $string = strip_tags($string);
104 return strlen($string);
107 function showmore_prepare_body(&$a,&$b) {
110 if(get_pconfig(local_user(),'showmore','disable'))
113 $chars = (int)get_pconfig(local_user(),'showmore','chars');
117 if (get_body_length($b['html']) > $chars) {
119 $shortened = trim(showmore_cutitem($b['html'], $chars))."...";
123 $rnd = random_string(8);
124 $b['html'] = '<span id="showmore-teaser-'.$rnd.'" class="showmore-teaser" style="display: block;">'.$shortened." ".
125 '<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>'.
126 '<div id="showmore-'.$rnd.'" class="showmore-content" style="display: none;">'.$b['html'].'</div>';
130 function showmore_cutitem($text, $limit) {
133 $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
135 $text = substr($text, 0, $limit);
137 $pos1 = strrpos($text, "<");
138 $pos2 = strrpos($text, ">");
139 $pos3 = strrpos($text, "&");
140 $pos4 = strrpos($text, ";");
144 $text = substr($text, 0, $pos1);
147 $text = substr($text, 0, $pos3);
150 $doc = new DOMDocument();
151 $doc->preserveWhiteSpace = false;
153 $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
154 @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
156 $text = $doc->saveHTML();
157 $text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $text);