<?
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/process/image_choice_save.php - version 1.01
*/

// Send back "1" on success, error string if not

$nRelID = $nImgPath = $nType = $nImgID = "";
if(isset($_GET["iid"])){ $nRelID = preg_replace("/[[:^digit:]]/", '',$_GET["iid"]); } else { $nRelID = preg_replace("/[[:^digit:]]/", '',$_POST["iid"]); }
if(isset($_GET["path"])){ $nImgPath = trim(nSanitizeGeneralText($_GET["path"])); } else { $nImgPath = trim(nSanitizeGeneralText($_POST["path"])); }
if(isset($_GET["t"])){ $nType = trim(nSanitizeGeneralText($_GET["t"])); } else { $nType = trim(nSanitizeGeneralText($_POST["t"])); }
if(isset($_GET["imgid"])){ $nImgID = preg_replace("/[[:^digit:]]/", '',$_GET["imgid"]); } else { $nImgID = preg_replace("/[[:^digit:]]/", '',$_POST["imgid"]); }
if(isset($_GET["div"])){ $nDivToUpdate = trim(nSanitizeGeneralText($_GET["div"])); } else { $nDivToUpdate = trim(nSanitizeGeneralText($_POST["div"])); }

// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");
require("../../inc/config.php");
require("../inc/lang.php");


// server-side form error checking
//if(!$nImgPath){ exit("The image path can not be empty. Please try again."); }

if($nRelID && $nImgPath){
	switch($nType){
		case "prdMain":
		case "Prd":
		case "Cat":
		case "Tag":
			/* remove all prior images from category
			$Query = "DELETE FROM nImage WHERE nRelID=$nRelID";
			mysql_select_db($DBName);
			mysql_query($Query, $Link);
			*/
			// add this image ID to the category (don't remove first if image is unlinked prior or more than one image is allowed)
			$Query = "INSERT INTO nImage (nPath,nType,nRelID) VALUES ('$nImgPath','$nType','$nRelID')";
			mysql_select_db($DBName);
			mysql_query($Query, $Link);
			break;
		case "comp":
			// Get associated content ID
			$nSQL = "SELECT nContentID FROM xComponentPage WHERE nID=$nRelID";
			mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
			if($Row = mysql_fetch_array($Result)){
				$nContentID = $Row["nContentID"];
			}
			// nTranslate is updated in component_edit.php
	}
	// close and exit
	require("../inc/db_close.php");
}
exit("1");


function nSanitizeGeneralText($nText){
	$notAllowed = array("<",">","1=1","1 =1","1= 1","1 = 1");
	$nText = trim(str_replace($notAllowed,"",$nText));
	$nText = trim(str_replace("\"","&quot;",$nText));
	return $nText;
}
function nSanitizeNumeric($nNumber){
	$nNumber = preg_replace("/[^0-9\.]/", "", $nNumber);
	return $nNumber;
}

require("../inc/db_close.php");
?>