Merge pull request #566 from MrPetovan/task/add-content_filter-hook
[friendica-addons.git/.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(\Friendica\App $a, &$hook_data)
111 {
112         // No combination with content filters
113         if (!empty($hook_data['filter_reasons'])) {
114                 return;
115         }
116
117         if (PConfig::get(local_user(), 'showmore', 'disable')) {
118                 return;
119         }
120
121         $chars = (int) PConfig::get(local_user(), 'showmore', 'chars');
122         if (!$chars) {
123                 $chars = 1100;
124         }
125
126         if (get_body_length($hook_data['html']) > $chars) {
127                 $found = true;
128                 $shortened = trim(showmore_cutitem($hook_data['html'], $chars)) . "...";
129         }
130
131         if ($found) {
132                 $rnd = random_string(8);
133                 $hook_data['html'] = '<span id="showmore-teaser-' . $rnd . '" class="showmore-teaser" style="display: block;">' . $shortened . " " .
134                         '<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>' .
135                         '<div id="showmore-' . $rnd . '" class="showmore-content" style="display: none;">' . $hook_data['html'] . '</div>';
136         }
137 }
138
139 function showmore_cutitem($text, $limit) {
140         $text = trim($text);
141
142         $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
143
144         $text = substr($text, 0, $limit);
145
146         $pos1 = strrpos($text, "<");
147         $pos2 = strrpos($text, ">");
148         $pos3 = strrpos($text, "&");
149         $pos4 = strrpos($text, ";");
150
151         if ($pos1 > $pos3) {
152                 if ($pos1 > $pos2)
153                         $text = substr($text, 0, $pos1);
154         } else {
155                 if ($pos3 > $pos4)
156                         $text = substr($text, 0, $pos3);
157         }
158
159         $doc = new DOMDocument();
160         $doc->preserveWhiteSpace = false;
161
162         $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
163         @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
164
165         $text = $doc->saveHTML();
166         $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);
167
168         return($text);
169 }