<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/

	include("modules/$module_name/l_config.php");
	incoh();
	if (!isset($min)) $min=0;
	if (!isset($max)) $max=$min+$linksresults;
	if(isset($orderby)) {
		$orderby = convertorderbyin($orderby);
	} else {
		$orderby = "title ASC";
	}
	if ($show!="") {
		$linksresults = $show;
	} else {
		$show=$linksresults;
	}
	$query = check_html($query, nohtml);
	$query = addslashes($query);
	if(!is_numeric($linksresults) AND $linksresults==0)
	{
	$linksresults=10;
	}
	$result = $db->sql_query("SELECT lid, cid, sid, title, url, description, date, hits, linkratingsummary, totalvotes, totalcomments from ".TABLE_LINKS_LINKS." where title LIKE '%$query%' OR description LIKE '%$query%' ORDER BY $orderby LIMIT ".$db->XMFnum($min).",$linksresults");
	$fullcountresult = $db->sql_query("SELECT lid, title, description, date, hits, linkratingsummary, totalvotes, totalcomments from ".TABLE_LINKS_LINKS." where title LIKE '%$query%' OR description LIKE '%$query%'");
	$totalselectedlinks = $db->sql_numrows($fullcountresult);
	$nrows = $db->sql_numrows($result);
	$x=0;
	$the_query = stripslashes($query);
	$the_query = str_replace("\'", "'", $the_query);
	menu(1);
	br('1');
	Max_OpenTable();
	if ($query != "") {
		if ($nrows>0) {
			echo "<font class=\"option\">"._SEARCHRESULTS4.": <b>$the_query</b></font><br /><br />"
			."<table width=\"100%\" bgcolor=\"$bgcolor2\"><tr><td><font class=\"option\"><b>"._USUBCATEGORIES."</b></font></td></tr></table>";
			$result2 = $db->sql_query("SELECT cid, title from ".TABLE_LINKS_CATEGORIES." where title LIKE '%$query%' ORDER BY title DESC");
			while ($row2 = $db->sql_fetchrow($result2)) {
				$cid = $db->XMFnum($row2['cid']);
				$stitle = stripslashes(check_html($row2['title'], "nohtml"));
				$res = $db->sql_query("SELECT * from ".TABLE_LINKS_LINKS." where cid='$cid'");
				$numrows = $db->sql_numrows($res);
				$row3 = $db->sql_fetchrow($db->sql_query("SELECT cid,title,parentid from ".TABLE_LINKS_CATEGORIES." where cid='$cid'"));
				$cid3 = $db->XMFnum($row3['cid']);
				$title3 = stripslashes(check_html($row3['title'], "nohtml"));
				$parentid3 = $db->XMFnum($row3['parentid']);
				if ($parentid3>0) $title3 = getparent($parentid3,$title3);
				$title3 = ereg_replace($query, "<b>$query</b>", $title3);
				echo "<strong><big></big></strong>&nbsp;<a href=\"modules.php?name=$module_name&amp;l_op=viewlink&amp;cid=$cid\">$title3</a> ($numrows)<br />";
			}
			echo "<br /><table width=\"100%\" bgcolor=\"$bgcolor2\"><tr><td><font class=\"option\"><b>"._LINKS."</b></font></td></tr></table>";
			$orderbyTrans = convertorderbytrans($orderby);
			echo "<br /><font class=\"content\">"._SORTLINKSBY.": "
			.""._TITLE." (<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=titleA\">A</a>\<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=titleD\">D</a>)"
			.""._DATE." (<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=dateA\">A</a>\<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=dateD\">D</a>)"
			.""._RATING." (<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=ratingA\">A</a>\<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=ratingD\">D</a>)"
			.""._POPULARITY." (<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=hitsA\">A</a>\<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;orderby=hitsD\">D</a>)"
			."<br />"._SITESSORTED.": $orderbyTrans<br /><br />";
			while($row = $db->sql_fetchrow($result)) {
				$lid = $db->XMFnum($row['lid']);
				$cid = $db->XMFnum($row['cid']);
				$sid = $db->XMFnum($row['sid']);
				$title = stripslashes(check_html($row['title'], "nohtml"));
				$url = stripslashes($row['url']);
				$description = stripslashes($row['description']);
				$time = $row['date'];
				$hits = $db->XMFnum($row['hits']);
				$linkratingsummary = $row['linkratingsummary'];
				$totalvotes = $db->XMFnum($row['totalvotes']);
				$totalcomments = $row['totalcomments'];
				$linkratingsummary = number_format($linkratingsummary, $mainvotedecimal);
				$transfertitle = str_replace (" ", "_", $title);
				$title = ereg_replace($query, "<b>$query</b>", $title);
				echo "<a href=\"modules.php?name=$module_name&amp;l_op=visit&amp;lid=$lid\" target=\"new\">$title</a>";
				newlinkgraphic($datetime, $time);
				popgraphic($hits);
				br('1');
				$description = ereg_replace($query, "<b>$query</b>", $description);
				echo ""._DESCRIPTION.": $description<br />";
				setlocale (LC_TIME, $locale);
				ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $time, $datetime);
				$datetime = strftime(""._LINKSDATESTRING."", mktime($datetime[4],$datetime[5],$datetime[6],$datetime[2],$datetime[3],$datetime[1]));
				$datetime = ucfirst($datetime);
				echo ""._ADDEDON.": $datetime "._HITS.": $hits";
				/* voting & comments stats */
				if ($totalvotes == 1) {
					$votestring = _VOTE;
				} else {
					$votestring = _VOTES;
				}
				if ($linkratingsummary!="0" || $linkratingsummary!="0.0") {
					echo " "._RATING.": $linkratingsummary ($totalvotes $votestring)";
				}
				echo "<br /><a href=\"modules.php?name=$module_name&amp;l_op=ratelink&amp;lid=$lid&amp;ttitle=$transfertitle\">"._RATESITE."</a>";
				if ($totalvotes != 0) {
					echo " | <a href=\"modules.php?name=$module_name&amp;l_op=viewlinkdetails&amp;lid=$lid&amp;ttitle=$transfertitle\">"._DETAILS."</a>";
				}
				if ($totalcomments != 0) {
					echo " | <a href=\"modules.php?name=$module_name&amp;l_op=viewlinkcomments&amp;lid=$lid&amp;ttitle=$transfertitle>"._SCOMMENTS." ($totalcomments)</a>";
				}
				detecteditorial($lid, $transfertitle);
				br('1');
				$row4 = $db->sql_fetchrow($db->sql_query("SELECT cid,title,parentid from ".TABLE_LINKS_CATEGORIES." where cid='$cid'"));
				$cid3 = $db->XMFnum($row4['cid']);
				$title3 = stripslashes(check_html($row4['title'], "nohtml"));
				$parentid3 = $db->XMFnum($row4['parentid']);
				if ($parentid3>0) $title3 = getparent($parentid3,$title3);
				echo ""._CATEGORY.": $title3<br /><br />";
				$x++;
			}
			echo "</font>";
			$orderby = convertorderbyout($orderby);
		} else {
			echo "<br /><br /><center><font class=\"option\"><b>"._NOMATCHES."</b></font><br /><br />"._GOBACK."<br /></center>";
		}
		/* Calculates how many pages exist.  Which page one should be on, etc... */
		$linkpagesint = ($totalselectedlinks / $linksresults);
		$linkpageremainder = ($totalselectedlinks % $linksresults);
		if ($linkpageremainder != 0) {
			$linkpages = ceil($linkpagesint);
			if ($totalselectedlinks < $linksresults) {
				$linkpageremainder = 0;
			}
		} else {
			$linkpages = $linkpagesint;
		}
		/* Page Numbering */
		if ($linkpages!=1 && $linkpages!=0) {
			echo "<br /><br />"
			.""._SELECTPAGE.": ";
			$prev=$min-$linksresults;
			if ($prev>=0) {
				echo "&nbsp;&nbsp;<b>[ <a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;min=$prev&amp;orderby=$orderby&amp;show=$show\">"
				." &lt;&lt; "._PREVIOUS."</a> ]</b> ";
			}
			$counter = 1;
			$currentpage = ($max / $linksresults);
			while ($counter<=$linkpages ) {
				$cpage = $counter;
				$mintemp = ($perpage * $counter) - $linksresults;
				if ($counter == $currentpage) {
					echo "<b>$counter</b>&nbsp;";
				} else {
					echo "<a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;min=$mintemp&amp;orderby=$orderby&amp;show=$show\">$counter</a> ";
				}
				$counter++;
			}
			$next=$min+$linksresults;
			if ($x>=$perpage) {
				echo "&nbsp;&nbsp;<b>[ <a href=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$the_query&amp;min=$max&amp;orderby=$orderby&amp;show=$show\">"
				." "._NEXT." &gt;&gt;</a> ]</b>";
			}
		}
		echo "<br /><br /><center><font class=\"content\">"
		.""._TRY2SEARCH." \"$the_query\" "._INOTHERSENGINES."<br />"
		."<a target=\"_blank\" href=\"http://www.altavista.com/cgi-bin/query?pg=q&amp;sc=on&amp;hl=on&amp;act=2006&amp;par=0&amp;q=$the_query&amp;kl=XX&amp;stype=stext\">Alta Vista</a> - "
		."<a target=\"_blank\" href=\"http://www.hotbot.com/?MT=$the_query&amp;DU=days&amp;SW=web\">HotBot</a> - "
		."<a target=\"_blank\" href=\"http://www.infoseek.com/Titles?qt=$the_query\">Infoseek</a> - "
		."<a target=\"_blank\" href=\"http://www.dejanews.com/dnquery.xp?QRY=$the_query\">Deja News</a> - "
		."<a target=\"_blank\" href=\"http://www.lycos.com/cgi-bin/pursuit?query=$the_query&amp;maxhits=20\">Lycos</a> - "
		."<a target=\"_blank\" href=\"http://search.yahoo.com/bin/search?p=$the_query\">Yahoo</a>"
		."<br />"
		."<a target=\"_blank\" href=\"http://es.linuxstart.com/cgi-bin/sqlsearch.cgi?pos=1&amp;query=$the_query&amp;language=&amp;advanced=&amp;urlonly=&amp;withid=\">LinuxStart</a> - "
		."<a target=\"_blank\" href=\"http://search.1stlinuxsearch.com/compass?scope=$the_query&amp;ui=sr\">1stLinuxSearch</a> - "
		."<a target=\"_blank\" href=\"http://www.google.com/search?q=$the_query\">Google</a> - "
		."<a target=\"_blank\" href=\"http://www.linuxlinks.com/cgi-bin/search.cgi?query=$the_query&amp;engine=Links\">LinuxLinks</a> - "
		."<a target=\"_blank\" href=\"http://www.freshmeat.net/search/?q=$the_query&amp;section=projects\">Freshmeat</a> - "
		."<a target=\"_blank\" href=\"http://www.justlinux.com/bin/search.pl?key=$the_query\">JustLinux</a>"
		."</font>";
	} else {
		echo "<center><font class=\"option\"><b>"._NOMATCHES."</b></font></center><br /><br />";
	}
	Max_CloseTable();
	incof();