<?php
/**
 * $Source: /home/cvs/nukescripts/modules/Web_Links/index.php,v $
 * $Revision: 1.0.1-1 $
 * $Author: cyril $
 * $Date: 2006/06/24 22:44:02 $
 * Fix frenatus ligne 90 - bug 112
 */
/************************************************************************/
/* 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                                 */
/************************************************************************/
defined('MAXIMUS') or Header("Location: ../../index.php");

if (isset($min)) {
    $min = $GLOBALS['db']->XMFnum($min);
}

if (isset($show)) {
    $show = $GLOBALS['db']->XMFnum($show);
}

$index = 1;
$module_name = "Web_Links";
get_lang($module_name);
$pagetitle = "- "._WEBLINKS."";
if (is_file("modules/Web_Links/l_config.php")) {
include_once("modules/Web_Links/l_config.php");}

function getparent($parentid,$title) {
	$parentid = $GLOBALS['db']->XMFnum($parentid);
	$title    = stripslashes($title);
	$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `cid`, `title`, `parentid` from `".TABLE_LINKS_CATEGORIES."` where `cid`='$parentid'"));
	$cid    = $GLOBALS['db']->XMFnum($row['cid']);
	$ptitle = stripslashes(check_html($row['title'], "nohtml"));
	$pparentid = $GLOBALS['db']->XMFnum($row['parentid']);
	if ($ptitle=="$title") $title=$title;
	elseif ($ptitle!="") $title=$ptitle."/".$title;
	if ($pparentid!=0) {
		$title=getparent($pparentid,$ptitle);
	}
	return $title;
}

function getparentlink($parentid,$title) {
	global $module_name;
	$parentid = $GLOBALS['db']->XMFnum($parentid);
	$title    = stripslashes($title);
	$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `cid`, `title`, `parentid` from `".TABLE_LINKS_CATEGORIES."` where `cid`='$parentid'"));
	$cid       = $GLOBALS['db']->XMFnum($row['cid']);
	$ptitle    = stripslashes(check_html($row['title'], "nohtml"));
	$pparentid = $GLOBALS['db']->XMFnum($row['parentid']);
	if ($ptitle!="") $title="<a href='modules.php?name=$module_name&amp;l_op=viewlink&amp;cid=$cid'>$ptitle</a>/".$title;
	if ($pparentid!=0) {
		$title=getparentlink($pparentid,$ptitle);
	}
	return $title;
}

function menu($mainlink) {
	global $module_name;
	Max_OpenTable();
	$ThemeSel = get_theme();
	if (file_exists("themes/$ThemeSel/images/link-logo.gif")) {
		echo "<br /><center><a href=\"modules.php?name=$module_name\"><img src=\"themes/$ThemeSel/images/link-logo.gif\" border=\"0\" alt=\"\" /></a><br /><br />";
	} else {
		echo "<br /><center><a href=\"modules.php?name=$module_name\"><img src=\"modules/$module_name/images/link-logo.gif\" border=\"0\" alt=\"\" /></a><br /><br />";
	}
	echo "<form action=\"modules.php?name=$module_name&amp;l_op=search&amp;query=$query\" method=\"post\">"
	."<font class=\"content\"><input type=\"text\" size=\"25\" name=\"query\"> <input type=\"submit\" value=\""._SEARCH."\" /></font>"
	."</form>";
	echo "<font class=\"content\">[ ";
	if ($mainlink>0) {
		echo "<a href=\"modules.php?name=$module_name\">"._LINKSMAIN."</a> | ";
	}
	echo "<a href=\"modules.php?name=$module_name&amp;l_op=AddLink\">"._ADDLINK."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=NewLinks\">"._NEW."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=MostPopular\">"._POPULAR."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=TopRated\">"._TOPRATED."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=RandomLink\" target='new'>"._RANDOM."</a> ]"
	."</font></center>";
	Max_CloseTable();
}

function SearchForm() {
	echo "<form action='modules.php?name=$module_name&amp;l_op=search&amp;query=$query' method='post'>"
	."<table border='0' cellspacing='0' cellpadding='0' align='center'>"
	."<tr><td><font class='content'><input type='text' size='25' name='query' /> <input type='submit' value='"._SEARCH."' /></td></tr>"
	."</table>"
	."</form>";
}

