<?php
if(!defined('BASE_DIR')) exit;
if(!perm('inline_inlineshots')) exit;
function MakeInFn() {
	$fn = time().mt_rand(10000, 19999);
	if(@file_exists(BASE_DIR . "uploads/inlineshots/" . $fn )) 
	{
		return MakeInFn();
	} else {
		return $fn;
	}
}


function clean_is_text($text)
{
	#$text = str_replace(array("\"","'",'"','\''),array("","","",""),$text);
	#$text = preg_replace('/([^-!%&~*#\/}{§|?)(A-Za-zÀ-ÖØ-öø-ÿ0-9.\r\n[\] ]*)/', '', $text);
	return $text;
}

if($_REQUEST['action']=="add_inline") 
{

	switch(@$_FILES['shot']['type'])
	{
		case 'image/pjpeg':
		case 'image/jpeg':
		default:
			$endg = '.jpg';
			$CREATOR = 'imagecreatefromjpeg';
			$OUTPUT  = 'imagejpeg';
		break;
		
		case 'image/x-png':
		case 'image/png':
			$endg = '.png';
			$CREATOR = 'imagecreatefrompng';
			$OUTPUT  = 'imagepng';
		break;
		
		case 'image/gif':
			$endg = '.gif';
			$CREATOR = 'imagecreatefromgif';
			$OUTPUT  = 'imagegif';
		break;
	}
	
	//$_allowed = array('image/pjpeg','image/jpeg','image/x-png','image/png','image/gif');
	$_allowed = array('image/pjpeg','image/jpeg');
	
	$array = unserialize(base64_decode($_REQUEST['code']));
	$id = MakeInFn();
	$fn = BASE_DIR . "uploads/inlineshots/{$id}{$endg}";
	
	$array[] = array(
	'titel' => stripslashes($_REQUEST['titel']),
	'text'  => clean_is_text($_REQUEST['text']),
	'id'    => $id);
	
	
	if(in_array($_FILES['shot']['type'],$_allowed))
	{
		if(move_uploaded_file($_FILES['shot']['tmp_name'], $fn))
		{
			$altes_bild = $CREATOR(BASE_DIR . "uploads/inlineshots/{$id}{$endg}");
			$sz_alt = @getimagesize(BASE_DIR . "uploads/inlineshots/{$id}{$endg}");
			
			$sz  = ($sz_alt[0]>800) ? 800 : $sz_alt[0];
			
			$im_width  = imageSX($altes_bild);
			$im_height = imageSY($altes_bild);
			$factor    = $sz / $im_width;
			$w         = $sz;
			$h         = $im_height * $factor;
			
			$neues_bild = @imagecreatetruecolor($w, $h);
			@unlink(BASE_DIR . "uploads/inlineshots/{$id}{$endg}");
			imagecopyresampled($neues_bild, $altes_bild, 0, 0, 0, 0, imagesx($neues_bild), imagesy($neues_bild), imagesx($altes_bild), imagesy($altes_bild));
			
			$OUTPUT($neues_bild, BASE_DIR . "uploads/inlineshots/{$id}{$endg}",100);
		}
		
		@chmod(BASE_DIR . 'uploads/inlineshots/' . $id . $endg, 0777);
		
		$_REQUEST['code'] = base64_encode(serialize($array));
		$_SESSION['code'] = $_REQUEST['code'];
		
	}
	$_REQUEST['action'] = "inline_screens";
}

if($_REQUEST['action']=="rm_inline") {
	$array = unserialize(base64_decode($_REQUEST['code']));
	
	// <<-- Bild löschen -->>
	if ($_POST['submit'] == "delete") 
	{
		$_REQUEST['id'] = (!@$_REQUEST['id']) ? 0 : $_REQUEST['id'];
		$entry = $array[$_REQUEST['id']];
		unset($array[$_REQUEST['id']]);
		@unlink(BASE_DIR . 'uploads/inlineshots/' . $entry['id']);
		@unlink(BASE_DIR . 'uploads/inlineshots/' . $entry['id'] . $endg);
	} 
	// <<-- Bild bearbeiten -->>
	if ($_REQUEST['submit'] == "edit")
	{
		$_REQUEST['id'] = (!@$_REQUEST['id']) ? 0 : $_REQUEST['id'];
		$array[$_REQUEST['id']]['text'] = clean_is_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(isset($_REQUEST['is']) && $_REQUEST['is'] != '')
	{
		switch($_REQUEST['table'])
		{
			case 'news' : $sql = "SELECT " . $_REQUEST['fieldname'] . " AS Textbilder FROM ".PREFIX."_news WHERE Id='".$_REQUEST['id']."'"; break;
			case 'article' : $sql = "SELECT " . $_REQUEST['fieldname'] . " AS Textbilder FROM ".PREFIX."_artikel WHERE Id='".$_REQUEST['id']."'"; break;
			case 'content' : $sql = "SELECT " . $_REQUEST['fieldname'] . " AS Textbilder FROM ".PREFIX."_content WHERE Id='".$_REQUEST['id']."'"; break;
			case 'faq' : $sql = "SELECT " . $_REQUEST['fieldname'] . " AS Textbilder FROM ".PREFIX."_faq WHERE Id='".$_REQUEST['id']."'"; break;
			case 'products' : $sql = "SELECT " . $_REQUEST['fieldname'] . " AS Textbilder FROM ".PREFIX."_produkte WHERE Id='".$_REQUEST['id']."'"; break;
		}
		
		
		$sql = $GLOBALS['db']->Query($sql);
		$row = $sql->fetchrow();
		$_REQUEST['is'] = base64_encode(serialize($row->Textbilder));
	}
	
	if(@$_REQUEST['is']!=""){
		@$code  = unserialize(base64_decode($_REQUEST['is']));
	} else {
		@$code  .= unserialize(base64_decode($_REQUEST['is']));
	}
	
	$array = unserialize($code);
	$GLOBALS['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'] = htmlentities($val['titel']);
		$hiddenvalue[$i]['text'] = stripslashes(htmlentities($val['text']));
		$i++;
	}
	
	
	$GLOBALS['tmpl']->assign('hiddenvalue', @$hiddenvalue);
	$GLOBALS['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()){
	$GLOBALS['tmpl']->assign('buttoninsert', 1);
#}

$GLOBALS['tmpl']->assign('buttoninsert', 1);
$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/inline/main.tpl'));
?>