You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. class DatabaseSetupException extends \OC\HintException
  3. {
  4. }
  5. class OC_Setup {
  6. static $dbSetupClasses = array(
  7. 'mysql' => '\OC\Setup\MySQL',
  8. 'pgsql' => '\OC\Setup\PostgreSQL',
  9. 'oci' => '\OC\Setup\OCI',
  10. 'mssql' => '\OC\Setup\MSSQL',
  11. 'sqlite' => '\OC\Setup\Sqlite',
  12. 'sqlite3' => '\OC\Setup\Sqlite',
  13. );
  14. public static function getTrans(){
  15. return OC_L10N::get('lib');
  16. }
  17. public static function install($options) {
  18. $l = self::getTrans();
  19. $error = array();
  20. $dbtype = $options['dbtype'];
  21. if(empty($options['adminlogin'])) {
  22. $error[] = $l->t('Set an admin username.');
  23. }
  24. if(empty($options['adminpass'])) {
  25. $error[] = $l->t('Set an admin password.');
  26. }
  27. if(empty($options['directory'])) {
  28. $options['directory'] = OC::$SERVERROOT."/data";
  29. }
  30. if (!isset(self::$dbSetupClasses[$dbtype])) {
  31. $dbtype = 'sqlite';
  32. }
  33. $class = self::$dbSetupClasses[$dbtype];
  34. $dbSetup = new $class(self::getTrans(), 'db_structure.xml');
  35. $error = array_merge($error, $dbSetup->validate($options));
  36. if(count($error) != 0) {
  37. return $error;
  38. }
  39. //no errors, good
  40. $username = htmlspecialchars_decode($options['adminlogin']);
  41. $password = htmlspecialchars_decode($options['adminpass']);
  42. $datadir = htmlspecialchars_decode($options['directory']);
  43. if (OC_Util::runningOnWindows()) {
  44. $datadir = rtrim(realpath($datadir), '\\');
  45. }
  46. //use sqlite3 when available, otherise sqlite2 will be used.
  47. if($dbtype=='sqlite' and class_exists('SQLite3')) {
  48. $dbtype='sqlite3';
  49. }
  50. //generate a random salt that is used to salt the local user passwords
  51. $salt = OC_Util::generateRandomBytes(30);
  52. OC_Config::setValue('passwordsalt', $salt);
  53. //write the config file
  54. OC_Config::setValue('trusted_domains', array(OC_Request::serverHost()));
  55. OC_Config::setValue('datadirectory', $datadir);
  56. OC_Config::setValue('dbtype', $dbtype);
  57. OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
  58. try {
  59. $dbSetup->initialize($options);
  60. $dbSetup->setupDatabase($username);
  61. } catch (DatabaseSetupException $e) {
  62. $error[] = array(
  63. 'error' => $e->getMessage(),
  64. 'hint' => $e->getHint()
  65. );
  66. return($error);
  67. } catch (Exception $e) {
  68. $error[] = array(
  69. 'error' => 'Error while trying to create admin user: ' . $e->getMessage(),
  70. 'hint' => ''
  71. );
  72. return($error);
  73. }
  74. //create the user and group
  75. try {
  76. OC_User::createUser($username, $password);
  77. }
  78. catch(Exception $exception) {
  79. $error[] = $exception->getMessage();
  80. }
  81. if(count($error) == 0) {
  82. $appConfig = \OC::$server->getAppConfig();
  83. $appConfig->setValue('core', 'installedat', microtime(true));
  84. $appConfig->setValue('core', 'lastupdatedat', microtime(true));
  85. OC_Group::createGroup('admin');
  86. OC_Group::addToGroup($username, 'admin');
  87. OC_User::login($username, $password);
  88. //guess what this does
  89. OC_Installer::installShippedApps();
  90. // create empty file in data dir, so we can later find
  91. // out that this is indeed an ownCloud data directory
  92. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.ocdata', '');
  93. // Update htaccess files for apache hosts
  94. if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
  95. self::updateHtaccess();
  96. self::protectDataDirectory();
  97. }
  98. //and we are done
  99. OC_Config::setValue('installed', true);
  100. }
  101. return $error;
  102. }
  103. /**
  104. * Append the correct ErrorDocument path for Apache hosts
  105. */
  106. public static function updateHtaccess() {
  107. $content = "\n";
  108. $content.= "ErrorDocument 403 ".OC::$WEBROOT."/core/templates/403.php\n";//custom 403 error page
  109. $content.= "ErrorDocument 404 ".OC::$WEBROOT."/core/templates/404.php";//custom 404 error page
  110. @file_put_contents(OC::$SERVERROOT.'/.htaccess', $content, FILE_APPEND); //suppress errors in case we don't have permissions for it
  111. }
  112. public static function protectDataDirectory() {
  113. //Require all denied
  114. $now = date('Y-m-d H:i:s');
  115. $content = "# Generated by ownCloud on $now\n";
  116. $content.= "# line below if for Apache 2.4\n";
  117. $content.= "<ifModule mod_authz_core>\n";
  118. $content.= "Require all denied\n";
  119. $content.= "</ifModule>\n\n";
  120. $content.= "# line below if for Apache 2.2\n";
  121. $content.= "<ifModule !mod_authz_core>\n";
  122. $content.= "deny from all\n";
  123. $content.= "</ifModule>\n\n";
  124. $content.= "# section for Apache 2.2 and 2.4\n";
  125. $content.= "IndexIgnore *\n";
  126. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.htaccess', $content);
  127. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/index.html', '');
  128. }
  129. /**
  130. * @brief Post installation checks
  131. */
  132. public static function postSetupCheck($params) {
  133. // setup was successful -> webdav testing now
  134. $l = self::getTrans();
  135. if (OC_Util::isWebDAVWorking()) {
  136. header("Location: ".OC::$WEBROOT.'/');
  137. } else {
  138. $error = $l->t('Your web server is not yet properly setup to allow files synchronization because the WebDAV interface seems to be broken.');
  139. $hint = $l->t('Please double check the <a href=\'%s\'>installation guides</a>.',
  140. \OC_Helper::linkToDocs('admin-install'));
  141. OC_Template::printErrorPage($error, $hint);
  142. exit();
  143. }
  144. }
  145. }