<?PHP
/**
* The frame that contains the image to be edited.
* @author Wei Zhuo
* @author Paul Moers <mail@saulmade.nl> - watermarking and replace code + 
* several small enhancements <http://fckplugins.saulmade.nl>
* @version $Id: editframe.php,v 1.7 2006/12/20 18:19:28 thierrybo Exp $
* @package ImageManager
*/

/**************************************
    File modified for:
    Webutler V3.0 - www.webutler.de
    Copyright (c) 2008 - 2014
    Autor: Sven Zinke
    Free for any use
    Lizenz: GPL
**************************************/

require_once dirname(dirname(dirname(__FILE__))).'/includes/loader.php';

$webutlerimgedit = new WebutlerAdminClass;
$webutlerimgedit->config = $webutler_config;

if(!$webutlerimgedit->checkadmin())
    exit('no access');

require_once $webutlerimgedit->config['server_path'].'/admin/imageedit/lang/'.$_SESSION['loggedin']['userlang'].'.php';

require_once($webutlerimgedit->config['server_path'].'/admin/imageedit/config.php');
require_once($webutlerimgedit->config['server_path'].'/admin/imageedit/classes/files.php');
require_once($webutlerimgedit->config['server_path'].'/admin/imageedit/classes/transform.php');
require_once($webutlerimgedit->config['server_path'].'/admin/imageedit/classes/managerclass.php');
require_once($webutlerimgedit->config['server_path'].'/admin/imageedit/classes/editorclass.php');

//$manager = new ImageManager($IMConfig);
$manager = new ImageManager;
$manager->config = $IMConfig;

//$editor = new ImageEditor($manager, $IMConfig);
$editor = new ImageEditor;
$editor->manager = $manager;
$editor->IMConfig = $IMConfig;
$editor->wbconfig = $webutler_config;
//$webfonts = $editor->sort_webfonts();
$webfonts = $webutlerimgedit->webfonts();

//$manager = new ImageManager($IMConfig);
//$editor = new ImageEditor($manager);

//$webfonts = $editor->sort_webfonts();

$ckeparams = '';
if(isset($_GET['CKEditor']))
    $ckeparams.= '&CKEditor='.$_GET['CKEditor'];
if(isset($_GET['CKEditorFuncNum']))
    $ckeparams.= '&CKEditorFuncNum='.$_GET['CKEditorFuncNum'];

// get image info and process any action
$iseditfile = $webutlerimgedit->config['server_path'].'/content/media/temp/'.basename($_GET['img']);
if(!file_exists($iseditfile))
{
    $imgfilename = $editor->createUnique(basename($_GET['img']));
    $oldfullpath = $webutlerimgedit->config['server_path'].'/content/media/image'.$_GET['img'];
    if(file_exists($oldfullpath))
    {
        $newfullpath = $webutlerimgedit->config['server_path'].'/content/media/temp/'.$imgfilename;
        $editor->CopyStartFile($oldfullpath, $newfullpath);
        header('Location: editframe.php?img='.$imgfilename.$ckeparams);
    }
    else
    {
        header('Location: editframe.php?img=');
    }
}
else
{
    $imageInfo = $editor->processImage();
}

$watermarkarrays = $editor->watermarkimgs($imageInfo);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="content-script-type" content="text/javascript" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="robots" content="noindex, nofollow" />
<link href="assets/editframe.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
/* <![CDATA[ */
	var messages = new Array(<?php echo "'"._WBLANGADMIN_IMGEDIT_MESSAGE_CROP_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_TEXT_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_SCAL_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_FLIP_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_ROTATE_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_SAVE_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_WATERMARK_."','"._WBLANGADMIN_IMGEDIT_MESSAGE_NOFILENAME_."'"; ?>);
/* ]]> */
</script>
<script type="text/javascript" src="assets/common.js"></script>
<script type="text/javascript" src="assets/wz_jsgraphics.js"></script>
<script type="text/javascript" src="assets/wz_dragdrop.js"></script>
<script type="text/javascript" src="assets/editframe.js"></script>
<script type="text/javascript" src="assets/content.js"></script>

<script type="text/javascript">
/* <![CDATA[ */

	var processedAction = "<?php echo (isset($_GET['action']) ? $_GET['action'] : ''); ?>";
	var currentImageFile = "<?php if(count($imageInfo)>0) echo rawurlencode($imageInfo['file']); ?>";
	var CKEparams = "<?php echo $ckeparams; ?>";

	//show action buttons and current action's controls - were hidden during processing
	if(processedAction != '')
	{
		var buttons = parent.document.getElementById('buttons');
		buttons.style.display = 'block';
	}

