Merge pull request #1007 from annando/notice-info
[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\Hook;
11 use Friendica\DI;
12 use Friendica\Util\Strings;
13
14 function showmore_install()
15 {
16         Hook::register('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
17         Hook::register('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
18         Hook::register('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
19 }
20
21 function showmore_uninstall()
22 {
23         Hook::unregister('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
24         Hook::unregister('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
25         Hook::unregister('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
26 }
27
28 function showmore_addon_settings(&$a, &$s)
29 {
30         if (!local_user()) {
31                 return;
32         }
33
34         /* Add our stylesheet to the page so we can make our settings look nice */
35
36         DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="'.DI::baseUrl()->get().'/addon/showmore/showmore.css'.'" media="all"/>'."\r\n";
37
38         $enable_checked = (intval(DI::pConfig()->get(local_user(), 'showmore', 'disable')) ? '' : ' checked="checked"');
39         $chars = DI::pConfig()->get(local_user(), 'showmore', 'chars', 1100);
40
41         $s .= '<span id="settings_showmore_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
42         $s .= '<h3>' . DI::l10n()->t('"Show more" Settings').'</h3>';
43         $s .= '</span>';
44         $s .= '<div id="settings_showmore_expanded" class="settings-block" style="display: none;">';
45         $s .= '<span class="fakelink" onclick="openClose(\'settings_showmore_expanded\'); openClose(\'settings_showmore_inflated\');">';
46         $s .= '<h3>' . DI::l10n()->t('"Show more" Settings').'</h3>';
47         $s .= '</span>';
48
49         $s .= '<div id="showmore-wrapper">';
50
51         $s .= '<label id="showmore-enable-label" for="showmore-enable">'.DI::l10n()->t('Enable Show More').'</label>';
52         $s .= '<input id="showmore-enable" type="checkbox" name="showmore-enable" value="1"'.$enable_checked.' />';
53         $s .= '<div class="clear"></div>';
54         $s .= '<label id="showmore-label" for="showmore-chars">'.DI::l10n()->t('Cutting posts after how much characters').' </label>';
55         $s .= '<input id="showmore-words" type="text" name="showmore-chars" value="'.$chars.'" />';
56         $s .= '</div><div class="clear"></div>';
57
58         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="showmore-submit" name="showmore-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div>';
59 //      $s .= '<div class="showmore-desc">' . DI::l10n()->t('Use /expression/ to provide regular expressions') . '</div>';
60         $s .= '</div>';
61
62         return;
63 }
64
65 function showmore_addon_settings_post(&$a, &$b)
66 {
67         if (!local_user()) {
68                 return;
69         }
70
71         if (!empty($_POST['showmore-submit'])) {
72                 DI::pConfig()->set(local_user(), 'showmore', 'chars', trim($_POST['showmore-chars']));
73                 $enable = (!empty($_POST['showmore-enable']) ? intval($_POST['showmore-enable']) : 0);
74                 $disable = 1-$enable;
75                 DI::pConfig()->set(local_user(), 'showmore', 'disable', $disable);
76         }
77 }
78
79 function get_body_length($body)
80 {
81         $string = trim($body);
82
83         // DomDocument doesn't like empty strings
84         if (!strlen($string)) {
85                 return 0;
86         }
87
88         // We need to get rid of hidden tags (display: none)
89
90         // Get rid of the warning. It would be better to have some valid html as input
91         $dom = @DomDocument::loadHTML($body);
92         $xpath = new DOMXPath($dom);
93
94         /*
95          * Checking any possible syntax of the style attribute with xpath is impossible
96          * So we just get any element with a style attribute, and check them with a regexp
97          */
98         $xr = $xpath->query('//*[@style]');
99         foreach ($xr as $node) {
100                 if (preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
101                         // Hidden, remove it from its parent
102                         $node->parentNode->removeChild($node);
103                 }
104         }
105         // Now we can get the body of our HTML DomDocument, it contains only what is visible
106         $string = $dom->saveHTML();
107
108         $string = strip_tags($string);
109         return strlen($string);
110 }
111
112 function showmore_prepare_body(\Friendica\App $a, &$hook_data)
113 {
114         // No combination with content filters
115         if (!empty($hook_data['filter_reasons'])) {
116                 return;
117         }
118
119         if (DI::pConfig()->get(local_user(), 'showmore', 'disable')) {
120                 return;
121         }
122
123         $chars = (int) DI::pConfig()->get(local_user(), 'showmore', 'chars', 1100);
124
125         if (get_body_length($hook_data['html']) > $chars) {
126                 $found = true;
127                 $shortened = trim(showmore_cutitem($hook_data['html'], $chars)) . "...";
128         } else {
129                 $found = false;
130         }
131
132         if ($found) {
133                 $rnd = Strings::getRandomHex(8);
134                 $hook_data['html'] = '<span id="showmore-teaser-' . $rnd . '" class="showmore-teaser" style="display: block;" aria-hidden="true">' . $shortened . " " .
135                         '<span id="showmore-wrap-' . $rnd . '" style="white-space:nowrap;" class="showmore-wrap fakelink" onclick="openClose(\'showmore-' . $rnd . '\'); openClose(\'showmore-teaser-' . $rnd . '\');" >' . DI::l10n()->t('show more') . '</span></span>' .
136                         '<div id="showmore-' . $rnd . '" class="showmore-content" style="display: none;" aria-hidden="false">' . $hook_data['html'] . '</div>';
137         }
138 }
139
140 function showmore_cutitem($text, $limit)
141 {
142         $text = trim($text);
143
144         $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
145
146         $text = substr($text, 0, $limit);
147
148         $pos1 = strrpos($text, "<");
149         $pos2 = strrpos($text, ">");
150         $pos3 = strrpos($text, "&");
151         $pos4 = strrpos($text, ";");
152
153         if ($pos1 > $pos3) {
154                 if ($pos1 > $pos2)
155                         $text = substr($text, 0, $pos1);
156         } else {
157                 if ($pos3 > $pos4)
158                         $text = substr($text, 0, $pos3);
159         }
160
161         $doc = new DOMDocument();
162         $doc->preserveWhiteSpace = false;
163
164         $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
165         @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
166
167         $text = $doc->saveHTML();
168         $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);
169
170         return $text;
171 }