function linkinfomenu($lid, $ttitle) {
	global $module_name;
	echo "<br /><font class=\"content\">[ "
	."<a href=\"modules.php?name=$module_name&amp;l_op=viewlinkcomments&amp;lid=$lid&amp;ttitle=$ttitle\">"._LINKCOMMENTS."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=viewlinkdetails&amp;lid=$lid&amp;ttitle=$ttitle\">"._ADDITIONALDET."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=viewlinkeditorial&amp;lid=$lid&amp;ttitle=$ttitle\">"._EDITORREVIEW."</a>"
	." | <a href=\"modules.php?name=$module_name&amp;l_op=modifylinkrequest&amp;lid=$lid\">"._MODIFY."</a>";
	if (defined('USER_ALLOW')) {
		echo " | <a href=\"modules.php?name=$module_name&amp;l_op=brokenlink&amp;lid=$lid\">"._REPORTBROKEN."</a>";
	}
	echo " ]</font>";
}

function RandomLink() {
	$result  = $GLOBALS['db']->sql_query("SELECT * from `".TABLE_LINKS_LINKS."`");
	$numrows = $GLOBALS['db']->sql_numrows($result);
	if ($numrows == 1) {
		$random = 1;
	} else {
		srand((double)microtime()*1000000);
		$random = rand(1,$numrows);
		$random = $GLOBALS['db']->XMFnum($random);
	}
	$row2 = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `url` from `".TABLE_LINKS_LINKS."` where `lid`='$random'"));
	$url = stripslashes($row2['url']);
	$GLOBALS['db']->sql_query("update `".TABLE_LINKS_LINKS."` set `hits`=hits+1 where `lid`='$random'");
	Header("Location: $url");
}

function newlinkgraphic($datetime, $time) {
	global $module_name;
	echo "&nbsp;";
	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);
	$startdate = time();
	$count = 0;
	while ($count <= 7) {
		$daysold = date("d-M-Y", $startdate);
		if ("$daysold" == "$datetime") {
			if ($count<=1) {
				echo "<img src=\"modules/$module_name/images/newred.gif\" alt=\""._NEWTODAY."\" title=\""._NEWTODAY."\" />";
			}
			if ($count<=3 && $count>1) {
				echo "<img src=\"modules/$module_name/images/newgreen.gif\" alt=\""._NEWLAST3DAYS."\" title=\""._NEWLAST3DAYS."\" />";
			}
			if ($count<=7 && $count>3) {
				echo "<img src=\"modules/$module_name/images/newblue.gif\" alt=\""._NEWTHISWEEK."\" title=\""._NEWTHISWEEK."\" />";
			}
		}
		$count++;
		$startdate = (time()-(86400 * $count));
	}
}

function categorynewlinkgraphic($cat) {
	global $module_name;
	$cat = $GLOBALS['db']->XMFnum(trim($cat));
	$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `date` from `".TABLE_LINKS_LINKS."` where `cid`='$cat' order by `date` desc limit 1"));
	$time = $row['date'];
	echo "&nbsp;";
	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);
	$startdate = time();
	$count = 0;
	while ($count <= 7) {
		$daysold = date("d-M-Y", $startdate);
		if ("$daysold" == "$datetime") {
			if ($count<=1) {
				echo "<img src=\"modules/$module_name/images/newred.gif\" alt=\""._CATNEWTODAY."\" title=\""._CATNEWTODAY."\" />";
			}
			if ($count<=3 && $count>1) {
				echo "<img src=\"modules/$module_name/images/newgreen.gif\" alt=\""._CATLAST3DAYS."\" title=\""._CATLAST3DAYS."\" />";
			}
			if ($count<=7 && $count>3) {
				echo "<img src=\"modules/$module_name/images/newblue.gif\" alt=\""._CATTHISWEEK."\" title=\""._CATTHISWEEK."\" />";
			}
		}
		$count++;
		$startdate = (time()-(86400 * $count));
	}
}

function popgraphic($hits) {
	global $module_name;
	include("modules/$module_name/l_config.php");
	if ($hits>=$popular) {
		echo "&nbsp;<img src=\"modules/$module_name/images/pop.gif\" alt=\""._POPULAR."\" title=\""._POPULAR."\" />";
	}
}

function convertorderbyin($orderby) {
	if ($orderby != "titleA" AND $orderby != "dateA" AND $orderby != "hitsA" AND $orderby != "ratingA" AND $orderby != "titleD" AND $orderby != "dateD" AND $orderby != "hitsD" AND $orderby != "ratingD") {
	    Header("Location: index.php");
	    die();
	}
	if ($orderby == "titleA")	$orderby = "title ASC";
	if ($orderby == "dateA")	$orderby = "date ASC";
	if ($orderby == "hitsA")	$orderby = "hits ASC";
	if ($orderby == "ratingA")	$orderby = "linkratingsummary ASC";
	if ($orderby == "titleD")	$orderby = "title DESC";
	if ($orderby == "dateD")	$orderby = "date DESC";
	if ($orderby == "hitsD")	$orderby = "hits DESC";
	if ($orderby == "ratingD")	$orderby = "linkratingsummary DESC";
	return $orderby;
}

