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