<?php

$file = __FILE__;
include ("inc/inc_common.php");
include ("inc/inc_connect.php");
include ("inc/inc_admincheck.php");
include ("inc/inc_getdate.php");
include ("inc/inc_forumname.php");
$page = "Search Results for \"$search\"";
$pagelink = $file;
include("inc/inc_setonline.php");
$title = "Search";
include ("inc/inc_head.php");
include ("inc/inc_body.php");
$title = "Search";
include ("inc/inc_title.php");
if ($groupid == "")
{
    print "</centeR><table border=0 width=\"100%\" cellpadding=1 cellspacing=0><tr><td  width=\"20%\">
    <table border=0 cellpadding=1 cellspacing=0><tr><td>
    <font face=\"$font\" size=$fontsize><image src=\"images/folder_icon.gif\">&nbsp;<a href=\"index.php\">$name1&nbsp;Home</a>
    </td></tr>
    <tr><td><image src=\"images/folder_line.gif\">
    <font face=\"$font\" size=$fontsize><image src=\"images/open_folder_icon.gif\">&nbsp;<a href=\"search.php?groupid=$groupid\">Search</a></td></tr>
    </table>
    </td><td><center>";
}
else
{
    print "</centeR><table border=0 width=\"100%\" cellpadding=1 cellspacing=0><tr><td  width=\"20%\">
    <table border=0 cellpadding=1 cellspacing=0><tr><td>
    <font face=\"$font\" size=$fontsize><image src=\"images/folder_icon.gif\">&nbsp;<a href=\"index.php\">$name1&nbsp;Home</a>
    </td></tr>
    <tr><td><image src=\"images/folder_line.gif\">
    <font face=\"$font\" size=$fontsize><image src=\"images/folder_icon.gif\">&nbsp;<a href=\"forums.php\">$name2</a></td></tr>
    <tr><td><font face=\"$font\" size=$fontsize>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<image src=\"images/folder_line.gif\">
    <image src=\"images/folder_icon.gif\">&nbsp;<a href=\"topics.php?groupid=$groupid\">$name3</a></td></tr><tr><td><font face=\"$font\" size=$fontsize>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<image src=\"images/folder_line.gif\">
    <image src=\"images/open_folder_icon.gif\">&nbsp;<a href=\"search.php?groupid=$groupid\">Search</a></td></tr>
    </table>
    </td><td><center>";
}

include ("inc/inc_navigation.php");
include ("inc/inc_newmailsdisplay.php");
print "</td></tr></table><br><center>";

/* end of include files...

 Damn the code on this page is ugly! I'm sure it could be tidied up - I'll do that in the next realease ( v4.2 )... Maybe...

 No, it's version 5.0, and it's still ugly. It's just needs some exit; commands but I just want to get GF5 out now - just the inbox and (shudder) admin sections to go, now...

 Still, as long as it works, eh? */

$searchresult = 1;
$search = strtolower($search);