function convertorderbytrans($orderby) {
	if ($orderby != "hits ASC" AND $orderby != "hits DESC" AND $orderby != "title ASC" AND $orderby != "title DESC" AND $orderby != "date ASC" AND $orderby != "date DESC" AND $orderby != "linkratingsummary ASC" AND $orderby != "linkratingsummary DESC") {
	    Header("Location: index.php");
	    die();
	}
	if ($orderby == "hits ASC")			$orderbyTrans = ""._POPULARITY1."";
	if ($orderby == "hits DESC")		$orderbyTrans = ""._POPULARITY2."";
	if ($orderby == "title ASC")		$orderbyTrans = ""._TITLEAZ."";
	if ($orderby == "title DESC")		$orderbyTrans = ""._TITLEZA."";
	if ($orderby == "date ASC")			$orderbyTrans = ""._DATE1."";
	if ($orderby == "date DESC")		$orderbyTrans = ""._DATE2."";
	if ($orderby == "linkratingsummary ASC")	$orderbyTrans = ""._RATING1."";
	if ($orderby == "linkratingsummary DESC")	$orderbyTrans = ""._RATING2."";
	return $orderbyTrans;
}

function convertorderbyout($orderby) {
	if ($orderby != "title ASC" AND $orderby != "date ASC" AND $orderby != "hits ASC" AND $orderby != "linkratingsummary ASC" AND $orderby != "title DESC" AND $orderby != "date DESC" AND $orderby != "hits DESC" AND $orderby != "linkratingsummary DESC") {
	    Header("Location: index.php");
	    die();
	}
	if ($orderby == "title ASC")		$orderby = "titleA";
	if ($orderby == "date ASC")			$orderby = "dateA";
	if ($orderby == "hits ASC")			$orderby = "hitsA";
	if ($orderby == "linkratingsummary ASC")	$orderby = "ratingA";
	if ($orderby == "title DESC")		$orderby = "titleD";
	if ($orderby == "date DESC")		$orderby = "dateD";
	if ($orderby == "hits DESC")		$orderby = "hitsD";
	if ($orderby == "linkratingsummary DESC")	$orderby = "ratingD";
	return $orderby;
}

function visit($lid) {
	$lid = $GLOBALS['db']->XMFnum($lid);
	$GLOBALS['db']->sql_query("update `".TABLE_LINKS_LINKS."` set `hits`=hits+1 where `lid`='$lid'");
	//update_points(14);
	$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `url` from `".TABLE_LINKS_LINKS."` where `lid`='$lid'"));
	$url = stripslashes($row['url']);
	Header("Location: $url");
}

function detecteditorial($lid, $ttitle) {
	global $module_name;
	$lid = $GLOBALS['db']->XMFnum($lid);
	$resulted2 = $GLOBALS['db']->sql_query("SELECT `adminid` from `".TABLE_LINKS_EDITORIALS."` where `linkid`='$lid'");
	$recordexist = $GLOBALS['db']->sql_numrows($resulted2);
	if ($recordexist != 0) {
		echo " | <a href=\"modules.php?name=$module_name&amp;l_op=viewlinkeditorial&amp;lid=$lid&amp;ttitle=$ttitle\">"._EDITORIAL."</a>";
	}
}

function linkfooter($lid,$ttitle) {
	global $module_name;
	echo "<font class=\"content\">[ <a href=\"modules.php?name=$module_name&amp;l_op=visit&amp;lid=$lid\" target=\"_blank\">"._VISITTHISSITE."</a> | <a href=\"modules.php?name=$module_name&amp;l_op=ratelink&amp;lid=$lid&amp;ttitle=$ttitle\">"._RATETHISSITE."</a> ]</font><br /><br />";
	linkfooterchild($lid);
}

function linkfooterchild($lid) {
	global $module_name;
	include("modules/$module_name/l_config.php");
	if ($useoutsidevoting = 1) {
		echo "<br /><font class=\"content\">"._ISTHISYOURSITE." <a href=\"modules.php?name=$module_name&amp;l_op=outsidelinksetup&amp;lid=$lid\">"._ALLOWTORATE."</a></font>";
	}
}

