<?php
require_once("init.php");
$incdir = dirname(__FILE__);
define("INC", $incdir);

$path = "../uploads/affiliates/";

$area = ((!isset($area)) || ($_REQUEST['area']=="")) ? 1 : $_REQUEST['area'];
$limit = (isset($_REQUEST['pp']) && $_REQUEST['pp']!="") ? $_REQUEST['pp'] : 10;

$tmpl = new Koobi("templates/".ADMINTHEME."/affiliates/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->assign("kversion", KVERSION);
$tmpl->assign('uid', UID);
$tmpl->assign('pref', $pref);

if( (!permission('affiliate_new')) && (!permission('affiliate_edit')) ) { echo error($langadmin['noperms']); exit(); }

switch($_REQUEST['action']){
	case "" :
	case "view" :
	$items = array();
	$sql = new dbc("SELECT * FROM ".PREFIX."_affiliates WHERE area='$area' order by posi asc");
	while($row = $sql->fetchrow()){
		array_push($items, $row); 
	}
	$tmpl->assign('items', $items);
	$tmpl->display('affiliates_show.tpl');
	break;
	
	case "newimage" :
	
	if($_FILES['file']['type']=="image/pjpeg" || 
	$_FILES['file']['type']=="image/jpeg" || 
	$_FILES['file']['type']=="image/x-png" || 
	$_FILES['file']['type']=="image/png" || 
	$_FILES['file']['type']=="image/gif") {
		
		if($_REQUEST['delbutton']==1) @unlink($path.$_REQUEST['oldbutton']);
		
		$fupload_name = ktrim($_FILES['file']['name'],1);
		$fupload_name = rand_tostring($path ,$fupload_name);
		
		move_uploaded_file($_FILES['file']['tmp_name'], $path . $fupload_name);
		@chmod($path . $fupload_name,0777);
		
		$sql = new dbc("UPDATE ".PREFIX."_affiliates SET image = '$fupload_name' WHERE id = '$_REQUEST[id]'");
		header("Location:affiliates.php?area=$_REQUEST[area]");
		alog("alog_new_affiimage"  ," " . $_REQUEST['id']);
	}
	$sql = new dbc("SELECT * FROM ".PREFIX."_affiliates WHERE id='$_REQUEST[id]'");
	$row = $sql->fetchrow();
	$tmpl->assign('row', $row);
	$tmpl->display('affiliates_show.tpl');
	break;
	
	
	
	case "new" :
	if(!permission('affiliate_new')){ echo error($langadmin['noperms']); exit(); }
	if($_FILES['file']['type']=="image/pjpeg" || 
	$_FILES['file']['type']=="image/jpeg" || 
	$_FILES['file']['type']=="image/x-png" || 
	$_FILES['file']['type']=="image/png" || 
	$_FILES['file']['type']=="image/gif") {
		
		$fupload_name = ktrim($_FILES['file']['name'],1);
		$fupload_name = rand_tostring($path ,$fupload_name);
		
		move_uploaded_file($_FILES['file']['tmp_name'], $path . $fupload_name);
		@chmod($path . $fupload_name,0777);
		$sql_in = "$fupload_name";
		
		
	}
	
	if($_REQUEST['url']!=""){
		$sql = new dbc("INSERT ".PREFIX."_affiliates (id,url,posi,name,image,area) VALUES (
		'',
		'$_REQUEST[url]',
		'$_REQUEST[posi]',
		'".strip_tags($_REQUEST['name'])."',
		'$fupload_name',
		'$_REQUEST[area]'
		)");
	}
	alog("alog_new_affiliate"  , " " . $sql->getid());
	header("Location:affiliates.php?area=$_REQUEST[area]");
	break;
	
	
	case "save";
	if(!permission('affiliate_edit')){ echo error($langadmin['noperms']); exit(); }
	if($_POST['posi']>=1){
		foreach ($_POST['posi'] as $id => $posi) {
			if( $posi != "")  {
				$result = new dbc("UPDATE ".PREFIX."_affiliates  set posi='$posi' WHERE id = '$id' and area='$_REQUEST[area]'");
			}
		}
	}
	
	if($_POST['url']>=1){
		foreach ($_POST['url'] as $id => $url) {
			if( $url != "")  {
				$result = new dbc("UPDATE ".PREFIX."_affiliates  set url='$url' WHERE id = '$id' and area='$_REQUEST[area]'");
			}
		}
	}
	
	if($_POST['hits']>=1){
		foreach ($_POST['hits'] as $id => $hits) {
			if( $hits != "")  {
				$result = new dbc("UPDATE ".PREFIX."_affiliates  set hits='$hits' WHERE id = '$id' and area='$_REQUEST[area]'");
			}
		}
	}
	header("Location:affiliates.php?area=$_REQUEST[area]");
	break;
	
	
	if($_POST['name']>=1){
		foreach ($_POST['name'] as $id => $name) {
			if( $name != "")  {
				$result = new dbc("UPDATE ".PREFIX."_affiliates  set name='$name' WHERE id = '$id' and area='$_REQUEST[area]'");
			}
		}
	}
	
	case "del" :
	if(!permission('affiliate_edit')){ echo error($langadmin['noperms']); exit(); }
	$result = new dbc("DELETE FROM ".PREFIX."_affiliates WHERE id = '$_REQUEST[id]'");
	@unlink($path.$_REQUEST['button']);
	alog("alog_del_affiliate"  ,  $result->getid());
	header("Location:affiliates.php?area=$_REQUEST[area]");
	break;
}
?>