<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: search.php,v 1.8.2.6 2004/08/01 17:04:19 kirilt Exp $




/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)            */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

if (!isset($op)) { $op = null; }
if (!isset($author)) { $author = null; }
if (!isset($query)) { $query = null; }
if (!isset($type)) { $type = null; }
if (!isset($category)) { $category = null; }
if (!isset($days)) { $days = null; }


$offset=30;
if (!isset($min)) $min=0;
if (!isset($max)) $max=$min+$offset;
$query = stripslashes($query);
include("header.$phpEx");

if ($topic>0) {
    $result = mysql_query("select topicimage, topictext from ".$mpnTables['topics']." where topicid=$topic");
    list($topicimage, $topictext) = mysql_fetch_row($result);
} else {
    $topictext = "".translate("All Topics")."";
    $topicimage = "AllTopics.gif";
}
OpenTable();

if ($type == "users")
{
    echo "<center><font size=4><b>".translate("Search in Users Database")."</b></center><br>";
} elseif ($type == "sections") {
    echo "<center><font size=4><b>".translate("Search in Special Sections")."</b></center><br>";
} elseif ($type == "reviews") {
    echo "<center><font size=4><b>".translate("Search in Reviews")."</b></center><br>";
} else {
    echo "<center><font size=4><b>".translate("Search in")." $topictext</b></center><br>";
}
?>
        <TABLE WIDTH=100% BORDER=0>
        <TR><TD>
<?php
if (($type == "users") OR ($type == "sections") OR ($type == "reviews")) {
    echo "<img src = \"".$tipath."AllTopics.gif\" border = 0 align = \"right\">";
} else {
echo "<img src = \"".$tipath.$topicimage."\" border = 0 align = \"right\" alt = \"$topictext\">";
}
echo "<form action=\"search.$phpEx\" method=\"post\">"; ?>
        <input size=25 type=text name=query value="<?php echo $query; ?>">
<input class='button' type=submit value="<?php echo translate("Search"); ?>"><br>
        <!-- Topic Selection -->
<?php
$toplist = mysql_query("select topicid, topictext from ".$mpnTables['topics']." order by topictext");
echo "<SELECT NAME=\"topic\">";
echo "<OPTION VALUE=\"\">".translate("All Topics")."</option>\n";

$sel = null;

while(list($topicid, $topics) = mysql_fetch_row($toplist)) {
    if ($topicid==$topic) { $sel = "selected "; }
    echo "<option $sel value=\"$topicid\">$topics</option>\n";
    $sel = "";
}
echo "</SELECT>";

echo "<SELECT NAME=\"category\">";
echo "<OPTION VALUE=\"0\">".translate("Articles")."</option>\n";
$catlist = mysql_query("select catid, title from ".$mpnTables['stories_cat']." order by title");
        while(list($catid, $title) = mysql_fetch_row($catlist)) {
                if ($catid==$category) { $sel = "selected "; }
                echo "<option $sel value=\"$catid\">$title</option>\n";
    $sel = "";
        }
echo "</SELECT>";


// Authors Selection -->
$thing = mysql_query("select aid from ".$mpnTables['authors']." order by aid");
echo "<SELECT NAME=\"author\">";
        echo "<OPTION VALUE=\"\">".translate("All Authors")."</option>\n";
        while(list($authors) = mysql_fetch_row($thing)) {
                if ($authors==$author) { $sel = "selected "; }
    echo "<option value=\"$authors\">$authors</option>\n";
    $sel = "";
        }
        echo "</SELECT>";
        ?>
        <!-- Date Selection -->
        <select name="days">
                <option <?php echo $days == 0 ? "selected " : ""; ?> value=0><?php echo translate("All"); ?></option>
                <option <?php echo $days == 7 ? "selected " : ""; ?> value=7>1 <?php echo translate("week"); ?></option>
                <option <?php echo $days == 14 ? "selected " : ""; ?> value=14>2 <?php echo translate("weeks"); ?></option>
                <option <?php echo $days == 30 ? "selected " : ""; ?> value=30>1 <?php echo translate("month"); ?></option>
    <option <?php echo $days == 60 ? "selected " : ""; ?> value=60>2 <?php echo translate("months"); ?></option>
                <option <?php echo $days == 90 ? "selected " : ""; ?> value=90>3 <?php echo translate("months"); ?></option>
        </select><br>
