]> git.mxchange.org Git - friendica-addons.git/blob - showmore/showmore.php
Merge commit 'upstream/master'
[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></div>';
50
51         return;
52 }
53
54 function showmore_addon_settings_post(&$a,&$b) {
55
56         if(! local_user())
57                 return;
58
59         if($_POST['showmore-submit']) {
60                 set_pconfig(local_user(),'showmore','chars',trim($_POST['showmore-chars']));
61                 $enable = ((x($_POST,'showmore-enable')) ? intval($_POST['showmore-enable']) : 0);
62                 $disable = 1-$enable;
63                 set_pconfig(local_user(),'showmore','disable', $disable);
64                 info( t('Show More Settings saved.') . EOL);
65         }
66 }
67
68 function showmore_prepare_body(&$a,&$b) {
69
70         $words = null;
71         if(get_pconfig(local_user(),'showmore','disable'))
72                 return;
73
74         $chars = (int)get_pconfig(local_user(),'showmore','chars');
75         if(!$chars)
76                 $chars = 1100;
77
78         if (strlen(strip_tags(trim($b['html']))) > $chars) {
79                 $found = true;
80                 $shortened = trim(showmore_cutitem($b['html'], $chars))."...";
81         }
82
83         if($found) {
84                 $rnd = random_string(8);
85                 $b['html'] = '<span id="showmore-teaser-'.$rnd.'" style="display: block;">'.$shortened." ".
86                                 '<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>'.
87                                 '<div id="showmore-'.$rnd.'" style="display: none;">'.$b['html'].'</div>';
88         }
89 }
90
91 function showmore_cutitem($text, $limit) {
92         $text = trim($text);
93
94         $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
95
96         $text = substr($text, 0, $limit);
97
98         $pos1 = strrpos($text, "<");
99         $pos2 = strrpos($text, ">");
100         $pos3 = strrpos($text, "&");
101         $pos4 = strrpos($text, ";");
102
103         if ($pos1 > $pos3) {
104                 if ($pos1 > $pos2)
105                         $text = substr($text, 0, $pos1);
106         } else {
107                 if ($pos3 > $pos4)
108                         $text = substr($text, 0, $pos3);
109         }
110
111         $doc = new DOMDocument();
112         $doc->preserveWhiteSpace = false;
113
114         $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
115         @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
116
117         $text = $doc->saveHTML();
118         $text = str_replace(array("<html><body>", "</body></html>", $doctype), array("", "", ""), $text);
119
120         return($text);
121 }