2012-02-24 06:11:26 +00:00
|
|
|
<?php
|
2018-01-04 02:12:19 +00:00
|
|
|
|
|
|
|
require_once 'include/html2bbcode.php';
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2012-03-18 16:52:51 +00:00
|
|
|
function breaklines($line, $level, $wraplength = 75)
|
2012-02-24 06:11:26 +00:00
|
|
|
{
|
2018-01-04 02:12:19 +00:00
|
|
|
if ($wraplength == 0) {
|
2012-03-21 09:08:40 +00:00
|
|
|
$wraplength = 2000000;
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
2012-03-21 09:08:40 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
$wraplen = $wraplength - $level;
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
$newlines = array();
|
|
|
|
|
|
|
|
do {
|
|
|
|
$oldline = $line;
|
|
|
|
|
|
|
|
$subline = substr($line, 0, $wraplen);
|
|
|
|
|
|
|
|
$pos = strrpos($subline, ' ');
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if ($pos == 0) {
|
2012-02-24 06:11:26 +00:00
|
|
|
$pos = strpos($line, ' ');
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2017-06-09 01:03:44 +00:00
|
|
|
if (($pos > 0) && strlen($line) > $wraplen) {
|
2012-02-24 06:11:26 +00:00
|
|
|
$newline = trim(substr($line, 0, $pos));
|
2018-01-04 02:12:19 +00:00
|
|
|
if ($level > 0) {
|
|
|
|
$newline = str_repeat(">", $level) . ' ' . $newline;
|
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
$newlines[] = $newline . " ";
|
|
|
|
$line = substr($line, $pos + 1);
|
2012-02-24 06:11:26 +00:00
|
|
|
}
|
2017-06-09 01:03:44 +00:00
|
|
|
} while ((strlen($line) > $wraplen) && !($oldline == $line));
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if ($level > 0) {
|
|
|
|
$line = str_repeat(">", $level) . ' ' . $line;
|
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
$newlines[] = $line;
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
return implode($newlines, "\n");
|
2012-02-24 06:11:26 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 16:52:51 +00:00
|
|
|
function quotelevel($message, $wraplength = 75)
|
2012-02-24 06:11:26 +00:00
|
|
|
{
|
|
|
|
$lines = explode("\n", $message);
|
|
|
|
|
|
|
|
$newlines = array();
|
|
|
|
$level = 0;
|
2018-01-04 02:12:19 +00:00
|
|
|
foreach ($lines as $line) {
|
2012-02-24 06:11:26 +00:00
|
|
|
$line = trim($line);
|
|
|
|
$startquote = false;
|
2018-01-04 02:12:19 +00:00
|
|
|
while (strpos("*" . $line, '[quote]') > 0) {
|
2012-02-24 06:11:26 +00:00
|
|
|
$level++;
|
|
|
|
$pos = strpos($line, '[quote]');
|
2018-01-04 02:12:19 +00:00
|
|
|
$line = substr($line, 0, $pos) . substr($line, $pos + 7);
|
2012-02-24 06:11:26 +00:00
|
|
|
$startquote = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$currlevel = $level;
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
while (strpos("*" . $line, '[/quote]') > 0) {
|
2012-02-24 06:11:26 +00:00
|
|
|
$level--;
|
2018-01-04 02:12:19 +00:00
|
|
|
if ($level < 0) {
|
2012-02-24 06:11:26 +00:00
|
|
|
$level = 0;
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
$pos = strpos($line, '[/quote]');
|
2018-01-04 02:12:19 +00:00
|
|
|
$line = substr($line, 0, $pos) . substr($line, $pos + 8);
|
2012-02-24 06:11:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if (!$startquote || ($line != '')) {
|
2012-03-18 16:52:51 +00:00
|
|
|
$newlines[] = breaklines($line, $currlevel, $wraplength);
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
}
|
2018-01-04 02:12:19 +00:00
|
|
|
|
|
|
|
return implode($newlines, "\n");
|
2012-02-24 06:11:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
function collecturls($message)
|
|
|
|
{
|
2012-03-21 09:08:40 +00:00
|
|
|
$pattern = '/<a.*?href="(.*?)".*?>(.*?)<\/a>/is';
|
|
|
|
preg_match_all($pattern, $message, $result, PREG_SET_ORDER);
|
|
|
|
|
|
|
|
$urls = array();
|
|
|
|
foreach ($result as $treffer) {
|
2013-10-19 05:11:50 +00:00
|
|
|
$ignore = false;
|
|
|
|
|
2012-03-21 09:08:40 +00:00
|
|
|
// A list of some links that should be ignored
|
2012-04-24 05:41:32 +00:00
|
|
|
$list = array("/user/", "/tag/", "/group/", "/profile/", "/search?search=", "/search?tag=", "mailto:", "/u/", "/node/",
|
2018-01-04 02:12:19 +00:00
|
|
|
"//facebook.com/profile.php?id=", "//plus.google.com/", "//twitter.com/");
|
|
|
|
foreach ($list as $listitem) {
|
|
|
|
if (strpos($treffer[1], $listitem) !== false) {
|
2012-03-21 09:08:40 +00:00
|
|
|
$ignore = true;
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-21 09:08:40 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if ((strpos($treffer[1], "//twitter.com/") !== false) && (strpos($treffer[1], "/status/") !== false)) {
|
|
|
|
$ignore = false;
|
|
|
|
}
|
2013-10-14 22:44:46 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/posts") !== false)) {
|
|
|
|
$ignore = false;
|
|
|
|
}
|
2012-03-31 14:28:39 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if ((strpos($treffer[1], "//plus.google.com/") !== false) && (strpos($treffer[1], "/photos") !== false)) {
|
|
|
|
$ignore = false;
|
|
|
|
}
|
2012-11-04 22:18:52 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if (!$ignore) {
|
2012-03-21 09:08:40 +00:00
|
|
|
$urls[$treffer[1]] = $treffer[1];
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
2012-03-21 09:08:40 +00:00
|
|
|
}
|
2018-01-04 02:12:19 +00:00
|
|
|
|
|
|
|
return $urls;
|
2012-03-21 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function html2plain($html, $wraplength = 75, $compact = false)
|
2012-02-24 06:11:26 +00:00
|
|
|
{
|
|
|
|
global $lang;
|
|
|
|
|
|
|
|
$message = str_replace("\r", "", $html);
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
$doc->preserveWhiteSpace = false;
|
|
|
|
|
|
|
|
$message = mb_convert_encoding($message, 'HTML-ENTITIES', "UTF-8");
|
|
|
|
|
|
|
|
@$doc->loadHTML($message);
|
|
|
|
|
|
|
|
$xpath = new DomXPath($doc);
|
|
|
|
$list = $xpath->query("//pre");
|
|
|
|
foreach ($list as $node) {
|
|
|
|
$node->nodeValue = str_replace("\n", "\r", $node->nodeValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = $doc->saveHTML();
|
|
|
|
$message = str_replace(array("\n<", ">\n", "\r", "\n", "\xC3\x82\xC2\xA0"), array("<", ">", "<br>", " ", ""), $message);
|
|
|
|
$message = preg_replace('= [\s]*=i', " ", $message);
|
|
|
|
|
2012-03-21 09:08:40 +00:00
|
|
|
// Collecting all links
|
|
|
|
$urls = collecturls($message);
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
@$doc->loadHTML($message);
|
|
|
|
|
|
|
|
node2bbcode($doc, 'html', array(), '', '');
|
|
|
|
node2bbcode($doc, 'body', array(), '', '');
|
|
|
|
|
|
|
|
// MyBB-Auszeichnungen
|
2012-03-18 16:52:51 +00:00
|
|
|
/*
|
2018-01-04 02:12:19 +00:00
|
|
|
node2bbcode($doc, 'span', array('style'=>'text-decoration: underline;'), '_', '_');
|
|
|
|
node2bbcode($doc, 'span', array('style'=>'font-style: italic;'), '/', '/');
|
|
|
|
node2bbcode($doc, 'span', array('style'=>'font-weight: bold;'), '*', '*');
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
node2bbcode($doc, 'strong', array(), '*', '*');
|
|
|
|
node2bbcode($doc, 'b', array(), '*', '*');
|
|
|
|
node2bbcode($doc, 'i', array(), '/', '/');
|
|
|
|
node2bbcode($doc, 'u', array(), '_', '_');
|
|
|
|
*/
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if ($compact) {
|
2012-03-21 09:08:40 +00:00
|
|
|
node2bbcode($doc, 'blockquote', array(), "»", "«");
|
2018-01-04 02:12:19 +00:00
|
|
|
} else {
|
2012-03-21 09:08:40 +00:00
|
|
|
node2bbcode($doc, 'blockquote', array(), '[quote]', "[/quote]\n");
|
2018-01-04 02:12:19 +00:00
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
node2bbcode($doc, 'br', array(), "\n", '');
|
|
|
|
|
|
|
|
node2bbcode($doc, 'span', array(), "", "");
|
|
|
|
node2bbcode($doc, 'pre', array(), "", "");
|
|
|
|
node2bbcode($doc, 'div', array(), "\r", "\r");
|
|
|
|
node2bbcode($doc, 'p', array(), "\n", "\n");
|
|
|
|
|
|
|
|
//node2bbcode($doc, 'ul', array(), "\n[list]", "[/list]\n");
|
|
|
|
//node2bbcode($doc, 'ol', array(), "\n[list=1]", "[/list]\n");
|
|
|
|
node2bbcode($doc, 'li', array(), "\n* ", "\n");
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
node2bbcode($doc, 'hr', array(), "\n" . str_repeat("-", 70) . "\n", "");
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
node2bbcode($doc, 'tr', array(), "\n", "");
|
|
|
|
node2bbcode($doc, 'td', array(), "\t", "");
|
|
|
|
|
|
|
|
node2bbcode($doc, 'h1', array(), "\n\n*", "*\n");
|
|
|
|
node2bbcode($doc, 'h2', array(), "\n\n*", "*\n");
|
|
|
|
node2bbcode($doc, 'h3', array(), "\n\n*", "*\n");
|
|
|
|
node2bbcode($doc, 'h4', array(), "\n\n*", "*\n");
|
|
|
|
node2bbcode($doc, 'h5', array(), "\n\n*", "*\n");
|
|
|
|
node2bbcode($doc, 'h6', array(), "\n\n*", "*\n");
|
|
|
|
|
2012-03-18 16:52:51 +00:00
|
|
|
// Problem: there is no reliable way to detect if it is a link to a tag or profile
|
2014-05-18 16:57:31 +00:00
|
|
|
//node2bbcode($doc, 'a', array('href'=>'/(.+)/'), ' $1 ', ' ', true);
|
|
|
|
//node2bbcode($doc, 'a', array('href'=>'/(.+)/', 'rel'=>'oembed'), ' $1 ', '', true);
|
2012-03-18 16:52:51 +00:00
|
|
|
//node2bbcode($doc, 'img', array('alt'=>'/(.+)/'), '$1', '');
|
|
|
|
//node2bbcode($doc, 'img', array('title'=>'/(.+)/'), '$1', '');
|
|
|
|
//node2bbcode($doc, 'img', array(), '', '');
|
2018-01-04 02:12:19 +00:00
|
|
|
if (!$compact) {
|
|
|
|
node2bbcode($doc, 'img', array('src' => '/(.+)/'), ' [img]$1', '[/img] ');
|
|
|
|
} else {
|
|
|
|
node2bbcode($doc, 'img', array('src' => '/(.+)/'), ' ', ' ');
|
|
|
|
}
|
2012-03-21 09:08:40 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
node2bbcode($doc, 'iframe', array('src' => '/(.+)/'), ' $1 ', '', true);
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
$message = $doc->saveHTML();
|
|
|
|
|
2012-03-21 09:08:40 +00:00
|
|
|
if (!$compact) {
|
|
|
|
$message = str_replace("[img]", "", $message);
|
|
|
|
$message = str_replace("[/img]", "", $message);
|
|
|
|
}
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
// was ersetze ich da?
|
|
|
|
// Irgendein stoerrisches UTF-Zeug
|
2018-01-04 02:12:19 +00:00
|
|
|
$message = str_replace(chr(194) . chr(160), ' ', $message);
|
2012-02-24 06:11:26 +00:00
|
|
|
|
|
|
|
$message = str_replace(" ", " ", $message);
|
|
|
|
|
|
|
|
// Aufeinanderfolgende DIVs
|
|
|
|
$message = preg_replace('=\r *\r=i', "\n", $message);
|
|
|
|
$message = str_replace("\r", "\n", $message);
|
|
|
|
|
|
|
|
$message = strip_tags($message);
|
|
|
|
|
|
|
|
$message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
|
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
if (!$compact && ($message != '')) {
|
|
|
|
foreach ($urls as $id => $url) {
|
|
|
|
if ($url != '' && strpos($message, $url) === false) {
|
|
|
|
$message .= "\n" . $url . ' ';
|
|
|
|
}
|
|
|
|
}
|
2012-03-21 09:08:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-28 18:38:48 +00:00
|
|
|
$message = str_replace("\n«", "«\n", $message);
|
|
|
|
$message = str_replace("»\n", "\n»", $message);
|
|
|
|
|
2012-02-24 06:11:26 +00:00
|
|
|
do {
|
|
|
|
$oldmessage = $message;
|
|
|
|
$message = str_replace("\n\n\n", "\n\n", $message);
|
|
|
|
} while ($oldmessage != $message);
|
|
|
|
|
2012-03-18 16:52:51 +00:00
|
|
|
$message = quotelevel(trim($message), $wraplength);
|
2012-02-24 06:11:26 +00:00
|
|
|
|
2018-01-04 02:12:19 +00:00
|
|
|
return trim($message);
|
2012-02-24 06:11:26 +00:00
|
|
|
}
|