<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

include("imgedit_gd.php");
require("stop_cache.php");

// Receives a form upload call, uploads the image and returns the full image tag

// Get uploaded image
$nBaseImgPath = "../../usr/system/";
$nUpImage = $_FILES['nSiteLogo'];
if($nUpImage['name']){
	$nUpImageFilename = $nUpImage['name'];
	$nUpImageTMPFile = $nUpImage['tmp_name'];
	$nUpImageSize = $nUpImage['size'];
	$nUpImageError = $nUpImage['error'];
	// Copy file to public folder and save to db
	$nImgExtension = nImg_GetExtension($nUpImageFilename);
	if($nImgExtension!="png") { $nImgError.="Filetype not supported ($nImgExtension). Please use a PNG image only."; }
	$max_size = "5000000"; // in bytes
	if($nUpImageSize > $max_size) { $nImgError.="Image is too large (over the ".($max_size/1000)."Kb limit) to upload. Please use a smaller file.<BR>"; }
	if($nUpImageError){ $nImgError.="An error occurred uploading your image Please make sure you are using a valid image file and is not greater than ".($max_size/1000000)."Mb.<BR>"; }
	$nNewFilename = "logo.png";
	if($nNewFilename && !$nImgError){
		$nImgPath = $nBaseImgPath.$nNewFilename;
		if(file_exists($nUpImageTMPFile)){ copy($nUpImageTMPFile,$nImgPath); } else { $nImgError.="Unable to read temp diectory for uploaded image.";  }
		// Post-processing
		// Resize uploaded image only if W or H greater than 400 x 80
		nResizeImage($nImgPath, $nImgPath, 400, 80);
	}
}
if($nImgError || !$nUpImage['name']){
	// form did NOT process successfully
	if($nImgError){ echo "<script>alert('IMAGE ERROR: $nImgError');</script>";; }
} else {
	echo nGetImgTagHTML($nImgPath);
}
?>