Ejemplo n.º 1
0
 /**
  * @param $options
  * @return array
  */
 public static function install($options)
 {
     $l = self::getTrans();
     $error = array();
     $dbtype = $options['dbtype'];
     if (empty($options['adminlogin'])) {
         $error[] = $l->t('Set an admin username.');
     }
     if (empty($options['adminpass'])) {
         $error[] = $l->t('Set an admin password.');
     }
     if (empty($options['directory'])) {
         $options['directory'] = OC::$SERVERROOT . "/data";
     }
     if (!isset(self::$dbSetupClasses[$dbtype])) {
         $dbtype = 'sqlite';
     }
     $username = htmlspecialchars_decode($options['adminlogin']);
     $password = htmlspecialchars_decode($options['adminpass']);
     $datadir = htmlspecialchars_decode($options['directory']);
     $class = self::$dbSetupClasses[$dbtype];
     /** @var \OC\Setup\AbstractDatabase $dbSetup */
     $dbSetup = new $class(self::getTrans(), 'db_structure.xml');
     $error = array_merge($error, $dbSetup->validate($options));
     // validate the data directory
     if (!is_dir($datadir) and !mkdir($datadir) or !is_writable($datadir)) {
         $error[] = $l->t("Can't create or write into the data directory %s", array($datadir));
     }
     if (count($error) != 0) {
         return $error;
     }
     //no errors, good
     if (isset($options['trusted_domains']) && is_array($options['trusted_domains'])) {
         $trustedDomains = $options['trusted_domains'];
     } else {
         $trustedDomains = array(\OC_Request::getDomainWithoutPort(\OC_Request::serverHost()));
     }
     if (OC_Util::runningOnWindows()) {
         $datadir = rtrim(realpath($datadir), '\\');
     }
     //use sqlite3 when available, otherise sqlite2 will be used.
     if ($dbtype == 'sqlite' and class_exists('SQLite3')) {
         $dbtype = 'sqlite3';
     }
     //generate a random salt that is used to salt the local user passwords
     $salt = OC_Util::generateRandomBytes(30);
     OC_Config::setValue('passwordsalt', $salt);
     OC_Config::setValue('secret', OC_Util::generateRandomBytes(96));
     //write the config file
     OC_Config::setValue('trusted_domains', $trustedDomains);
     OC_Config::setValue('datadirectory', $datadir);
     OC_Config::setValue('overwrite.cli.url', \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . OC::$WEBROOT);
     OC_Config::setValue('dbtype', $dbtype);
     OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
     try {
         $dbSetup->initialize($options);
         $dbSetup->setupDatabase($username);
     } catch (DatabaseSetupException $e) {
         $error[] = array('error' => $e->getMessage(), 'hint' => $e->getHint());
         return $error;
     } catch (Exception $e) {
         $error[] = array('error' => 'Error while trying to create admin user: '******'hint' => '');
         return $error;
     }
     //create the user and group
     try {
         OC_User::createUser($username, $password);
     } catch (Exception $exception) {
         $error[] = $exception->getMessage();
     }
     if (count($error) == 0) {
         $appConfig = \OC::$server->getAppConfig();
         $appConfig->setValue('core', 'installedat', microtime(true));
         $appConfig->setValue('core', 'lastupdatedat', microtime(true));
         OC_Group::createGroup('admin');
         OC_Group::addToGroup($username, 'admin');
         OC_User::login($username, $password);
         //guess what this does
         OC_Installer::installShippedApps();
         // create empty file in data dir, so we can later find
         // out that this is indeed an ownCloud data directory
         file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data') . '/.ocdata', '');
         // Update htaccess files for apache hosts
         if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
             self::updateHtaccess();
             self::protectDataDirectory();
         }
         //and we are done
         OC_Config::setValue('installed', true);
     }
     return $error;
 }
Ejemplo n.º 2
0
 /**
  * @dataProvider hostWithPortProvider
  */
 public function testGetDomainWithoutPort($hostWithPort, $host)
 {
     $this->assertEquals($host, OC_Request::getDomainWithoutPort($hostWithPort));
 }