setup.php 5.3 KB

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