baseline
[omp.pkp.sfu.ca.git] / classes / install / form / InstallForm.inc.php
blob1cfa6d3b765fd050a1764ea4c1ce04ba14d8c226
1 <?php
3 /**
4 * @defgroup install_form
5 */
7 /**
8 * @file classes/install/form/InstallForm.inc.php
10 * Copyright (c) 2003-2008 John Willinsky
11 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
13 * @class InstallForm
14 * @ingroup install_form
15 * @see Install
17 * @brief Form for system installation.
20 // $Id: InstallForm.inc.php,v 1.4 2009/09/22 19:22:09 asmecher Exp $
23 import('install.Install');
24 import('site.VersionCheck');
25 import('form.Form');
27 define('PHP_REQUIRED_VERSION', '4.2.0');
29 class InstallForm extends Form {
31 /** @var array locales supported by this system */
32 var $supportedLocales;
34 /** @var array locale completeness booleans */
35 var $localesComplete;
37 /** @var array client character sets supported by this system */
38 var $supportedClientCharsets;
40 /** @var array connection character sets supported by this system */
41 var $supportedConnectionCharsets;
43 /** @var array database character sets supported by this system */
44 var $supportedDatabaseCharsets;
46 /** @var array database drivers supported by this system */
47 var $supportedDatabaseDrivers;
49 /**
50 * Constructor.
52 function InstallForm() {
53 parent::Form('install/install.tpl');
55 // FIXME Move the below options to an external configuration file?
56 $this->supportedLocales = Locale::getAllLocales();
57 $this->localesComplete = array();
58 foreach ($this->supportedLocales as $key => $name) {
59 $this->localesComplete[$key] = Locale::isLocaleComplete($key);
62 $this->supportedClientCharsets = array (
63 'utf-8' => 'Unicode (UTF-8)',
64 'iso-8859-1' => 'Western (ISO-8859-1)'
67 $this->supportedConnectionCharsets = array (
68 '' => Locale::translate('common.notApplicable'),
69 'utf8' => 'Unicode (UTF-8)'
72 $this->supportedDatabaseCharsets = array (
73 '' => Locale::translate('common.notApplicable'),
74 'utf8' => 'Unicode (UTF-8)'
77 $this->supportedEncryptionAlgorithms = array (
78 'md5' => 'MD5'
80 if (function_exists('sha1')) {
81 $this->supportedEncryptionAlgorithms['sha1'] = 'SHA1';
84 $this->supportedDatabaseDrivers = array (
85 // <adodb-driver> => array(<php-module>, <name>)
86 'mysql' => array('mysql', 'MySQL'),
87 'postgres' => array('pgsql', 'PostgreSQL'),
88 'oracle' => array('oci8', 'Oracle'),
89 'mssql' => array('mssql', 'MS SQL Server'),
90 'fbsql' => array('fbsql', 'FrontBase'),
91 'ibase' => array('ibase', 'Interbase'),
92 'firebird' => array('ibase', 'Firebird'),
93 'informix' => array('ifx', 'Informix'),
94 'sybase' => array('sybase', 'Sybase'),
95 'odbc' => array('odbc', 'ODBC'),
98 // Validation checks for this form
99 $this->addCheck(new FormValidatorInSet($this, 'locale', 'required', 'installer.form.localeRequired', array_keys($this->supportedLocales)));
100 $this->addCheck(new FormValidatorCustom($this, 'locale', 'required', 'installer.form.localeRequired', array('Locale', 'isLocaleValid')));
101 $this->addCheck(new FormValidatorInSet($this, 'clientCharset', 'required', 'installer.form.clientCharsetRequired', array_keys($this->supportedClientCharsets)));
102 $this->addCheck(new FormValidator($this, 'filesDir', 'required', 'installer.form.filesDirRequired'));
103 $this->addCheck(new FormValidatorInSet($this, 'encryption', 'required', 'installer.form.encryptionRequired', array_keys($this->supportedEncryptionAlgorithms)));
104 $this->addCheck(new FormValidator($this, 'adminUsername', 'required', 'installer.form.usernameRequired'));
105 $this->addCheck(new FormValidatorAlphaNum($this, 'adminUsername', 'required', 'installer.form.usernameAlphaNumeric'));
106 $this->addCheck(new FormValidator($this, 'adminPassword', 'required', 'installer.form.passwordRequired'));
107 $this->addCheck(new FormValidatorCustom($this, 'adminPassword', 'required', 'installer.form.passwordsDoNotMatch', create_function('$password,$form', 'return $password == $form->getData(\'adminPassword2\');'), array(&$this)));
108 $this->addCheck(new FormValidatorEmail($this, 'adminEmail', 'required', 'installer.form.emailRequired'));
109 $this->addCheck(new FormValidatorInSet($this, 'databaseDriver', 'required', 'installer.form.databaseDriverRequired', array_keys($this->supportedDatabaseDrivers)));
110 $this->addCheck(new FormValidator($this, 'databaseName', 'required', 'installer.form.databaseNameRequired'));
111 $this->addCheck(new FormValidatorPost($this));
115 * Display the form.
117 function display() {
118 $templateMgr =& TemplateManager::getManager();
119 $templateMgr->assign('localeOptions', $this->supportedLocales);
120 $templateMgr->assign('localesComplete', $this->localesComplete);
121 $templateMgr->assign('clientCharsetOptions', $this->supportedClientCharsets);
122 $templateMgr->assign('connectionCharsetOptions', $this->supportedConnectionCharsets);
123 $templateMgr->assign('databaseCharsetOptions', $this->supportedDatabaseCharsets);
124 $templateMgr->assign('encryptionOptions', $this->supportedEncryptionAlgorithms);
125 $templateMgr->assign('databaseDriverOptions', $this->checkDBDrivers());
126 $templateMgr->assign('supportsMBString', String::hasMBString() ? Locale::translate('common.yes') : Locale::translate('common.no'));
127 $templateMgr->assign('phpIsSupportedVersion', version_compare(PHP_REQUIRED_VERSION, PHP_VERSION) != 1);
128 $templateMgr->assign('phpRequiredVersion', PHP_REQUIRED_VERSION);
129 $templateMgr->assign('phpVersion', PHP_VERSION);
130 $templateMgr->assign('version', VersionCheck::getCurrentCodeVersion());
132 parent::display();
136 * Initialize form data.
138 function initData() {
139 $cwd = getcwd();
140 if (Core::isWindows()) {
141 // Replace backslashes with slashes for the default files directory.
142 $cwd = str_replace('\\', '/', $cwd);
145 $this->_data = array(
146 'locale' => Locale::getLocale(),
147 'additionalLocales' => array(),
148 'clientCharset' => 'utf-8',
149 'connectionCharset' => '',
150 'databaseCharset' => '',
151 'encryption' => 'md5',
152 'filesDir' => $cwd . '/files',
153 'skipFilesDir' => 0,
154 'databaseDriver' => 'mysql',
155 'databaseHost' => 'localhost',
156 'databaseUsername' => 'omp',
157 'databasePassword' => '',
158 'databaseName' => 'omp',
159 'createDatabase' => 1,
160 'oaiRepositoryId' => 'omp.' . Request::getServerHost()
165 * Assign form data to user-submitted data.
167 function readInputData() {
168 $this->readUserVars(array(
169 'locale',
170 'additionalLocales',
171 'clientCharset',
172 'connectionCharset',
173 'databaseCharset',
174 'filesDir',
175 'skipFilesDir',
176 'encryption',
177 'adminUsername',
178 'adminPassword',
179 'adminPassword2',
180 'adminEmail',
181 'databaseDriver',
182 'databaseHost',
183 'databaseUsername',
184 'databasePassword',
185 'databaseName',
186 'createDatabase',
187 'oaiRepositoryId',
188 'manualInstall'
191 if ($this->getData('additionalLocales') == null || !is_array($this->getData('additionalLocales'))) {
192 $this->setData('additionalLocales', array());
197 * Perform installation.
199 function execute() {
200 $templateMgr =& TemplateManager::getManager();
201 $installer = new Install($this->_data);
203 if ($installer->execute()) {
204 if ($this->getData('manualInstall')) {
205 // Display SQL statements that would have been executed during installation
206 $templateMgr->assign(array('manualInstall' => true, 'installSql' => $installer->getSQL()));
209 if (!$installer->wroteConfig()) {
210 // Display config file contents for manual replacement
211 $templateMgr->assign(array('writeConfigFailed' => true, 'configFileContents' => $installer->getConfigContents()));
214 $templateMgr->display('install/installComplete.tpl');
216 } else {
217 switch ($installer->getErrorType()) {
218 case INSTALLER_ERROR_DB:
219 $this->dbInstallError($installer->getErrorMsg());
220 break;
221 default:
222 $this->installError($installer->getErrorMsg());
223 break;
227 $installer->destroy();
231 * Check if database drivers have the required PHP module loaded.
232 * The names of drivers that appear to be unavailable are bracketed.
233 * @return array
235 function checkDBDrivers() {
236 $dbDrivers = array();
237 foreach ($this->supportedDatabaseDrivers as $driver => $info) {
238 list($module, $name) = $info;
239 if (!extension_loaded($module)) {
240 $name = '[ ' . $name . ' ]';
242 $dbDrivers[$driver] = $name;
244 return $dbDrivers;
248 * Fail with a generic installation error.
249 * @param $errorMsg string
251 function installError($errorMsg) {
252 $templateMgr =& TemplateManager::getManager();
253 $templateMgr->assign(array('isInstallError' => true, 'errorMsg' => $errorMsg));
254 error_log($errorMsg);
255 $this->display();
259 * Fail with a database installation error.
260 * @param $errorMsg string
262 function dbInstallError($errorMsg) {
263 $templateMgr =& TemplateManager::getManager();
264 if (empty($errorMsg)) $errorMsg = Locale::translate('common.error.databaseErrorUnknown');
265 $templateMgr->assign(array('isInstallError' => true, 'dbErrorMsg' => $errorMsg));
266 error_log($errorMsg);
267 $this->display();