PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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