=?utf-8?q?Gestion=20du=20multipart=20suite=20:
[banana.git] / banana / misc.inc.php
index 9510b14..d90ba56 100644 (file)
@@ -18,6 +18,47 @@ function to_entities($str) {
     return utf8entities(htmlentities($str, ENT_NOQUOTES, 'UTF-8'));
 }
 
+function is_utf8($s) { return iconv('utf-8', 'utf-8', $s) == $s; }
+
+function textFormat_translate($format)
+{
+    switch (strtolower($format)) {
+        case 'plain':       return _b_('Texte brut');
+        case 'richtext':    return _b_('Texte enrichi');
+        case 'html':        return _b_('HTML');
+        default:            return $format;
+    }
+}
+
+/********************************************************************************
+ * HTML STUFF
+ * Taken from php.net
+ */
+
+ /**
+ * @return string
+ * @param string
+ * @desc Strip forbidden tags and delegate tag-source check to removeEvilAttributes()
+ */
+function removeEvilTags($source)
+{
+    $allowedTags = '<h1><b><i><a><ul><li><pre><hr><blockquote><img><br><font><p>';
+    $source = strip_tags($source, $allowedTags);
+    return preg_replace('/<(.*?)>/ie', "'<'.removeEvilAttributes('\\1').'>'", $source);
+}
+
+/**
+ * @return string
+ * @param string
+ * @desc Strip forbidden attributes from a tag
+ */
+function removeEvilAttributes($tagSource)
+{
+    $stripAttrib = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|'.
+                   'onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
+    return stripslashes(preg_replace("/$stripAttrib/i", '', $tagSource));
+}
+
 /********************************************************************************
  *  HEADER STUFF
  */
@@ -33,6 +74,15 @@ function headerDecode($value) {
     return preg_replace('/=\?([^?]*)\?([BQ])\?([^?]*)\?=/e', '_headerdecode("\1", "\2", "\3")', $val);
 }
 
+function headerEncode($value, $trim = 0) {
+    if ($trim) {
+        if (strlen($value) > $trim) {
+            $value = substr($value, 0, $trim) . "[...]";
+        }
+    }
+    return "=?UTF-8?B?".base64_encode($value)."?=";
+}
+
 function header_translate($hdr) {
     switch ($hdr) {
         case 'from':            return _b_('De');
@@ -205,12 +255,21 @@ function wrap($text, $_prefix="")
     return $_prefix.join("\n$_prefix", $result).($_prefix ? '' : $sign);
 }
 
-function formatbody($_text) {
-    $res  = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
+function formatbody($_text, $format='plain')
+{
+    if ($format == 'html') {
+        $res = '<br/>'.removeEvilTags(html_entity_decode(to_entities($_text))).'<br/>';
+    } else {
+        $res  = "\n\n" . to_entities(wrap($_text, ""))."\n\n";
+    }
     $res  = preg_replace("/(&lt;|&gt;|&quot;)/", " \\1 ", $res);
     $res  = preg_replace('/(["\[])?((https?|ftp|news):\/\/[a-z@0-9.~%$£µ&i#\-+=_\/\?]*)(["\]])?/i', "\\1<a href=\"\\2\">\\2</a>\\4", $res);
     $res  = preg_replace("/ (&lt;|&gt;|&quot;) /", "\\1", $res);
-   
+
+    if ($format == 'html') {
+        return $res;
+    }
+
     $parts = preg_split("/\n-- ?\n/", $res);
 
     if (count($parts) > 1) {