<?php
/*
 * $RCSfile: InstallCoreModuleStep.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

class InstallCoreModuleStep extends InstallStep {
    function stepName() {
	return _('Install Gallery Core');
    }

    function loadTemplateData(&$templateData) {
	global $galleryStub;
	define('GALLERY_CONFIG_DIR', $_SESSION['configPath']);
	require(dirname(__FILE__) . '/../../bootstrap.inc');
	require(dirname(__FILE__) . '/../../init.inc');

	/* We want to log all debug output in our install log */
	global $gallery;
	$gallery->setDebug('logged');
	$tag = substr(md5(microtime() . rand(1, 32767)), 0, 5);
	$dataBase = $gallery->getConfig('data.gallery.base');
	$installLogPath = sprintf('%s/install_%s.log', $dataBase, $tag);
	$gallery->setDebugLogFile($installLogPath);
	$templateData['installLogPath'] = $installLogPath;
	$this->_createDebugLogHeader();

	$gallery->debug('Init first pass');
	$ret = GalleryInitFirstPass(array('debug' => 'logged',
					  'noDatabase' => 1));
	if ($ret->isError()) {
	    $gallery->debug(sprintf('Error: Unable to initialize our Gallery data, this is the ' .
				    'error stack trace: %s', $ret->getAsText()));
	    $templateData['errors'][] = _('Unable to initialize our Gallery data');
	    $templateData['stackTrace'] = $ret->getAsHtml();
	}

	$this->_addSystemInformationToDebugLog();

	/* We want to avoid using the cache */
	GalleryDataCache::setFileCachingEnabled(false);

	/* Gallery init selects language from browser; reset to language currently in use */
	$gallery->debug('Initialize translator');
	$translator =& $gallery->getTranslator();
	$translator->init($_SESSION['language']);

	/*
	 * Delete anything in the cache, which can be left around if we're
	 * installing on top of an older install.
	 */
	$platform = $gallery->getPlatform();
	$gallery->debug('Clear the cache directory');
	$cacheDirs = array('entity', 'theme', 'module', 'derivative');
	foreach ($cacheDirs as $dir) {
	    $dir = sprintf('%s/cache/%s', $dataBase, $dir);
	    if ($platform->file_exists($dir)) {
		if (!$platform->recursiveRmDir($dir)) {
		    return false;
		}
	    }
	}

	if (empty($templateData['errors'])) {
	    $gallery->debug('Check if the persistent storage is installed');
	    /*
	     * Check to see if the database tables already exist.  If they do then
	     * we should assume that they said that it was ok to reuse existing tables
	     * in the Database setup step, which means we don't have to perform an
	     * install now.
	     */
	    $storage =& $gallery->getStorage();
	    list ($ret, $isInstalled) = $storage->isInstalled();
	    if ($ret->isError()) {
		$gallery->debug(sprintf('Error: Unable to communicate with the database, this ' .
					'is the error stack trace; %s', $ret->getAsText()));
		$templateData['errors'][] = _('Unable to communicate with the database');
		$templateData['stackTrace'] = $ret->getAsHtml();
	    }
	}

	if (empty($templateData['errors'])) {
	    $gallery->debug('Load core module');
	    list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core', true);
	    $this->resetL10Domain();
	    if ($ret->isError()) {
		$gallery->debug(sprintf('Error: Unable to load the core module, this ' .
					'is the error stack trace; %s', $ret->getAsText()));
		$templateData['errors'][] = _('Unable to load the core module');
		$templateData['stackTrace'] = $ret->getAsHtml();
	    }
	}

	$freshInstall = $galleryStub->getConfig('freshInstall');
	if ($freshInstall) {
	    $gallery->debug('Hand over admin user parameters');
	    /* It's a fresh install. Hand over install config parameters */
	    $gallery->setConfig('setup.admin.userName',
				$galleryStub->getConfig('setup.admin.userName'));
	    $gallery->setConfig('setup.admin.email',
				$galleryStub->getConfig('setup.admin.email'));
	    $gallery->setConfig('setup.admin.fullName',
				$galleryStub->getConfig('setup.admin.fullName'));
	}

	if (empty($templateData['errors'])) {
	    if (!$isInstalled) {
		$gallery->debug('Install core module now!');
		$ret = $core->installOrUpgrade(true);
		if ($ret->isError()) {
		    $this->resetL10Domain();
		    $gallery->debug(sprintf('Error: Unable to install the core module, this ' .
					'is the error stack trace; %s', $ret->getAsText()));
		    $templateData['errors'][] = _('Unable to install the core module');
		    $templateData['stackTrace'] = $ret->getAsHtml();
		} else {
		    $gallery->debug('Core module installed successfully');
		}

		$gallery->debug('Activate core module');
		list ($ret, $ignored) = $core->activate();
		$this->resetL10Domain();
		if ($ret->isError()) {
		    $gallery->debug(sprintf('Error: Unable to activate the core module, this ' .
					'is the error stack trace; %s', $ret->getAsText()));
		    $templateData['errors'][] = _('Unable to activate the core module');
		    $templateData['stackTrace'] = $ret->getAsHtml();
		} else {
		    $gallery->debug('Core module activated successfully');
		}

		$gallery->debug('Commit transaction');
		$ret = $storage->commitTransaction();
		if ($ret->isError()) {
		    $gallery->debug(sprintf('Error: Unable to commit database transaction, this ' .
					'is the error stack trace; %s', $ret->getAsText()));
		    $templateData['errors'][] = _('Unable to commit database transaction');
		    $templateData['stackTrace'] = $ret->getAsHtml();
		} else {
		    $gallery->debug('Committed transaction successfully');
		}
	    } else {
		$gallery->debug('NOT installing, rollback!');
		$storage->rollbackTransaction(); /* Ignore any errors from this */
		/*
		 * Don't allow this step to complete when only partially installed.
		 * Verify that versions.dat is up to date.
		 */
		$versions = $core->getInstalledVersions();
		if (empty($versions['core']) || $versions['core'] != $core->getVersion()) {
		    $gallery->debug('Error: Core module is only partially installed');
		    $templateData['errors'][] = _('Core module is only partially installed.');
		    $templateData['stackTrace'] = '';
		} else {
		    $gallery->debug('Core module version is ok');
		}
	    }
	}

	$gallery->debug('Finish install core module step');
	if (empty($templateData['errors'])) {
	    $gallery->debug('Install core module step completed successfully');
	    $this->setComplete(true);
	    $templateData['bodyFile'] = 'InstallCoreModuleSuccess.html';
	} else {
	    $gallery->debug('Error: Failure during install core module step');
	    $templateData['bodyFile'] = 'InstallCoreModuleError.html';
	}
    }

    /* Adds a header to the debug log */
    function _createDebugLogHeader() {
	global $gallery;

	$gallery->debug("\n\n
--------------------------------------------------------
        Prepare installation of the core module
--------------------------------------------------------\n\n");
    }

    /* Adds some system information to the log */
    function _addSystemInformationToDebugLog() {
	global $gallery;
	global $galleryStub;

	$storage =& $gallery->getStorage();
	$isCvsInstall = $galleryStub->getConfig('systemchecks.iscvsinstall');
	$isCvsInstall = empty($isCvsInstall) ? "No" : "Yes";

	$gallery->debug("\n
--------------------------------------------------------
System and Gallery information:
--------------------------------------------------------
    Gallery version:\t" . $galleryStub->getConfig('codebase.version') . "
    File integrity:\t" . $galleryStub->getConfig('systemchecks.fileintegrity') . "
    CVS install:\t" . $isCvsInstall . "
    PHP version:\t" . phpversion() . " " . php_sapi_name() . "
    PHP memory limit:\t" . ini_get('memory_limit') . "
    PHP disable_functions:\t" . ini_get('disable_functions') . "
    PHP zend.ze1_compatibility_mode:\t" . ini_get('zend.ze1_compatibility_mode') . "
    Webserver:\t" . GalleryUtilities::getServerVar('SERVER_SOFTWARE') . "
    Database:\t" . $storage->_impl->getAdoDbType() . " " . @$storage->_impl->getVersion() . "
    Operating system:\t" . php_uname() . "
    Browser:\t " . GalleryUtilities::getServerVar('HTTP_USER_AGENT') . "
--------------------------------------------------------\n\n
");
    }
}
?>
