<?php
/*
 * This file is part of the easy-CMS project.
 * easy-CMS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easy-CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easy-CMS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

 
// if there is no config file then redirect to the installation script
if(!is_file('config.inc.php')) {
 	$scriptPath = substr(dirname($_SERVER["PHP_SELF"]), 1);
	$location = "http://".$_SERVER["SERVER_NAME"];
	if(strlen($scriptPath)) {
    $location .= "/".$scriptPath;
	}
  $location .= '/install.php';
  header('Location: '.$location);
  exit(0);
}


include_once('config.inc.php');
include_once('egConfig.inc.php');
include_once('classes/core/Site.class.php');
include_once('classes/core/engines/MailingEngine.class.php');


// obtain a new instance of the easy-CMS site object
$site = &Site::getInstance();

// by default, we want to display a page
$display = $site->getSubmittedVar('display', RENDER);

// by default, page is displayed in the view mode
$mode = $site->getSubmittedVar('mode', VIEW_MODE);

$web_page = '';

// check what is to be displayed :
// either an administration popup
if($display != RENDER) {
  if(($formsManager = &$site->getFormsManager()) != null) {
  	$web_page = &$formsManager->getFormHTML($display, $mode);
  }
}
// or a page of the website
else {
  if(($page = &$site->getCurrentPage()) != null) {
  	$web_page = &$site->generatePageHTML($page);
  }
}

// output generated html
print($web_page);
flush();

// send a batch of e-mails if required
$contentsManager = &$site->getContentsManager();
$mailingEngine = new MailingEngine($contentsManager);
$mailingEngine->start();

// Destroy the instance (close the DB connection and save session vars)
$site->siteDestructor();
?>