<?php
/*
   The Index software may be freely distributed.
   See license.txt for details.
*/

function escapequotes($string)
{
    return str_replace("'", "\'", $string);
}

function html2text($string)
{
    // Remove links
    $string = ereg_replace("<a href=[^>]*>", "", $string);
    $string = ereg_replace("</a>", "", $string);
    // escape /*_ chars (but NOT / after < or before >)
    $string = ereg_replace("([^<])([*_/])([^>])", "\\1\\\\\\2\\3", $string);
    // bold
    $string = ereg_replace("<b>", "*", $string);
    $string = ereg_replace("</b>", "*", $string);
    // underline
    $string = ereg_replace("<u>", "_", $string);
    $string = ereg_replace("</u>", "_", $string);
    // italic
    $string = ereg_replace("<i>", "/", $string);
    $string = ereg_replace("</i>", "/", $string);
    // newlines: php retains \n's, so just remove
    $string = ereg_replace("<br>", "", $string);
    $string = ereg_replace("<br />", "", $string);

    return $string;
}

function text2html($string)
{
    $domain = "(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9])+";
    $path = "[^\t \r\n<>]+[^\t \r\n,.:;'\"\)<>!]";
    
    $substitutes = array(
    // Escaped char: \char => char
    array(
        'in' => "\\\\(.)",
        'out' => "\\1",
        'contains' => 0
    ),
    // mail
    array(
        'in' => "(([a-zA-Z0-9_.-])+@$domain)",
        'out' => "<a href=\"mailto:\\1\">\\1</a>",
        'contains' => 0
    ),
    // link
    array(
        'in' => "((http|ftp|https|gopher|news|telnet|wais)://$domain(:[0-9]*)?/?($path)?)",
        'out' => "<a href=\"\\1\">\\1</a>",
        'contains' => 0
    ),
    // bold: * *
    array(
        'in' => "\*([^*]*[^*\])\*",
        'out' => "<b>\\1</b>",
        'contains' => 1
    ),
    // underline: _ _
    array(
        'in' => "_([^_]*[^_\])_",
        'out' => "<u>\\1</u>",
        'contains' => 1
    ),
    // italic: / /
    array(
        'in' => "/([^/]*[^</\])/",
        'out' => "<i>\\1</i>",
        'contains' => 1
    )
    );

    // Scrape off special things one by one
    while (strlen($string))
    {
        $changed = false;

        reset($substitutes);
        while (list(, $subst) = each($substitutes))
        {
            // Find a special pattern
            if (ereg("^" . $subst['in'], $string, $match))
            {
                // Found: Change and add to newstring
                // 1. Remove from string
                $string = substr($string, strlen($match[0]));
                // 2. Replace in chunk
                $done = ereg_replace("^" . $subst['in'], $subst['out'], $match[0]);
                // 3. Recursively process if allowed
                if ($subst['contains'])
                {
                    $done = text2html($done);
                }
                // 3. Add to new string
                $string2 .= $done;
                $changed = true;
                break;
            }
        }
        // If not changed, copy one character
        if (!$changed)
        {
            $string2 .= $string[0];
            $string = substr($string, 1);
        }
    }
    $string2 = nl2br($string2);
    return $string2;
}


function removelocallinks($string)
{
    $string = ereg_replace("<a href=\"item\.php\?key=[^>]+>([^<]+)</a>", "[\\1]", $string);

    return $string;
}


function addlocallinks($string)
{
    global $combilist;

    $skip = array(
        "the" => 1,
        "nethack" => 1,
        // Order matters: superstrings first please.
        "homepage" => 1,
        "page" => 1,
        "site" => 1
    );

    while (ereg("(.*)\[(.*)\](.*)", $string, $regs))
    {
        $hits = array();
        $words = explode(" ", $regs[2]);
        for ($i = 0; $i < count($words); $i++)
        {
            if ($skip[strtolower($words[$i])])
                continue;
            $words[$i] = ereg_replace("'s$", "", $words[$i]);
            $list2 = $combilist;
            reset($list2);
            while (list($key, $value) = each($list2))
            {
                if ($words[$i] and stristr($value['name'], $words[$i]))
                {
                    $hits[$key]++;
                    //$scorelog .= " matched $words[$i] in name ";
                }
                if ($words[$i] and stristr($value['author'], $words[$i]))
                {
                    $hits[$key]++;
                    //$scorelog .= " matched $words[$i] in author ";
                }
                if ($hits[$key] == 1)
                {
                    // Store the page name's length, so we can match 
                    // shortest first.
                    // Remove meaningless words.
                    $site = $value['name'];
                    reset($skip);
                    while (list($rm,) = each($skip))
                    {
                        $site = eregi_replace($rm, "", $site);
                    }
                    $len[$key] = strlen($site);
                }
            }
        }
        $maxhits = 0;
        $maxkey = 0;
        if (is_array($hits))
        {
            while (list($key, $value) = each($hits))
            {
                if (($value > $maxhits) 
                 or (($value == $maxhits and $len[$key] < $len[$maxkey])))
                {
                    /*if (($value == $maxhits))
                    {
                        $scorelog .= " $maxkey and $key both match; $len[$maxkey] > $len[$key] ";
                    }
                    else
                    {
                        $scorelog .= " $key and $maxkey: $value > $maxhits ";
                    }*/
                    $maxhits = $value;
                    $maxkey = $key;
                }
            }
        }
        if ($maxhits > 0)
            $link = "<a href=\"item.php?key=$maxkey\">$regs[2]</a>";
        else
            $link = "$regs[2]";
                
        
        $string = $regs[1] . $link . $regs[3];
    }
    return $string;
}

function submit2text($string)
{
    $string = stripslashes($string);
    $string = htmlentities($string);
    return $string;
}
?>