/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/sitesetup.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');
require_once 'classes'.DIRECTORY_SEPARATOR.'fieldfactory.php';
/**
 * Sets up site directories and status
 * Manages everything that goes in the site config file
 *
 * @package Showkase
 */
Class SiteSetup
{
    
    /**
     * @var object site Singleton
     */
    protected $config;
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->config = SkConfig::getInstance();
    }
    
    /**
     * Setup the site
     *
     */
    public function doSetup(Request $request)
    { 
        $oldContentPath = $this->config->getDocRootRelativeContentPath();
        $oldAbsoluteContentPath = $this->config->getAbsoluteContentPath();
        try {
            $contentPath =
                DIRECTORY_SEPARATOR.
                str_replace(array('\\', '/'), DIRECTORY_SEPARATOR,
                trim($request->getProperty('contentPath'), '\\/'));
            // e.g. '/' or '/my-install'
            $installDirectory = dirname(
                dirname(
                    str_replace(
                        array('\\', '/'),
                        DIRECTORY_SEPARATOR,
                        $_SERVER['SCRIPT_NAME']
                    )
                )
            );
            switch (true) {
                case ($contentPath == $installDirectory) :
                    $absoluteContentPath = rtrim(dirname(getcwd()), '\\/').DIRECTORY_SEPARATOR;
                    break;
                case ($installDirectory == dirname($contentPath)) :
                    $absoluteContentPath = rtrim(dirname(getcwd()), '\\/').DIRECTORY_SEPARATOR.basename($contentPath).DIRECTORY_SEPARATOR;
                    break;
                default :
                    throw new Exception('Site must be in installation folder or a sub-folder');
            }
            $this->config->setDocRootRelativeContentPath($contentPath);
            $this->config->setAbsoluteContentPath($absoluteContentPath);
            $this->setupDirectories();
        } catch (Exception $e) {
            $this->config->setDocRootRelativeContentPath($oldContentPath);
            $this->config->setAbsoluteContentPath($oldAbsoluteContentPath);
            throw $e;
        }
    }
    
    /**
     * Create new content directory and subdirectories
     *
     */
    private function setupDirectories()
    {
        if (!is_dir($this->config->getAbsoluteContentPath())) {
            throw new Exception('Cannot find content folder ');
        }
        if (!is_dir($this->config->getThemesPath())) {
            throw new Exception('Content folder does not contain a themes folder');
        }
        if (   !is_dir($this->config->getSiteDataPath())
            && !Filer::skMkdir($this->config->getSiteDataPath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Check permissions for site folder – cannot create data folder '.basename($this->config->getSiteDataPath()));
        }
        if (   !is_dir($this->config->getShowkaseDataPath())
            && !Filer::skMkdir($this->config->getShowkaseDataPath(), NEW_DIR_MODE) 
        ) {
            throw new Exception('Cannot create Showkase data folder');
        }
        if (!file_exists($this->config->getAuthPath())) {
            $auth = new Auth();
            try {
                $auth->changeLogin('admin', 'admin');
            } catch (Exception $e) {
                Board::addExceptionMessage($e);
            }
        }
        if (   !is_dir($this->config->getAbsoluteLibraryPath())
            && !Filer::skMkdir($this->config->getAbsoluteLibraryPath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create library folder');
        }
        if (   !is_dir($this->config->getAbsoluteSiteTrashPath())
            && !Filer::skMkdir($this->config->getAbsoluteSiteTrashPath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create trash folder');
        }
        if (   !is_dir($this->config->getAbsoluteViewersPath())
            && !Filer::skMkdir($this->config->getAbsoluteViewersPath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create viewers cache folder');
        }
        if (   !is_dir($this->config->getSmartyDataPath())
            && !Filer::skMkdir($this->config->getSmartyDataPath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create Smarty data folder');
        }
        if (   !is_dir($this->config->getSmartyConfigsPath())
            && !Filer::skMkdir($this->config->getSmartyConfigsPath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create Smarty configs folder');
        }
        if (   !is_dir($this->config->getSmartyCachePath())
            && !Filer::skMkdir($this->config->getSmartyCachePath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create Smarty cache folder');
        }
        if (   !is_dir($this->config->getSmartyCompilePath())
            && !Filer::skMkdir($this->config->getSmartyCompilePath(), NEW_DIR_MODE)
        ) {
            throw new Exception('Cannot create Smarty compile folder');
        }
    }
        
    /**
     * Save site config file and update SkConfig
     * Windows backslashes may be read as escape characters so change to forward slash
     *
     * @return boolean saved
     */
    public function saveConfig()
    {
				$siteConfigPath = $this->config->getSiteConfigPath();
				$contentPath = $this->config->getDocRootRelativeContentPath();
				$configData =
'; Configuration file for SimpleViewer.net web site management package
; Content path from web document root
; Leading separator and trailing separator
contentPath = "'.str_replace('\\', '/', $contentPath).'"
; Content path from server root
; Leading separator and trailing separator
absoluteContentPath = "'.str_replace('\\', '/', $this->config->getAbsoluteContentPath()).'"
; URL for Showkase index.php relative to web document root
; Leading slash
showkaseUrl = "'.htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8').'"
';
        if (false === Filer::skPutContents($siteConfigPath, $configData)) {
            throw new Exception('Cannot save to '.basename($siteConfigPath).'. Check installation folder permissions.');
        }
    }
}