<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://www.gekkoware.org
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

	require_once GEKKO_ROOT."lib/auth.inc.php";

	gpGlobalize("object, auth, larger_side, createlink");

	if ($auth) {
		//header("content-type: text/plain");
		if (gauth("admin") && checkAuthStr($auth)) {

			require_once GEKKO_ROOT."lib/file.lib.php";

			$dest = "data/files/uploaded";

			mkworkdir(GEKKO_ROOT.$dest);

			$file = file_get("local_file", "remote_file", $dest);

			if ($file) {

				$url = _l("C_SITE.URL").$file["rel_loc"];
				$ext = file_extension($file["rel_loc"]);

				// is image?
				$is_image = false;
				switch ($ext) {
					case "jpg": case "jpeg": case "png": case "gif": case "bmp":
						$is_image = true;
					break;
					default:
						if (GEKKO_DEMO_MODE) {
							// haha, keeping out those damn kiddies.
							abort(_l("E_DEMO_MODE"));
						}
					break;
				}

				if ($is_image && !$createlink) {
					if ($larger_side > 0) {
						require_once GEKKO_ROOT."lib/image.lib.php";
						// thumbnail, save in the same file
						$thumb = mkthumbnail($file["abs_loc"], $larger_side, true, false, "data/uploads");
						$url = _l("C_SITE.URL").$thumb["url"];
					}
					$script = "window.opener.gExec('{$object}', 'insertimage', '".addslashes($url)."');";
				} elseif (!$is_image || $createlink) {
					// the is a file... hmm...
					$script = "window.opener.gExec('{$object}', 'createlink', '".addslashes($url)."')";
				}

				// saving preferences
				conf::setKey("misc", "thumb.larger_side", $larger_side);

				echo "
				<script type=\"text/javascript\">
					$script
					window.close();
				</script>
				";
			} else {
				abort(uploadError());
			}
		} else {
			abort(accessDenied(true));
		}
		die;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" type="text/css" href="<?=_l("C_SITE.REL_URL")?>templates/<?=_l("C_SITE.TEMPLATE")?>/<?=_l("C_SITE.STYLESHEET")?>.css" />
	<title><?=_l("L_GET_FILE")?>-<?=_l("C_SITE.TITLE")?></title>
</head>
<body style="margin: 0px; padding: 0px;">
	<div class="info">
		<h1><?=_l("L_GET_FILE")?></h1>
		<form action="uploadimage" method="post" enctype="multipart/form-data">
		<input type="hidden" name="auth" value="<?=_l("C_AUTH_STR")?>" />
		<input type="hidden" name="object" value="<?=$object?>" />
		<?=_l("L_URL")?>:<br />
		<input class="text" type="text" name="remote_file" /><br />
		<?=_l("L_FILE")?>:<br />
		<input type="file" name="local_file" /><br />
		<?=_l("L_LARGER_SIDE")?>:<br />
		<input type="text" name="larger_side" value="<?=conf::getKey("misc", "thumb.larger_side", "250", "int")?>" size="5" />px<br />

		<input type="checkbox" name="createlink" value="1"> <?=_l("L_CREATE_LINK")?><br />

		<div class="buttons">
			<button type="submit"><?=_l("L_SUBMIT")?></button>
			<button type="button" onclick="window.close()"><?=_l("L_CANCEL")?></button>
		</div>
		</form>
	</div>
</body>
</html>