<?
/*
 * 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/doc_save.php - version 1.0
*/

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

if(isset($_GET["rid"])){ $nRelID = preg_replace("/[[:^digit:]]/", '',$_GET["rid"]); } else { $nRelID = preg_replace("/[[:^digit:]]/", '',$_POST["rid"]); }
if(isset($_GET["path"])){ $nFileTmpPath = trim(nSanitizeGeneralText($_GET["path"])); } else { $nFileTmpPath = trim(nSanitizeGeneralText($_POST["path"])); }

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

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

if($nFileTmpPath){
	// setup paths for files
	$nFilePath = "../../usr/docs/$nFileTmpPath";
	$nBaseFilePath = "../../usr/docs/";
	$nDBFilePath = "usr/docs/";
	
	$nFileExtension = nImg_GetExtension($nFileTmpPath);
	$nFileExists = false;
	// See if this file already exists in the db, and if so, just leave it with the same ID
	$Query = "SELECT nID FROM nFile WHERE nPath='$nDBFilePath$nFileTmpPath'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nFileExists = true;
	}
	if(!$nFileExists){
		// Save to db
		$Query = "INSERT INTO nFile (nPath,nType) VALUES ('$nDBFilePath$nFileTmpPath','$nFileExtension')";
		mysql_select_db($DBName);
		mysql_query($Query, $Link);
	}
	// 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;
}
function nImg_GetExtension($nImgFile){
	if(!$nImgFile){ return; }
	$nImgInfo = pathinfo($nImgFile);
	$nExt = strtolower($nImgInfo["extension"]);
	return $nExt;
}

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