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