Skip to content

Commit

Permalink
Setup - remove chat server config from Admin config and implement it …
Browse files Browse the repository at this point in the history
…into setup
  • Loading branch information
daniwe4 authored and klees committed Nov 6, 2020
1 parent 0f695e6 commit a638276
Show file tree
Hide file tree
Showing 7 changed files with 597 additions and 229 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php declare(strict_types=1);

/* Copyright (c) 2020 Daniel Weise <[email protected]> Extended GPL, see docs/LICENSE */

use ILIAS\Setup;
use ILIAS\DI;

/**
* Store information about https is enabled
*/
class ilChatroomServerConfigStoredObjective implements Setup\Objective
{
/**
* @var \ilChatroomSetupConfig
*/
protected $config;

public function __construct(\ilChatroomSetupConfig $config)
{
$this->config = $config;
}

public function getHash() : string
{
return hash("sha256", self::class);
}

public function getLabel() : string
{
return "Store information about chatroom server to db";
}

public function isNotable() : bool
{
return true;
}

public function getPreconditions(Setup\Environment $environment) : array
{
$common_config = $environment->getConfigFor("common");
$db_config = $environment->getConfigFor("database");
return [
new \ilIniFilesPopulatedObjective($common_config),
new \ilDatabasePopulatedObjective($db_config),
new \ilDatabaseUpdatedObjective($db_config)
];
}

public function achieve(Setup\Environment $environment) : Setup\Environment
{
$db = $environment->getResource(Setup\Environment::RESOURCE_DATABASE);
$common_config = $environment->getConfigFor("common");
$filesystem_config = $environment->getConfigFor("filesystem");

// ATTENTION: This is a total abomination. It only exists to allow various
// sub components of the various readers to run. This is a memento to the
// fact, that dependency injection is something we want. Currently, every
// component could just service locate the whole world via the global $DIC.
$DIC = $GLOBALS["DIC"];
$GLOBALS["DIC"] = new DI\Container();
$GLOBALS["DIC"]["ilDB"] = $db;
$GLOBALS["DIC"]["ilBench"] = null;

$chat_admin = ilChatroomAdmin::getDefaultConfiguration();
$settings = $chat_admin->loadGeneralSettings();

$settings['address'] = $this->config->getAddress();
$settings['port'] = $this->config->getPort();
$settings['sub_directory'] = $this->config->getSubDirectory();
$settings['protocol'] = $this->config->getProtocol();
$settings['cert'] = $this->config->getCert();
$settings['key'] = $this->config->getKey();
$settings['dhparam'] = $this->config->getDhparam();
$settings['log'] = (int)$this->config->getLog();
$settings['log_level'] = $this->config->getLogLevel();
$settings['error_log'] = $this->config->getErrorLog();
$settings['ilias_proxy'] = (int)$this->config->hasIliasProxy();
$settings['ilias_url'] = $this->config->getIliasUrl();
$settings['client_proxy'] = (int)$this->config->hasClientProxy();
$settings['client_url'] = $this->config->getClientUrl();
$settings['deletion_mode'] = (int)$this->config->hasDeletionMode();
$settings['deletion_unit'] = $this->config->getDeletionUnit();
$settings['deletion_value'] = $this->config->getDeletionValue();
$settings['deletion_time'] = $this->config->getDeletionTime();

$chat_admin->saveGeneralSettings((object) $settings);

if (!defined("CLIENT_DATA_DIR")) {
define(
"CLIENT_DATA_DIR",
$filesystem_config->getDataDir() . "/" . $common_config->getClientId()
);
}

$fileHandler = new ilChatroomConfigFileHandler();
$fileHandler->createServerConfigFile($settings);

$GLOBALS["DIC"] = $DIC;

return $environment;
}

/**
* @inheritDoc
*/
public function isApplicable(Setup\Environment $environment) : bool
{
return $this->config->getAddress() != '' && $this->config->getPort() != 0;
}
}
186 changes: 186 additions & 0 deletions Modules/Chatroom/classes/Setup/class.ilChatroomSetupAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php declare(strict_types=1);

/* Copyright (c) 2020 Daniel Weise <[email protected]> Extended GPL, see docs/LICENSE */

use ILIAS\Setup;
use ILIAS\Refinery;
use ILIAS\UI;

