Passed
Push — master ( 8c7a35...ac1c73 )
by Blizzz
23:14 queued 14s
created
lib/private/Repair.php 1 patch
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -89,202 +89,202 @@
 block discarded – undo
89 89
 
90 90
 class Repair implements IOutput {
91 91
 
92
-	/** @var IRepairStep[] */
93
-	private $repairSteps;
92
+    /** @var IRepairStep[] */
93
+    private $repairSteps;
94 94
 
95
-	private IEventDispatcher $dispatcher;
95
+    private IEventDispatcher $dispatcher;
96 96
 
97
-	/** @var string */
98
-	private $currentStep;
97
+    /** @var string */
98
+    private $currentStep;
99 99
 
100
-	private LoggerInterface $logger;
100
+    private LoggerInterface $logger;
101 101
 
102
-	/**
103
-	 * Creates a new repair step runner
104
-	 *
105
-	 * @param IRepairStep[] $repairSteps array of RepairStep instances
106
-	 */
107
-	public function __construct(array $repairSteps, IEventDispatcher $dispatcher, LoggerInterface $logger) {
108
-		$this->repairSteps = $repairSteps;
109
-		$this->dispatcher = $dispatcher;
110
-		$this->logger = $logger;
111
-	}
102
+    /**
103
+     * Creates a new repair step runner
104
+     *
105
+     * @param IRepairStep[] $repairSteps array of RepairStep instances
106
+     */
107
+    public function __construct(array $repairSteps, IEventDispatcher $dispatcher, LoggerInterface $logger) {
108
+        $this->repairSteps = $repairSteps;
109
+        $this->dispatcher = $dispatcher;
110
+        $this->logger = $logger;
111
+    }
112 112
 
113
-	/**
114
-	 * Run a series of repair steps for common problems
115
-	 */
116
-	public function run() {
117
-		if (count($this->repairSteps) === 0) {
118
-			$this->dispatcher->dispatchTyped(new RepairInfoEvent('No repair steps available'));
113
+    /**
114
+     * Run a series of repair steps for common problems
115
+     */
116
+    public function run() {
117
+        if (count($this->repairSteps) === 0) {
118
+            $this->dispatcher->dispatchTyped(new RepairInfoEvent('No repair steps available'));
119 119
 
120
-			return;
121
-		}
122
-		// run each repair step
123
-		foreach ($this->repairSteps as $step) {
124
-			$this->currentStep = $step->getName();
125
-			$this->dispatcher->dispatchTyped(new RepairStepEvent($this->currentStep));
126
-			try {
127
-				$step->run($this);
128
-			} catch (\Exception $e) {
129
-				$this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]);
130
-				$this->dispatcher->dispatchTyped(new RepairErrorEvent($e->getMessage()));
131
-			}
132
-		}
133
-	}
120
+            return;
121
+        }
122
+        // run each repair step
123
+        foreach ($this->repairSteps as $step) {
124
+            $this->currentStep = $step->getName();
125
+            $this->dispatcher->dispatchTyped(new RepairStepEvent($this->currentStep));
126
+            try {
127
+                $step->run($this);
128
+            } catch (\Exception $e) {
129
+                $this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]);
130
+                $this->dispatcher->dispatchTyped(new RepairErrorEvent($e->getMessage()));
131
+            }
132
+        }
133
+    }
134 134
 
135
-	/**
136
-	 * Add repair step
137
-	 *
138
-	 * @param IRepairStep|string $repairStep repair step
139
-	 * @throws \Exception
140
-	 */
141
-	public function addStep($repairStep) {
142
-		if (is_string($repairStep)) {
143
-			try {
144
-				$s = \OC::$server->get($repairStep);
145
-			} catch (QueryException $e) {
146
-				if (class_exists($repairStep)) {
147
-					try {
148
-						// Last resort: hope there are no constructor arguments
149
-						$s = new $repairStep();
150
-					} catch (Throwable $inner) {
151
-						// Well, it was worth a try
152
-						throw new \Exception("Repair step '$repairStep' can't be instantiated: " . $e->getMessage(), 0, $e);
153
-					}
154
-				} else {
155
-					throw new \Exception("Repair step '$repairStep' is unknown", 0, $e);
156
-				}
157
-			}
135
+    /**
136
+     * Add repair step
137
+     *
138
+     * @param IRepairStep|string $repairStep repair step
139
+     * @throws \Exception
140
+     */
141
+    public function addStep($repairStep) {
142
+        if (is_string($repairStep)) {
143
+            try {
144
+                $s = \OC::$server->get($repairStep);
145
+            } catch (QueryException $e) {
146
+                if (class_exists($repairStep)) {
147
+                    try {
148
+                        // Last resort: hope there are no constructor arguments
149
+                        $s = new $repairStep();
150
+                    } catch (Throwable $inner) {
151
+                        // Well, it was worth a try
152
+                        throw new \Exception("Repair step '$repairStep' can't be instantiated: " . $e->getMessage(), 0, $e);
153
+                    }
154
+                } else {
155
+                    throw new \Exception("Repair step '$repairStep' is unknown", 0, $e);
156
+                }
157
+            }
158 158
 
159
-			if ($s instanceof IRepairStep) {
160
-				$this->repairSteps[] = $s;
161
-			} else {
162
-				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
163
-			}
164
-		} else {
165
-			$this->repairSteps[] = $repairStep;
166
-		}
167
-	}
159
+            if ($s instanceof IRepairStep) {
160
+                $this->repairSteps[] = $s;
161
+            } else {
162
+                throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
163
+            }
164
+        } else {
165
+            $this->repairSteps[] = $repairStep;
166
+        }
167
+    }
168 168
 
