Location: PHPKode > projects > synType CMS > setup/wizards/cmssetup/page.settings.php
<?php
/**
 *  synType CMS Setup
 *  Module: Wizard Page Configuration Settings
 *
 *  Copyright © 2004-2009 V. Puttrich, MindArray
 *
 *  http://www.mindarray.eu
 *
 *  Author: V. Puttrich, MindArray
 * ----------------------------------------------------------------------------
 *  $Id$
 * ----------------------------------------------------------------------------
 *
 *  This file is part of synType CMS.
 *
 *  synType 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  synType 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 libMail. If not, see <http://www.gnu.org/licenses/>.
 *
 * ----------------------------------------------------------------------------
 **/


class wizPageSettings extends wizardPage
{
	static $aData = array(
		'syspath'	=> '',
		'wwwpath'	=> '',
		'wwwurl'	=> ''
	);


	public function __construct($oWizard, &$in)
	{
		parent::__construct($oWizard, $in);
	}

	public function __destruct()
	{
		parent::__destruct();
	}

	protected function onPrepare()
	{
		if(($aData = $this->_oWizard->getProperty('config')) === null){
			$aData = self::$aData;

			// Convert Windows to Unix path and remove drive letters and trailing slash
			/*
			$sDocRoot = str_replace("\\", '/', $_SERVER['DOCUMENT_ROOT']);
			$sDocRoot = preg_replace("/^([a-zA-Z]:)?(.*)\/?$/U", "\\2", $sDocRoot);

			$aData['syspath'] = $sDocRoot;
			$aData['wwwpath'] = $sDocRoot;
			*/
			$sCmsRoot = realpath(ROOTDIR ."/..");
			$sCmsRoot = str_replace("\\", '/', $sCmsRoot);
			$sCmsRoot = preg_replace("/^([a-zA-Z]:)?(.*)\/?$/U", "\\2", $sCmsRoot);

			$aData['syspath'] = $sCmsRoot;
			$aData['wwwpath'] = $sCmsRoot;
		}

		$this->_oWizard->setProperty('config', $aData);

		return 0;
	}

	protected function onAfterSubmit($nGoto)
	{
		if($nGoto != wizWizard::PAGE_SELF){
			if(isset($this->_in['conf'])){
				if(($aData = $this->_oWizard->getProperty('config')) === null)
					throw new wizException("Persistent data not found!");

				$aData = array_merge($aData, $this->_in['conf']);

				// Strip trailing slashes
				$aData['syspath'] = preg_replace("/\/$/", '', $aData['syspath']);
				$aData['wwwpath'] = preg_replace("/\/$/", '', $aData['wwwpath']);
				// $aData['wwwurl'] = preg_replace("/\/$/", '', $aData['wwwurl']);

				$aData['wwwurl'] = substr($aData['wwwpath'], strlen($_SERVER['DOCUMENT_ROOT']));
				$aData['wwwurl'] = str_replace("\\", '/', $aData['wwwurl']);
				$aData['wwwurl'] = rtrim($aData['wwwurl'], "\n\t /");

				// We're not using 'syspath' yet, but require it for installation routines
				$aData['syspath'] = $aData['wwwpath'];

				$this->_oWizard->setProperty('config', $aData);
			}
		}

		return 0;
	}

	protected function onPage($nErr)
	{
		if($nErr)
			return $this->getContents();

		$this->setTextTop("Installation Settings");

		if(($aData = $this->_oWizard->getProperty('config')) === null)
			throw new wizException("Persistent data not found!");

		$sInstruct = "<strong>Instructions:</strong><br />\n"
					."<ul>\n"
					."<li>You may pick a path from the suggestions which will be entered in the input fields to save you time typing.</li>\n"
					."<li>Do not enter trailing slashes for any of the directories below.</li>\n"
					."<li>Setup will try to create directories that do not exist, so it's important that the last existing directory is writable.</li>\n"
					."<li>A highlighted input field indicates that the directory is either not writable or not accessible because of 'open_basedir' restrictions.</li>\n"
					."</ul>\n";
		$this->addText($sInstruct);

		// Convert Windows to Unix path and remove drive letters and trailing slash
		$sPath = str_replace("\\", '/', $_SERVER['DOCUMENT_ROOT']);
		$sPath = preg_replace("/^([a-zA-Z]:)?(.*)\/?$/U", "\\2", $sPath);

		$nStrip = strlen($sPath);
		$sAttr = "\" onchange=\"changePath(this, {$nStrip})";
		$sChk  = "\" onblur=\"checkDirAccess(this)";

		/* The following code is intended for an extended installation
		$aSugg = $this->_enumSysDirs($aData['syspath'], $sPath);
		array_unshift($aSugg, array($aData['syspath'], $aData['syspath']));
		$this->formAddText("Enter the absolute path where system files will be copied to.<br />A path outside of the 'Web root' is recommended.<br />The last existing directory must have write permissions!");
		$this->formAddInput('System path', 'conf[syspath]'.$sChk, $aData['syspath'], 30, null, (!$this->_is_accessible($aData['syspath'])));
		$this->formAddDropdown('&nbsp;&middot;&nbsp;Suggestions', 'sysdirs'.$sAttr, $aData['syspath'], $aSugg);

		$this->formAddSeparator();

		$aSugg = $this->_enumWwwDirs($aData['wwwpath'], $sPath);
		array_unshift($aSugg, array($aData['wwwpath'], $aData['wwwpath']));
		$this->formAddText("Enter the absolute path where files will be copied to.<br />This must be a path under the 'Web root'.<br />The last existing directory must have write permissions!");
		$this->formAddInput('Web path', 'conf[wwwpath]'.$sAttr, $aData['wwwpath'], 30, null, (!$this->_is_accessible($aData['wwwpath'])));
		$this->formAddDropdown('&nbsp;&middot;&nbsp;Suggestions', 'wwwdirs'.$sAttr, $aData['wwwpath'], $aSugg);

		$this->formAddSeparator();
		*/

		$aSugg = $this->_enumWwwDirs($aData['wwwpath'], $sPath);
		array_unshift($aSugg, array($aData['wwwpath'], $aData['wwwpath']));
		$this->formAddText("Enter the absolute path on the server where files will be copied to.<br />This must be a path under the 'Web root'.<br />The last existing directory must have write permissions!");
		$this->formAddInput('Path on server', 'conf[wwwpath]', $aData['wwwpath'], 30, null, (!$this->_is_accessible($aData['wwwpath'])));
		$this->formAddDropdown('&nbsp;&middot;&nbsp;Suggestions', 'wwwdirs'.$sAttr, $aData['wwwpath'], $aSugg);

		// $this->formAddText("Enter the relative path from the 'Web root', pointing to the path above.<br />This is where the 'index.php' will reside.");
		// $this->formAddInput('Web directory', 'conf[wwwurl]', $aData['wwwurl'], 30);

		return $this->getContents();
	}

