<?php

class MSSPClass{

  var $ArrMsSp = '';
  
  var $Sp_DbConf = array();

  var $SP_DbConnect = '';
  
  var $SP_DbDsnInfo = '';
	
  function MSSPClass($ArrMsSp){
    
		$this->ArrMsSp = $ArrMsSp;
    
    $this->Sp_DbConf = $this->ArrMsSp->Sp_DbConf;
    
    $this->SP_DbConnect = $this->ArrMsSp->SP_DbConnect;
    
    $this->SP_DbDsnInfo = $this->ArrMsSp->SP_DbDsnInfo;
	}
}

class SitePage {

	var $Sp_DbConf = array();
  
  var $SP_DbConnect = '';
  
  var $SP_DbDsnInfo = '';

	var $Sp_Config = array();
  
  var $Sp_LangMessage = array();

	var $Sp_PathToSmartyLibs = '/libs/SmartyLibs';

	var $Sp_PathToPear = '/libs/pear';

	var $Sp_PathToSite = '/libs/site';

  var $Sp_PathToJquery = '/libs/jquery';

  var $Sp_PathToZend = '/libs';
  
  var $Sp_PathToSiteScript = '';

	var $Sp_smarty_template_dir = '';

	var $Sp_smarty_compile_dir = '';

	var $Sp_smarty_config_dir = '';

	var $Sp_smarty_cache_dir = '';

	var $Sp_PathToSmarty = '/smarty';

	var $Sp_PathTop = '';

	var $Sp_Lang = '';

  var $Sp_Images = '/images';

	//var $Sp_i18n = array();

	var $Sp_TemplateName = 'ru';

	var $SP_CurrentHref = '';

	var $SP_CurrPage = '';

	//var $SP_TitleName = '';

	var $SP_CurrFileNameText = '';
  
  var $SP_UsersPref = array();
  
  var $SP_ProgrammPref = array();
  
  var $ObjLangMod = null;

	function SitePage(LangMod $ObjLangMod) {
		ini_set('include_path', $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToZend . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToSmartyLibs . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToPear . PATH_SEPARATOR . ini_get('include_path'));
    $this->ObjLangMod = $ObjLangMod;
    //var_dump($this->SP_DbConnect);
		$this->Sp_DbConf = $this->GetDbConf();
    //var_dump($this->Sp_DbConf['dbconfig']);
    $this->SP_DbConnect = DbMod::DbConnect($this->Sp_DbConf['dbconfig']['DbDsn'], $this->Sp_DbConf['dbconfig']['DbDefaultCharSet']);
    //var_dump($this->SP_DbConnect);
		$this->Sp_Config = $this->GetConfig();
		$this->Sp_Lang = $this->SetLang($this->Sp_Config['DefaultLangName']);
		$this->Sp_PathTop = $this->GetPathTop();
//    $this->Sp_TemplateName = $this->GetTemplateName($VarLang);
    $this->Sp_smarty_template_dir = $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToSmarty .'/'. $this->Sp_TemplateName . '/templates';
    $this->Sp_smarty_compile_dir = $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToSmarty .'/'. $this->Sp_TemplateName . '/templates_c';
		$this->Sp_smarty_config_dir = $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToSmarty .'/configs';
		$this->Sp_smarty_cache_dir = $_SERVER['DOCUMENT_ROOT'] . $this->Sp_PathToSmarty .'/cache';
		$this->SP_CurrentHref = $this->GetCurrentHref();
		//$this->SP_CurrPage = $this->GetCurrPage();
		//$this->SP_TitleName = $this->GetTitleName();
		$this->SP_CurrFileNameText = $this->GetCurrFileNameText();
		//$this->SP_CurrFolder = $this->GetCurrFolder();
	}

	function GetDbConf(){
    if (true === file_exists($_SERVER['DOCUMENT_ROOT'] .'/../config.ini.php')){
			$ArrReturn = parse_ini_file($_SERVER['DOCUMENT_ROOT'] .'/../config.ini.php', true);
			return $ArrReturn;
		}
		else {
			return false;
		}
	}