169
-	/**
170
-	 * Returns the default repair steps to be run on the
171
-	 * command line or after an upgrade.
172
-	 *
173
-	 * @return IRepairStep[]
174
-	 */
175
-	public static function getRepairSteps() {
176
-		return [
177
-			new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->getDatabaseConnection(), false),
178
-			new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
179
-			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
180
-			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
181
-			new MoveUpdaterStepFile(\OC::$server->getConfig()),
182
-			new MoveAvatars(
183
-				\OC::$server->getJobList(),
184
-				\OC::$server->getConfig()
185
-			),
186
-			new CleanPreviews(
187
-				\OC::$server->getJobList(),
188
-				\OC::$server->getUserManager(),
189
-				\OC::$server->getConfig()
190
-			),
191
-			new MigrateOauthTables(\OC::$server->get(Connection::class)),
192
-			new FixMountStorages(\OC::$server->getDatabaseConnection()),
193
-			new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
194
-			new AddLogRotateJob(\OC::$server->getJobList()),
195
-			new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(JSCombiner::class)),
196
-			new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
197
-			new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
198
-			new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
199
-			new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->get(LoggerInterface::class)),
200
-			new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
201
-			new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OC::$server->query(ITimeFactory::class)),
202
-			new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OC::$server->query(IManager::class)),
203
-			\OC::$server->query(ResetGeneratedAvatarFlag::class),
204
-			\OC::$server->query(EncryptionLegacyCipher::class),
205
-			\OC::$server->query(EncryptionMigration::class),
206
-			\OC::$server->get(ShippedDashboardEnable::class),
207
-			\OC::$server->get(AddBruteForceCleanupJob::class),
208
-			\OC::$server->get(AddCheckForUserCertificatesJob::class),
209
-			\OC::$server->get(RepairDavShares::class),
210
-			\OC::$server->get(LookupServerSendCheck::class),
211
-			\OC::$server->get(AddTokenCleanupJob::class),
212
-		];
213
-	}
169
+    /**
170
+     * Returns the default repair steps to be run on the
171
+     * command line or after an upgrade.
172
+     *
173
+     * @return IRepairStep[]
174
+     */
175
+    public static function getRepairSteps() {
176
+        return [
177
+            new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->getDatabaseConnection(), false),
178
+            new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
179
+            new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
180
+            new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
181
+            new MoveUpdaterStepFile(\OC::$server->getConfig()),
182
+            new MoveAvatars(
183
+                \OC::$server->getJobList(),
184
+                \OC::$server->getConfig()
185
+            ),
186
+            new CleanPreviews(
187
+                \OC::$server->getJobList(),
188
+                \OC::$server->getUserManager(),
189
+                \OC::$server->getConfig()
190
+            ),
191
+            new MigrateOauthTables(\OC::$server->get(Connection::class)),
192
+            new FixMountStorages(\OC::$server->getDatabaseConnection()),
193
+            new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
194
+            new AddLogRotateJob(\OC::$server->getJobList()),
195
+            new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(JSCombiner::class)),
196
+            new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
197
+            new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
198
+            new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
199
+            new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->get(LoggerInterface::class)),
200
+            new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
201
+            new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OC::$server->query(ITimeFactory::class)),
202
+            new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OC::$server->query(IManager::class)),
203
+            \OC::$server->query(ResetGeneratedAvatarFlag::class),
204
+            \OC::$server->query(EncryptionLegacyCipher::class),
205
+            \OC::$server->query(EncryptionMigration::class),
206
+            \OC::$server->get(ShippedDashboardEnable::class),
207
+            \OC::$server->get(AddBruteForceCleanupJob::class),
208
+            \OC::$server->get(AddCheckForUserCertificatesJob::class),
209
+            \OC::$server->get(RepairDavShares::class),
210
+            \OC::$server->get(LookupServerSendCheck::class),
211
+            \OC::$server->get(AddTokenCleanupJob::class),
212
+        ];
213
+    }
214 214
 
215
-	/**
216
-	 * Returns expensive repair steps to be run on the
217
-	 * command line with a special option.
218
-	 *
219
-	 * @return IRepairStep[]
220
-	 */
221
-	public static function getExpensiveRepairSteps() {
222
-		return [
223
-			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
224
-			\OC::$server->get(ValidatePhoneNumber::class),
225
-		];
226
-	}
215
+    /**
216
+     * Returns expensive repair steps to be run on the
217
+     * command line with a special option.
218
+     *
219
+     * @return IRepairStep[]
220
+     */
221
+    public static function getExpensiveRepairSteps() {
222
+        return [
223
+            new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
224
+            \OC::$server->get(ValidatePhoneNumber::class),
225
+        ];
226
+    }
227 227
 
228
-	/**
229
-	 * Returns the repair steps to be run before an
230
-	 * upgrade.
231
-	 *
232
-	 * @return IRepairStep[]
233
-	 */
234
-	public static function getBeforeUpgradeRepairSteps() {
235
-		/** @var Connection $connection */
236
-		$connection = \OC::$server->get(Connection::class);
237
-		/** @var ConnectionAdapter $connectionAdapter */
238
-		$connectionAdapter = \OC::$server->get(ConnectionAdapter::class);
239
-		$config = \OC::$server->getConfig();
240
-		$steps = [
241
-			new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), $connectionAdapter, true),
242
-			new SqliteAutoincrement($connection),
243
-			new SaveAccountsTableData($connectionAdapter, $config),
244
-			new DropAccountTermsTable($connectionAdapter),
245
-		];
228
+    /**
229
+     * Returns the repair steps to be run before an
230
+     * upgrade.
231
+     *
232
+     * @return IRepairStep[]
233
+     */
234
+    public static function getBeforeUpgradeRepairSteps() {
235
+        /** @var Connection $connection */
236
+        $connection = \OC::$server->get(Connection::class);
237
+        /** @var ConnectionAdapter $connectionAdapter */
238
+        $connectionAdapter = \OC::$server->get(ConnectionAdapter::class);
239
+        $config = \OC::$server->getConfig();
240
+        $steps = [
241
+            new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), $connectionAdapter, true),
242
+            new SqliteAutoincrement($connection),
243
+            new SaveAccountsTableData($connectionAdapter, $config),
244
+            new DropAccountTermsTable($connectionAdapter),
245
+        ];
246 246
 
247
-		return $steps;
248
-	}
247
+        return $steps;
248
+    }
249 249
 
250
-	/**
251
-	 * @param string $message
252
-	 */
253
-	public function info($message) {
254
-		// for now just emit as we did in the past
255
-		$this->dispatcher->dispatchTyped(new RepairInfoEvent($message));
256
-	}
250
+    /**
251
+     * @param string $message
252
+     */
253
+    public function info($message) {
254
+        // for now just emit as we did in the past
255
+        $this->dispatcher->dispatchTyped(new RepairInfoEvent($message));
256
+    }
257 257
 
258
-	/**
259
-	 * @param string $message
260
-	 */
261
-	public function warning($message) {
262
-		// for now just emit as we did in the past
263
-		$this->dispatcher->dispatchTyped(new RepairWarningEvent($message));
264
-	}
258
+    /**
259
+     * @param string $message
260
+     */
261
+    public function warning($message) {
262
+        // for now just emit as we did in the past
263
+        $this->dispatcher->dispatchTyped(new RepairWarningEvent($message));
264
+    }
265 265
 
266
-	/**
267
-	 * @param int $max
268
-	 */
269
-	public function startProgress($max = 0) {
270
-		// for now just emit as we did in the past
271
-		$this->dispatcher->dispatchTyped(new RepairStartEvent($max, $this->currentStep));
272
-	}
266
+    /**
267
+     * @param int $max
268
+     */
269
+    public function startProgress($max = 0) {
270
+        // for now just emit as we did in the past
271
+        $this->dispatcher->dispatchTyped(new RepairStartEvent($max, $this->currentStep));
272
+    }
273 273
 
274
-	/**
275
-	 * @param int $step number of step to advance
276
-	 * @param string $description
277
-	 */
278
-	public function advance($step = 1, $description = '') {
279
-		// for now just emit as we did in the past
280
-		$this->dispatcher->dispatchTyped(new RepairAdvanceEvent($step, $description));
281
-	}
274
+    /**
275
+     * @param int $step number of step to advance
276
+     * @param string $description
277
+     */
278
+    public function advance($step = 1, $description = '') {
279
+        // for now just emit as we did in the past
280
+        $this->dispatcher->dispatchTyped(new RepairAdvanceEvent($step, $description));
281
+    }
282 282
 
283
-	/**
284
-	 * @param int $max
285
-	 */
286
-	public function finishProgress() {
287
-		// for now just emit as we did in the past
288
-		$this->dispatcher->dispatchTyped(new RepairFinishEvent());
289
-	}
283
+    /**
284
+     * @param int $max
285
+     */
286
+    public function finishProgress() {
287
+        // for now just emit as we did in the past
288
+        $this->dispatcher->dispatchTyped(new RepairFinishEvent());
289
+    }
290 290
 }
Please login to merge, or discard this patch.
lib/private/App/AppStore/Bundles/BundleFetcher.php 1 patch
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -27,39 +27,39 @@
 block discarded – undo
27 27
 use OCP\IL10N;
28 28
 
