setup.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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('datadirectory', $datadir);
  55. OC_Config::setValue('dbtype', $dbtype);
  56. OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
  57. try {
  58. $dbSetup->initialize($options);
  59. $dbSetup->setupDatabase($username);
  60. } catch (DatabaseSetupException $e) {
  61. $error[] = array(
  62. 'error' => $e->getMessage(),
  63. 'hint' => $e->getHint()
  64. );
  65. return($error);
  66. } catch (Exception $e) {
  67. $error[] = array(
  68. 'error' => 'Error while trying to create admin user: ' . $e->getMessage(),
  69. 'hint' => ''
  70. );
  71. return($error);
  72. }
  73. //create the user and group
  74. try {
  75. OC_User::createUser($username, $password);
  76. }
  77. catch(Exception $exception) {
  78. $error[] = $exception->getMessage();
  79. }
  80. if(count($error) == 0) {
  81. OC_Appconfig::setValue('core', 'installedat', microtime(true));
  82. OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true));
  83. OC_AppConfig::setValue('core', 'remote_core.css', '/core/minimizer.php');
  84. OC_AppConfig::setValue('core', 'remote_core.js', '/core/minimizer.php');
  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 htaccess files for apache hosts
  91. if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
  92. self::createHtaccess();
  93. }
  94. //and we are done
  95. OC_Config::setValue('installed', true);
  96. }
  97. return $error;
  98. }
  99. /**
  100. * create .htaccess files for apache hosts
  101. */
  102. private static function createHtaccess() {
  103. $content = "<IfModule mod_fcgid.c>\n";
  104. $content.= "<IfModule mod_setenvif.c>\n";
  105. $content.= "<IfModule mod_headers.c>\n";
  106. $content.= "SetEnvIfNoCase ^Authorization$ \"(.+)\" XAUTHORIZATION=$1\n";
  107. $content.= "RequestHeader set XAuthorization %{XAUTHORIZATION}e env=XAUTHORIZATION\n";
  108. $content.= "</IfModule>\n";
  109. $content.= "</IfModule>\n";
  110. $content.= "</IfModule>\n";
  111. $content.= "ErrorDocument 403 ".OC::$WEBROOT."/core/templates/403.php\n";//custom 403 error page
  112. $content.= "ErrorDocument 404 ".OC::$WEBROOT."/core/templates/404.php\n";//custom 404 error page
  113. $content.= "<IfModule mod_php5.c>\n";
  114. $content.= "php_value upload_max_filesize 512M\n";//upload limit
  115. $content.= "php_value post_max_size 512M\n";
  116. $content.= "php_value memory_limit 512M\n";
  117. $content.= "php_value mbstring.func_overload 0\n";
  118. $content.= "<IfModule env_module>\n";
  119. $content.= " SetEnv htaccessWorking true\n";
  120. $content.= "</IfModule>\n";
  121. $content.= "</IfModule>\n";
  122. $content.= "<IfModule mod_rewrite.c>\n";
  123. $content.= "RewriteEngine on\n";
  124. $content.= "RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\n";
  125. $content.= "RewriteRule ^.well-known/host-meta /public.php?service=host-meta [QSA,L]\n";
  126. $content.= "RewriteRule ^.well-known/carddav /remote.php/carddav/ [R]\n";
  127. $content.= "RewriteRule ^.well-known/caldav /remote.php/caldav/ [R]\n";
  128. $content.= "RewriteRule ^apps/([^/]*)/(.*\.(css|php))$ index.php?app=$1&getfile=$2 [QSA,L]\n";
  129. $content.= "RewriteRule ^remote/(.*) remote.php [QSA,L]\n";
  130. $content.= "</IfModule>\n";
  131. $content.= "<IfModule mod_mime.c>\n";
  132. $content.= "AddType image/svg+xml svg svgz\n";
  133. $content.= "AddEncoding gzip svgz\n";
  134. $content.= "</IfModule>\n";
  135. $content.= "<IfModule dir_module>\n";
  136. $content.= "DirectoryIndex index.php index.html\n";
  137. $content.= "</IfModule>\n";
  138. $content.= "AddDefaultCharset utf-8\n";
  139. $content.= "Options -Indexes\n";
  140. @file_put_contents(OC::$SERVERROOT.'/.htaccess', $content); //supress errors in case we don't have permissions for it
  141. self::protectDataDirectory();
  142. }
  143. public static function protectDataDirectory() {
  144. //Require all denied
  145. $now = date('Y-m-d H:i:s');
  146. $content = "# Generated by ownCloud on $now\n";
  147. $content.= "# line below if for Apache 2.4\n";
  148. $content.= "<ifModule mod_authz_core>\n";
  149. $content.= "Require all denied\n";
  150. $content.= "</ifModule>\n\n";
  151. $content.= "# line below if for Apache 2.2\n";
  152. $content.= "<ifModule !mod_authz_core>\n";
  153. $content.= "deny from all\n";
  154. $content.= "</ifModule>\n\n";
  155. $content.= "# section for Apache 2.2 and 2.4\n";
  156. $content.= "IndexIgnore *\n";
  157. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.htaccess', $content);
  158. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/index.html', '');
  159. }
  160. /**
  161. * @brief Post installation checks
  162. */
  163. public static function postSetupCheck($params) {
  164. // setup was successful -> webdav testing now
  165. $l = self::getTrans();
  166. if (OC_Util::isWebDAVWorking()) {
  167. header("Location: ".OC::$WEBROOT.'/');
  168. } else {
  169. $error = $l->t('Your web server is not yet properly setup to allow files synchronization because the WebDAV interface seems to be broken.');
  170. $hint = $l->t('Please double check the <a href=\'%s\'>installation guides</a>.',
  171. \OC_Helper::linkToDocs('admin-install'));
  172. OC_Template::printErrorPage($error, $hint);
  173. exit();
  174. }
  175. }
  176. }