/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/skconfig.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.'pathparser.php';
 
/**
 * contains parameters for the whole site
 *
 * @package Showkase
 */
class SkConfig
{

    /**
     * @var object instance of PathParser class
     */
    private $pathParser;
    
    /**   
     * @var object
     */
    private static $instance;
    
    /**   
     * @var array
     */
    private $messages = array();
    
    /**   
     * @var array
     */
    private $sitePaths = array();
    
    /**
     * @var Absolute content path from server root with trailing separator
     */
    private $absoluteContentPath;
    
    /**   
     * @var content path relative to web document root with leading separator and no trailing
     * Restricted use - may not work if site path contains symlink
     */
    private $docRootRelativeContentPath;
    
    /**   
     * Constructor
     * Looks for content directory in showkaseconfig.ini
     * Config file always stores paths with forward slashes to prevent confusion with escape characters
     * If site is not set up, assumes that content directory is one level up from showkase admin.
     */
    private function __construct()
    {
        $this->pathParser = new PathParser();
        if (!$sitePaths = @parse_ini_file('settings'.DIRECTORY_SEPARATOR.'paths.ini.php')) {
            throw new Exception('cannot read the paths ini file');
        }
        $this->sitePaths = $this->cleanPathsArray($sitePaths);
        $configPath = $this->getSiteConfigPath();
        if (is_readable($configPath)) {
            $config = @parse_ini_file($configPath);
            if ($config === false) throw new Exception('Cannot read config file '.$configPath);
            $this->docRootRelativeContentPath = 
                str_replace(
                    array('\\', '/'),
                    DIRECTORY_SEPARATOR,
                    rtrim(
                        $config['contentPath'],
                        '\\/'
                    )
                ).DIRECTORY_SEPARATOR;
            $this->absoluteContentPath =
                str_replace(
                    array('\\', '/'),
                    DIRECTORY_SEPARATOR,
                    rtrim(
                        $config['absoluteContentPath'],
                        '\\/'
                    )
                ).DIRECTORY_SEPARATOR;
        } else {
            // set to admin parent directory
            $this->docRootRelativeContentPath =
                rtrim(
                    dirname(
                        dirname(
                            str_replace(
                                array('\\', '/'),
                                DIRECTORY_SEPARATOR,
                                $_SERVER['SCRIPT_NAME']
                            )
                        )
                    ),
                    '\\/'
                )
                .DIRECTORY_SEPARATOR;
            $this->absoluteContentPath = rtrim(dirname(getcwd()), '\\/').DIRECTORY_SEPARATOR;
        }
    }
        
    /**   
     * Gets instance of this class
     *
     * @return object
     */
    public static function getInstance()
    {
        if (empty(self::$instance)) {
            self::$instance = new SkConfig();
        }
        return self::$instance;
    }
    
    /**   
     * Get absolute content path from server root with trailing separator
     *
     * @return string
     */
    public function getAbsoluteContentPath()
    {
        return $this->absoluteContentPath;
    }
    
    /**   
     * Trim leading and trailing slashes and fix separators
     * 
     * @return arrray sitePaths
     * @param array sitePaths
     */
    private function cleanPathsArray($sitePaths)
    {
        foreach ($sitePaths as $key=>$value) {
            $sitePaths[$key] = $this->cleanPath($value);
        }
        return $sitePaths;
    }
    