<?php

$sel1 = $sel2 = $sel3 = $sel4 = $sel5 = null;

if ($type == "stories") {
    $sel1 = "checked";
} elseif ($type == "comments") {
    $sel2 = "checked";
} elseif ($type == "sections") {
    $sel3 = "checked";
} elseif ($type == "users") {
    $sel4 = "checked";
} elseif ($type == "reviews") {
    $sel5 = "checked";
}
$num_sec = mysql_num_rows(mysql_query("select * from ".$mpnTables['sections']));
$num_rev = mysql_num_rows(mysql_query("select * from ".$mpnTables['reviews']));
echo "".translate("Search on:")."";
echo "<input type=radio name=type value=stories $sel1> ".translate("Stories")."";
echo "<input type=radio name=type value=comments $sel2> ".translate("Comments")."";
if ($num_sec > 0) {
    echo "<input type=radio name=type value=sections $sel3> ".translate("Sections")."";
}
echo "<input type=radio name=type value=users $sel4> ".translate("Users")."";
if ($num_rev > 0) {
    echo "<input type=radio name=type value=reviews $sel5> ".translate("Reviews")."";
}
echo "</form></td></tr></TABLE><P>";

if ($type=="stories" OR !$type) {
    if ($category > 0) {
        $categ = "AND catid=$category ";
    } elseif ($category == 0) {
        $categ = "";
    }
    $q = "select s.sid, s.aid, s.informant, s.title, s.time, s.hometext, s.bodytext, a.url, s.comments, s.topic from ".$mpnTables['stories']." s, ".$mpnTables['authors']." a where s.aid=a.aid $categ";
    //$q = "select s.sid, s.aid, s.informant, s.title, s.time, s.hometext, s.bodytext, a.url, s.comments, s.topic from ".$mpnTables['stories']." s, ".$mpnTables['authors']." a where s.aid=a.aid $categ";
    if (isset($query)) $q .= "AND (s.title LIKE '%$query%' OR s.hometext LIKE '%$query%' OR s.bodytext LIKE '%$query%' OR s.notes LIKE '%$query%') ";
    if ($author != "") $q .= "AND s.aid='$author' ";
    if ($topic != "") $q .= "AND s.topic='$topic' ";
    if ($days != "" && $days!=0) $q .= "AND TO_DAYS(NOW()) - TO_DAYS(time) <= $days ";
    $q .= " ORDER BY s.time DESC LIMIT $min,$offset";
$t = $topic;
    $result = mysql_query($q);
if ($result) $nrows = mysql_num_rows($result);
    $x=0;
    echo "<br><hr noshade size=\"1\"><center><b>".translate("Search Results")."</b></center><br><br>";
    echo "<table width=99% cellspacing=0 cellpadding=0 border=0>\n";

    $match = null;

    if ($nrows>0) {
        //while(list($sid, $aid, $title, $time, $url, $comments, $topic) = mysql_fetch_row($result)) {
        while(list($sid, $aid, $informant, $title, $time, $hometext, $bodytext, $url, $comments, $topic) = mysql_fetch_row($result)) {
            $result2=mysql_query("select topictext from ".$mpnTables['topics']." where topicid='$topic'");
            list($topictext) = mysql_fetch_row($result2);
            $furl = "article.$phpEx?sid=$sid";
            formatTimestamp($time);
            if ($informant == "") {
                $informant = $anonymous;
            } else {
                $informant = "<a href=\"user.$phpEx?op=userinfo&amp;uname=$informant\">$informant</a>";
            }
            if ($query != "") {
                if (eregi("$query",$title)) {
                    $a = 1;
                }
                $text = "$hometext$bodytext";
                if (eregi("$query",$text)) {
                    $a = 2;
                }
                if (eregi("$query",$text) AND eregi("$query",$title)) {
                    $a = 3;
                }
                if ($a == 1) {
                    $match = translate("Match on Article Title");
                } elseif ($a == 2) {
                    $match = translate("Match on Article Text");
                } elseif ($a == 3) {
                    $match = translate("Match on Article Title and Text");
                }
                if (!isset($a)) {
                    $match = "";
                } else {
                    $match = "$match<br>";
                }
            }
            printf("<tr><td><img src=\"images/links/urlgo.gif\" border=\"0\" alt=\"\">&nbsp;<font size=3><a href=\"%s\"><b>%s</b></a> </font><br><font size=2> ".translate("Contributed by")." $informant<br>".translate("by")." <a href=\"%s\">%s</a>",$furl,$title,$url,$aid,$informant);
            //print " ".translate("on")." $datetime (<b>$comments</b>)</td></tr>\n";
            print " ".translate("on")." $datetime<br>"."$match".translate("Topic ").": <a href=\"search.$phpEx?query=&amp;topic=$topic\">$topictext</a> ";
            if ($comments == 0) {
                echo "(".translate("No Comments").")";
            } elseif ($comments == 1) {
                echo "($comments ".translate("comment.").")";
            } elseif ($comments >1) {
                echo "($comments ".translate("comments.").")";
            }


            if (_MPN_ADMIN) {
                echo " [ <a href='admin.$phpEx?op=EditStory&sid=$sid'>".translate("Edit")."</a> | <a href='admin.$phpEx?op=RemoveStory&sid=$sid'>".translate("Delete")."</a> ]";
            }
            echo "</font><br><br><br></td></tr>\n";
            $x++;
        }

//  echo "</td></tr></table>";
echo "</table>";
} else {
                echo "<tr><td><center><font color=Red>".translate("No matches found to your query")."</font></center><br><br>";
    echo "</td></tr></table>";
        }

        $prev=$min-$offset;
        if ($prev>=0) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$prev&query=$query&type=$type&category=$category\">";
                print "<br><br><center><b>$min ".translate("previous matches")."</b></a>";
        }

        $next=$min+$offset;
