a9d8334f3e41dea7532d2df1d11599cf95fb2a70
[friendica-addons.git] / showmore / showmore.php
1 <?php
2 /**
3  * Name: Show More
4  * Description: Collapse posts
5  * Version: 1.0
6  * Author: Michael Vogel <ike@piratenpartei.de>
7  *         based upon NSFW from Mike Macgirvin <http://macgirvin.com/profile/mike>
8  *
9  */
10
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');
15 }
16
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');
21 }
22
23 function showmore_addon_settings(&$a,&$s) {
24
25         if(! local_user())
26                 return;
27
28         /* Add our stylesheet to the page so we can make our settings look nice */
29
30         $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
31
32         $enable_checked = (intval(get_pconfig(local_user(),'showmore','disable')) ? '' : ' checked="checked"');
33         $chars = get_pconfig(local_user(),'showmore','chars');
34         if(!$chars)
35                 $chars = '1100';
36
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>';
39         $s .= '</span>';
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>';
43         $s .= '</span>';
44
45         $s .= '<div id="showmore-wrapper">';
46
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>';
53
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>';
56         $s .= '</div>';
57
58         return;
59 }
60
61 function showmore_addon_settings_post(&$a,&$b) {
62
63         if(! local_user())
64                 return;
65
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);
69                 $disable = 1-$enable;
70                 set_pconfig(local_user(),'showmore','disable', $disable);
71                 info( t('Show More Settings saved.') . EOL);
72         }
73 }
74
75 function get_body_length($body) {
76         $string = trim($body);
77
78         // DomDocument doesn't like empty strings
79         if(! strlen($string)) {
80                 return 0;
81         }
82
83         // We need to get rid of hidden tags (display: none)
84
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);
88
89         /*
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
92          */
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);
98                 }
99         }
100         // Now we can get the body of our HTML DomDocument, it contains only what is visible
101         $string = $dom->saveHTML();
102
103         $string = strip_tags($string);
104         return strlen($string);
105 }
106
107 function showmore_prepare_body(&$a,&$b) {
108
109         $words = null;
110         if(get_pconfig(local_user(),'showmore','disable'))
111                 return;
112
113         $chars = (int)get_pconfig(local_user(),'showmore','chars');
114         if(!$chars)
115                 $chars = 1100;
116
117         if (get_body_length($b['html']) > $chars) {
118                 $found = true;
119                 $shortened = trim(showmore_cutitem($b['html'], $chars))."...";
120         }
121
122         if($found) {
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>';
127         }
128 }
129
130 function showmore_cutitem($text, $limit) {
131         $text = trim($text);
132
133         $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
134
135         $text = substr($text, 0, $limit);
136
137         $pos1 = strrpos($text, "<");
138         $pos2 = strrpos($text, ">");
139         $pos3 = strrpos($text, "&");
140         $pos4 = strrpos($text, ";");
141
142         if ($pos1 > $pos3) {
143                 if ($pos1 > $pos2)
144                         $text = substr($text, 0, $pos1);
145         } else {
146                 if ($pos3 > $pos4)
147                         $text = substr($text, 0, $pos3);
148         }
149
150         $doc = new DOMDocument();
151         $doc->preserveWhiteSpace = false;
152
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>");
155
156         $text = $doc->saveHTML();
157         $text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $text);
158
159         return($text);
160 }