<?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
 */

/**
 * @package Core
 */

/**
 */
include_once('config.inc.php');
include_once('classes/core/DBAccessorObject.class.php');
include_once('classes/core/pages/Page.class.php');
include_once('classes/core/pages/PagesManager.class.php');
include_once('classes/core/HTMLManager.class.php');
include_once('classes/system/File.class.php');

/**
 * Manage the pages. Checks wether a page needs to be generated or not.
 *
 * @access  public
 * @author	Cedric Francoys <cedric.francoys@easy-cms.org>
 * @version	CacheManager.class, Sun Nov 14 16:57:18 CET 2004
 * @package	Core
 */
class CacheManager extends DBAccessorObject {

	var $pagesManager;
	var $aclManager;

	/**
	 * Class Constructor
	 *
	 * @param object $dbManipulator instance of DBManipulator<i>Driver</i>
	 */
	function CacheManager(&$dbManipulator, &$pagesManager, &$aclManager) {
		parent::DBAccessorObject($dbManipulator);
		$this->pagesManager = &$pagesManager;
		$this->aclManager = &$aclManager;
	}

	function &getHTML(&$page, &$user, $langId) {
		$html = &$this->getPage($page, $user, $langId);
		$this->debug("CacheManager::getHTML(), success : got cache for Page #".$page->getPageId(), 9);
		return $html;
	}

	function getPage(&$page, &$user, $langId) {
		$html = '';
		$cacheName = &$this->getCacheName($page, $langId);

		$acl = &$this->aclManager->getACL($page->getPageAclId());

		if ($acl->userHasViewRight($user) && $page->isPublished()) {
			$pageId = $page->getPageId();
			if (!$page->getPageCache() || !file_exists($cacheName)){
				$this->debug("CacheManager.checkPage, notice : page '".$pageId."' cached in file ".$cacheName.".", 10);
				$html = $this->cachePage($page, $langId);
				$this->pagesManager->setPageCachedAndStore($pageId);
			}
			else{
				$this->debug("CacheManager.checkPage, notice : page '".$pageId."' already cached in file ".$cacheName.".", 10);
				$file = &new File($cacheName);
				if (!($html = $file->read())) {
					$this->debug("CacheManager.getHTML, error : unable to read page '".$pageId."' from cache.", 1);
					$html = "";
				}
			}
		}
		else {
			$html = "<html><body>You don't have permissions to view this page or this page has not been published yet.<br /><br />Try to switch to <a href=\"?mode=edit\">edit mode</a> or <a href=\"?action=logout\">logout</a> and <a href=\"login\">log on</a> as a different user.</body></html>";
		}

		return $html;
	}

	function &getCacheName(&$page, $langId) {
		return REPOSITORY_FOLDER."/".CACHE_FOLDER."/easy_cms_html_".$page->getPageId()."_".$langId.".html";
	}

	function cachePage(&$page, $langId) {

		// get start time
		$mtime = microtime();
		$startTime = explode(" ", $mtime);

		$cacheName = &$this->getCacheName($page, $langId);
		$file = &new File($cacheName);

		$htmlManager = &new HTMLManager($page->getPageTitle($langId));

		$rootDir = str_replace("index.php", "", $_SERVER["PHP_SELF"]).REPOSITORY_FOLDER;

    if(substr($rootDir, 0, 2) == '//') {
      $rootDir = substr($rootDir, 1);
    }

		$htmlManager->addScript("includes/html/scripts/scripts.js");
		$htmlManager->addScript("includes/html/scripts/md5.js");
		$htmlManager->addMeta("Description", $page->getPageDescription($langId));
		$htmlManager->addMeta("Keywords", $page->getPageKeywords($langId));

		$frame = &$page->getPageFrame();

		// add page specific scripts
		foreach($frame->getFrameScripts() as $script){
			$htmlManager->addScript($rootDir."/".SCRIPTS_FOLDER."/".$script);
		}
		// add page specific styles
		foreach($frame->getFrameStyles() as $style){
			$htmlManager->addStyle($rootDir."/".STYLES_FOLDER."/".$style);
		}

    $site = &Site::getInstance();
    $user = &$site->getCurrentUser();
    $mode = $site->getCurrentMode();
		$htmlManager->addHTML($page->getHTMLBody($langId, $user, $mode));

		// get end time
		$mtime = microtime();
		$endTime = explode(" ", $mtime);
		$totaltime = round($endTime[1] + $endTime[0] - $startTime[1] - $startTime[0], 2);
		$now = date("D M j G:i:s T Y");
		$htmlManager->addHTML("<!-- This page was generated on $now in $totaltime seconds -->");

		$html = &$htmlManager->getHTML();
		if ($file->write($html, false) <= 0) {
			$this->debug("CacheManager.cachePage, error : unable to write page '".$page->getPageId()."'.", 1);
		}
		else {
			$this->debug("CacheManager.cachePage(".$page->getPageId()."), satus : success.", 10);
		}
		return $html;
	}

}
?>