c2d4b1942a8d1a8f9ff4caab8de7bd90f4be2314
[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 use Friendica\Core\Addon;
11 use Friendica\Core\L10n;
12 use Friendica\Core\PConfig;
13
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');
18 }
19
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');
24 }
25
26 function showmore_addon_settings(&$a,&$s) {
27
28         if(! local_user())
29                 return;
30
31         /* Add our stylesheet to the page so we can make our settings look nice */
32
33         $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.$a->get_baseurl().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
34
35         $enable_checked = (intval(PConfig::get(local_user(),'showmore','disable')) ? '' : ' checked="checked"');
36         $chars = PConfig::get(local_user(),'showmore','chars');
37         if(!$chars)
38                 $chars = '1100';
39
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>';
42         $s .= '</span>';
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>';
46         $s .= '</span>';
47
48         $s .= '<div id="showmore-wrapper">';
49
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>';
56
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>';
59         $s .= '</div>';
60
61         return;
62 }
63
64 function showmore_addon_settings_post(&$a,&$b) {
65
66         if(! local_user())
67                 return;
68
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);
72                 $disable = 1-$enable;
73                 PConfig::set(local_user(),'showmore','disable', $disable);
74                 info(L10n::t('Show More Settings saved.') . EOL);
75         }
76 }
77
78 function get_body_length($body) {
79         $string = trim($body);
80
81         // DomDocument doesn't like empty strings
82         if(! strlen($string)) {
83                 return 0;
84         }
85
86         // We need to get rid of hidden tags (display: none)
87
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);
91
92         /*
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
95          */
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);
101                 }
102         }
103         // Now we can get the body of our HTML DomDocument, it contains only what is visible
104         $string = $dom->saveHTML();
105
106         $string = strip_tags($string);
107         return strlen($string);
108 }
109
110 function showmore_prepare_body(&$a,&$b) {
111
112         $words = null;
113         if(PConfig::get(local_user(),'showmore','disable'))
114                 return;
115
116         $chars = (int)PConfig::get(local_user(),'showmore','chars');
117         if(!$chars)
118                 $chars = 1100;
119
120         if (get_body_length($b['html']) > $chars) {
121                 $found = true;
122                 $shortened = trim(showmore_cutitem($b['html'], $chars))."...";
123         }
124
125         if($found) {
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.'\');" >'.sprintf(L10n::t('show more')).'</span></span>'.
129                                 '<div id="showmore-'.$rnd.'" class="showmore-content" style="display: none;">'.$b['html'].'</div>';
130         }
131 }
132
133 function showmore_cutitem($text, $limit) {
134         $text = trim($text);
135
136         $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
137
138         $text = substr($text, 0, $limit);
139
140         $pos1 = strrpos($text, "<");
141         $pos2 = strrpos($text, ">");
142         $pos3 = strrpos($text, "&");
143         $pos4 = strrpos($text, ";");
144
145         if ($pos1 > $pos3) {
146                 if ($pos1 > $pos2)
147                         $text = substr($text, 0, $pos1);
148         } else {
149                 if ($pos3 > $pos4)
150                         $text = substr($text, 0, $pos3);
151         }
152
153         $doc = new DOMDocument();
154         $doc->preserveWhiteSpace = false;
155
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>");
158
159         $text = $doc->saveHTML();
160         $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);
161
162         return($text);
163 }