<?php if($editor->isFileSaved() == 1) { ?>

    /*
	alert(window.top.opener.name);
	var win = parent;
	if(document.all)
		win = parent;
	else
		win = window.top;
    */

    try {
        var CKEopener = window.top.opener.CKEDITOR;
    
    	//Reload editors image
    	if(window.top.opener && (window.top.opener.name == 'CKBrowseNoneDialog' || window.top.opener.name == 'CKBrowseDialog')) //base system is Mediabrowser
        {
    		window.top.opener.location.reload();
    	}
        <?php if(isset($_GET['CKEditorFuncNum'])) { ?>
    	else if(CKEopener) //base system is CKEditor
        {
            CKEopener.tools.callFunction( <?php echo $_GET['CKEditorFuncNum']; ?>,
            {
                newName : '<?php echo $editor->FileNewName(); ?>',
                newWidth : <?php echo $imageInfo['width']; ?>,
                newHeight : <?php echo $imageInfo['height']; ?>
            });
    	}
        <?php } ?>
    } catch(e) {}

	alert('<?PHP echo _WBLANGADMIN_IMGEDIT_SAVED_; ?>');
	
<?php } elseif($editor->isFileSaved() == -1) { ?>
	
    alert('<?PHP echo _WBLANGADMIN_IMGEDIT_NOTSAVED_; ?>');
    
<?php } ?>
	
    var watermarkBox = parent.document.getElementById("watermark_file");
    <?php
	foreach($watermarkarrays as $watermarkarray) {
        echo $watermarkarray."\n";
    }
    ?>
	
	var watermarkingEnabled;
	if(watermarkBox.options[1]) //show watermark controls
	{
		parent.document.getElementById("watermarkControls").style.display = "block";
		parent.document.getElementById("watermarkMessage").style.display = "none";
		watermarkingEnabled = true;
	}
	
	else //no watermarks found
	{
		parent.document.getElementById("watermarkControls").style.display = "none";
		parent.document.getElementById("watermarkMessage").style.display = "block";
		watermarkingEnabled = false;
	}
/* ]]> */
</script>

<style type="text/css">
/* <![CDATA[ */
<?PHP
    foreach($webfonts as $webfont => $names)
    {
        echo "@font-face {\n";
        echo "  font-family: '".$webfont."';\n";
        foreach($names as $ext => $name) {
            if($ext == 'eot') {
                echo "  src: url('../../../includes/webfonts/".$webfont.".eot');\n";
                echo "  src: url('../../../includes/webfonts/".$webfont.".eot?iefix') format('eot'),\n";
            }
            if($ext == 'ttf') {
                echo "       url('../../../includes/webfonts/".$webfont.".ttf') format('truetype'),\n";
            }
            if($ext == 'woff') {
                echo "       url('../../../includes/webfonts/".$webfont.".woff') format('woff');\n";
            }
        }
        echo "}\n";
    }
?>
/* ]]> */
</style>
</head>

<body scroll="no">
<table style="height: 100%; width: 100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td style="height: 100%; width: 100%">
<?php if($editor->isGDEditable() == -1) { ?>
			<div class="error"><?PHP echo _WBLANGADMIN_IMGEDIT_NOGIF1_; ?><br /><?PHP echo _WBLANGADMIN_IMGEDIT_NOGIF2_; ?></div>
<?php } elseif(count($imageInfo) > 0 && is_file($imageInfo['fullpath'])) { ?>
			<div id="ant" class="noselection"><img src="img/blank.gif" id="cropContent" alt="" /></div>
			
			<div id="background" name="background" style="width: <?php echo $imageInfo['width']; ?>px; height: <?php echo $imageInfo['height']; ?>px; background-image: url('<?php echo $imageInfo['src']; ?>');"><img src="img/blank.gif" name="floater" id="floater" /></div>
			
			<div id="TextBG" name="TextBG" style="width: <?php echo $imageInfo['width']; ?>px; height: <?php echo $imageInfo['height']; ?>px; background-image: url('<?php echo $imageInfo['src']; ?>')"><div id="insertText" name="insertText" onmouseup="parent.updatePosition()"></div></div>
			
			<div id="imgCanvas" name="imgCanvas" class="crop"><img src="<?php echo $imageInfo['src']; ?>" orgwidth="<?php echo $imageInfo['width']; ?>" orgheight="<?php echo $imageInfo['height']; ?>" style="width: <?php echo $imageInfo['width']; ?>px; height: <?php echo $imageInfo['height']; ?>px" name="theImage" id="theImage" /></div>
<?php } else { ?>
			<div class="error"><?PHP echo _WBLANGADMIN_IMGEDIT_NOIMAGE_; ?></div>
<?php } ?>
		</td>
	</tr>
</table>

<script type="text/javascript">
/* <![CDATA[ */

	SET_DHTML("TextBG"+NO_DRAG, "insertText"+CURSOR_MOVE, "background"+NO_DRAG, "floater"+CURSOR_MOVE);

	dd.elements.TextBG.addChild("insertText");
	dd.elements.TextBG.hide(true);
	dd.elements.insertText.hide(true);
	dd.elements.insertText.moveTo(0, 0);
	verify_insertText();

	if (watermarkingEnabled == true)
	{
		dd.elements.background.addChild("floater");
		dd.elements.floater.swapImage(eval("window.blankPreload.src"));
		dd.elements.floater.resizeTo(1, 1);
        dd.elements.floater.<?php echo (isset($_GET['action']) && $_GET['action'] == 'watermark') ? 'show' : 'hide'; ?>(true);
		verifyBounds();
	
		// make sure the slider of the watermark's opacity if at max
		parent.window.updateSlider(100, 'watermark');
	}
	else
	{
		dd.elements.background.show(true);
		dd.elements.floater.hide(true);
	}

	// hiding parent processing message
	parent.window.hideMessage();

/* ]]> */
</script>
</body>
</html>
