<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	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, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

//------
//whr: 7
//------

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0 ;
$title = (isset($_GET['title'])) ? inCode($_GET['title']) : "" ;
$rank = (isset($_GET['rank'])) ? intval($_GET['rank']) : 1 ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;
$pg = (isset($_GET['pg'])) ? intval($_GET['pg']) : 1 ;
$ofsppg = 10; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

function catList() {
	global $dblink;
	
	if ($result = $dblink->get_list("SELECT * FROM memht_mylinks_cat WHERE parent=0 ORDER BY name")) {
		echo "<table width='100%' border='0' cellspacing='0' cellpadding='2'>\n";
		$a = 0;
		foreach ($result as $row) {
			$cid = intval($row['id']);
			$cname = outCode($row['name']);
			$cdesc = outCode($row['description']);
			
			$clinks = "<span class='info'>(".$dblink->get_num("SELECT id FROM memht_mylinks WHERE cat=$cid AND flag=1").")</span>";
							
			if (($a%2)==0) { echo "<tr>"; }
				echo "<td width='50%' valign='top'><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=mylinks&op=showCategory&id=$cid&title=".mem_urlencode($cname)."' title=\"header=[$cname] body=[$cdesc]\"><b>$cname</b></a> $clinks</td>";
			if (($a++%2)!=0) { echo "</tr>\n"; }
		}
		echo "</table>\n";
	} else {
		echo "<div align='center' id='errorText' class='box'><b>"._EMPTY_."</b></div>";
	}
}

function showCategory($id,$title,$ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;
	$id = intval($id);
	
	$userating = 1;
	
	if ($row = $dblink->get_row("SELECT * FROM memht_mylinks_cat WHERE id=$id")) {
		$cid = intval($row['id']);
		$cparent = intval($row['parent']);
		$cname = outCode($row['name']);
		
		echo "<div align='right'><a href='index.php?page=mylinks&op=addLink&id=$cid' title='"._ADD_LINK_."'><i>"._ADD_LINK_."</i></a></div>\n";
		echo "<div class='box' style='margin-bottom:10px;'><b>$cname</b></div>\n";
		//Show subcategories
		if ($sresult = $dblink->get_list("SELECT * FROM memht_mylinks_cat WHERE parent=$cid ORDER BY name")) {
			echo "<table width='100%' border='0' cellspacing='0' cellpadding='2' style='margin-bottom:10px;'>\n";
			$a = 0;
			foreach ($sresult as $srow) {
				$sid = intval($srow['id']);
				$sname = outCode($srow['name']);
				$sdesc = outCode($srow['description']);
				
				$slinks = "<span class='info'>(".$dblink->get_num("SELECT id FROM memht_mylinks WHERE cat=$sid AND flag=1").")</span>";
					
				if (($a%2)==0) { echo "<tr>"; }
					echo "<td width='50%' valign='top'><img src='images/bullet_paper.gif' border='0' alt='Bullet'> <a href='index.php?page=mylinks&op=showCategory&id=$sid&title=".mem_urlencode($sname)."' title=\"header=[$sname] body=[$sdesc]\"><b>$sname</b></a> $slinks</td>";
				if (($a++%2)!=0) { echo "</tr>\n"; }
			}
			echo "</table>\n";
			echo "<div class='underlined' style='margin-bottom:5px;'></div>\n";
		}
		
		//Show links
		if ($fresult = $dblink->get_list("SELECT *,(SELECT ROUND(SUM(vote)/COUNT(id)) AS irank FROM memht_ratings WHERE whr=7 AND wid=l.id) AS rank FROM memht_mylinks AS l WHERE l.cat=$id AND l.flag=1 ORDER BY l.id DESC LIMIT $ofsbgn,$ofsppg")) {
			foreach ($fresult as $frow) {
				$fid = intval($frow['id']);
				$fname = outCode($frow['name']);
				$furl = outCode($frow['url']);
				$fdesc = outCode($frow['description']);
				$frank = intval($frow['rank']);
				
				echo "<div><a href='$furl' title='$fname' target='_blank'><span style='font-size:140%;' class='tpl_comments_title'><b>$fname</b></span></a>";
				if ($userating==1) {
					echo "&nbsp;&nbsp;";
					//Rating
					include_once("inc/class/rating.class.php");
					$rt = new rating();
					$rt->whr = 7;
					$rt->wid = $fid;
					$rt->rank = $frank;
					$rt->show();
				}
				echo "</div>";
				echo "<div style='margin:2px 0;'>$fdesc</div>";
				echo "<div class='info' style='margin-bottom:10px;'><i>$furl</i></div>";
			}
			
			//Pages
			include_once("inc/class/paginationSystem.class.php");
			$ps = new paginationSystem();
			$ps->items = $ofsppg;
			$ps->actpg = $pg;
			$ps->query = "SELECT id FROM memht_mylinks WHERE cat=$id AND flag=1";
			$ps->url = "index.php?page=mylinks&op=showCategory&id=$cid&title=".mem_urlencode($title)."&pg={{N}}";
			$ps->show();
		} else {
			echo "<div align='center'>"._NOLINKSINTHISCATEGORY_."</div>\n";
		}
	} else {
		echo "<div align='center' id='errorText' class='box'><b>"._EMPTY_."</b></div>";
	}
}

