mirror of
https://github.com/nextcloud/all-in-one.git
synced 2025-12-20 14:36:52 +00:00
Initial import
This commit is contained in:
commit
2295a33590
884 changed files with 93939 additions and 0 deletions
230
php/src/Data/ConfigurationManager.php
Normal file
230
php/src/Data/ConfigurationManager.php
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
<?php
|
||||
|
||||
namespace AIO\Data;
|
||||
|
||||
use AIO\Auth\PasswordGenerator;
|
||||
use AIO\Controller\DockerController;
|
||||
|
||||
class ConfigurationManager
|
||||
{
|
||||
public function GetConfig() : array
|
||||
{
|
||||
if(file_exists(DataConst::GetConfigFile()))
|
||||
{
|
||||
$configContent = file_get_contents(DataConst::GetConfigFile());
|
||||
return json_decode($configContent, true);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
public function GetUserName() : string {
|
||||
return $this->GetConfig()['username'];
|
||||
}
|
||||
|
||||
public function GetPassword() : string {
|
||||
return $this->GetConfig()['password'];
|
||||
}
|
||||
|
||||
public function GetToken() : string {
|
||||
return $this->GetConfig()['AIO_TOKEN'];
|
||||
}
|
||||
|
||||
public function GetIsContainerUpateAvailable() : bool {
|
||||
return isset($this->GetConfig()['isContainerUpateAvailable']) ? $this->GetConfig()['isContainerUpateAvailable'] : false;
|
||||
}
|
||||
|
||||
public function SetIsContainerUpateAvailable(bool $value) : void {
|
||||
$config = $this->GetConfig();
|
||||
$config['isContainerUpateAvailable'] = $value;
|
||||
$this->WriteConfig($config);
|
||||
}
|
||||
|
||||
public function SetPassword(string $password) : void {
|
||||
$config = $this->GetConfig();
|
||||
$config['username'] = 'admin';
|
||||
$config['password'] = $password;
|
||||
$this->WriteConfig($config);
|
||||
}
|
||||
|
||||
public function GetSecret(string $secretId) : string {
|
||||
$config = $this->GetConfig();
|
||||
if(!isset($config['secrets'][$secretId])) {
|
||||
$config['secrets'][$secretId] = bin2hex(random_bytes(24));
|
||||
$this->WriteConfig($config);
|
||||
}
|
||||
|
||||
if ($secretId === 'BORGBACKUP_PASSWORD' && !file_exists(DataConst::GetBackupSecretFile())) {
|
||||
$this->DoubleSafeBackupSecret($config['secrets'][$secretId]);
|
||||
}
|
||||
|
||||
return $config['secrets'][$secretId];
|
||||
}
|
||||
|
||||
private function DoubleSafeBackupSecret(string $borgBackupPassword) {
|
||||
file_put_contents(DataConst::GetBackupSecretFile(), $borgBackupPassword);
|
||||
}
|
||||
|
||||
public function hasBackupRunOnce() : bool {
|
||||
if (!file_exists(DataConst::GetBackupKeyFile())) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function GetLastBackupTime() : string {
|
||||
if (!file_exists(DataConst::GetBackupArchivesList())) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$content = file_get_contents(DataConst::GetBackupArchivesList());
|
||||
if ($content === "") {
|
||||
return '';
|
||||
}
|
||||
|
||||
$lastBackupLines = explode("\n", $content);
|
||||
$lastBackupLine = $lastBackupLines[sizeof($lastBackupLines) - 2];
|
||||
if ($lastBackupLine === "") {
|
||||
return '';
|
||||
}
|
||||
|
||||
$lastBackupTimes = explode(",", $lastBackupLine);
|
||||
$lastBackupTime = $lastBackupTimes[1];
|
||||
if ($lastBackupTime === "") {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $lastBackupTime;
|
||||
}
|
||||
|
||||
public function wasStartButtonClicked() : bool {
|
||||
if (isset($this->GetConfig()['wasStartButtonClicked'])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidSettingConfigurationException
|
||||
*/
|
||||
public function SetDomain(string $domain) : void {
|
||||
// Validate URL
|
||||
if (!filter_var('http://' . $domain, FILTER_VALIDATE_URL)) {
|
||||
throw new InvalidSettingConfigurationException("Domain is not in a valid format!");
|
||||
}
|
||||
|
||||
$dnsRecordIP = gethostbyname($domain);
|
||||
|
||||
// Validate IP
|
||||
if(!filter_var($dnsRecordIP, FILTER_VALIDATE_IP)) {
|
||||
throw new InvalidSettingConfigurationException("DNS config is not set or domain is not in a valid format!");
|
||||
}
|
||||
|
||||
$connection = @fsockopen($domain, 443, $errno, $errstr, 0.1);
|
||||
if ($connection) {
|
||||
fclose($connection);
|
||||
} else {
|
||||
throw new InvalidSettingConfigurationException("The server is not reachable on Port 443.");
|
||||
}
|
||||
|
||||
// Get Instance ID
|
||||
$instanceID = $this->GetSecret('INSTANCE_ID');
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL,'http://' . $domain . ':443');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = curl_exec($ch);
|
||||
# Get rid of trailing \n
|
||||
$response = str_replace("\n", "", $response);
|
||||
|
||||
if($response !== $instanceID) {
|
||||
throw new InvalidSettingConfigurationException("Domain does not point to this server.");
|
||||
}
|
||||
|
||||
// Write domain
|
||||
$config = $this->GetConfig();
|
||||
$config['domain'] = $domain;
|
||||
$this->WriteConfig($config);
|
||||
}
|
||||
|
||||
public function GetDomain() : string {
|
||||
$config = $this->GetConfig();
|
||||
if(!isset($config['domain'])) {
|
||||
$config['domain'] = '';
|
||||
}
|
||||
|
||||
return $config['domain'];
|
||||
}
|
||||
|
||||
public function GetBackupMode() : string {
|
||||
$config = $this->GetConfig();
|
||||
if(!isset($config['backup-mode'])) {
|
||||
$config['backup-mode'] = '';
|
||||
}
|
||||
|
||||
return $config['backup-mode'];
|
||||
}
|
||||
|
||||
public function GetAIOURL() : string {
|
||||
$config = $this->GetConfig();
|
||||
if(!isset($config['AIO_URL'])) {
|
||||
$config['AIO_URL'] = '';
|
||||
}
|
||||
|
||||
return $config['AIO_URL'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidSettingConfigurationException
|
||||
*/
|
||||
public function SetBorgBackupHostLocation(string $location) : void {
|
||||
$allowedPrefixes = [
|
||||
'/mnt/',
|
||||
'/media/',
|
||||
];
|
||||
|
||||
$isValidPath = false;
|
||||
foreach($allowedPrefixes as $allowedPrefix) {
|
||||
if(str_starts_with($location, $allowedPrefix)) {
|
||||
$isValidPath = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!$isValidPath) {
|
||||
throw new InvalidSettingConfigurationException("Path must start with /mnt/ or /media/.");
|
||||
}
|
||||
|
||||
|
||||
$config = $this->GetConfig();
|
||||
$config['borg_backup_host_location'] = $location;
|
||||
$this->WriteConfig($config);
|
||||
}
|
||||
|
||||
public function WriteConfig(array $config) : void {
|
||||
if(!is_dir(DataConst::GetDataDirectory())) {
|
||||
mkdir(DataConst::GetDataDirectory());
|
||||
}
|
||||
file_put_contents(DataConst::GetConfigFile(), json_encode($config));
|
||||
}
|
||||
|
||||
public function GetBorgBackupHostLocation() : string {
|
||||
$config = $this->GetConfig();
|
||||
if(!isset($config['borg_backup_host_location'])) {
|
||||
$config['borg_backup_host_location'] = '';
|
||||
}
|
||||
|
||||
return $config['borg_backup_host_location'];
|
||||
}
|
||||
|
||||
public function GetBorgBackupMode() : string {
|
||||
$config = $this->GetConfig();
|
||||
if(!isset($config['backup-mode'])) {
|
||||
$config['backup-mode'] = '';
|
||||
}
|
||||
|
||||
return $config['backup-mode'];
|
||||
}
|
||||
}
|
||||
37
php/src/Data/DataConst.php
Normal file
37
php/src/Data/DataConst.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace AIO\Data;
|
||||
|
||||
class DataConst {
|
||||
public static function GetDataDirectory() : string {
|
||||
if(is_dir('/mnt/docker-aio-config/data/')) {
|
||||
return '/mnt/docker-aio-config/data/';
|
||||
}
|
||||
|
||||
return realpath(__DIR__ . '/../../data/');
|
||||
}
|
||||
|
||||
public static function GetSessionDirectory() : string {
|
||||
if(is_dir('/mnt/docker-aio-config/session/')) {
|
||||
return '/mnt/docker-aio-config/session/';
|
||||
}
|
||||
|
||||
return realpath(__DIR__ . '/../../session/');
|
||||
}
|
||||
|
||||
public static function GetConfigFile() : string {
|
||||
return self::GetDataDirectory() . '/configuration.json';
|
||||
}
|
||||
|
||||
public static function GetBackupSecretFile() : string {
|
||||
return self::GetDataDirectory() . '/backupsecret';
|
||||
}
|
||||
|
||||
public static function GetBackupKeyFile() : string {
|
||||
return self::GetDataDirectory() . '/borg.config';
|
||||
}
|
||||
|
||||
public static function GetBackupArchivesList() : string {
|
||||
return self::GetDataDirectory() . '/backup_archives.list';
|
||||
}
|
||||
}
|
||||
6
php/src/Data/InvalidSettingConfigurationException.php
Normal file
6
php/src/Data/InvalidSettingConfigurationException.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
namespace AIO\Data;
|
||||
|
||||
class InvalidSettingConfigurationException extends \Exception {}
|
||||
32
php/src/Data/Setup.php
Normal file
32
php/src/Data/Setup.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace AIO\Data;
|
||||
|
||||
use AIO\Auth\PasswordGenerator;
|
||||
|
||||
class Setup
|
||||
{
|
||||
private PasswordGenerator $passwordGenerator;
|
||||
private ConfigurationManager $configurationManager;
|
||||
|
||||
public function __construct(
|
||||
PasswordGenerator $passwordGenerator,
|
||||
ConfigurationManager $configurationManager) {
|
||||
$this->passwordGenerator = $passwordGenerator;
|
||||
$this->configurationManager = $configurationManager;
|
||||
}
|
||||
|
||||
public function Setup() : string {
|
||||
if(!$this->CanBeInstalled()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$password = $this->passwordGenerator->GeneratePassword(6);
|
||||
$this->configurationManager->SetPassword($password);
|
||||
return $password;
|
||||
}
|
||||
|
||||
public function CanBeInstalled() : bool {
|
||||
return !file_exists(DataConst::GetConfigFile());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue