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