	private function _enumSysDirs($sDir, $sPath)
	{
		$aDirs = array();

		// Up one directory
		$aTmp = explode('/', $sPath);
		$sDirLast = array_pop($aTmp);
		$sPath = implode('/', $aTmp);

		if($sPath != $sDir)
			$aDirs[] = array($sPath, $sPath);

		// Add sub-directories
		$nDepth = 2;
		if(!$this->_walkDirs($sPath, $aDirs, $sDir, $nDepth)){
			$aDirs = array();
			$nDepth = 2;
			$sPath .= "/{$sDirLast}";
			$this->_walkDirs($sPath, $aDirs, $sDir, $nDepth);
		}

		// PHP include directories
		$tp = ini_get('include_path');
		if($tp){
			$atp = explode(PATH_SEPARATOR, trim($tp));
			foreach($atp as $sFile){
				if(!@is_dir($sFile) || $sFile[0] == '.')
					continue;

				$sFile = str_replace("\\", '/', $sFile);
				$sFile = preg_replace("/^(.*)\/?$/U", "\\1", $sFile);
				$sFile = "{$sPath}/{$sFile}";
					$aDirs[] = array($sFile, $sFile);
			}
		}

		return $aDirs;
	}

	private function _enumWwwDirs($sDir, $sPath)
	{
		$aDirs = array();

		if($sPath != $sDir)
			$aDirs[] = array($sPath, $sPath);

		// Add sub-directories
		$nDepth = 2;
		if(!$this->_walkDirs($sPath, $aDirs, $sDir, $nDepth)){
			$aDirs = array();
			$nDepth = 1;
			$this->_walkDirs(realpath(dirname($_SERVER['SCRIPT_NAME'])."/.."), $aDirs, $sDir, $nDepth);
		}

		return $aDirs;
	}

	private function _walkDirs($sPath, &$aDirs, $sDir, &$nDepth)
	{
		if($nDepth <= 0)
			return true;

		$aTmp = @scandir($sPath);
		if($aTmp){
			foreach($aTmp as $sFile){
				if(!@is_dir("{$sPath}/{$sFile}") || $sFile[0] == '.')
					continue;

				$sFile = "{$sPath}/{$sFile}";
				if($sFile != $sDir)
					$aDirs[] = array($sFile, $sFile);

				$nNewDepth = $nDepth - 1;
				if(!$this->_walkDirs($sFile, $aDirs, $sDir, $nNewDepth))
					return false;
			}
		}
		else {
			return false;
		}

		return true;
	}

	private function _is_accessible($sPath)
	{
		$bAcc = false;
		$ainc = explode('/', $sPath);
		array_shift($ainc);

		$obd = ini_get('open_basedir');
		if(!empty($obd) && strtolower($obd) != 'none'){
			$dirSep = (stripos(PHP_OS, 'win'))? ';': ':';
			$obd = str_replace("\\", '/', $obd);
			$aobd = explode($dirSep, $obd);

			$dir = '';
			foreach($ainc as $inc){
				$dir .= '/'. $inc;
				foreach($aobd as $obd){
					if(strpos($dir.'/', $obd) !== false){
						$bAcc = true;
						break;
					}
				}
				if($bAcc)
					break;
			}
		}
		else {
			$bAcc = true;
		}

		$sinc = '';
		foreach($ainc as $inc){
			$sinc .= '/'. $inc;
			if($bAcc && @is_writable($sinc))
				return true;
		}

		return false;
	}

}

?>
Return current item: synType CMS