if ( $where == "Members" )
{
    $result = mysql_query( "SELECT * FROM $userstable WHERE strName LIKE '%$search%' ORDER BY strName" );
    $num_rows = mysql_num_rows( $result );
    if ( $num_rows > 0 )
    {
    print "<table border=1 cellpadding=4 cellspacing=0 style=\"border-collapse: collapse\" bordercolor=#111111 bgcolor=$fgcolour>
                 <td><center><font face=$font size=2>View Profile</td>
                 <td><center><font face=$font size=2>User Name</td>
                 <td><center><font face=$font size=2>Admin Level</td>
                 <td><center><font face=$font size=2>Posts</td>
                 <td><center><font face=$font size=2>Topics</td>
                 <td><center><font face=$font size=2>Join Date</td>
                 <td><center><font face=$font size=2>Birthday</td>
                 <td><center><font face=$font size=2>Last Online</td>
                 <td><center><font face=$font size=2>Last Post</td>
                 <td><center><font face=$font size=2>Photo</td></tr>";
    while ( $a_row = mysql_fetch_object( $result ) )
    {
    if ( $a_row->strLastOnline == "")
    {
        $timeonlinetext = "Never!";
    }
    else
    {
    $timeonline = ($epochtime - $a_row->strLastOnline);
    $timeonline = $timeonline / 86400;
    $timeonline = round($timeonline, 1);

        //sort out wierd bug where negative values are returned...

        if ( $timeonline < 0 )
        {
            $timeonline = $timeonline*-1;
        }

    $timeonlinetext = "$timeonline days ago";
    if ( $timeonline > 0.99 )
    {
        if ($timeonline < 2 )
        {
            $timeonlinetext = "1 day ago";
        }
    }

    if ($timeonline < 0.0035) //5 minutes
    {
        $timeonlinetext = "Online now!";
    }
    }
    if ( $counter == 1)
    {
        $counter = 0;
    print "<tr><td><center><a href=\"profile.php?userid=$a_row->strName&groupid=$groupid\"><img src=\"images/profile_icon.gif\" border=0></a></td>";
    print "<td><center><font face=$font size=2>$a_row->strName</td>";
    print "<td><center><font face=$font size=2>$a_row->intAdminLevel</td>";
    print "<td><center><font face=$font size=2>$a_row->intPosts</td>";
    print "<td><center><font face=$font size=2>$a_row->intTopics</td>";
    print "<td><center><font face=$font size=2>$a_row->datRegistrationDate</td>";

    if ( $a_row->intBirthDay == 0 )
    {
        print "<td><center><font face=$font size=2>Unknown</td>";
    }
    else
    {
        if ( $a_row->intBirthDay == $date_array[mday] )
            {
                if ( $a_row->intBirthMonth == $date_array[mon] )
                {
                    print "<td><center><font face=$font size=2 color=red>Today!</td></font>";
                }
            }
            else
            {
                print "<td><center><font face=$font size=2>$a_row->intBirthDay/$a_row->intBirthMonth</td>";
            }
    }

        print "<td><center><font face=$font size=2>$timeonlinetext</td>";

        if ( $a_row->strLastPostTitle == "" )
        {
            print "<td><center><font face=$font size=2>Never Posted!</td>";
        }
        else
        {
            print "<td><center><font face=$font size=2><a href=\"topic.php?&groupid=$a_row->strLastPostGroupId&topicid=$a_row->strLastPostTopicId&searchword=$search\">$a_row->strLastPostTitle</a> - $a_row->strLastPostDate</td>";
    }

    print "<td><center><font face=$font size=2>";
    if ($a_row->strPhoto == "")
    {
        print "No";
    }
    else
    {
        print "<a target=\"_blank\" href=\"profilephoto2.php?photourl=$a_row->strPhoto&username=$a_row->strName\">Yes</a>";
    }

    print "</td></tr>";
    }
    else
    {
        $counter = 1;
        print "<tr bgcolor=$afgcolour><td><center><a href=\"profile.php?userid=$a_row->strName&groupid=$groupid\"><img src=\"images/profile_icon.gif\" border=0></a></td>";
        print "<td><center><font face=$font size=2>$a_row->strName</td>";
        print "<td><center><font face=$font size=2>$a_row->intAdminLevel</td>";
        print "<td><center><font face=$font size=2>$a_row->intPosts</td>";
        print "<td><center><font face=$font size=2>$a_row->intTopics</td>";
        print "<td><center><font face=$font size=2>$a_row->datRegistrationDate</td>";

        if ( $a_row->intBirthDay == 0 )
        {
            print "<td><center><font face=$font size=2>Unknown</td>";
        }
        else
        {
            if ( $a_row->intBirthDay == $date_array[mday] )
                {
                    if ( $a_row->intBirthMonth == $date_array[mon] )
                    {
                        print "<td><center><font face=$font size=2 color=red>Today!</td></font>";
                    }
                }
                else
                {
                    print "<td><center><font face=$font size=2>$a_row->intBirthDay/$a_row->intBirthMonth</td>";
                }
        }

            print "<td><center><font face=$font size=2>$timeonlinetext</td>";

            if ( $a_row->strLastPostTitle == "" )
            {
                print "<td><center><font face=$font size=2>Never Posted!</td>";
            }
            else
            {
                print "<td><center><font face=$font size=2><a href=\"topic.php?&groupid=$a_row->strLastPostGroupId&topicid=$a_row->strLastPostTopicId\">$a_row->strLastPostTitle</a> - $a_row->strLastPostDate</td>";
        }

        print "<td><center><font face=$font size=2>";
        if ($a_row->strPhoto == "")
        {
            print "No";
        }
        else
        {
            print "<a target=\"_blank\" href=\"profilephoto2.php?photourl=$a_row->strPhoto&username=$a_row->strName\">Yes</a>";
        }

        print "</td></tr>";
    }




    }
}
else
{
    print "<font size=$fontsize face=\"$font\" color=\"$fontcolor\">Sorry, there are no results. Please <a href=\"search.php?groupid=$groupid\">try again</a>.";
    $searchresult = 0;
    }
}
else
{



if ( $what == "Subject" )
{
if ( $where == "All" )
    {
        $resultb = mysql_query( "SELECT * FROM $forumtable WHERE strTopicName LIKE '%$search%' AND ynTopicRow = 'Yes' ORDER BY strTopicName" );
    }
    else
    {
        $result = mysql_query( "SELECT * FROM $grouptable WHERE intId = '$where'" );
        $num_rows = mysql_num_rows( $result );
        while ( $a_row = mysql_fetch_object( $result ) )
        {
            if ( $a_row->ynGroupMaster == 1 )
            {
                $resultb = mysql_query( "SELECT * FROM $forumtable WHERE strTopicName LIKE '%$search%' AND intForumGroup = $a_row->intGroupId AND ynTopicRow = 'Yes' ORDER BY intForumGroup, strTopicName" );
            }
            else
            {
                $resultb = mysql_query( "SELECT * FROM $forumtable WHERE strTopicName LIKE '%$search%' AND intForumId = $a_row->intId AND ynTopicRow = 'Yes' ORDER BY strTopicName" );
            }

        }
    }

    $num_rows = mysql_num_rows( $resultb );

    if ( $num_rows > 0 )
    {

    print "<center><font size=3 face=$font>Found <b>$num_rows</b> Results!<br><bR>
    <table border=1 cellpadding=0 cellspacing=0 style=\"border-collapse: collapse\" bordercolor=#111111 width=80%>

    <tr><td width=40% bgcolor=$fgcolour align=center><font face=$font size=2><b>Topic Name</td>
        <td width=20% bgcolor=$fgcolour align=center><font face=$font size=2><b>Started By</td>
    <td width=40% bgcolor=$fgcolour align=center><font face=$font size=2><b>Last Post By</td></tr>";


    while ( $a_row = mysql_fetch_object( $resultb ) )
    {
        if ( $a_row->strTopicName == $lasttopicname )
        //Make sure a topic is only displayed once no matter how many times the search string comes up within it...
        {
            // maybe add a count one day...
        }
        else
        {
            print "<tr><td width=40% align=center><font face=$font size=2>
            <a href=\"topic.php?topicid=$a_row->intTopicId&groupid=$a_row->intForumId&searchword=$search\">$a_row->strTopicName</a></td>

            <td width=20% align=center><font face=$font size=2>
            <a title=\"See $a_row->strName's Profile\" href=\"profile.php?userid=$a_row->strName&groupid=$groupid\">$a_row->strName</a></td>

            <td width=40% align=center><font face=$font size=2>
            <a title=\"See $a_row->strLastPostName's Profile\" href=\"profile.php?userid=$a_row->strLastPostName&groupid=$$a_row->intForumId\">$a_row->strLastPostName</a> at $a_row->strLastPostTime on the $a_row->strLastPostDate</a></td></tr>


            ";



            $lasttopicname = $a_row->strTopicName;
        }
    }
}
else
{ print "<font size=$fontsize face=\"$font\" color=\"$fontcolor\">Sorry, there are no results. Please <a href=\"search.php?groupid=$groupid\">try again</a>.";
$searchresult = 0;
}
}




if ( $what == "Post" )
{
    if ( $where == "All" )
    {
        $resultb = mysql_query( "SELECT * FROM $forumtable WHERE memPost LIKE '%$search%' ORDER BY strTopicName" );
    }
    else
    {
        $result = mysql_query( "SELECT * FROM $grouptable WHERE intId = '$where'" );
        $num_rows = mysql_num_rows( $result );
        while ( $a_row = mysql_fetch_object( $result ) )
        {
            if ( $a_row->ynGroupMaster == 1 )
            {
                $resultb = mysql_query( "SELECT * FROM $forumtable WHERE memPost LIKE '%$search%' AND intForumGroup = $a_row->intGroupId ORDER BY intForumGroup, strTopicName" );
            }
            else
            {
                $resultb = mysql_query( "SELECT * FROM $forumtable WHERE memPost LIKE '%$search%' AND intForumId = $a_row->intId ORDER BY strTopicName" );
            }

        }
    }

    $num_rows = mysql_num_rows( $resultb );

    if ( $num_rows > 0 )
    {

    print "<center><font size=3 face=$font>Found <b>$num_rows</b> Results!<br><bR>
    <table border=1 cellpadding=0 cellspacing=0 style=\"border-collapse: collapse\" bordercolor=#111111 width=80%>

    <tr><td width=40% bgcolor=$fgcolour align=center><font face=$font size=2><b>Topic Name</td>
        <td width=20% bgcolor=$fgcolour align=center><font face=$font size=2><b>Started By</td>
    <td width=40% bgcolor=$fgcolour align=center><font face=$font size=2><b>Last Post By</td></tr>";


    while ( $a_row = mysql_fetch_object( $resultb ) )
    {
        if ( $a_row->strTopicName == $lasttopicname )
        //Make sure a topic is only displayed once no matter how many times the search string comes up within it...
        {
            // maybe add a count one day...
        }
        else
        {
            print "<tr><td width=40% align=center><font face=$font size=2>
            <a href=\"topic.php?topicid=$a_row->intTopicId&groupid=$a_row->intForumId&searchword=$search\">$a_row->strTopicName</a></td>

            <td width=20% align=center><font face=$font size=2>
            <a title=\"See $a_row->strName's Profile\" href=\"profile.php?userid=$a_row->strName&groupid=$groupid\">$a_row->strName</a></td>

            <td width=40% align=center><font face=$font size=2>
            <a title=\"See $a_row->strLastPostName's Profile\" href=\"profile.php?userid=$a_row->strLastPostName&groupid=$$a_row->intForumId\">$a_row->strLastPostName</a> at $a_row->strLastPostTime on the $a_row->strLastPostDate</a></td></tr>


            ";



            $lasttopicname = $a_row->strTopicName;
        }
    }
}
else
{ print "<font size=$fontsize face=\"$font\" color=\"$fontcolor\">Sorry, there are no results. Please <a href=\"search.php?groupid=$groupid\">try again</a>.";
$searchresult = 0;
}
}
}

print "</table><br><br>";
include ("inc/inc_footer.php");

$query = "INSERT INTO $searchtable (strName, strSearch, strWhat, intSuccess, strTime, strDate) values( '$gfcookie[1]', '$search', '$what', '$searchresult', '$time$am', '$date')";
mysql_query( $query, $link ) or die ( "Error! : ".mysql_error() );


?>