	function GetConfig(){
    $MSSP = new MSSPClass($this);
    $ObjConfigMod = new ConfigMod($MSSP);
    $ArrConfig = $ObjConfigMod->GetFromDb('SqlAssoc', 'GetAssoc');
   	return $ArrConfig;
	}
  
  function GetLangMessage($VarLang = ''){
    $VarLangFile = $this->ObjLangMod->VarLangsFolder . $VarLang .'.ini.php';
    if (true === file_exists($VarLangFile)){
			$ArrReturn = parse_ini_file($VarLangFile, true);
			return $ArrReturn;
		}
		else {
			return false;
		}
	}

	function GetPathTop(){
		$VarCountSlashes = substr_count($_SERVER['PHP_SELF'], '/');
		switch ($VarCountSlashes){
		case 0:
		case 1:
      return '.';
  		break;
		default:
      if (true === isset($VarCountSlashes) && true === is_numeric($VarCountSlashes)){
        $VarCountSlashes--;
        return substr(str_repeat('../', $VarCountSlashes), 0, -1);
      }
      else {
        return false;
      }
  		break;
		}
	}

	function GetCurrentHref(){
		$VarReturn = $_SERVER['SCRIPT_NAME'];
		return $VarReturn;
	}

	function GetCurrFileNameText(){
		$VarReturn = str_replace('.php', '.htm', $this->SP_CurrentHref);
		$VarReturn = $_SERVER['DOCUMENT_ROOT'] .'/'. $this->Sp_Config['FolderText'] .'/'. $this->Sp_Lang . $VarReturn;
		if (true === is_file($VarReturn)){
			$VarSP_CurrFileNameText = $VarReturn;
		}
		else {
      $VarSP_CurrFileNameText = '';
			//$VarSP_CurrFileNameText = $_SERVER['DOCUMENT_ROOT'] .'/distr/default.htm';
		}
    $this->SP_CurrFileNameText = str_replace($_SERVER['DOCUMENT_ROOT'], '', $VarSP_CurrFileNameText);
		return $VarSP_CurrFileNameText;
	}
	
	function SetLang($VarLang = ''){
    if('' != $VarLang && 2 == strlen($VarLang)){
      $this->Sp_LangMessage = $this->GetLangMessage($VarLang);
      return $VarLang;
    }
    else{
      return false;
    }
	}
	
//	function GetTemplateName($VarLang = ''){
//    if ('' != $VarLang){
//			return $VarLang;
//		}
//		else {
//			return false;
//		}
//	}

//	function FileToVar($VarFile = ''){
//		require_once ('File.php');
//		$FileMod = new File();
//		$result = $FileMod->readAll($VarFile);
//		$FileMod->close($VarFileTmp, FILE_MODE_READ);
//		if (PEAR::isError($result)){
//			die($result->getMessage());
//		}
//		else {
//			return $result;
//		}
//	}

	function GetCurrPageText(){
		$VarReturn = SiteLibs::FileToVar($this->GetCurrFileNameText());
		return $VarReturn;
	}

	function GetCurrPage(){
		return $this->SP_CurrPage;
	}

/*
	function GetCurrFolder(){
		$VarCurrentHref = $this->SP_CurrentHref;
		$VarPos = strrpos($VarCurrentHref, '/');
		$VarCurrentHref = substr($VarCurrentHref, $VarPos + 1);
		$VarPos = strpos($VarCurrentHref, '.');
		$VarCurrFolder = substr($VarCurrentHref, 0, $VarPos);
		return $VarCurrFolder;
	}
*//*
	function GetTitleName($ArrCurr = array(), $VarCurrPage = ''){
		if ('' == $VarCurrPage){
			$VarCurrPage = $_SERVER['REQUEST_URI'];
		}
		if ('/' == $VarCurrPage){
			$VarCurrPage = '/index.php';
		}
		foreach ($ArrCurr AS $key => $val){
			if ($val['href'] == $VarCurrPage){
				return $val['title'];
			}
		}
		SitePage::GetVarDump($ArrCurr);
		return false;
	}
	*/
}
?>