if ($x>=29) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$max&query=$query&type=$type&category=$category\">";
                print "<br><br><center><b>".translate("next matches")."</b></a>";
        }


} elseif ($type=="comments") {
$result = mysql_query("select tid, sid, subject, date, name from ".$mpnTables['comments']." where (subject like '%$query%' OR comment like '%$query%') order by date DESC limit $min,$offset");
        $nrows  = mysql_num_rows($result);
        $x=0;
        echo "<br><hr noshade size=\"1\"><center><b>".translate("Search Results")."</b></center><br><br>";
echo "<table width=99% cellspacing=0 cellpadding=0 border=0>\n";
if ($nrows>0) {
while(list($tid, $sid, $subject, $date, $name) = mysql_fetch_row($result)) {
$q = "select title from ".$mpnTables['stories']." where sid=$sid";
    $res = mysql_query($q);
    list($title) = mysql_fetch_row($res);
    $reply = mysql_num_rows(mysql_query("select * from ".$mpnTables['comments']." where pid=$tid"));
        $furl = "article.$phpEx?thold=-1&mode=flat&order=1&sid=$sid#$tid";
if(!$name) {
        $name = "$anonymous";
    } else {
        $name = "<a href=\"user.$phpEx?op=userinfo&amp;uname=$name\">$name</a>";
}
    formatTimestamp($date);
echo "<tr><td><img src=\"images/links/urlgo.gif\" border=\"0\" alt=\"\">&nbsp;<font size=\"3\"><a href=\"$furl\"><b>$subject</b></a></font><font size=\"2\"><br>".translate("by")." $name".translate("on")." $datetime<br>"."".translate("Attach to article").": $title<br>";
// echo "<font size=3><a href='$furl'><b>$subject</b></a>";
// echo "<font size=2>".translate("by")." $name".translate("on")." $datetime<br>".translate("Attached to article").": $title<br>";
if ($reply == 1) {
        echo "($reply ".translate("Reply").")";
        if (_MPN_ADMIN) {
            echo " [ <a href=\"admin.$phpEx?op=RemoveComment&amp;tid=$tid&amp;sid=$sid\">".translate("Delete")."</a> ]";
        }
        echo "<br><br><br></td></tr>\n";
        } else {
        echo "($reply ".translate("Replies").")";
        if (_MPN_ADMIN) {
            echo " [ <a href=\"admin.$phpEx?op=RemoveComment&amp;tid=$tid&amp;sid=$sid\">".translate("Delete")."</a> ]";
        }
        echo "<br><br><br></td></tr>\n";
        }

                    $x++;
                }

//      echo "</td></tr></table>";
echo "</table>";
} else {
                echo "<tr><td><center><font color=Red>".translate("No matches found to your query")."</font></center><br><br>";
    echo "</td></tr></table>";
        }

        $prev=$min-$offset;
        if ($prev>=0) {
                print "<a href=\"search.$phpEx?author=$author&topic=$topic&min=$prev&query=$query&type=$type\">";
                print "<br><br><center><b>$min ".translate("previous matches")."</b></a>";
        }

        $next=$min+$offset;
if ($x>=29) {
                print "<a href=\"search.$phpEx?author=$author&topic=$topic&min=$max&query=$query&type=$type\">";
                print "<br><br><center><b>".translate("next matches")."</b></a>";
        }

} elseif ($type=="reviews") {

        $result = mysql_query("select id, title, text, reviewer, score from ".$mpnTables['reviews']." where (title like '%$query%' OR text like '%$query%') order by date DESC limit $min,$offset");
        $nrows  = mysql_num_rows($result);
        $x=0;
        echo "<br><hr noshade size=\"1\"><center><b>".translate("Search Results")."</b></center><br><br>";
echo "<table width=99% cellspacing=0 cellpadding=0 border=0>\n";
if ($nrows>0) {
                while(list($id, $title, $text, $reviewer, $score) = mysql_fetch_row($result)) {
        $furl = "reviews.$phpEx?op=showcontent&id=$id";
                    echo "<tr><td><img src=\"images/links/urlgo.gif\" border=\"0\" alt=\"\">&nbsp;<font size=3><a href=\"$furl\"><b>$title</b></a> <font size=2> ".translate("by")." $reviewer<br>".translate("Score").": $score/10<br>";
if (_MPN_ADMIN) {
        echo " [ <a href=\"reviews.$phpEx?op=mod_review&amp;id=$id\">".translate("Edit")."</a> | <a href=\"reviews.$phpEx?op=del_review&amp;id_del=$id\">".translate("Delete")."</a> ]";
    }
                print "<br><br><br></font></td></tr>\n";
//                            print "</td></tr>\n";
                    $x++;
                }

echo "</table>";
} else {
                echo "<center><font color=Red>".translate("No matches found to your query")."</font></center><br><br>";
    echo "</td></tr></table>";
        }

        $prev=$min-$offset;
        if ($prev>=0) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$prev&query=$query&type=$type\">";
                print "<br><br><center><b>$min ".translate("previous matches")."</b></a>";
        }

        $next=$min+$offset;