function addLink($id) {
	global $dblink;
	$id = intval($id);
	
	if ($row = $dblink->get_row("SELECT name FROM memht_mylinks_cat WHERE id=$id")) {
		$cat = outCode($row['name']);
		
		echo "<form name='form_com' method='post' action='index.php?page=mylinks&op=addLinkCheck&id=$id'>\n";		
			echo "<div align='center'><table border='0' cellpadding='3' cellspacing='0' class='box' style='text-align:left; padding:1px 6px;'>\n";
				echo "<tr><td>"._CATEGORY_."</td><td><input type='text' value='$cat' size='40' maxlength='255' disabled></td></tr>\n";
				echo "<tr><td>"._SITENAME_."</td><td><input type='text' name='sitename' size='40' maxlength='255'></td></tr>\n";
				echo "<tr><td>"._SITEURL_."</td><td><input type='text' name='siteurl' value='http://' size='40' maxlength='255'></td></tr>\n";
				echo "<tr><td>"._EMAIL_."</td><td><input type='text' name='email' size='40' maxlength='255'></td></tr>\n";
				echo "<tr><td valign='top'>"._DESCRIPTION_."</td><td><textarea name='description' cols='35' rows='3'></textarea></td></tr>\n";
				echo "<tr><td colspan='2' align='center'><input type='submit' name='Submit' value='"._ADD_LINK_."'></td></tr>\n";
			echo "</table></div>\n";
			echo "<input type='hidden' name='category' value='$id'>\n";
		echo "</form>\n";
	} else {
		echo "<meta http-equiv='refresh' content='0;URL=index.php?page=mylinks'>";
	}
}

function addLinkCheck($id,$ok=false) {
	global $dblink,$siteConfig;
	$id = intval($id);
	
	$category = (isset($_POST['category'])) ? intval($_POST['category']) : 0 ;
	$sitename = (isset($_POST['sitename'])) ? trim(inCode($_POST['sitename'])) : "" ;
	$siteurl = (isset($_POST['siteurl'])) ? inCode($_POST['siteurl']) : "" ;
	$email = (isset($_POST['email'])) ? inCode($_POST['email']) : "" ;
	$description = (isset($_POST['description'])) ? trim(inCode($_POST['description'])) : "" ;
	
	if ($ok OR !(extension_loaded('gd') AND $siteConfig['graphic_check']==1)) {
		@session_start();
		if ((isset($_POST['post_code_page']) AND isset($_SESSION['chk_addlink']) AND strtolower($_POST['post_code_page'])==strtolower($_SESSION['chk_addlink'])) OR !(extension_loaded('gd') AND $siteConfig['graphic_check']==1)) {				
			$save = true;
			if ($sitename=="") { $save = false; $msg = _SITENAME_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
			if (!validate($siteurl,"url")) { $save = false; $msg = _SITEURL_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
			if (!validEmail($email)) { $save = false; $msg = "<b>"._FIELDINVALID_."</b>: "._EMAIL_; }
			if ($description=="") { $save = false; $msg = _DESCRIPTION_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
			
			if ($save) {
				$dblink->query("INSERT INTO memht_mylinks (id,cat,name,url,email,description,flag)
								VALUES (null,'$category','$sitename','$siteurl','$email','$description','0')");
				echo "<div align='center'><b>"._THANKFORCONTRIBUTE_."</b></div>";
				echo "<meta http-equiv='refresh' content='3;URL=".$siteConfig['site_url']."/index.php?page=mylinks'>";
			} else {
				echo "<div align='center' id='errorText'><b>$msg</b></div>";
			}
		} else {
			echo "<div align='center' id='errorText'><b>"._WRONGSECCODE_."</b></div>";
		}
	} else {
		echo "<div align='center'><table border='0' class='box'>\n";
			echo "<form name='vote' method='post' action='index.php?page=mylinks&op=addLinkCheck&id=$id&ok=true'>\n";
			echo "<input type='hidden' name='category' value=\"$category\">\n";
			echo "<input type='hidden' name='sitename' value=\"$sitename\">\n";
			echo "<input type='hidden' name='siteurl' value=\"$siteurl\">\n";
			echo "<input type='hidden' name='email' value=\"$email\">\n";
			echo "<input type='hidden' name='description' value=\"$description\">\n";
			echo "<tr><td><img src='".$siteConfig['site_url']."/imgCode.php?name=chk_addlink&nc=".md5(time()+rand())."' title='header=["._SECURITY_CODE_."] body=["._SECURITY_CODE_TEXT_."]' alt='Captcha'></td></tr>\n";
			echo "<tr><td valign='top'><div align='left'><input type='text' name='post_code_page' size='10' maxlength='255'> <input type='submit' name='Submit' value='"._ADD_LINK_."'></form></div></td></tr>\n";
		echo "</table></div>\n";
	}
}

	openTable();
	
	switch($op) {
		case "addLinkCheck":
			addLinkCheck($id,$ok);
		break;
		
		case "addLink":
			addLink($id);
		break;
		
		case "showCategory":
			showCategory($id,$title,$ofsbgn,$ofsppg,$pg);
		break;
		
		case "catList":
		default:
			catList();
		break;
	}
	
	closeTable();

?>