Location: PHPKode > projects > synType CMS > setup/wizards/cmssetup/page.dbserver.php
<?php
/**
 *  synType CMS Setup
 *  Module: Wizard Page DB Server
 *
 *  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 wizPageDbserver extends wizardPage
{
	static $aData = array(
		'UUIDs'				=> array(
			'mandator_id'	=> null,
			'org_id'		=> null,
			'org_person_id'	=> null,
			'user_id'		=> null,
			'person_id'		=> null,
			'acl_group_id'	=> null,
			'acl_role_id'	=> null,
		),
		'coredb'			=> array(
			'dbdrv'			=> 'mysqli',
			'dbsubdrv'		=> null,
			'dbhost'		=> 'localhost',
			'dbport'		=> '',
			'dbname'		=> 'syntype',
			'dbprefix'		=> 'cms',
			'dbsysadm'		=> '',
			'dbsysadmpwd'	=> '',
			'dbcmsuser'		=> 'cmsuser',
			'dbcmsuserpwd'	=> '',
			'create_db'		=> true,
			'create_schema'	=> true,
			'create_users'	=> true
		),
		'userdb'			=> array(
			'dbdrv'			=> 'coredb',
			'dbsubdrv'		=> null,
			'dbhost'		=> 'localhost',
			'dbport'		=> '',
			'dbproto'		=> '',
			'dbbasedn'		=> '',
			'dbuserdn'		=> '',
			'dbuserpwd'		=> '',
			'dbloginattr'	=> '',
			'dbuserattr'	=> '',
			'dbpwdattr'		=> '',
			'dbemailattr'	=> '',
			'dbnameattr'	=> ''
		)
	);


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

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

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

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

		return 0;
	}

	protected function onAfterSubmit($nGoto)
	{
		if($nGoto != wizWizard::PAGE_SELF){
			if(($aData = $this->_oWizard->getProperty('cmsdb')) === null)
				$aData = self::$aData;

			if($aData['UUIDs']['org_id'] === null){
				foreach($aData['UUIDs'] as $key => $val){
					$aData['UUIDs'][$key] = getUUID();
				}
			}

			if(isset($this->_in['coredb'])){
				$aDBDrv = explode('_', $this->_in['coredb']['dbdrv']);
				$this->_in['coredb']['dbdrv'] = $aDBDrv[0];
				$aData['coredb']['dbsubdrv'] = (isset($aDBDrv[1]))? $aDBDrv[1]: null;
				$aData['coredb'] = array_merge($aData['coredb'], $this->_in['coredb']);
			}

			if(isset($this->_in['userdb'])){
				$aDBDrv = explode('_', $this->_in['userdb']['dbdrv']);
				$this->_in['userdb']['dbdrv'] = $aDBDrv[0];
				$aData['userdb']['dbsubdrv'] = (isset($aDBDrv[1]))? $aDBDrv[1]: null;
				$aData['userdb'] = array_merge($aData['userdb'], $this->_in['userdb']);
			}

			if($aData['userdb']['dbdrv'] == 'coredb')
				$this->setPageStatus(wizWizard::STATUS_SKIP, $this->getCurrentPage() + 2);
			else
				$this->setPageStatus(wizWizard::STATUS_OPEN, $this->getCurrentPage() + 2, true);

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

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

		$this->setTextTop("Database Server");

		$sInstruct = "The dropdown lists below let you choose drivers for the 'Core DBMS' and, optionally, a driver for the database where your users are stored.<br />"
					."The 'Core' database will contain all tables and data to run the CMS. This database will be set up automatically.<br />"
					."If you already have a database containing user data, you may pick a driver from the second dropdown list. This database should be existing and contain fields for a user name and password.<br />"
					."Credentials to connect and use the DBMS-Server(s) are collected on the next page(s).";
		$this->addText($sInstruct);

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

		$aDrivers = $this->_enumDrivers();

		if($aData['coredb']['dbsubdrv'])
			$aData['coredb']['dbdrv'] .= "_{$aData['coredb']['dbsubdrv']}";

		$this->formAddText("Please pick a DBMS driver for the Core database from the list below ...");
		$this->formAddDropdown('DBMS driver', 'coredb[dbdrv]', $aData['coredb']['dbdrv'], $aDrivers);

		$this->formAddSeparator();

		$aDrivers = array();
		// SOAP feature is not yet implemented
//		array_unshift($aDrivers, array('soap', 'SOAP'));
		array_unshift($aDrivers, array('ldap', 'LDAP or ActiveDirectory (experimental)'));
		array_unshift($aDrivers, array('coredb', 'Use the Core Database'));
		if($aData['userdb']['dbsubdrv'])
			$aData['userdb']['dbdrv'] .= "_{$aData['userdb']['dbsubdrv']}";

		$this->formAddText("Please pick a driver for the User database from the list below ...");
		$this->formAddDropdown('DBMS driver', 'userdb[dbdrv]', $aData['userdb']['dbdrv'], $aDrivers);

		return $this->getContents();
	}

	private function _enumDrivers()
	{
		// Scan divers directory
		$drvDir = DBLDIR ."/drivers/";

		if(($files = @scandir($drvDir)) === false)
			throw new wizException("Cannot scan directory '{$drvDir}' for drivers!");

		$aDrvFiles = array();
		foreach($files as $file){
			if(is_dir($drvDir . $file) || $file[1] == '.')
				continue;

			$tmp = explode('.', $file);
			$drv = array_shift($tmp);

			if(array_shift($tmp) != 'drv')
				continue;

			$aDrvFiles[] = $drv;
		}

		// Get suported drivers from ini file
		$aIni = parse_ini_file('./setup.ini', true);

		if(!isset($aIni['PHP DBMSModules']))
			throw new wizException("Section [PHP DBMSModules] not found in 'setup.ini'!");

		$aExts = $aIni['PHP DBMSModules'];

		if(!isset($aIni['DBMS Drivers']))
			throw new wizException("Section [DBMS Drivers] not found in 'setup.ini'!");

		$aDrvIni = $aIni['DBMS Drivers'];

		// Get PHP DBMS extensions loaded and build result
		$aResult = array();
		foreach($aExts as $sExt => $sVal){
			$bMsOdbtp = ($sExt == 'mssql' && !extension_loaded($sExt) && function_exists('mssql_connect'));
			if(extension_loaded($sExt) || $bMsOdbtp){
				$sDrv = $sExt;
				if($sExt == 'ibm_db2')
					$sDrv = 'ibm';
				else if($sExt == 'oci8')
					$sDrv = 'oracle';

				if(in_array($sDrv, $aDrvFiles)){
					if(isset($aIni["DSN {$sDrv}"]) && count($aIni["DSN {$sDrv}"])){
						$aResult[] = array($sDrv, $aDrvIni[$sDrv], true);
						foreach($aIni["DSN {$sDrv}"] as $sSub => $sVal2){
							$aResult[] = array("{$sDrv}_{$sSub}", $sVal2);
						}
						$aResult[] = array($sDrv, $aDrvIni[$sDrv], false);
					}
					else {
						$aResult[] = array($sDrv, $aDrvIni[$sDrv]);
					}
				}
			}
		}

		return $aResult;
	}

}

?>
Return current item: synType CMS