if ($x>=29) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$max&query=$query&type=$type\">";
                print "<br><br><center><b>".translate("next matches")."</b></a>";
        }

} elseif ($type=="sections") {

        $result = mysql_query("select artid, secid, title, content from ".$mpnTables['seccont']." where (title like '%$query%' OR content like '%$query%') order by artid DESC limit $min,$offset");
        $nrows  = mysql_num_rows($result);
        $x=0;
echo "<br><hr noshade size=\"1\"><center><b>".translate("Search Result")."</b></center><br><br>";
    echo "<table width=99% cellspacing=0 cellpadding=0 border=0>\n";
if ($nrows>0) {
                while(list($artid, $secid, $title, $content) = mysql_fetch_row($result)) {
        $result2 = mysql_query("select secname from ".$mpnTables['sections']." where secid='$secid'");
        list($sectitle) = mysql_fetch_row($result2);
        $surl = "sections.$phpEx?op=listarticles&secid=$secid";
        $furl = "sections.$phpEx?op=viewarticle&artid=$artid";
                    echo "<tr><td><img src=\"images/links/urlgo.gif\" border=\"0\" alt=\"\">&nbsp;<font size=3><a href=\"$furl\"><b>$title</b></a> ".translate("in the Section")." <a href=$surl>$sectitle</a></td></tr>\n";
if (_MPN_ADMIN) {
        echo " [ <a href=\"admin.$phpEx?op=secartedit&amp;artid=$artid\">".translate("Edit")."</a> | <a href=\"admin.$phpEx?op=secartdelete&amp;artid=$artid&amp;ok=0\">".translate("Delete")."</a> ]";
        }
        echo "</font><br><br><br></td></tr>\n";
                    $x++;
                }

echo "</table>";
} else {
                echo "<center><font color=Red>".translate("No matches found to your query")."</font></center><br><br>";
    echo "</td></tr></table>";
        }

        $prev=$min-$offset;
        if ($prev>=0) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$prev&query=$query&type=$type\">";
                print "<br><br><center><b>$min ".translate("previous matches")."</b></a>";
        }

        $next=$min+$offset;
