<?php
/*
 * 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/
 * 
 * FILE: inc/config.php
*/

// note: logo config in CSS
$nCPVersion = "1.12a";

// Manage site status and redirect
$nSettingsARRAY = array();
$nSQL = "SELECT * FROM nSettings";
mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
if($Result===FALSE){ exit("Settings for your website could not be loaded.  This most likely means that your installation has failed.<BR><BR>Please run the <a href=\"./installer.php\">WPM installer</a> again, or if it does not exist, copy it again along with the 'installer' folder to the root of your website."); }
while($Row = mysql_fetch_array($Result)){
	$nSetting = $Row["nSetting"];
	$nValue = $Row["nValue"];
	$nSettingsARRAY[$nSetting] = $nValue;
}

if(count($nSettingsARRAY)>0){
	// site settings
	$nSiteEnabled = (isset($nSettingsARRAY["siteEnabled"]) ? $nSettingsARRAY["siteEnabled"] : NULL);
	$nSiteRedirect = (isset($nSettingsARRAY["siteRedirect"]) ? $nSettingsARRAY["siteRedirect"] : NULL);
	$nDefaultLang = (isset($nSettingsARRAY["defLanguage"]) ? $nSettingsARRAY["defLanguage"] : NULL);
	$nCurrentTheme = (isset($nSettingsARRAY["activeTemplate"]) ? $nSettingsARRAY["activeTemplate"] : NULL);
	$nPermalinks = (isset($nSettingsARRAY["permalinks"]) ? $nSettingsARRAY["permalinks"] : NULL);
	$nAllowMobile = (isset($nSettingsARRAY["allowMobile"]) ? $nSettingsARRAY["allowMobile"] : NULL);
	// system settings
	$nCoName = (isset($nSettingsARRAY["coName"]) ? $nSettingsARRAY["coName"] : NULL);
	$nSiteName = (isset($nSettingsARRAY["siteName"]) ? $nSettingsARRAY["siteName"] : NULL);
	$nFullSitePath = (isset($nSettingsARRAY["fullSitePath"]) ? $nSettingsARRAY["fullSitePath"] : NULL);
	$nForceWWW = (isset($nSettingsARRAY["forceWWW"]) ? $nSettingsARRAY["forceWWW"] : NULL);
	$nSiteRootPath = (isset($nSettingsARRAY["siteRootPath"]) ? $nSettingsARRAY["siteRootPath"] : NULL);
}

// Define site root if not yet set
if(!$nSiteRootPath || $nSiteRootPath=="/"){
	if(PHP_OS == 'WINNT'){
		$nSiteRootPath = realpath(".\\")."\\";
		if(!$nSiteRootPath){ $nSiteRootPath = dirname($_SERVER['SCRIPT_FILENAME'])."\\"; }
		$nSiteRootPath = str_replace("\\inc\\","\\",$nSiteRootPath);
		$nSiteRootPath = str_replace("\\manage\\","\\",$nSiteRootPath);
		$nSiteRootPath = str_replace("\\list\\","\\",$nSiteRootPath);
		$nSiteRootPath = str_replace("\\","\\\\",$nSiteRootPath);
	} else {
		$nSiteRootPath = realpath('./')."/";
		if(!$nSiteRootPath){ $nSiteRootPath = dirname($_SERVER['SCRIPT_FILENAME'])."/"; }
		$nSiteRootPath = str_replace("/inc/","/",$nSiteRootPath);
		$nSiteRootPath = str_replace("/manage/","/",$nSiteRootPath);
		$nSiteRootPath = str_replace("/list/","/",$nSiteRootPath);
	}
	$Query = "UPDATE nSettings SET nValue='$nSiteRootPath' WHERE nSetting='siteRootPath'";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
}
// ******** end legacy fix


// Force "www" if necessary
if($nForceWWW){
	if(strpos($nFullSitePath,"www.")===false){ $nFullSitePath = str_replace("://","://www.",$nFullSitePath); }
	if(strpos($_SERVER['HTTP_HOST'],"www.")===false) { header("Location: $nFullSitePath"); exit(); }
}

if(!isset($_SESSION)){ session_start(); }
$ValidUser = (isset($_SESSION['ValidUser']) ? $_SESSION['ValidUser'] : NULL);
$nPHPSelf = $_SERVER["PHP_SELF"];
if(!$ValidUser){
	if($nPHPSelf!="/manage/" && $nPHPSelf!="/manage/index.php" && $nPHPSelf!="/manage/pwd_recovery.php" && $nPHPSelf!="/manage/process_login.php"){
		if(!$nSiteEnabled && $nSiteRedirect){ header ("Location: $nSiteRedirect"); exit(); }
		if(!$nSiteEnabled){ exit("Site currently offline for maintenance.  Please check back again later."); }
	}
}
?>
