]> git.mxchange.org Git - friendica.git/blob - include/html2plain.php
Merge pull request #185 from fabrixxm/master
[friendica.git] / include / html2plain.php
1 <?php
2 require_once "html2bbcode.php";
3
4 function breaklines($line, $level, $wraplength = 75)
5 {
6
7         if ($wraplength == 0)
8                 $wraplength = 2000000;
9
10         //      return($line);
11
12         $wraplen = $wraplength-$level;
13
14         $newlines = array();
15
16         do {
17                 $oldline = $line;
18
19                 $subline = substr($line, 0, $wraplen);
20
21                 $pos = strrpos($subline, ' ');
22
23                 if ($pos == 0)
24                         $pos = strpos($line, ' ');
25
26                 if (($pos > 0) and strlen($line) > $wraplen) {
27                         $newline = trim(substr($line, 0, $pos));
28                         if ($level > 0)
29                                 $newline = str_repeat(">", $level).' '.$newline;
30
31                         $newlines[] = $newline." ";
32                         $line = substr($line, $pos+1);
33                 }
34
35         } while ((strlen($line) > $wraplen) and !($oldline == $line));
36
37         if ($level > 0)
38                 $line = str_repeat(">", $level).' '.$line;
39
40         $newlines[] = $line;
41
42
43         return(implode($newlines, "\n"));
44 }
45
46 function quotelevel($message, $wraplength = 75)
47 {
48         $lines = explode("\n", $message);
49
50         $newlines = array();
51         $level = 0;
52         foreach($lines as $line) {;
53                 $line = trim($line);
54                 $startquote = false;
55                 while (strpos("*".$line, '[quote]') > 0) {
56                         $level++;
57                         $pos = strpos($line, '[quote]');
58                         $line = substr($line, 0, $pos).substr($line, $pos+7);
59                         $startquote = true;
60                 }
61
62                 $currlevel = $level;
63
64                 while (strpos("*".$line, '[/quote]') > 0) {
65                         $level--;
66                         if ($level < 0)
67                                 $level = 0;
68
69                         $pos = strpos($line, '[/quote]');
70                         $line = substr($line, 0, $pos).substr($line, $pos+8);
71                 }
72
73                 if (!$startquote or ($line != ''))
74                         $newlines[] = breaklines($line, $currlevel, $wraplength);
75         }
76         return(implode($newlines, "\n"));
77 }
78
79 function collecturls($message) {
80         $pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
81         preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
82
83         $urls = array();
84         foreach ($result as $treffer) {
85                 // A list of some links that should be ignored
86                 $list = array("/user/", "/tag/", "/profile/", "/search?search=", "mailto:", "/u/", "/node/",
87                                 "//facebook.com/profile.php?id=", "//plus.google.com/");
88                 foreach ($list as $listitem)
89                         if (strpos($treffer[1], $listitem) !== false)
90                                 $ignore = true;
91
92                 if (!$ignore)
93                         $urls[$treffer[1]] = $treffer[1];
94         }
95         return($urls);
96 }
97
98 function html2plain($html, $wraplength = 75, $compact = false)
99 {
100         global $lang;
101
102         $message = str_replace("\r", "", $html);
103
104         $doc = new DOMDocument();
105         $doc->preserveWhiteSpace = false;
106
107         $message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
108
109         @$doc->loadHTML($message);
110
111         $xpath = new DomXPath($doc);
112         $list = $xpath->query("//pre");
113         foreach ($list as $node) {
114                 $node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
115         }
116
117         $message = $doc->saveHTML();
118         $message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
119         $message = preg_replace('= [\s]*=i', " ", $message);
120
121         // Collecting all links
122         $urls = collecturls($message);
123
124         @$doc->loadHTML($message);
125
126         node2bbcode($doc, 'html', array(), '', '');
127         node2bbcode($doc, 'body', array(), '', '');
128
129         // MyBB-Auszeichnungen
130         /*
131         node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
132         node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
133         node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
134
135         node2bbcode($doc, 'strong', array(), '*', '*');
136         node2bbcode($doc, 'b', array(), '*', '*');
137         node2bbcode($doc, 'i', array(), '/', '/');
138         node2bbcode($doc, 'u', array(), '_', '_');
139         */
140
141         if ($compact)
142                 node2bbcode($doc, 'blockquote', array(), "»", "«");
143         else
144                 node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
145
146         node2bbcode($doc, 'br', array(), "\n", '');
147
148         node2bbcode($doc, 'span', array(), "", "");
149         node2bbcode($doc, 'pre', array(), "", "");
150         node2bbcode($doc, 'div', array(), "\r", "\r");
151         node2bbcode($doc, 'p', array(), "\n", "\n");
152
153         //node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
154         //node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
155         node2bbcode($doc, 'li', array(), "\n* ", "\n");
156
157         node2bbcode($doc, 'hr', array(), str_repeat("-", 70), "");
158
159         node2bbcode($doc, 'tr', array(), "\n", "");
160         node2bbcode($doc, 'td', array(), "\t", "");
161
162         node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
163         node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
164         node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
165         node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
166         node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
167         node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
168
169         // Problem: there is no reliable way to detect if it is a link to a tag or profile
170         //node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', '', true);
171         node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
172         //node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
173         //node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
174         //node2bbcode($doc, 'img', array(), '', '');
175         if (!$compact)
176                 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '[img]$1', '[/img]');
177         else
178                 node2bbcode($doc, 'img', array('src'=>'/(.+)/'), '', '');
179
180         node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), ' $1 ', '', true);
181
182         $message = $doc->saveHTML();
183
184         if (!$compact) {
185                 $message = str_replace("[img]", "", $message);
186                 $message = str_replace("[/img]", "", $message);
187         }
188
189         // was ersetze ich da?
190         // Irgendein stoerrisches UTF-Zeug
191         $message = str_replace(chr(194).chr(160), ' ', $message);
192
193         $message = str_replace("&nbsp;", " ", $message);
194
195         // Aufeinanderfolgende DIVs
196         $message = preg_replace('=\r *\r=i', "\n", $message);
197         $message = str_replace("\r", "\n", $message);
198
199         $message = strip_tags($message);
200
201         $message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
202
203         if (!$compact) {
204                 $counter = 1;
205                 foreach ($urls as $id=>$url)
206                         if (strpos($message, $url) == false)
207                                 $message .= "\n".$url." ";
208                                 //$message .= "\n[".($counter++)."] ".$url;
209         }
210
211         do {
212                 $oldmessage = $message;
213                 $message = str_replace("\n\n\n", "\n\n", $message);
214         } while ($oldmessage != $message);
215
216         $message = quotelevel(trim($message), $wraplength);
217
218         return(trim($message));
219 }
220 ?>