if ($x>=29) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$max&query=$query&type=$type\">";
                print "<br><br><center><b>".translate("next matches")."</b></a>";
        }

} elseif ($type=="users") {

        $result = mysql_query("select uid, uname, name from ".$mpnTables['users']." where (uname like '%$query%' OR name like '%$query%' OR bio like '%$query%') order by uname ASC limit $min,$offset");
        $nrows  = mysql_num_rows($result);
        $x=0;
        echo "<br><hr noshade size=\"1\"><center><b>".translate("Search Result")."</b></center><br><br>";
echo "<table width=99% cellspacing=0 cellpadding=0 border=0>\n";
if ($nrows>0) {
                while(list($uid,$uname, $name) = mysql_fetch_row($result)) {
        $furl = "user.$phpEx?op=userinfo&uname=$uname";
        if ($name=="") {
        $name = "".translate("No name entered")."";
        }
                    echo "<tr><td><img src=\"images/links/urlgo.gif\" border=\"0\" alt=\"\">&nbsp;<font size=3><a href=\"$furl\"><b>$uname</b></a> ($name)";
if (_MPN_ADMIN) {
        echo " [ <a href=\"admin.$phpEx?chng_uid=$uid&amp;op=modifyUser\">".translate("Edit")."</a> | <a href=\"admin.$phpEx?op=delUser&amp;chng_uid=$uid\">".translate("Delete")."</a> ]";
        }
        echo "</font></td></tr>\n";
                    $x++;
                }

echo "</table>";
} else {
                echo "<center><font color=Red>".translate("No matches found to your query")."</font></center><br><br>";
    echo "</td></tr></table>";
        }

        $prev=$min-$offset;
        if ($prev>=0) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$prev&query=$query&type=$type\">";
                print "<br><br><center><b>$min ".translate("previous matches")."</b></a>";
        }

        $next=$min+$offset;
if ($x>=29) {
                print "<a href=\"search.$phpEx?author=$author&topic=$t&min=$max&query=$query&type=$type\">";
                print "<br><br><center><b>".translate("next matches")."</b></a>";
        }



}

CloseTable();
include("footer.$phpEx");

?>