29 29
 class BundleFetcher {
30
-	private IL10N $l10n;
30
+    private IL10N $l10n;
31 31
 
32
-	public function __construct(IL10N $l10n) {
33
-		$this->l10n = $l10n;
34
-	}
32
+    public function __construct(IL10N $l10n) {
33
+        $this->l10n = $l10n;
34
+    }
35 35
 
36
-	/**
37
-	 * @return Bundle[]
38
-	 */
39
-	public function getBundles(): array {
40
-		return [
41
-			new EnterpriseBundle($this->l10n),
42
-			new HubBundle($this->l10n),
43
-			new GroupwareBundle($this->l10n),
44
-			new SocialSharingBundle($this->l10n),
45
-			new EducationBundle($this->l10n),
46
-		];
47
-	}
36
+    /**
37
+     * @return Bundle[]
38
+     */
39
+    public function getBundles(): array {
40
+        return [
41
+            new EnterpriseBundle($this->l10n),
42
+            new HubBundle($this->l10n),
43
+            new GroupwareBundle($this->l10n),
44
+            new SocialSharingBundle($this->l10n),
45
+            new EducationBundle($this->l10n),
46
+        ];
47
+    }
48 48
 
49
-	/**
50
-	 * Get the bundle with the specified identifier
51
-	 *
52
-	 * @param string $identifier
53
-	 * @return Bundle
54
-	 * @throws \BadMethodCallException If the bundle does not exist
55
-	 */
56
-	public function getBundleByIdentifier(string $identifier): Bundle {
57
-		foreach ($this->getBundles() as $bundle) {
58
-			if ($bundle->getIdentifier() === $identifier) {
59
-				return $bundle;
60
-			}
61
-		}
49
+    /**
50
+     * Get the bundle with the specified identifier
51
+     *
52
+     * @param string $identifier
53
+     * @return Bundle
54
+     * @throws \BadMethodCallException If the bundle does not exist
55
+     */
56
+    public function getBundleByIdentifier(string $identifier): Bundle {
57
+        foreach ($this->getBundles() as $bundle) {
58
+            if ($bundle->getIdentifier() === $identifier) {
59
+                return $bundle;
60
+            }
61
+        }
62 62
 
63
-		throw new \BadMethodCallException('Bundle with specified identifier does not exist');
64
-	}
63
+        throw new \BadMethodCallException('Bundle with specified identifier does not exist');
64
+    }
65 65
 }
Please login to merge, or discard this patch.
lib/private/Setup.php 1 patch
Indentation   +532 added lines, -532 removed lines patch added patch discarded remove patch
@@ -64,536 +64,536 @@
 block discarded – undo
64 64
 use Psr\Log\LoggerInterface;
65 65
 
