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