+function to_entities($str) {
+ require_once dirname(__FILE__).'/utf8.php';
+ 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;
+ }
+}
+
+/** Redirect to the page with the given parameter
+ * @ref makeLink
+ */
+function redirectInBanana($params)
+{
+ header('Location: ' . makeLink($params));
+}
+
+/** Make a link using the given parameters
+ * @param ARRAY params, the parameters with
+ * key => value
+ * Known key are:
+ * - group = group name
+ * - artid/first = article id the the group
+ * - subscribe = to show the subscription page
+ * - action = action to do (new, cancel, view)
+ * - part = to show the given MIME part of the article
+ * - pj = to get the given attachment
+ * - xface = to make a link to an xface
+ *
+ * Can be overloaded by defining a hook_makeLink function
+ */
+function makeLink($params)
+{
+ if (function_exists('hook_makeLink')
+ && $res = hook_makeLink($params)) {
+ return $res;
+ }
+ $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
+ $host = $_SERVER['HTTP_HOST'];
+ $file = $_SERVER['PHP_SELF'];
+
+ if (isset($params['xface'])) {
+ $file = dirname($file) . '/xface.php';
+ $get = 'face=' . urlencode(base64_encode($params['xface']));
+ } else if (count($params) != 0) {
+ $get = '?';
+ foreach ($params as $key=>$value) {
+ if (strlen($get) != 1) {
+ $get .= '&';
+ }
+ $get .= $key . '=' . $value;
+ }
+ } else {
+ $get = '';
+ }
+
+ return $proto . $host . $file . $get;
+}
+
+/** Format a link to be use in a link
+ * @ref makeLink
+ */
+function makeHREF($params, $text = null, $popup = null, $class = null)
+{
+ $link = makeLink($params);
+ if (is_null($text)) {
+ $text = $link;
+ }
+ if (!is_null($popup)) {
+ $popup = ' title="' . $popup . '"';
+ }
+ if (!is_null($class)) {
+ $class = ' class="' . $class . '"';
+ }
+ $target = null;
+ if (isset($params['action']) && $params['action'] == 'view') {
+ $target = ' target="_blank"';
+ }
+ return '<a href="' . htmlentities($link) . $target . '"' . $popup . $class . '>' . $text . '</a>';
+}
+
+/** Format tree images links
+ * @param img STRING Image name (without extension)
+ * @param alt STRING alternative string
+ * @param width INT to force width of the image (null if not defined)
+ *
+ * This function can be overloaded by defining hook_makeImg()
+ */
+function makeImg($img, $alt, $height = null, $width = null)
+{
+ if (function_exists('hook_makeImg')
+ && $res = hook_makeImg($img, $alt, $height, $width)) {
+ return $res;
+ }
+
+ if (!is_null($width)) {
+ $width = ' width="' . $width . '"';
+ }
+ if (!is_null($height)) {
+ $height = ' height="' . $height . '"';
+ }
+
+ $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
+ $host = $_SERVER['HTTP_HOST'];
+ $file = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
+ $url = $proto . $host . $file;
+
+ return '<img src="' . $url . '"' . $height . $width . ' alt="' . $alt . '" />';
+}
+
+/** Make a link using an image
+ */
+function makeImgLink($params, $img, $alt, $height = null, $width = null, $class = null)
+{
+ return makeHREF($params,
+ makeImg($img, ' [' . $alt . ']', $height, $width),
+ $alt,
+ $class);
+}
+
+/********************************************************************************
+ * 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><small><big><sup><sub><code><em>';
+ $source = preg_replace('|</div>|i', '<br />', $source);
+ $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));
+}
+
+/** Convert html to plain text
+ */
+function htmlToPlainText($res)
+{
+ $res = trim(html_entity_decode(strip_tags($res, '<div><br><p>')));
+ $res = preg_replace("@</?(br|p|div)[^>]*>@i", "\n", $res);
+ if (!is_utf8($res)) {
+ $res = utf8_encode($res);
+ }
+ return $res;
+}
+
+/** Match **, // and __ to format plain text
+ */
+function formatPlainText($text)
+{
+ $formatting = Array('\*' => 'strong',
+ '_' => 'u',
+ '/' => 'em');
+ foreach ($formatting as $limit=>$mark) {
+ $text = preg_replace('@(^|\W)' . $limit . '(\w+)' . $limit . '(\W|$)@'
+ ,'\1<' . $mark . '>\2</' . $mark . '>\3'
+ , $text);
+ }
+ return $text;
+}
+
+/********************************************************************************
+ * RICHTEXT STUFF
+ */
+
+/** Convert richtext to html
+ */
+function richtextToHtml($source)
+{
+ $tags = Array('bold' => 'b',
+ 'italic' => 'i',
+ 'smaller' => 'small',
+ 'bigger' => 'big',
+ 'underline' => 'u',
+ 'subscript' => 'sub',
+ 'superscript' => 'sup',
+ 'excerpt' => 'blockquote',
+ 'paragraph' => 'p',
+ 'nl' => 'br'
+ );
+
+ // clean unsupported tags
+ $protectedTags = '<signature><lt><comment><'.join('><', array_keys($tags)).'>';
+ $source = strip_tags($source, $protectedTags);
+
+ // convert richtext tags to html
+ foreach (array_keys($tags) as $tag) {
+ $source = preg_replace('@(</?)'.$tag.'([^>]*>)@i', '\1'.$tags[$tag].'\2', $source);
+ }
+
+ // some special cases
+ $source = preg_replace('@<signature>@i', '<br>-- <br>', $source);
+ $source = preg_replace('@</signature>@i', '', $source);
+ $source = preg_replace('@<lt>@i', '<', $source);
+ $source = preg_replace('@<comment[^>]*>((?:[^<]|<(?!/comment>))*)</comment>@i', '<!-- \1 -->', $source);
+ return removeEvilAttributes($source);