<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: new_infusion.php
| CVS Version: 1.00
| Author: INSERT NAME HERE
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
require_once "../../maincore.php";
require_once THEMES."templates/header.php";



// Check if locale file is available matching the current site locale setting.
if (file_exists(INFUSIONS."auto_avatar_resizer_infusion/locale/".$settings['locale'].".php")) {
	// Load the locale file matching the current site locale setting.
	include INFUSIONS."auto_avatar_resizer_infusion/locale/".$settings['locale'].".php";
} else {
	// Load the infusion's default locale file.
	include INFUSIONS."auto_avatar_resizer_infusion/locale/English.php";
}
require_once "cnf.php";


// FUNCTIONS
function printTemplateSelector($selected) {
	global $db_prefix, $locale , $userdata;
	$sql = "SELECT * FROM ".$db_prefix."auto_avatar_resizer_templates ORDER BY tem_name ASC";
	$result = dbquery($sql);
	if (dbrows($result) != 0) {
		echo "<select name=\"lstTemplate\" class=\"textbox\">";
		while ($data = dbarray($result)) {
			$format = "";
			switch ((int) $data['tem_format']) {
				case IMAGETYPE_GIF:
					$format = ".gif";
					break;
				case IMAGETYPE_JPEG:
					$format = ".jpg";
					break;
				case IMAGETYPE_PNG:
					$format = ".png";
					break;
			}
			if ($data['tem_id'] == $selected) {
				echo "<option value=\"".$data['tem_id']."\" selected>".$data['tem_name']." (".$data['tem_maxX']."x".$data['tem_maxY']." ".$format.")</option>";
			} else {
				echo "<option value=\"".$data['tem_id']."\">".$data['tem_name']." (".$data['tem_maxX']."x".$data['tem_maxY']." ".$format.")</option>";
			}
		}
		echo "</select>";
	} else {
		echo $locale['rsze950'];
	}
}
function printResizeForm($template, $width, $height, $error) {
	global $locale, $userdata;
	opentable($locale['rsze200']);
	echo "<center>";

if ($userdata['user_avatar']) {

if (isset($_POST['update_profile'])) require_once INCLUDES."update_profile_include.php";

echo "<form name='inputform' method='post' action='".FUSION_SELF."' enctype='multipart/form-data'>
	<input type='hidden' name='user_avatar' value='".$userdata['user_avatar']."'>\n";

echo "<center><br>
							<table summary='Images'>
								<tr>
									<td>
									<center><img src='".IMAGES."avatars/".$userdata['user_avatar']."' alt=''></td>
									<td>
									</td>
								</tr>
							</table>
							<br><a href='".BASEDIR."profile.php?lookup=$userdata[user_id]' class='side'><img border='0' style='vertical-align:middle'  src='".BASEDIR."/images/check.gif' alt=''>  ".$locale['rsze213']."</a><br><a href='".BASEDIR."edit_profile.php#no1' class='side'><img border='0' style='vertical-align:middle' src='".BASEDIR."images/uncheck.gif' alt=''>  ".$locale['rsze214']."</a></div>";

		}else{
echo "<form enctype=\"multipart/form-data\" name=\"auto_avatar_resizerForm\" action=\"auto_avatar_resizer.php\" method=\"post\">";
	echo "<table width=\"500\"><tr><td colspan=\"2\">".$locale['rsze207']."</td></tr>";
	echo "<tr><td width=\"180\">".$locale['rsze201'].":</td><td>";
		printTemplateSelector($template);
	echo "</td></tr>";

	echo "<tr><td>".$locale['rsze202'].":</td><td><input type=\"file\" name=\"fileImage\" class=\"textbox\"></td></tr>";
	echo "<tr><td>&nbsp;</td><td>
<br>
".$locale['rsze215']."
</td></tr>";
	echo "<tr><td><strong>".$error."</strong></td><td align=\"left\"><input type=\"submit\" class=\"button\" name=\"btnResize\" value='".$locale['rsze216']."'></td></tr>";
	echo "</table></form></center>";


}


	closetable();
	tablebreak();
}
/**
* Checks the file size and type of a given image.
* @name checkImage
* @param string [$imagefile] Where the image is located on the server ($_FILES[...]['tmp_name'])
* @param integer [$imagesize] Image size ($_FILES[...]['size'])
* @global array $resize_settings Array containing the Infusion Settings.
* @global array $locale Array from language-file (locale-file)
* @return string Returns blank if usable, error-message if too big or wrong filetype
* @author XtraLars <xtralars@baluba.org>
*/
function checkImage($imagefile, $imagesize) {
	global $resize_settings, $locale;
	if ($imagefile != "") {
		$maxSize = (int) $resize_settings['maxFileSize'];
		$return = "";
		$imagesize = (int) $imagesize;
		if ($imagesize > $maxSize) {
			$return .= "### too big... maxSize=$maxSize bytes<br />";
		}
		list($width, $height, $type, $attr) = getimagesize($imagefile);
		//echo " type: ".$type." width: ".$width." height ".$height." attr ".$attr;
		switch (exif_imagetype($imagefile)) {
			case IMAGETYPE_GIF:
				break;
			case IMAGETYPE_JPEG:
				break;
			case IMAGETYPE_PNG:
				break;
			default:
				$return .= "### ".$locale['rsze219']."<br />";
		}
	} else {
		$return = "### ".$locale['rsze220']."<br />";
	}
	return $return;
}
// START
$resize_settings = dbarray(dbquery("SELECT * FROM ".$db_prefix."auto_avatar_resizer_settings"));
$absolutemaxX = 2000;
$absolutemaxY = 2000;
if(checkgroup($resize_settings['group_id'])) {
	if (isset($_POST['btnResize'])) {
		// START VARIABLES

		$template = isNum($_POST['lstTemplate']) ? (int) $_POST['lstTemplate'] : -1;
		if (isset($_FILES['fileImage'])) {
			$tmpname = $_FILES['fileImage']['tmp_name'];
			$orgname = $_FILES['fileImage']['name'];
			$size = (int) $_FILES['fileImage']['size'];
			$image = new auto_avatar_resizerImage($tmpname, $orgname);
			if ($template != -1) {
				$template = dbarray(dbquery("SELECT * FROM ".$db_prefix."auto_avatar_resizer_templates WHERE tem_id=".$template));
				$width = (int) $template['tem_maxX'];
				$height = (int) $template['tem_maxY'];
				$outType = (int) $template['tem_format'];
				//echo "<br /> format : $outType";
				$userHeight = "";
				$userWidth = "";
			} else if ($userWidth != -1 && $userHeight != -1) {
				if ($userWidth > $absolutemaxX) {
					$userWidth = $absolutemaxX;
				}
				if ($userHeight > $absolutemaxY) {
					$userHeight = $absolutemaxY;
				}
				$width = $userWidth;
				$height = $userHeight;
				$outType = $image->imageType;
			} else {
				$error = $locale['rsze207'];
			}
			if ($image->ok) {
				$imageloc = $image->getResized($width, $height, $outType, 100);
				//echo "<br />".$image->file_location;
				//echo "<br />geir: ".$imageloc;
				//move_uploaded_file($tmpname, $imageloc);

				$image->dbAdd();

				opentable($locale['rsze208']);
				echo "<center>".$locale['rsze204']." : ".$width.", ".$locale['rsze205']." : ".$height."<br />".$locale['rsze212']."<br /><br /><img src=\"".$imageloc."\" /></center><br />";
                                                        echo "<center><a href='".BASEDIR."profile.php?lookup=$userdata[user_id]' class='side'><img border='0' style='vertical-align:middle'  src='".BASEDIR."/images/check.gif' alt=''>  ".$locale['rsze217']."</a></center>";
                                                        echo "<center><a href='".BASEDIR."edit_profile.php#no1' class='side'><img border='0' style='vertical-align:middle' src='".BASEDIR."images/uncheck.gif' alt=''>  ".$locale['rsze218']."</a></center><br />";
				closetable();
				tablebreak();
			} else {
				printResizeForm($template, $userWidth, $userHeight, $error);
			}

		}
	} else {
		printResizeForm(0, "", "", "");
	}

} else {
	fallback("../../index.php");
}


require_once THEMES."templates/footer.php";
?>