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