<?php

require_once('inc/common.php');

if (!isset($_GET['route'])) {
	$_GET['route']='';
}

Class CPage extends CBasePage {
	private $sDefPage = 'home.html';
	private $sDefPage404 = '404';

	function js_css() {
		$this->jsStart();
		$this->jsEnd();
	}
	
	function run() {
		$this->_default();
	}
	
	function _default() {
		$sRoute = $_GET['route'];
		$rgRoute = explode('/', $sRoute);
		
		if (count($rgRoute)<1||!$rgRoute[0]) {
			$rgRoute = array($this->sDefPage);
		}
		
		CHtml::$rgRoute = $rgRoute;
		switch ($rgRoute[0]) {
			case "action": $this->_action($rgRoute); break;
			default: $this->_page($rgRoute); break;
		}
	}
	
	function _page($rgRoute) {
		$oPage = new CModelPage();
		$oPageInfo = $oPage->get_item($rgRoute[0]);
    $this->form_prepare_captcha();
		
		if ($oPageInfo) {
			CHtml::html_init_ohtml($oPageInfo, $rgRoute, $this, $this->sDefPage);
			
			CHtml::shortcuts_parse_body();
			switch ($rgRoute[0]) {
				case "home.html": {
					$this->_page_home();
				} break;
			}
			
			$this->header();
			$this->js_css();
			echo '<div class="divHostPage">'.CHtml::$oHtml['sText'].'</div>';
			$this->footer();
		} else {
			header("Location: ".WWW_PATH.$this->sDefPage);
			exit;
		}
	}
	
	function _page_home() {
	}
	
	function die403() {
		header('HTTP/1.1 403 Forbidden');
		exit;
	}
	
	function _action($rgRoute) {
		$rgAllowed = array(
			'connect_connect', 'lang_sel'
		);
		if (!in_array($rgRoute[1], $rgAllowed)) {
			$this->die403();
		}
		
		if (in_array($rgRoute[1], $rgAllowed)) {
			$sMethod = '_action_'.$rgRoute[1];
			$this->$sMethod($rgRoute);
		}
	}
	
	function _action_connect_connect() {
    $nCaptcha = $_SESSION['captcha'][$_POST['sCaptchaId']];
    if ($this->prep_str($_POST['sCaptcha'])!=$nCaptcha) {
      $this->answer($_POST, array('sError2'=>'Невірна відповідь'));
      return;
    }    
    
		$sName = $this->prep_str($_POST['sConnectName']);
		$sPhone = $this->prep_str($_POST['sConnectPhone']);
		$sAddr = $this->prep_str($_POST['sConnectAddr']);
		$sTariff = $this->prep_str($_POST['sTariff']);

		$to      = CONNECT_EMAIL;
		$subject = 'Заявка на подключение';
		$message = "Имя: ".$sName."\n"."Телефон: ".$sPhone."\n"."Адрес: ".$sAddr."\nТарифный пакет: ".$sTariff;
		$headers = 'From: cc@dctel.net' . "\r\n" .
				'Reply-To: cc@dctel.net' . "\r\n" .
				'X-Mailer: PHP/' . phpversion();
		
		$sError2 = null;
		$fRes = mail($to, $subject, $message, $headers, " -fcc@dctel.net");
    $this->log(array(
      'msg'=>$_POST,
      'mailres'=>$fRes
    ));
		//var_dump($fRes);
		if (!$fRes) {
			$sError2 = 'Произошла ошибка при отправке запроса на подключение. Сожалеем об этом.';
		}

		$this->answer($_POST, array('sError2'=>$sError2));
	}
	
  function form_prepare_captcha() {
    $sId = uniqid();
    $rgList = isset($_SESSION['captcha'])?$_SESSION['captcha']:array();
    $a = rand(1,9);
    $b = rand(1,9);
    $sign = rand(1,2)==1?1:-1;
    if ($sign==-1&&$a<=$b) {
      $a = $b+1;
    }
    $rgList[$sId] = $a+($b*$sign);
    $_SESSION['captcha'] = $rgList;
    $_SESSION['__CAPTCHA_ID__'] = $sId;
    $_SESSION['__CAPTCHA_FORMULA__'] = $a.($sign==1?' + ':' - ').$b.' =';
  }
 
	function _action_lang_sel() {
		$rgAllowed = array('ua', 'ru');
		$sLang = $this->prep_str($_POST['sLang']);
		if (!in_array($sLang, $rgAllowed)) {
			return;
		}
		$_SESSION['sLang'] = $sLang;

		$this->answer($_POST, $_POST);
	}
	
	/*function _action_login() {
		$sUserName = $this->prep_str($_POST['sUserName']);
		$sPassword = $this->prep_str($_POST['sPassword']);
		$oModU = new CModelUser();
		$oCurUser = $oModU->check($sUserName, $sPassword);
		
		$fOk = null;
		$sError = null;
		if ($oCurUser&&$oCurUser['nId']) {
			$fOk = 1;
			$_SESSION['__sUserName'] = $sUserName;
			$_SESSION['__sPassword'] = $sPassword;
		} else {
			$sError = MSG_LOGIN_MISMATCH;
		}

		$this->answer($_POST, array('fOk'=>$fOk, 'sError'=>$sError));
	}*/
  
	function log($sMsg) {
		$fp = fopen("/var/www/data/dctel.log/__log.log", "a+");
		fputs($fp, date("Y-m-d H:i:s")."\n".print_r($sMsg, true)."\n\n");
		fclose($fp);
	}  
  
}

$oPage = new CPage();

$nTime1 = microtime(true);
ob_start();
$oPage->run();
ob_end_flush();
$nTime2 = microtime(true);
?>