66 66
 class Setup {
67
-	/** @var SystemConfig */
68
-	protected $config;
69
-	/** @var IniGetWrapper */
70
-	protected $iniWrapper;
71
-	/** @var IL10N */
72
-	protected $l10n;
73
-	/** @var Defaults */
74
-	protected $defaults;
75
-	/** @var LoggerInterface */
76
-	protected $logger;
77
-	/** @var ISecureRandom */
78
-	protected $random;
79
-	/** @var Installer */
80
-	protected $installer;
81
-
82
-	public function __construct(
83
-		SystemConfig $config,
84
-		IniGetWrapper $iniWrapper,
85
-		IL10N $l10n,
86
-		Defaults $defaults,
87
-		LoggerInterface $logger,
88
-		ISecureRandom $random,
89
-		Installer $installer
90
-	) {
91
-		$this->config = $config;
92
-		$this->iniWrapper = $iniWrapper;
93
-		$this->l10n = $l10n;
94
-		$this->defaults = $defaults;
95
-		$this->logger = $logger;
96
-		$this->random = $random;
97
-		$this->installer = $installer;
98
-	}
99
-
100
-	protected static $dbSetupClasses = [
101
-		'mysql' => \OC\Setup\MySQL::class,
102
-		'pgsql' => \OC\Setup\PostgreSQL::class,
103
-		'oci' => \OC\Setup\OCI::class,
104
-		'sqlite' => \OC\Setup\Sqlite::class,
105
-		'sqlite3' => \OC\Setup\Sqlite::class,
106
-	];
107
-
108
-	/**
109
-	 * Wrapper around the "class_exists" PHP function to be able to mock it
110
-	 *
111
-	 * @param string $name
112
-	 * @return bool
113
-	 */
114
-	protected function class_exists($name) {
115
-		return class_exists($name);
116
-	}
117
-
118
-	/**
119
-	 * Wrapper around the "is_callable" PHP function to be able to mock it
120
-	 *
121
-	 * @param string $name
122
-	 * @return bool
123
-	 */
124
-	protected function is_callable($name) {
125
-		return is_callable($name);
126
-	}
127
-
128
-	/**
129
-	 * Wrapper around \PDO::getAvailableDrivers
130
-	 *
131
-	 * @return array
132
-	 */
133
-	protected function getAvailableDbDriversForPdo() {
134
-		if (class_exists(\PDO::class)) {
135
-			return \PDO::getAvailableDrivers();
136
-		}
137
-		return [];
138
-	}
139
-
140
-	/**
141
-	 * Get the available and supported databases of this instance
142
-	 *
143
-	 * @param bool $allowAllDatabases
144
-	 * @return array
145
-	 * @throws Exception
146
-	 */
147
-	public function getSupportedDatabases($allowAllDatabases = false) {
148
-		$availableDatabases = [
149
-			'sqlite' => [
150
-				'type' => 'pdo',
151
-				'call' => 'sqlite',
152
-				'name' => 'SQLite',
153
-			],
154
-			'mysql' => [
155
-				'type' => 'pdo',
156
-				'call' => 'mysql',
157
-				'name' => 'MySQL/MariaDB',
158
-			],
159
-			'pgsql' => [
160
-				'type' => 'pdo',
161
-				'call' => 'pgsql',
162
-				'name' => 'PostgreSQL',
163
-			],
164
-			'oci' => [
165
-				'type' => 'function',
166
-				'call' => 'oci_connect',
167
-				'name' => 'Oracle',
168
-			],
169
-		];
170
-		if ($allowAllDatabases) {
171
-			$configuredDatabases = array_keys($availableDatabases);
172
-		} else {
173
-			$configuredDatabases = $this->config->getValue('supportedDatabases',
174
-				['sqlite', 'mysql', 'pgsql']);
175
-		}
176
-		if (!is_array($configuredDatabases)) {
177
-			throw new Exception('Supported databases are not properly configured.');
178
-		}
179
-
180
-		$supportedDatabases = [];
181
-
182
-		foreach ($configuredDatabases as $database) {
183
-			if (array_key_exists($database, $availableDatabases)) {
184
-				$working = false;
185
-				$type = $availableDatabases[$database]['type'];
186
-				$call = $availableDatabases[$database]['call'];
187
-
188
-				if ($type === 'function') {
189
-					$working = $this->is_callable($call);
190
-				} elseif ($type === 'pdo') {
191
-					$working = in_array($call, $this->getAvailableDbDriversForPdo(), true);
192
-				}
193
-				if ($working) {
194
-					$supportedDatabases[$database] = $availableDatabases[$database]['name'];
195
-				}
196
-			}
197
-		}
198
-
199
-		return $supportedDatabases;
200
-	}
201
-
202
-	/**
203
-	 * Gathers system information like database type and does
204
-	 * a few system checks.
205
-	 *
206
-	 * @return array of system info, including an "errors" value
207
-	 * in case of errors/warnings
208
-	 */
209
-	public function getSystemInfo($allowAllDatabases = false) {
210
-		$databases = $this->getSupportedDatabases($allowAllDatabases);
211
-
212
-		$dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data');
213
-
214
-		$errors = [];
215
-
216
-		// Create data directory to test whether the .htaccess works
217
-		// Notice that this is not necessarily the same data directory as the one
218
-		// that will effectively be used.
219
-		if (!file_exists($dataDir)) {
220
-			@mkdir($dataDir);
221
-		}
222
-		$htAccessWorking = true;
223
-		if (is_dir($dataDir) && is_writable($dataDir)) {
224
-			// Protect data directory here, so we can test if the protection is working
225
-			self::protectDataDirectory();
226
-
227
-			try {
228
-				$util = new \OC_Util();
229
-				$htAccessWorking = $util->isHtaccessWorking(\OC::$server->getConfig());
230
-			} catch (\OCP\HintException $e) {
231
-				$errors[] = [
232
-					'error' => $e->getMessage(),
233
-					'exception' => $e,
234
-					'hint' => $e->getHint(),
235
-				];
236
-				$htAccessWorking = false;
237
-			}
238
-		}
239
-
240
-		if (\OC_Util::runningOnMac()) {
241
-			$errors[] = [
242
-				'error' => $this->l10n->t(
243
-					'Mac OS X is not supported and %s will not work properly on this platform. ' .
244
-					'Use it at your own risk! ',
245
-					[$this->defaults->getProductName()]
246
-				),
247
-				'hint' => $this->l10n->t('For the best results, please consider using a GNU/Linux server instead.'),
248
-			];
249
-		}
250
-
251
-		if ($this->iniWrapper->getString('open_basedir') !== '' && PHP_INT_SIZE === 4) {
252
-			$errors[] = [
253
-				'error' => $this->l10n->t(
254
-					'It seems that this %s instance is running on a 32-bit PHP environment and the open_basedir has been configured in php.ini. ' .
255
-					'This will lead to problems with files over 4 GB and is highly discouraged.',
256
-					[$this->defaults->getProductName()]
257
-				),
258
-				'hint' => $this->l10n->t('Please remove the open_basedir setting within your php.ini or switch to 64-bit PHP.'),
259
-			];
260
-		}
261
-
262
-		return [
263
-			'hasSQLite' => isset($databases['sqlite']),
264
-			'hasMySQL' => isset($databases['mysql']),
265
-			'hasPostgreSQL' => isset($databases['pgsql']),
266
-			'hasOracle' => isset($databases['oci']),
267
-			'databases' => $databases,
268
-			'directory' => $dataDir,
269
-			'htaccessWorking' => $htAccessWorking,
270
-			'errors' => $errors,
271
-		];
272
-	}
273
-
274
-	/**
275
-	 * @param $options
276
-	 * @return array
277
-	 */
278
-	public function install($options) {
279
-		$l = $this->l10n;
280
-
281
-		$error = [];
282
-		$dbType = $options['dbtype'];
283
-
284
-		if (empty($options['adminlogin'])) {
285
-			$error[] = $l->t('Set an admin username.');
286
-		}
287
-		if (empty($options['adminpass'])) {
288
-			$error[] = $l->t('Set an admin password.');
289
-		}
290
-		if (empty($options['directory'])) {
291
-			$options['directory'] = \OC::$SERVERROOT . "/data";
292
-		}
293
-
294
-		if (!isset(self::$dbSetupClasses[$dbType])) {
295
-			$dbType = 'sqlite';
296
-		}
297
-
298
-		$username = htmlspecialchars_decode($options['adminlogin']);
299
-		$password = htmlspecialchars_decode($options['adminpass']);
300
-		$dataDir = htmlspecialchars_decode($options['directory']);
301
-
302
-		$class = self::$dbSetupClasses[$dbType];
303
-		/** @var \OC\Setup\AbstractDatabase $dbSetup */
304
-		$dbSetup = new $class($l, $this->config, $this->logger, $this->random);
305
-		$error = array_merge($error, $dbSetup->validate($options));
306
-
307
-		// validate the data directory
308
-		if ((!is_dir($dataDir) && !mkdir($dataDir)) || !is_writable($dataDir)) {
309
-			$error[] = $l->t("Cannot create or write into the data directory %s", [$dataDir]);
310
-		}
311
-
312
-		if (!empty($error)) {
313
-			return $error;
314
-		}
315
-
316
-		$request = \OC::$server->getRequest();
317
-
318
-		//no errors, good
319
-		if (isset($options['trusted_domains'])
320
-			&& is_array($options['trusted_domains'])) {
321
-			$trustedDomains = $options['trusted_domains'];
322
-		} else {
323
-			$trustedDomains = [$request->getInsecureServerHost()];
324
-		}
325
-
326
-		//use sqlite3 when available, otherwise sqlite2 will be used.
327
-		if ($dbType === 'sqlite' && class_exists('SQLite3')) {
328
-			$dbType = 'sqlite3';
329
-		}
330
-
331
-		//generate a random salt that is used to salt the local user passwords
332
-		$salt = $this->random->generate(30);
333
-		// generate a secret
334
-		$secret = $this->random->generate(48);
335
-
336
-		//write the config file
337
-		$newConfigValues = [
338
-			'passwordsalt' => $salt,
339
-			'secret' => $secret,
340
-			'trusted_domains' => $trustedDomains,
341
-			'datadirectory' => $dataDir,
342
-			'dbtype' => $dbType,
343
-			'version' => implode('.', \OCP\Util::getVersion()),
344
-		];
345
-
346
-		if ($this->config->getValue('overwrite.cli.url', null) === null) {
347
-			$newConfigValues['overwrite.cli.url'] = $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT;
348
-		}
349
-
350
-		$this->config->setValues($newConfigValues);
351
-
352
-		$dbSetup->initialize($options);
353
-		try {
354
-			$dbSetup->setupDatabase($username);
355
-		} catch (\OC\DatabaseSetupException $e) {
356
-			$error[] = [
357
-				'error' => $e->getMessage(),
358
-				'exception' => $e,
359
-				'hint' => $e->getHint(),
360
-			];
361
-			return $error;
362
-		} catch (Exception $e) {
363
-			$error[] = [
364
-				'error' => 'Error while trying to create admin user: ' . $e->getMessage(),
365
-				'exception' => $e,
366
-				'hint' => '',
367
-			];
368
-			return $error;
369
-		}
370
-		try {
371
-			// apply necessary migrations
372
-			$dbSetup->runMigrations();
373
-		} catch (Exception $e) {
374
-			$error[] = [
375
-				'error' => 'Error while trying to initialise the database: ' . $e->getMessage(),
376
-				'exception' => $e,
377
-				'hint' => '',
378
-			];
379
-			return $error;
380
-		}
381
-
382
-		//create the user and group
383
-		$user = null;
384
-		try {
385
-			$user = \OC::$server->getUserManager()->createUser($username, $password);
386
-			if (!$user) {
387
-				$error[] = "User <$username> could not be created.";
388
-			}
389
-		} catch (Exception $exception) {
390
-			$error[] = $exception->getMessage();
391
-		}
392
-
393
-		if (empty($error)) {
394
-			$config = \OC::$server->getConfig();
395
-			$config->setAppValue('core', 'installedat', microtime(true));
396
-			$config->setAppValue('core', 'lastupdatedat', microtime(true));
397
-			$config->setAppValue('core', 'vendor', $this->getVendor());
398
-
399
-			$group = \OC::$server->getGroupManager()->createGroup('admin');
400
-			if ($group instanceof IGroup) {
401
-				$group->addUser($user);
402
-			}
403
-
404
-			// Install shipped apps and specified app bundles
405
-			Installer::installShippedApps();
406
-
407
-			// create empty file in data dir, so we can later find
408
-			// out that this is indeed an ownCloud data directory
409
-			file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
410
-
411
-			// Update .htaccess files
412
-			self::updateHtaccess();
413
-			self::protectDataDirectory();
414
-
415
-			self::installBackgroundJobs();
416
-
417
-			//and we are done
418
-			$config->setSystemValue('installed', true);
419
-
420
-			$bootstrapCoordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class);
421
-			$bootstrapCoordinator->runInitialRegistration();
422
-
423
-			// Create a session token for the newly created user
424
-			// The token provider requires a working db, so it's not injected on setup
425
-			/* @var $userSession User\Session */
426
-			$userSession = \OC::$server->getUserSession();
427
-			$provider = \OC::$server->query(PublicKeyTokenProvider::class);
428
-			$userSession->setTokenProvider($provider);
429
-			$userSession->login($username, $password);
430
-			$userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password);
431
-
432
-			$session = $userSession->getSession();
433
-			$session->set('last-password-confirm', \OC::$server->query(ITimeFactory::class)->getTime());
434
-
435
-			// Set email for admin
436
-			if (!empty($options['adminemail'])) {
437
-				$user->setSystemEMailAddress($options['adminemail']);
438
-			}
439
-		}
440
-
441
-		return $error;
442
-	}
443
-
444
-	public static function installBackgroundJobs() {
445
-		$jobList = \OC::$server->getJobList();
446
-		$jobList->add(TokenCleanupJob::class);
447
-		$jobList->add(Rotate::class);
448
-		$jobList->add(BackgroundCleanupJob::class);
449
-	}
450
-
451
-	/**
452
-	 * @return string Absolute path to htaccess
453
-	 */
454
-	private function pathToHtaccess() {
455
-		return \OC::$SERVERROOT . '/.htaccess';
456
-	}
457
-
458
-	/**
459
-	 * Find webroot from config
460
-	 *
461
-	 * @param SystemConfig $config
462
-	 * @return string
463
-	 * @throws InvalidArgumentException when invalid value for overwrite.cli.url
464
-	 */
465
-	private static function findWebRoot(SystemConfig $config): string {
466
-		// For CLI read the value from overwrite.cli.url
467
-		if (\OC::$CLI) {
468
-			$webRoot = $config->getValue('overwrite.cli.url', '');
469
-			if ($webRoot === '') {
470
-				throw new InvalidArgumentException('overwrite.cli.url is empty');
471
-			}
472
-			if (!filter_var($webRoot, FILTER_VALIDATE_URL)) {
473
-				throw new InvalidArgumentException('invalid value for overwrite.cli.url');
474
-			}
475
-			$webRoot = rtrim((parse_url($webRoot, PHP_URL_PATH) ?? ''), '/');
476
-		} else {
477
-			$webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';
478
-		}
479
-
480
-		return $webRoot;
481
-	}
482
-
483
-	/**
484
-	 * Append the correct ErrorDocument path for Apache hosts
485
-	 *
486
-	 * @return bool True when success, False otherwise
487
-	 * @throws \OCP\AppFramework\QueryException
488
-	 */
489
-	public static function updateHtaccess() {
490
-		$config = \OC::$server->getSystemConfig();
491
-
492
-		try {
493
-			$webRoot = self::findWebRoot($config);
494
-		} catch (InvalidArgumentException $e) {
495
-			return false;
496
-		}
497
-
498
-		$setupHelper = new \OC\Setup(
499
-			$config,
500
-			\OC::$server->get(IniGetWrapper::class),
501
-			\OC::$server->getL10N('lib'),
502
-			\OC::$server->query(Defaults::class),
503
-			\OC::$server->get(LoggerInterface::class),
504
-			\OC::$server->getSecureRandom(),
505
-			\OC::$server->query(Installer::class)
506
-		);
507
-
508
-		$htaccessContent = file_get_contents($setupHelper->pathToHtaccess());
509
-		$content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";
510
-		$htaccessContent = explode($content, $htaccessContent, 2)[0];
511
-
512
-		//custom 403 error page
513
-		$content .= "\nErrorDocument 403 " . $webRoot . '/';
514
-
515
-		//custom 404 error page
516
-		$content .= "\nErrorDocument 404 " . $webRoot . '/';
517
-
518
-		// Add rewrite rules if the RewriteBase is configured
519
-		$rewriteBase = $config->getValue('htaccess.RewriteBase', '');
520
-		if ($rewriteBase !== '') {
521
-			$content .= "\n<IfModule mod_rewrite.c>";
522
-			$content .= "\n  Options -MultiViews";
523
-			$content .= "\n  RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]";
524
-			$content .= "\n  RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]";
525
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !\\.(css|js|svg|gif|png|html|ttf|woff2?|ico|jpg|jpeg|map|webm|mp4|mp3|ogg|wav|wasm|tflite)$";
526
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/core/ajax/update\\.php";
527
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/core/img/(favicon\\.ico|manifest\\.json)$";
528
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/(cron|public|remote|status)\\.php";
529
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs/v(1|2)\\.php";
530
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/robots\\.txt";
531
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/(ocm-provider|ocs-provider|updater)/";
532
-			$content .= "\n  RewriteCond %{REQUEST_URI} !^/\\.well-known/(acme-challenge|pki-validation)/.*";
533
-			$content .= "\n  RewriteCond %{REQUEST_FILENAME} !/richdocumentscode(_arm64)?/proxy.php$";
534
-			$content .= "\n  RewriteRule . index.php [PT,E=PATH_INFO:$1]";
535
-			$content .= "\n  RewriteBase " . $rewriteBase;
536
-			$content .= "\n  <IfModule mod_env.c>";
537
-			$content .= "\n    SetEnv front_controller_active true";
538
-			$content .= "\n    <IfModule mod_dir.c>";
539
-			$content .= "\n      DirectorySlash off";
540
-			$content .= "\n    </IfModule>";
541
-			$content .= "\n  </IfModule>";
542
-			$content .= "\n</IfModule>";
543
-		}
544
-
545
-		if ($content !== '') {
546
-			//suppress errors in case we don't have permissions for it
547
-			return (bool)@file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent . $content . "\n");
548
-		}
549
-
550
-		return false;
551
-	}
552
-
553
-	public static function protectDataDirectory() {
554
-		//Require all denied
555
-		$now = date('Y-m-d H:i:s');
556
-		$content = "# Generated by Nextcloud on $now\n";
557
-		$content .= "# Section for Apache 2.4 to 2.6\n";
558
-		$content .= "<IfModule mod_authz_core.c>\n";
559
-		$content .= "  Require all denied\n";
560
-		$content .= "</IfModule>\n";
561
-		$content .= "<IfModule mod_access_compat.c>\n";
562
-		$content .= "  Order Allow,Deny\n";
563
-		$content .= "  Deny from all\n";
564
-		$content .= "  Satisfy All\n";
565
-		$content .= "</IfModule>\n\n";
566
-		$content .= "# Section for Apache 2.2\n";
567
-		$content .= "<IfModule !mod_authz_core.c>\n";
568
-		$content .= "  <IfModule !mod_access_compat.c>\n";
569
-		$content .= "    <IfModule mod_authz_host.c>\n";
570
-		$content .= "      Order Allow,Deny\n";
571
-		$content .= "      Deny from all\n";
572
-		$content .= "    </IfModule>\n";
573
-		$content .= "    Satisfy All\n";
574
-		$content .= "  </IfModule>\n";
575
-		$content .= "</IfModule>\n\n";
576
-		$content .= "# Section for Apache 2.2 to 2.6\n";
577
-		$content .= "<IfModule mod_autoindex.c>\n";
578
-		$content .= "  IndexIgnore *\n";
579
-		$content .= "</IfModule>";
580
-
581
-		$baseDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data');
582
-		file_put_contents($baseDir . '/.htaccess', $content);
583
-		file_put_contents($baseDir . '/index.html', '');
584
-	}
585
-
586
-	/**
587
-	 * Return vendor from which this version was published
588
-	 *
589
-	 * @return string Get the vendor
590
-	 *
591
-	 * Copy of \OC\Updater::getVendor()
592
-	 */
593
-	private function getVendor() {
594
-		// this should really be a JSON file
595
-		require \OC::$SERVERROOT . '/version.php';
596
-		/** @var string $vendor */
597
-		return (string)$vendor;
598
-	}
67
+    /** @var SystemConfig */
68
+    protected $config;
69
+    /** @var IniGetWrapper */
70
+    protected $iniWrapper;
71
+    /** @var IL10N */
72
+    protected $l10n;
73
+    /** @var Defaults */
74
+    protected $defaults;
75
+    /** @var LoggerInterface */
76
+    protected $logger;
77
+    /** @var ISecureRandom */
78
+    protected $random;
79
+    /** @var Installer */
80
+    protected $installer;
81
+
82
+    public function __construct(
83
+        SystemConfig $config,
84
+        IniGetWrapper $iniWrapper,
85
+        IL10N $l10n,
86
+        Defaults $defaults,
87
+        LoggerInterface $logger,
88
+        ISecureRandom $random,
89
+        Installer $installer
90
+    ) {
91
+        $this->config = $config;
92
+        $this->iniWrapper = $iniWrapper;
93
+        $this->l10n = $l10n;
94
+        $this->defaults = $defaults;
95
+        $this->logger = $logger;
96
+        $this->random = $random;
97
+        $this->installer = $installer;
98
+    }
99
+
100
+    protected static $dbSetupClasses = [
101
+        'mysql' => \OC\Setup\MySQL::class,
102
+        'pgsql' => \OC\Setup\PostgreSQL::class,
103
+        'oci' => \OC\Setup\OCI::class,
104
+        'sqlite' => \OC\Setup\Sqlite::class,
105
+        'sqlite3' => \OC\Setup\Sqlite::class,
106
+    ];
107
+
108
+    /**
109
+     * Wrapper around the "class_exists" PHP function to be able to mock it
110
+     *
111
+     * @param string $name
112
+     * @return bool
113
+     */
114
+    protected function class_exists($name) {
115
+        return class_exists($name);
116
+    }
117
+
118
+    /**
119
+     * Wrapper around the "is_callable" PHP function to be able to mock it
120
+     *
121
+     * @param string $name
122
+     * @return bool
123
+     */
124
+    protected function is_callable($name) {
125
+        return is_callable($name);
126
+    }
127
+
128
+    /**
129
+     * Wrapper around \PDO::getAvailableDrivers
130
+     *
131
+     * @return array
132
+     */
133
+    protected function getAvailableDbDriversForPdo() {
134
+        if (class_exists(\PDO::class)) {
135
+            return \PDO::getAvailableDrivers();
136
+        }
137
+        return [];
138
+    }
139
+
140
+    /**
141
+     * Get the available and supported databases of this instance
142
+     *
143
+     * @param bool $allowAllDatabases
144
+     * @return array
145
+     * @throws Exception
146
+     */
147
+    public function getSupportedDatabases($allowAllDatabases = false) {
148
+        $availableDatabases = [
149
+            'sqlite' => [
150
+                'type' => 'pdo',
151
+                'call' => 'sqlite',
152
+                'name' => 'SQLite',
153
+            ],
154
+            'mysql' => [
155
+                'type' => 'pdo',
156
+                'call' => 'mysql',
157
+                'name' => 'MySQL/MariaDB',
158
+            ],
159
+            'pgsql' => [
160
+                'type' => 'pdo',
161
+                'call' => 'pgsql',
162
+                'name' => 'PostgreSQL',
163
+            ],
164
+            'oci' => [
165
+                'type' => 'function',
166
+                'call' => 'oci_connect',
167
+                'name' => 'Oracle',
168
+            ],
169
+        ];
170
+        if ($allowAllDatabases) {
171
+            $configuredDatabases = array_keys($availableDatabases);
172
+        } else {
173
+            $configuredDatabases = $this->config->getValue('supportedDatabases',
174
+                ['sqlite', 'mysql', 'pgsql']);
175
+        }
176
+        if (!is_array($configuredDatabases)) {
177
+            throw new Exception('Supported databases are not properly configured.');
178
+        }
179
+
180
+        $supportedDatabases = [];
181
+
182
+        foreach ($configuredDatabases as $database) {
183
+            if (array_key_exists($database, $availableDatabases)) {
184
+                $working = false;
185
+                $type = $availableDatabases[$database]['type'];
186
+                $call = $availableDatabases[$database]['call'];
187
+
188
+                if ($type === 'function') {
189
+                    $working = $this->is_callable($call);
190
+                } elseif ($type === 'pdo') {
191
+                    $working = in_array($call, $this->getAvailableDbDriversForPdo(), true);
192
+                }
193
+                if ($working) {
194
+                    $supportedDatabases[$database] = $availableDatabases[$database]['name'];
195
+                }
196
+            }
197
+        }
198
+
199
+        return $supportedDatabases;
200
+    }
201
+
202
+    /**
203
+     * Gathers system information like database type and does
204
+     * a few system checks.
205
+     *
206
+     * @return array of system info, including an "errors" value
207
+     * in case of errors/warnings
208
+     */
209
+    public function getSystemInfo($allowAllDatabases = false) {
210
+        $databases = $this->getSupportedDatabases($allowAllDatabases);
211
+
212
+        $dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data');
213
+
214
+        $errors = [];
215
+
216
+        // Create data directory to test whether the .htaccess works
217
+        // Notice that this is not necessarily the same data directory as the one
218
+        // that will effectively be used.
219
+        if (!file_exists($dataDir)) {
220
+            @mkdir($dataDir);
221
+        }
222
+        $htAccessWorking = true;
223
+        if (is_dir($dataDir) && is_writable($dataDir)) {
224
+            // Protect data directory here, so we can test if the protection is working
225
+            self::protectDataDirectory();
226
+
227
+            try {
228
+                $util = new \OC_Util();
229
+                $htAccessWorking = $util->isHtaccessWorking(\OC::$server->getConfig());
230
+            } catch (\OCP\HintException $e) {
231
+                $errors[] = [
232
+                    'error' => $e->getMessage(),
233
+                    'exception' => $e,
234
+                    'hint' => $e->getHint(),
235
+                ];
236
+                $htAccessWorking = false;
237
+            }
238
+        }
239
+
240
+        if (\OC_Util::runningOnMac()) {
241
+            $errors[] = [
242
+                'error' => $this->l10n->t(
243
+                    'Mac OS X is not supported and %s will not work properly on this platform. ' .
244
+                    'Use it at your own risk! ',
245
+                    [$this->defaults->getProductName()]
246
+                ),
247
+                'hint' => $this->l10n->t('For the best results, please consider using a GNU/Linux server instead.'),
248
+            ];
249
+        }
250
+
251
+        if ($this->iniWrapper->getString('open_basedir') !== '' && PHP_INT_SIZE === 4) {
252
+            $errors[] = [
253
+                'error' => $this->l10n->t(
254
+                    'It seems that this %s instance is running on a 32-bit PHP environment and the open_basedir has been configured in php.ini. ' .
255
+                    'This will lead to problems with files over 4 GB and is highly discouraged.',
256
+                    [$this->defaults->getProductName()]
257
+                ),
258
+                'hint' => $this->l10n->t('Please remove the open_basedir setting within your php.ini or switch to 64-bit PHP.'),
259
+            ];
260
+        }
261
+
262
+        return [
263
+            'hasSQLite' => isset($databases['sqlite']),
264
+            'hasMySQL' => isset($databases['mysql']),
265
+            'hasPostgreSQL' => isset($databases['pgsql']),
266
+            'hasOracle' => isset($databases['oci']),
267
+            'databases' => $databases,
268
+            'directory' => $dataDir,
269
+            'htaccessWorking' => $htAccessWorking,
270
+            'errors' => $errors,
271
+        ];
272
+    }
273
+
274
+    /**
275
+     * @param $options
276
+     * @return array
277
+     */
278
+    public function install($options) {
279
+        $l = $this->l10n;
280
+
281
+        $error = [];
282
+        $dbType = $options['dbtype'];
283
+
284
+        if (empty($options['adminlogin'])) {
285
+            $error[] = $l->t('Set an admin username.');
286
+        }
287
+        if (empty($options['adminpass'])) {
288
+            $error[] = $l->t('Set an admin password.');
289
+        }
290
+        if (empty($options['directory'])) {
291
+            $options['directory'] = \OC::$SERVERROOT . "/data";
292
+        }
293
+
294
+        if (!isset(self::$dbSetupClasses[$dbType])) {
295
+            $dbType = 'sqlite';
296
+        }
297
+
298
+        $username = htmlspecialchars_decode($options['adminlogin']);
299
+        $password = htmlspecialchars_decode($options['adminpass']);
300
+        $dataDir = htmlspecialchars_decode($options['directory']);
301
+
302
+        $class = self::$dbSetupClasses[$dbType];
303
+        /** @var \OC\Setup\AbstractDatabase $dbSetup */
304
+        $dbSetup = new $class($l, $this->config, $this->logger, $this->random);
305
+        $error = array_merge($error, $dbSetup->validate($options));
306
+
307
+        // validate the data directory
308
+        if ((!is_dir($dataDir) && !mkdir($dataDir)) || !is_writable($dataDir)) {
309
+            $error[] = $l->t("Cannot create or write into the data directory %s", [$dataDir]);
310
+        }
311
+
312
+        if (!empty($error)) {
313
+            return $error;
314
+        }
315
+
316
+        $request = \OC::$server->getRequest();
317
+
318
+        //no errors, good
319
+        if (isset($options['trusted_domains'])
320
+            && is_array($options['trusted_domains'])) {
321
+            $trustedDomains = $options['trusted_domains'];
322
+        } else {
323
+            $trustedDomains = [$request->getInsecureServerHost()];
324
+        }
325
+
326
+        //use sqlite3 when available, otherwise sqlite2 will be used.
327
+        if ($dbType === 'sqlite' && class_exists('SQLite3')) {
328
+            $dbType = 'sqlite3';
329
+        }
330
+
331
+        //generate a random salt that is used to salt the local user passwords
332
+        $salt = $this->random->generate(30);
333
+        // generate a secret
334
+        $secret = $this->random->generate(48);
335
+
336
+        //write the config file
337
+        $newConfigValues = [
338
+            'passwordsalt' => $salt,
339
+            'secret' => $secret,
340
+            'trusted_domains' => $trustedDomains,
341
+            'datadirectory' => $dataDir,
342
+            'dbtype' => $dbType,
343
+            'version' => implode('.', \OCP\Util::getVersion()),
344
+        ];
345
+
346
+        if ($this->config->getValue('overwrite.cli.url', null) === null) {
347
+            $newConfigValues['overwrite.cli.url'] = $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT;
348
+        }
349
+
350
+        $this->config->setValues($newConfigValues);
351
+
352
+        $dbSetup->initialize($options);
353
+        try {
354
+            $dbSetup->setupDatabase($username);
355
+        } catch (\OC\DatabaseSetupException $e) {
356
+            $error[] = [
357
+                'error' => $e->getMessage(),
358
+                'exception' => $e,
359
+                'hint' => $e->getHint(),
360
+            ];
361
+            return $error;
362
+        } catch (Exception $e) {
363
+            $error[] = [
364
+                'error' => 'Error while trying to create admin user: ' . $e->getMessage(),
365
+                'exception' => $e,
366
+                'hint' => '',
367
+            ];
368
+            return $error;
369
+        }
370
+        try {
371
+            // apply necessary migrations
372
+            $dbSetup->runMigrations();
373
+        } catch (Exception $e) {
374
+            $error[] = [
375
+                'error' => 'Error while trying to initialise the database: ' . $e->getMessage(),
376
+                'exception' => $e,
377
+                'hint' => '',
378
+            ];
379
+            return $error;
380
+        }
381
+
382
+        //create the user and group
383
+        $user = null;
384
+        try {
385
+            $user = \OC::$server->getUserManager()->createUser($username, $password);
386
+            if (!$user) {
387
+                $error[] = "User <$username> could not be created.";
388
+            }
389
+        } catch (Exception $exception) {
390
+            $error[] = $exception->getMessage();
391
+        }
392
+
393
+        if (empty($error)) {
394
+            $config = \OC::$server->getConfig();
395
+            $config->setAppValue('core', 'installedat', microtime(true));
396
+            $config->setAppValue('core', 'lastupdatedat', microtime(true));
397
+            $config->setAppValue('core', 'vendor', $this->getVendor());
398
+
399
+            $group = \OC::$server->getGroupManager()->createGroup('admin');
400
+            if ($group instanceof IGroup) {
401
+                $group->addUser($user);
402
+            }
403
+
404
+            // Install shipped apps and specified app bundles
405
+            Installer::installShippedApps();
406
+
407
+            // create empty file in data dir, so we can later find
408
+            // out that this is indeed an ownCloud data directory
409
+            file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
410
+
411
+            // Update .htaccess files
412
+            self::updateHtaccess();
413
+            self::protectDataDirectory();
414
+
415
+            self::installBackgroundJobs();
416
+
417
+            //and we are done
418
+            $config->setSystemValue('installed', true);
419
+
420
+            $bootstrapCoordinator = \OC::$server->query(\OC\AppFramework\Bootstrap\Coordinator::class);
421
+            $bootstrapCoordinator->runInitialRegistration();
422
+
423
+            // Create a session token for the newly created user
424
+            // The token provider requires a working db, so it's not injected on setup
425
+            /* @var $userSession User\Session */
426
+            $userSession = \OC::$server->getUserSession();
427
+            $provider = \OC::$server->query(PublicKeyTokenProvider::class);
428
+            $userSession->setTokenProvider($provider);
429
+            $userSession->login($username, $password);
430
+            $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password);
431
+
432
+            $session = $userSession->getSession();
433
+            $session->set('last-password-confirm', \OC::$server->query(ITimeFactory::class)->getTime());
434
+
435
+            // Set email for admin
436
+            if (!empty($options['adminemail'])) {
437
+                $user->setSystemEMailAddress($options['adminemail']);
438
+            }
439
+        }
440
+
441
+        return $error;
442
+    }
443
+
444
+    public static function installBackgroundJobs() {
445
+        $jobList = \OC::$server->getJobList();
446
+        $jobList->add(TokenCleanupJob::class);
447
+        $jobList->add(Rotate::class);
448
+        $jobList->add(BackgroundCleanupJob::class);
449
+    }
450
+
451
+    /**
452
+     * @return string Absolute path to htaccess
453
+     */
454
+    private function pathToHtaccess() {
455
+        return \OC::$SERVERROOT . '/.htaccess';
456
+    }
457
+
458
+    /**
459
+     * Find webroot from config
460
+     *
461
+     * @param SystemConfig $config
462
+     * @return string
463
+     * @throws InvalidArgumentException when invalid value for overwrite.cli.url
464
+     */
465
+    private static function findWebRoot(SystemConfig $config): string {
466
+        // For CLI read the value from overwrite.cli.url
467
+        if (\OC::$CLI) {
468
+            $webRoot = $config->getValue('overwrite.cli.url', '');
469
+            if ($webRoot === '') {
470
+                throw new InvalidArgumentException('overwrite.cli.url is empty');
471
+            }
472
+            if (!filter_var($webRoot, FILTER_VALIDATE_URL)) {
473
+                throw new InvalidArgumentException('invalid value for overwrite.cli.url');
474
+            }
475
+            $webRoot = rtrim((parse_url($webRoot, PHP_URL_PATH) ?? ''), '/');
476
+        } else {
477
+            $webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';
478
+        }
479
+
480
+        return $webRoot;
481
+    }
482
+
483
+    /**
484
+     * Append the correct ErrorDocument path for Apache hosts
485
+     *
486
+     * @return bool True when success, False otherwise
487
+     * @throws \OCP\AppFramework\QueryException
488
+     */
489
+    public static function updateHtaccess() {
490
+        $config = \OC::$server->getSystemConfig();
491
+
492
+        try {
493
+            $webRoot = self::findWebRoot($config);
494
+        } catch (InvalidArgumentException $e) {
495
+            return false;
496
+        }
497
+
498
+        $setupHelper = new \OC\Setup(
499
+            $config,
500
+            \OC::$server->get(IniGetWrapper::class),
501
+            \OC::$server->getL10N('lib'),
502
+            \OC::$server->query(Defaults::class),
503
+            \OC::$server->get(LoggerInterface::class),
504
+            \OC::$server->getSecureRandom(),
505
+            \OC::$server->query(Installer::class)
506
+        );
507
+
508
+        $htaccessContent = file_get_contents($setupHelper->pathToHtaccess());
509
+        $content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";
510
+        $htaccessContent = explode($content, $htaccessContent, 2)[0];
511
+
512
+        //custom 403 error page
513
+        $content .= "\nErrorDocument 403 " . $webRoot . '/';
514
+
515
+        //custom 404 error page
516
+        $content .= "\nErrorDocument 404 " . $webRoot . '/';
517
+
518
+        // Add rewrite rules if the RewriteBase is configured
519
+        $rewriteBase = $config->getValue('htaccess.RewriteBase', '');
520
+        if ($rewriteBase !== '') {
521
+            $content .= "\n<IfModule mod_rewrite.c>";
522
+            $content .= "\n  Options -MultiViews";
523
+            $content .= "\n  RewriteRule ^core/js/oc.js$ index.php [PT,E=PATH_INFO:$1]";
524
+            $content .= "\n  RewriteRule ^core/preview.png$ index.php [PT,E=PATH_INFO:$1]";
525
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !\\.(css|js|svg|gif|png|html|ttf|woff2?|ico|jpg|jpeg|map|webm|mp4|mp3|ogg|wav|wasm|tflite)$";
526
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/core/ajax/update\\.php";
527
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/core/img/(favicon\\.ico|manifest\\.json)$";
528
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/(cron|public|remote|status)\\.php";
529
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/ocs/v(1|2)\\.php";
530
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/robots\\.txt";
531
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/(ocm-provider|ocs-provider|updater)/";
532
+            $content .= "\n  RewriteCond %{REQUEST_URI} !^/\\.well-known/(acme-challenge|pki-validation)/.*";
533
+            $content .= "\n  RewriteCond %{REQUEST_FILENAME} !/richdocumentscode(_arm64)?/proxy.php$";
534
+            $content .= "\n  RewriteRule . index.php [PT,E=PATH_INFO:$1]";
535
+            $content .= "\n  RewriteBase " . $rewriteBase;
536
+            $content .= "\n  <IfModule mod_env.c>";
537
+            $content .= "\n    SetEnv front_controller_active true";
538
+            $content .= "\n    <IfModule mod_dir.c>";
539
+            $content .= "\n      DirectorySlash off";
540
+            $content .= "\n    </IfModule>";
541
+            $content .= "\n  </IfModule>";
542
+            $content .= "\n</IfModule>";
543
+        }
544
+
545
+        if ($content !== '') {
546
+            //suppress errors in case we don't have permissions for it
547
+            return (bool)@file_put_contents($setupHelper->pathToHtaccess(), $htaccessContent . $content . "\n");
548
+        }
549
+
550
+        return false;
551
+    }
552
+
553
+    public static function protectDataDirectory() {
554
+        //Require all denied
555
+        $now = date('Y-m-d H:i:s');
556
+        $content = "# Generated by Nextcloud on $now\n";
557
+        $content .= "# Section for Apache 2.4 to 2.6\n";
558
+        $content .= "<IfModule mod_authz_core.c>\n";
559
+        $content .= "  Require all denied\n";
560
+        $content .= "</IfModule>\n";
561
+        $content .= "<IfModule mod_access_compat.c>\n";
562
+        $content .= "  Order Allow,Deny\n";
563
+        $content .= "  Deny from all\n";
564
+        $content .= "  Satisfy All\n";
565
+        $content .= "</IfModule>\n\n";
566
+        $content .= "# Section for Apache 2.2\n";
567
+        $content .= "<IfModule !mod_authz_core.c>\n";
568
+        $content .= "  <IfModule !mod_access_compat.c>\n";
569
+        $content .= "    <IfModule mod_authz_host.c>\n";
570
+        $content .= "      Order Allow,Deny\n";
571
+        $content .= "      Deny from all\n";
572
+        $content .= "    </IfModule>\n";
573
+        $content .= "    Satisfy All\n";
574
+        $content .= "  </IfModule>\n";
575
+        $content .= "</IfModule>\n\n";
576
+        $content .= "# Section for Apache 2.2 to 2.6\n";
577
+        $content .= "<IfModule mod_autoindex.c>\n";
578
+        $content .= "  IndexIgnore *\n";
579
+        $content .= "</IfModule>";
580
+
581
+        $baseDir = \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data');
582
+        file_put_contents($baseDir . '/.htaccess', $content);
583
+        file_put_contents($baseDir . '/index.html', '');
584
+    }
585
+
586
+    /**
587
+     * Return vendor from which this version was published
588
+     *
589
+     * @return string Get the vendor
590
+     *
591
+     * Copy of \OC\Updater::getVendor()
592
+     */
593
+    private function getVendor() {
594
+        // this should really be a JSON file
595
+        require \OC::$SERVERROOT . '/version.php';
596
+        /** @var string $vendor */
597
+        return (string)$vendor;
598
+    }
599 599
 }
Please login to merge, or discard this patch.