    /**   
     * Trim leading and trailing slashes from path and fix separators
     *
     * @param string path to be cleaned
     * @return string
     */
    private function cleanPath($path)
    {
     return trim(str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $path), '\\/');
    }
    
    /**   
     * Returns absolute path to site config ini file
     * This file is normally located in the _showkase subdirectory of the showkase install directory
     *
     * @return string
     */
    public function getSiteConfigPath()
    {
        return dirname(ADMIN_PATH).DIRECTORY_SEPARATOR.$this->sitePaths['showkaseConfigPath'];
    }
    
    /**
     * Returns absolute path to showkase data directory
     * This file is normally located in the _showkase subdirectory of the showkase install directory
     *
     * @return string
     */
     public function getShowkaseDataPath()
     {
         return dirname(ADMIN_PATH).DIRECTORY_SEPARATOR.$this->sitePaths['showkaseDataPath'];
     }
     
    /**   
     * Returns absolute path to auth file
     *
     * @return string
     */
    public function getAuthPath()
    {
        return dirname(ADMIN_PATH).DIRECTORY_SEPARATOR.$this->sitePaths['authPath'];
    }
    
    /**   
     * Returns absolute path to password reset file
     *
     * @return string
     */
    public function getAuthResetPath()
    {
        return $this->pathParser->fix($this->absoluteContentPath.PASSWORD_RESET_FILE);
    }
    
    /**   
     * Returns absolute path to themes directory
     *
     * @return string
     */
    public function getThemesPath()
    {
        return
            $this->absoluteContentPath.
            $this->sitePaths['themesPath'];
    }
    
    /**
     * return absolute path to site overrides file
     *
     * @return string
     */
    public function getSiteOverridesPath()
    {
        return $this->absoluteContentPath
            .'_data'.DIRECTORY_SEPARATOR
            .'sitedata'.DIRECTORY_SEPARATOR
            .'site.xml';
    }
    
    /**   
     * Returns site setup status
     *
     * @return boolean
     */
    public function siteIsSetup()
    {
        try {
            return (
                         file_exists($this->getSiteConfigPath())
                      && is_dir($this->absoluteContentPath)
                      && file_exists($this->getShowkaseDataPath())
                      && file_exists($this->getSmartyDataPath())
                      && file_exists($this->getSmartyConfigsPath())
                      && file_exists($this->getSmartyCachePath())
                      && file_exists($this->getSmartyCompilePath())
            );
        } catch (Exception $e) {
            return false;
        }
    }
    
    /**   
     * returns content path relative to web document root with leading separator
     * may not work if site path contains symlinks
     *
     * @return string
     */
    public function getDocRootRelativeContentPath()
    {
        return $this->docRootRelativeContentPath;
    }
    
    /**   
     * set content path relative to web document root with leading separator
     *
     * @param string
     * @return void
     */
    public function setDocRootRelativeContentPath($path)
    {
        $this->docRootRelativeContentPath = $path;
    }
    
    /**   
     * set absolute content path from server root with leading separator
     *
     * @param string
     * @return void
     */
    public function setAbsoluteContentPath($path)
    {
        $this->absoluteContentPath = $path;
    }
    
    /**   
     * Returns content url with no trailing slash and empty string for root installation
     */
    public function getContentUrl()
    {
        return rtrim(str_replace('\\', '/', $this->docRootRelativeContentPath), '\\/');
    }
    
    
    /**   
     * Returns site data path with no trailing slash
     *
     * @return string
     */
    public function getSiteDataPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['siteDataPath'];
    }
    
    /**   
     * Returns absolute site trash directory with no trailing slash
     *
     * @return string
     */
    public function getAbsoluteSiteTrashPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['trashPath'];
    }
    
    /**   
     * Returns name of trash folder without any separators
     *
     * @return string
     */
    public function getTrashDirectoryName()
    {
        return $this->sitePaths['trashPath'];
    }
    
    /**   
     * Returns thumbcache path relative to server root
     *
     * @return string
     */
    public function getThumbCachePath()
    {
        return $this->absoluteContentPath.$this->sitePaths['thumbCachePath'];
    }
    
    /**   
     * Returns absolute thumbcache url with no trailing slash
     *
     * @return string
     */
    public function getThumbCacheUrl()
    {
        return $this->getContentUrl().'/'.str_replace('\\', '/', $this->sitePaths['thumbCachePath']);
    }
    
    /**
     * Returns absolute url for viewer cache parent directory
     *
     * @return string
     */
    public function getViewersUrl()
    {
        return $this->getContentUrl().'/'.str_replace('\\', '/', $this->sitePaths['viewersPath']);
    }
    
    /**   
     * Returns themes directory url relative to document root with leading slash and no trailing slash
     *
     * @return string
     */
    public function getThemesAbsUrl()
    {
        return str_replace('\\', '/', $this->docRootRelativeContentPath.$this->sitePaths['themesPath']);
    }
    
    /**   
     * Returns path to pages data file relative to server root
     *
     * @return string
     */
    public function getPagesDataPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['pagesDataPath'];
    }
    
    /**   
     * Returns path to groups data file relative to server root
     *
     * @return string
     */
    public function getGroupsDataPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['groupsDataPath'];
    }
    
    /**   
     * Returns path to links data file relative to server root
     *
     * @return string
     */
    public function getLinksDataPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['linksDataPath'];
    }
    
    /**   
     * Returns galleries preferences file path relative to server root
     *
     * @return string
     */
    public function getPagesPrefsPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['pagesPrefsPath'];
    }
    
    /**   
     * Returns theme settings directory path relative to server root with no trailing slash
     *
     * @return string
     */
    public function getThemeDataPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['themeDataPath'];
    }
    
    /**   
     * Returns absolute path to smarty data directory with no trailing slash
     *
     * @return string
     */
    public function getSmartyDataPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['smartyDataPath'];
    }
    
    /**   
     * Returns absolute path to smarty configs directory with no trailing slash
     *
     * @return string
     */
    public function getSmartyConfigsPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['smartyConfigsPath'];
    }
    
    /**   
     * Returns absolute path to smarty cache directory with no trailing slash
     *
     * @return string
     */
    public function getSmartyCachePath()
    {
        return $this->absoluteContentPath.$this->sitePaths['smartyCachePath'];
    }
    
    /**   
     * Returns absolute path to smarty templates cache directory with no trailing slash
     *
     * @return string
     */
    public function getSmartyCompilePath()
    {
        return $this->absoluteContentPath.$this->sitePaths['smartyCompilePath'];
    }

    
    /**   
     * Returns theme settings directory path relative to content directory with no trailing slash
     *
     * @return string
     */
    public function getThemeDataPathRelContent()
    {
        return $this->sitePaths['themeDataPath'];
    }
        
    /**   
     * Returns $path relative to content directory
     *
     * @param string server root absolute path
     * @return string relative path
     */
    public function getRelativeContentPath($path)
    {
        $relContentPath = $this->pathParser->findRelativePath($path, $this->absoluteContentPath);
        return str_replace('/', DIRECTORY_SEPARATOR, $relContentPath);
    }
    
    /**   
     * returns library path relative to cwd with no trailing separator
     *
     * @return string
     */
    public function getRelativeLibraryPath()
    {
        return
            $this->getRelativeContentPath(getcwd()).
            $this->sitePaths['libraryPath'];
    }
    
    /**   
     * returns absolute library directory path with no trailing separator
     *
     * @return string
     */
    public function getAbsoluteLibraryPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['libraryPath'];
    }
    
    /**
     * Returns absolute path to viewer caches parent directory with no trailing separator
     *
     * @return string
     */
    public function getAbsoluteViewersPath()
    {
        return $this->absoluteContentPath.$this->sitePaths['viewersPath'];
    }
    
    /**   
     * returns library directory absolute url with no trailing separator
     *
     * @return string
     */
    /*
    public function getLibraryUrl()
    {
        var_dump('/'.$this->getContentUrl().'/'.str_replace('\\', '/', $this->sitePaths['libraryPath']));
        return '/'.$this->getContentUrl().'/'.str_replace('\\', '/', $this->sitePaths['libraryPath']);
    }
    */
    
}