PageRenderTime 34ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ojs/ojs-2.2.3/classes/install/Install.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 313 lines | 202 code | 42 blank | 69 comment | 32 complexity | 90d9838f378624c8cc06ec24fdac32c2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @defgroup install
  4. */
  5. /**
  6. * @file classes/install/Install.inc.php
  7. *
  8. * Copyright (c) 2003-2009 John Willinsky
  9. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  10. *
  11. * @class Install
  12. * @ingroup install
  13. * @see Installer, InstallForm
  14. *
  15. * @brief Perform system installation.
  16. *
  17. * This script will:
  18. * - Create the database (optionally), and install the database tables and initial data.
  19. * - Update the config file with installation parameters.
  20. * It can also be used for a "manual install" to retrieve the SQL statements required for installation.
  21. */
  22. // $Id: Install.inc.php,v 1.15.2.2 2009/04/08 19:42:47 asmecher Exp $
  23. // Default installation data
  24. define('INSTALLER_DEFAULT_SITE_TITLE', 'common.openJournalSystems');
  25. define('INSTALLER_DEFAULT_MIN_PASSWORD_LENGTH', 6);
  26. import('install.Installer');
  27. class Install extends Installer {
  28. /**
  29. * Constructor.
  30. * @see install.form.InstallForm for the expected parameters
  31. * @param $params array installation parameters
  32. */
  33. function Install($params) {
  34. parent::Installer('install.xml', $params);
  35. }
  36. /**
  37. * Returns true iff this is an upgrade process.
  38. */
  39. function isUpgrade() {
  40. return false;
  41. }
  42. /**
  43. * Pre-installation.
  44. * @return boolean
  45. */
  46. function preInstall() {
  47. $this->currentVersion = Version::fromString('');
  48. $this->locale = $this->getParam('locale');
  49. $this->installedLocales = $this->getParam('additionalLocales');
  50. if (!isset($this->installedLocales) || !is_array($this->installedLocales)) {
  51. $this->installedLocales = array();
  52. }
  53. if (!in_array($this->locale, $this->installedLocales) && Locale::isLocaleValid($this->locale)) {
  54. array_push($this->installedLocales, $this->locale);
  55. }
  56. if ($this->getParam('manualInstall')) {
  57. // Do not perform database installation for manual install
  58. // Create connection object with the appropriate database driver for adodb-xmlschema
  59. $conn = &new DBConnection(
  60. $this->getParam('databaseDriver'),
  61. null,
  62. null,
  63. null,
  64. null
  65. );
  66. $this->dbconn = &$conn->getDBConn();
  67. } else {
  68. // Connect to database
  69. $conn = &new DBConnection(
  70. $this->getParam('databaseDriver'),
  71. $this->getParam('databaseHost'),
  72. $this->getParam('databaseUsername'),
  73. $this->getParam('databasePassword'),
  74. $this->getParam('createDatabase') ? null : $this->getParam('databaseName'),
  75. true,
  76. $this->getParam('connectionCharset') == '' ? false : $this->getParam('connectionCharset')
  77. );
  78. $this->dbconn = &$conn->getDBConn();
  79. if (!$conn->isConnected()) {
  80. $this->setError(INSTALLER_ERROR_DB, $this->dbconn->errorMsg());
  81. return false;
  82. }
  83. }
  84. DBConnection::getInstance($conn);
  85. return parent::preInstall();
  86. }
  87. //
  88. // Installer actions
  89. //
  90. /**
  91. * Create required files directories
  92. * FIXME No longer needed since FileManager will auto-create?
  93. * @return boolean
  94. */
  95. function createDirectories() {
  96. if ($this->getParam('skipFilesDir')) {
  97. return true;
  98. }
  99. // Check if files directory exists and is writeable
  100. if (!(file_exists($this->getParam('filesDir')) && is_writeable($this->getParam('filesDir')))) {
  101. // Files upload directory unusable
  102. $this->setError(INSTALLER_ERROR_GENERAL, 'installer.installFilesDirError');
  103. return false;
  104. } else {
  105. // Create required subdirectories
  106. $dirsToCreate = array('site', 'journals');
  107. foreach ($dirsToCreate as $dirName) {
  108. $dirToCreate = $this->getParam('filesDir') . '/' . $dirName;
  109. if (!file_exists($dirToCreate)) {
  110. import('file.FileManager');
  111. if (!FileManager::mkdir($dirToCreate)) {
  112. $this->setError(INSTALLER_ERROR_GENERAL, 'installer.installFilesDirError');
  113. return false;
  114. }
  115. }
  116. }
  117. }
  118. // Check if public files directory exists and is writeable
  119. $publicFilesDir = Config::getVar('files', 'public_files_dir');
  120. if (!(file_exists($publicFilesDir) && is_writeable($publicFilesDir))) {
  121. // Public files upload directory unusable
  122. $this->setError(INSTALLER_ERROR_GENERAL, 'installer.publicFilesDirError');
  123. return false;
  124. } else {
  125. // Create required subdirectories
  126. $dirsToCreate = array('site', 'journals');
  127. foreach ($dirsToCreate as $dirName) {
  128. $dirToCreate = $publicFilesDir . '/' . $dirName;
  129. if (!file_exists($dirToCreate)) {
  130. import('file.FileManager');
  131. if (!FileManager::mkdir($dirToCreate)) {
  132. $this->setError(INSTALLER_ERROR_GENERAL, 'installer.publicFilesDirError');
  133. return false;
  134. }
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Create a new database if required.
  142. * @return boolean
  143. */
  144. function createDatabase() {
  145. if (!$this->getParam('createDatabase')) {
  146. return true;
  147. }
  148. // Get database creation sql
  149. $dbdict = &NewDataDictionary($this->dbconn);
  150. if ($this->getParam('databaseCharset')) {
  151. $dbdict->SetCharSet($this->getParam('databaseCharset'));
  152. }
  153. list($sql) = $dbdict->CreateDatabase($this->getParam('databaseName'));
  154. unset($dbdict);
  155. if (!$this->executeSQL($sql)) {
  156. return false;
  157. }
  158. if (!$this->getParam('manualInstall')) {
  159. // Re-connect to the created database
  160. $this->dbconn->disconnect();
  161. $conn = &new DBConnection(
  162. $this->getParam('databaseDriver'),
  163. $this->getParam('databaseHost'),
  164. $this->getParam('databaseUsername'),
  165. $this->getParam('databasePassword'),
  166. $this->getParam('databaseName'),
  167. true,
  168. $this->getParam('connectionCharset') == '' ? false : $this->getParam('connectionCharset')
  169. );
  170. DBConnection::getInstance($conn);
  171. $this->dbconn = &$conn->getDBConn();
  172. if (!$conn->isConnected()) {
  173. $this->setError(INSTALLER_ERROR_DB, $this->dbconn->errorMsg());
  174. return false;
  175. }
  176. }
  177. return true;
  178. }
  179. /**
  180. * Create initial required data.
  181. * @return boolean
  182. */
  183. function createData() {
  184. if ($this->getParam('manualInstall')) {
  185. // Add insert statements for default data
  186. // FIXME use ADODB data dictionary?
  187. $this->executeSQL(sprintf('INSERT INTO site (primary_locale, installed_locales) VALUES (\'%s\', \'%s\')', $this->getParam('locale'), join(':', $this->installedLocales)));
  188. $this->executeSQL(sprintf('INSERT INTO site_settings (setting_name, setting_type, setting_value, locale) VALUES (\'%s\', \'%s\', \'%s\', \'%s\')', 'title', 'string', addslashes(Locale::translate(INSTALLER_DEFAULT_SITE_TITLE)), $this->getParam('locale')));
  189. $this->executeSQL(sprintf('INSERT INTO site_settings (setting_name, setting_type, setting_value, locale) VALUES (\'%s\', \'%s\', \'%s\', \'%s\')', 'contactName', 'string', addslashes(Locale::translate(INSTALLER_DEFAULT_SITE_TITLE)), $this->getParam('locale')));
  190. $this->executeSQL(sprintf('INSERT INTO site_settings (setting_name, setting_type, setting_value, locale) VALUES (\'%s\', \'%s\', \'%s\', \'%s\')', 'contactEmail', 'string', addslashes($this->getParam('adminEmail')), $this->getParam('locale')));
  191. $this->executeSQL(sprintf('INSERT INTO users (username, first_name, last_name, password, email, date_registered, date_last_login) VALUES (\'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\', \'%s\')', $this->getParam('adminUsername'), $this->getParam('adminUsername'), $this->getParam('adminUsername'), Validation::encryptCredentials($this->getParam('adminUsername'), $this->getParam('adminPassword'), $this->getParam('encryption')), $this->getParam('adminEmail'), Core::getCurrentDate(), Core::getCurrentDate()));
  192. $this->executeSQL(sprintf('INSERT INTO roles (journal_id, user_id, role_id) VALUES (%d, (SELECT user_id FROM users WHERE username = \'%s\'), %d)', 0, $this->getParam('adminUsername'), ROLE_ID_SITE_ADMIN));
  193. } else {
  194. // Add initial site data
  195. $locale = $this->getParam('locale');
  196. $siteDao = &DAORegistry::getDAO('SiteDAO', $this->dbconn);
  197. $site = &new Site();
  198. $site->setTitle(Locale::translate(INSTALLER_DEFAULT_SITE_TITLE), $locale);
  199. $site->setJournalRedirect(0);
  200. $site->setMinPasswordLength(INSTALLER_DEFAULT_MIN_PASSWORD_LENGTH);
  201. $site->setPrimaryLocale($locale);
  202. $site->setInstalledLocales($this->installedLocales);
  203. $site->setSupportedLocales($this->installedLocales);
  204. $site->setContactName($site->getTitle($locale), $locale);
  205. $site->setContactEmail($this->getParam('adminEmail'), $locale);
  206. if (!$siteDao->insertSite($site)) {
  207. $this->setError(INSTALLER_ERROR_DB, $this->dbconn->errorMsg());
  208. return false;
  209. }
  210. // Add initial site administrator user
  211. $userDao = &DAORegistry::getDAO('UserDAO', $this->dbconn);
  212. $user = &new User();
  213. $user->setUsername($this->getParam('adminUsername'));
  214. $user->setPassword(Validation::encryptCredentials($this->getParam('adminUsername'), $this->getParam('adminPassword'), $this->getParam('encryption')));
  215. $user->setFirstName($user->getUsername());
  216. $user->setLastName('');
  217. $user->setEmail($this->getParam('adminEmail'));
  218. if (!$userDao->insertUser($user)) {
  219. $this->setError(INSTALLER_ERROR_DB, $this->dbconn->errorMsg());
  220. return false;
  221. }
  222. $roleDao = &DAORegistry::getDao('RoleDAO', $this->dbconn);
  223. $role = &new Role();
  224. $role->setJournalId(0);
  225. $role->setUserId($user->getUserId());
  226. $role->setRoleId(ROLE_ID_SITE_ADMIN);
  227. if (!$roleDao->insertRole($role)) {
  228. $this->setError(INSTALLER_ERROR_DB, $this->dbconn->errorMsg());
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. /**
  235. * Write the configuration file.
  236. * @return boolean
  237. */
  238. function createConfig() {
  239. return $this->updateConfig(
  240. array(
  241. 'general' => array(
  242. 'installed' => 'On',
  243. 'base_url' => Request::getBaseUrl()
  244. ),
  245. 'database' => array(
  246. 'driver' => $this->getParam('databaseDriver'),
  247. 'host' => $this->getParam('databaseHost'),
  248. 'username' => $this->getParam('databaseUsername'),
  249. 'password' => $this->getParam('databasePassword'),
  250. 'name' => $this->getParam('databaseName')
  251. ),
  252. 'i18n' => array(
  253. 'locale' => $this->getParam('locale'),
  254. 'client_charset' => $this->getParam('clientCharset'),
  255. 'connection_charset' => $this->getParam('connectionCharset') == '' ? 'Off' : $this->getParam('connectionCharset'),
  256. 'database_charset' => $this->getParam('databaseCharset') == '' ? 'Off' : $this->getParam('databaseCharset')
  257. ),
  258. 'files' => array(
  259. 'files_dir' => $this->getParam('filesDir')
  260. ),
  261. 'security' => array(
  262. 'encryption' => $this->getParam('encryption')
  263. ),
  264. 'oai' => array(
  265. 'repository_id' => $this->getParam('oaiRepositoryId')
  266. )
  267. )
  268. );
  269. }
  270. }
  271. ?>