<?php

//what started as a simple little include to take a random post and trim it if it is too long turned into the following because I started hunting for words and puctuation...


$resultb=mysql_query("SELECT * FROM $forumtable WHERE strName = '$userid' AND intPostHidden = '0'");
$num_rowsb = mysql_num_rows( $resultb );
if ($num_rowsb == 0)
{
    exit;
}
$id = rand(1, $num_rowsb);

$resultb=mysql_query("SELECT * FROM $forumtable WHERE strName = '$userid' LIMIT $id, 1");
$num_rowsb = mysql_num_rows( $resultb );
while ( $b_row = mysql_fetch_object( $resultb ) )
{
    $gallery = 0;
    $imtitle = 0;
    $postthis = $b_row->memPost;
    $topicid = $b_row->intTopicId;
    $postid = $b_row->intId;
    if ($b_row->strGalleryImage != "")
    {
        $gallery = 1;
        $imtitle = "$b_row->strTopicName";
    }
}

if (strlen($postthis) > 25)
{
    $out = 0;
    $countfirst = 0;
    while ($first != " ")
    {
        $text = "";
        $end = strlen($postthis) - 25;
        if ($end < 1)
        {
            $end = 1;
        }
        $textstart = rand(0, $end);
        $text = substr("$postthis", $textstart, 26);
        $first = substr("$text", 0, 1);
        $countfirst ++;
        if ($countfirst > 750)
        {
            $out = 1;
            break;
        }
    }
    if ($out == 0)
    {
        $postthis = substr("$text", 1);
        $end = strlen($postthis);
        $countfirst = 0;
        while ($last != " " OR $last != "." OR $last != "?" OR $last != "!" OR $last != ":")
        {
            $end -= $countfirst;
            $last = substr("$postthis", $end, 1);
            $countfirst ++;
            if ($countfirst > 750)
            {
                $outb = 1;
                break;
            }
        }
        if ($outb == 1)
        {
            $postthis = $postthis."...";
        }
    }
    else
    {
        $postthis = "...".$text."...";
    }

}


$postthis = stripslashes($postthis);
$postthis = htmlspecialchars($postthis);


if ($cAdminLevel > 1)
{
    $postthis = str_replace ( "[swear]", "<font color=\"$swearcolor\">", $postthis);
    $postthis = str_replace ( "[/swear]", "<font color=\"$fontcolor\">", $postthis);
}
else
{
    $postthis = str_replace ( "[swear]", "", $postthis);
    $postthis = str_replace ( "[/swear]", "", $postthis);
}

if ($linkrandomquotes == 1)
{
    if ($gallery == 0)
    {
        print "&quot;<a href=\"topic.php?topicid=$topicid#post$postid\">$postthis</a>&quot;";
    }
    else
    {
        print "&quot;<a href=\"gallerypicture.php?topicid=$topicid&imageid=$postid&imtitle=$imtitle\">$postthis</a>&quot;";
    }

}
else
{
    print "&quot;$postthis&quot;";
}