function rateinfo($lid) {
	$lid = $GLOBALS['db']->XMFnum($lid);
	$GLOBALS['db']->sql_query("update `".TABLE_LINKS_LINKS."` set `hits`=hits+1 where `lid`='$lid'");
	$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `url` from `".TABLE_LINKS_LINKS."` where `lid`='$lid'"));
	$url = stripslashes($row['url']);
	Header("Location: $url");
}

function completevoteheader(){
	menu('1');
	br('1');
	Max_OpenTable();
}

function completevotefooter($lid, $ttitle, $ratinguser) {
	global $sitename, $module_name;
	include("modules/$module_name/l_config.php");
	$lid = $GLOBALS['db']->XMFnum($lid);
	$row = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `url` FROM `".TABLE_LINKS_LINKS."` WHERE `lid`='$lid'"));
	$url = stripslashes($row['url']);
	echo "<font class=\"content\">"._THANKSTOTAKETIME." $sitename. "._LETSDECIDE."</font><br /><br /><br />";
	if ($ratinguser=="outside") {
		echo "<center><font class=\"content\">".WEAPPREACIATE." $sitename!<br /><a href=\"$url\">"._RETURNTO." $ttitle</a></font><center><br /><br />";
		$row2 = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `title` FROM `".TABLE_LINKS_LINKS."` where `lid`='$lid'"));
		$title = stripslashes(check_html($row2['title'], "nohtml"));
		$ttitle = ereg_replace (" ", "_", $title);
	}
	echo "<center>";
	linkinfomenu($lid,$ttitle);
	echo "</center>";
	Max_CloseTable();
}

function completevote($error) {
	global $module_name;
	include("modules/$module_name/l_config.php");
	if ($error == "none") echo "<center><font class=\"content\"><b>"._COMPLETEVOTE1."</b></font></center>";
	if ($error == "anonflood") echo "<center><font class=\"option\"><b>"._COMPLETEVOTE2."</b></font></center><br />";
	if ($error == "regflood") echo "<center><font class=\"option\"><b>"._COMPLETEVOTE3."</b></font></center><br />";
	if ($error == "postervote") echo "<center><font class=\"option\"><b>"._COMPLETEVOTE4."</b></font></center><br />";
	if ($error == "nullerror") echo "<center><font class=\"option\"><b>"._COMPLETEVOTE5."</b></font></center><br />";
	if ($error == "outsideflood") echo "<center><font class=\"option\"><b>"._COMPLETEVOTE6."</b></font></center><br />";
}

if (isset($ratinglid) && isset ($ratinguser) && isset ($rating)) {
	$ret = addrating($ratinglid, $ratinguser, $rating, $ratinghost_name, $ratingcomments);
}

switch($l_op) {

	case 'AddLink':include_once('modules/Web_Links/includes/AddLink.php');	break;
	case 'NewLinks':include_once('modules/Web_Links/includes/NewLinks.php');break;
	case 'NewLinksDate':include_once('modules/Web_Links/includes/NewLinksDate.php');break;
	case 'TopRated':include_once('modules/Web_Links/includes/TopRated.php');break;
	case 'MostPopular':include_once('modules/Web_Links/includes/MostPopular.php');break;
	case 'RandomLink':RandomLink();break;
	case 'viewlink':include_once('modules/Web_Links/includes/viewlink.php');break;
	case 'brokenlink':include_once('modules/Web_Links/includes/brokenlink.php');break;
	case 'modifylinkrequest':include_once('modules/Web_Links/includes/modifylinkrequest.php');break;
	case 'modifylinkrequestS':include_once('modules/Web_Links/includes/modifylinkrequestS.php');break;
	case 'brokenlinkS':include_once('modules/Web_Links/includes/brokenlinkS.php');break;
	case 'visit':visit($lid);break;
	case 'Add':include_once('modules/Web_Links/includes/Add.php');break;
	case 'search':include_once('modules/Web_Links/includes/search.php');break;
	case 'rateinfo':rateinfo($lid, $user, $title);break;
	case 'ratelink':include_once('modules/Web_Links/includes/ratelink.php');break;
	case 'addrating':include_once('modules/Web_Links/includes/addrating.php');break;
	case 'viewlinkcomments':include_once('modules/Web_Links/includes/viewlinkcomments.php');break;
	case 'outsidelinksetup':include_once('modules/Web_Links/includes/outsidelinksetup.php');break;
	case 'viewlinkeditorial':include_once('modules/Web_Links/includes/viewlinkeditorial.php');break;
	case 'viewlinkdetails':include_once('modules/Web_Links/includes/viewlinkdetails.php');break;
	default:include_once('modules/Web_Links/includes/index.php');break;

}