<?php
ob_start();
require_once("init.php");
if(!permission("adminpanel")){
	echo "<pre>".$langadmin['nopermsinlineshots'];
	exit();
}

$tmpl = new Koobi("templates/".ADMINTHEME."/inlineshots");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->register_function("permission", "permission");



function MakeInFn() {
	$fn = time().mt_rand(0, 1000000000);
	//if(@file_exists("../uploads/inlineshots/".$fn)) {
	if(@file_exists(BASEDIR . "uploads/inlineshots/" . $fn )) {
		return MakeInFn();
	} else {
		return $fn;
	}
}

if($_REQUEST['action']=="add_inline") {
	
	$array = unserialize(base64_decode($_REQUEST['code']));
	$id = MakeInFn();
	$fn = BASEDIR . "uploads/inlineshots/" . $id;
	
	$array[] = array(
	'titel' => stripslashes($_REQUEST['titel']),
	'text'  => stripslashes($_REQUEST['text']),
	'id'    => $id
	);
	if($_FILES['shot']['type']=="image/pjpeg" || $_FILES['shot']['type']=="image/jpeg"){
		move_uploaded_file($_FILES['shot']['tmp_name'], $fn);
		chmod (BASEDIR . 'uploads/inlineshots/' . $id, 0777);
		$_REQUEST['code'] = base64_encode(serialize($array));
	}
	$_REQUEST['action'] = "inline_screens";
}

if($_REQUEST['action']=="rm_inline") {
	$array = unserialize(base64_decode($_REQUEST['code']));
	
	// <<-- Bild löschen -->>
	if ($_POST['submit'] == "delete") {
		
		$entry = $array[$_REQUEST['id']];
		unset($array[$_REQUEST['id']]);
		@unlink(BASEDIR . 'uploads/inlineshots/' . $entry['id']);
	} 
	// <<-- Bild bearbeiten -->>
	if ($_POST['submit'] == "edit") {
		$array[$_REQUEST['id']]['text'] = $_REQUEST['text'];
		$array[$_REQUEST['id']]['titel'] = $_REQUEST['titel'];
		$array[$_REQUEST['id']]['id'] = $_REQUEST['img_id'];
	}
	
	$_REQUEST['code'] = base64_encode(serialize($array));
	$_REQUEST['action'] = "inline_screens";
}


if($_REQUEST['action']=="inline_screens") {
	if(!isset($_REQUEST['code'])) {
		$code = serialize(array());
	} else {
		$code = base64_decode($_REQUEST['code']);
	}
	
	
	if($_REQUEST['is']!=""){
		$code  = unserialize(base64_decode($_REQUEST['is']));
	} else {
		$code  .= unserialize(base64_decode($_REQUEST['is']));
	}
	
	
	$array = unserialize($code);
	
	
	
	$tmpl->assign('thearray', base64_encode(serialize($array)));
	
	$i = 0;
	
	while(list($key,$val) = each($array)) {
		$hiddenvalue[$i]['hiddencode'] = base64_encode(serialize($array));
		$hiddenvalue[$i]['hiddenid'] = $key;
		$hiddenvalue[$i]['img_id'] = $val['id'];
		$hiddenvalue[$i]['titel'] = htmlspecialchars($val['titel']);
		$hiddenvalue[$i]['text'] = stripslashes(htmlspecialchars($val['text']));
		$i++;
	}
	
	
	$tmpl->assign('hiddenvalue', $hiddenvalue);
	
	
	if(permission("candelete")) {
		$tmpl->assign("candelete", 1);
	}
	
	if(permission("inlineshotupload")) { 
		$tmpl->assign("canupload", 1);
		$tmpl->assign("hiddens", base64_encode(serialize($array)));
	} 
}

function checksys()
{
	$sAgent = $_SERVER['HTTP_USER_AGENT'] ;
	
	if ( is_integer( strpos($sAgent, 'MSIE') ) && is_integer( strpos($sAgent, 'Windows') ) && !is_integer( strpos($sAgent, 'Opera') ) ){
		return TRUE ;
	} else {
		return FALSE ;
	}
}

if(checksys()){
	$tmpl->assign('buttoninsert', 1);
}

$tmpl->assign('buttoninsert', 1);
$tmpl->display('main.tpl');
?>