]> git.mxchange.org Git - friendica-addons.git/blob - showmore/showmore.php
Merge pull request 'Initial commit of url_replace addon.' (#1453) from toddy/friendic...
[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\App;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15 use Friendica\Util\Strings;
16
17 function showmore_install()
18 {
19         Hook::register('prepare_body', 'addon/showmore/showmore.php', 'showmore_prepare_body');
20         Hook::register('addon_settings', 'addon/showmore/showmore.php', 'showmore_addon_settings');
21         Hook::register('addon_settings_post', 'addon/showmore/showmore.php', 'showmore_addon_settings_post');
22 }
23
24 function showmore_addon_settings(array &$data)
25 {
26         if (!DI::userSession()->getLocalUserId()) {
27                 return;
28         }
29
30         DI::page()->registerStylesheet(__DIR__ . '/showmore.css', 'all');
31
32         $enabled = !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'disable');
33         $chars   = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'chars', 1100);
34
35         $t    = Renderer::getMarkupTemplate('settings.tpl', 'addon/showmore/');
36         $html = Renderer::replaceMacros($t, [
37                 '$enabled' => ['showmore-enable', DI::l10n()->t('Enable Show More'), $enabled],
38                 '$chars'   => ['showmore-chars', DI::l10n()->t('Cutting posts after how many characters'), $chars],
39         ]);
40
41         $data = [
42                 'addon' => 'showmore',
43                 'title' => DI::l10n()->t('"Show more" Settings'),
44                 'html'  => $html,
45         ];
46 }
47
48 function showmore_addon_settings_post(array &$b)
49 {
50         if (!DI::userSession()->getLocalUserId()) {
51                 return;
52         }
53
54         if (!empty($_POST['showmore-submit'])) {
55                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'showmore', 'chars', trim($_POST['showmore-chars']));
56                 $enable = (!empty($_POST['showmore-enable']) ? intval($_POST['showmore-enable']) : 0);
57                 $disable = 1-$enable;
58                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'showmore', 'disable', $disable);
59         }
60 }
61
62 function get_body_length($body)
63 {
64         $string = trim($body);
65
66         // DomDocument doesn't like empty strings
67         if (!strlen($string)) {
68                 return 0;
69         }
70
71         // We need to get rid of hidden tags (display: none)
72
73         // Get rid of the warning. It would be better to have some valid html as input
74         $doc = new DOMDocument();
75         @$doc->loadHTML($body);
76         $xpath = new DOMXPath($doc);
77
78         /*
79          * Checking any possible syntax of the style attribute with xpath is impossible
80          * So we just get any element with a style attribute, and check them with a regexp
81          */
82         $xr = $xpath->query('//*[@style]');
83         foreach ($xr as $node) {
84                 if (preg_match('/.*display: *none *;.*/',$node->getAttribute('style'))) {
85                         // Hidden, remove it from its parent
86                         $node->parentNode->removeChild($node);
87                 }
88         }
89         // Now we can get the body of our HTML DomDocument, it contains only what is visible
90         $string = $doc->saveHTML();
91
92         $string = strip_tags($string);
93         return strlen($string);
94 }
95
96 function showmore_prepare_body(&$hook_data)
97 {
98         // No combination with content filters
99         if (!empty($hook_data['filter_reasons'])) {
100                 return;
101         }
102
103         if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'disable')) {
104                 return;
105         }
106
107         $chars = (int) DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'showmore', 'chars', 1100);
108
109         if (get_body_length($hook_data['html']) > $chars) {
110                 $found = true;
111                 $shortened = trim(showmore_cutitem($hook_data['html'], $chars)) . "...";
112         } else {
113                 $found = false;
114         }
115
116         if ($found) {
117                 $rnd = Strings::getRandomHex(8);
118                 $hook_data['html'] = '<span id="showmore-teaser-' . $rnd . '" class="showmore-teaser" style="display: block;" aria-hidden="true" dir="auto">' . $shortened . " " .
119                         '<span id="showmore-wrap-' . $rnd . '" style="white-space:nowrap;" class="showmore-wrap fakelink" onclick="openClose(\'showmore-' . $rnd . '\'); openClose(\'showmore-teaser-' . $rnd . '\');">' . DI::l10n()->t('show more') . '</span></span>' .
120                         '<div id="showmore-' . $rnd . '" class="showmore-content" style="display: none;" aria-hidden="false" dir="auto">' . $hook_data['html'] . '</div>';
121         }
122 }
123
124 function showmore_cutitem($text, $limit)
125 {
126         $text = trim($text);
127
128         $text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
129
130         $text = substr($text, 0, $limit);
131
132         $pos1 = strrpos($text, "<");
133         $pos2 = strrpos($text, ">");
134         $pos3 = strrpos($text, "&");
135         $pos4 = strrpos($text, ";");
136
137         if ($pos1 > $pos3) {
138                 if ($pos1 > $pos2)
139                         $text = substr($text, 0, $pos1);
140         } else {
141                 if ($pos3 > $pos4)
142                         $text = substr($text, 0, $pos3);
143         }
144
145         $doc = new DOMDocument();
146         $doc->preserveWhiteSpace = false;
147
148         $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
149         @$doc->loadHTML($doctype."<html><body>".$text."</body></html>");
150
151         $text = $doc->saveHTML();
152         $text = str_replace(["<html><body>", "</body></html>", $doctype], ["", "", ""], $text);
153
154         return $text;
155 }