class ilChatroomSetupAgent implements Setup\Agent
{
const PORT_MIN = 1;
const PORT_MAX = 65535;

public static $LOG_LEVELS = [
'emerg',
'alert',
'crit',
'error',
'warning',
'notice',
'info',
'debug',
'silly'
];

public static $INTERVALS = [
'days',
'weeks',
'months',
'years'
];

/**
* @var Refinery\Factory
*/
protected $refinery;

public function __construct(Refinery\Factory $refinery)
{
$this->refinery = $refinery;
}

/**
* @inheritdoc
*/
public function hasConfig() : bool
{
return true;
}

/**
* @inheritdoc
*/
public function getConfigInput(Setup\Config $config = null) : UI\Component\Input\Field\Input
{
throw new \LogicException("Not yet implemented.");
}

/**
* @inheritdoc
*/
public function getArrayToConfigTransformation() : Refinery\Transformation
{
$levels = self::$LOG_LEVELS;
$intervals = self::$INTERVALS;
// TODO: clean this up
return $this->refinery->custom()->transformation(function ($data) use ($levels, $intervals) {
$protocol = 'http://';
if (isset($data['https']) && count($data['https']) > 0) {
$protocol = 'https://';
}

$deletion_interval = false;
if (isset($data['deletion_interval']) && count($data['deletion_interval']) > 0) {
$deletion_interval = true;
}

$ilias_proxy = false;
if (isset($data['ilias_proxy']) && count($data['ilias_proxy']) > 0) {
$ilias_proxy = true;
}

$client_proxy = false;
if (isset($data['client_proxy']) && count($data['client_proxy']) > 0) {
$client_proxy = true;
}

if (!is_null($data['port']) && (int) $data['port'] < self::PORT_MIN || (int) $data['port'] > self::PORT_MAX) {
throw new InvalidArgumentException(
$data['port'] . ' is not a valid value for port. Please check your config file.'
);
}

if ($data['log'] != '') {
if (!in_array($data['log_level'], $levels)) {
throw new InvalidArgumentException(
$data['log_level'] . ' is not a valid value for log_level. Please check your config file.'
);
}
}

if ($deletion_interval) {
if (!in_array($data['deletion_interval']['deletion_unit'], $intervals)) {
throw new InvalidArgumentException(
$data['deletion_interval']['deletion_unit'] . ' is not a valid value for deletion_unit. Please check your config file.'
);
}
if (!is_numeric($data['deletion_interval']['deletion_value'])) {
throw new InvalidArgumentException(
$data['deletion_interval']['deletion_value'] . ' is not a valid value for deletion_value. Please check your config file.'
);
}
if (!preg_match_all('/([01][0-9]|[2][0-3]):[0-5][0-9]/', $data['deletion_interval']['deletion_time'])) {
throw new InvalidArgumentException(
$data['deletion_interval']['deletion_time'] . ' is not a valid value for deletion_time. Please check your config file.'
);
}
}

return new \ilChatroomSetupConfig(
$data['address'] ?? '',
(int) $data['port'] ?? 0,
$data['sub_directory'] ?? '',
$protocol,
$data['https']['cert'] ?? '',
$data['https']['key'] ?? '',
$data['https']['dhparam'] ?? '',
$data['log'] ?? '',
$data['log_level'] ?? '',
$data['error_log'] ?? '',
$ilias_proxy,
$data['ilias_url'] ?? '',
$client_proxy,
$data['client_url'] ?? '',
$deletion_interval,
$data['deletion_interval']['deletion_unit'] ?? '',
(int) $data['deletion_interval']['deletion_value'] ?? 0,
$data['deletion_interval']['deletion_time'] ?? ''
);
});
}

/**
* @inheritdoc
*/
public function getInstallObjective(Setup\Config $config = null) : Setup\Objective
{
return new Setup\ObjectiveCollection(
"Complete objectives from Services/Chatroom",
false,
new ilChatroomServerConfigStoredObjective($config)
);
}

/**
* @inheritdoc
*/
public function getUpdateObjective(Setup\Config $config = null) : Setup\Objective
{
return new Setup\Objective\NullObjective();
}

/**
* @inheritdoc
*/
public function getBuildArtifactObjective() : Setup\Objective
{
return new Setup\Objective\NullObjective();
}

/**
* @inheritdoc
*/
public function getStatusObjective(Setup\Metrics\Storage $storage) : Setup\Objective
{
return new Setup\Objective\NullObjective();
}

/**
* @inheritDoc
*/
public function getMigrations() : array
{
return [];
}
}
Loading

0 comments on commit a638276

Please sign in to comment.