2020-09-06 09:32:56 +02:00
|
|
|
<?php declare(strict_types=1);
|
2020-08-27 00:57:01 +02:00
|
|
|
|
|
|
|
namespace Orbit;
|
|
|
|
|
2020-09-06 09:32:56 +02:00
|
|
|
/**
|
|
|
|
* Config
|
|
|
|
*
|
|
|
|
* @package Orbit
|
|
|
|
*/
|
2020-08-27 00:57:01 +02:00
|
|
|
class Config
|
|
|
|
{
|
|
|
|
public $host = "0.0.0.0";
|
|
|
|
public $port = 1965;
|
2020-08-27 05:16:05 +02:00
|
|
|
public $hostname = "localhost";
|
2020-08-27 00:57:01 +02:00
|
|
|
|
2020-08-31 08:04:44 +02:00
|
|
|
public $tls_certfile = "";
|
|
|
|
public $tls_keyfile = "";
|
2020-08-27 05:16:05 +02:00
|
|
|
public $key_passphrase = "";
|
|
|
|
|
2020-08-31 08:04:44 +02:00
|
|
|
public $log_file = ".log/orbit.log";
|
|
|
|
public $log_level = "info";
|
2020-08-27 08:37:46 +02:00
|
|
|
|
|
|
|
public $root_dir = ".";
|
2020-08-27 09:46:11 +02:00
|
|
|
public $index_file = "index.gmi";
|
|
|
|
public $enable_directory_index = true;
|
2020-08-31 08:04:44 +02:00
|
|
|
|
|
|
|
private $is_development_server = false;
|
|
|
|
|
2020-09-06 09:32:56 +02:00
|
|
|
/**
|
|
|
|
* __construct
|
|
|
|
*
|
|
|
|
* @param bool $is_development
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct($is_development_server = false)
|
2020-08-31 08:04:44 +02:00
|
|
|
{
|
2020-09-06 09:32:56 +02:00
|
|
|
$this->setIsDevelopmentServer($is_development_server);
|
2020-08-31 08:04:44 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 09:32:56 +02:00
|
|
|
public function setIsDevelopmentServer($is_development_server): void
|
2020-08-31 08:04:44 +02:00
|
|
|
{
|
|
|
|
$this->is_development_server = (bool) $is_development_server;
|
|
|
|
}
|
|
|
|
|
2020-09-06 09:32:56 +02:00
|
|
|
public function getIsDevelopmentServer(): bool
|
2020-08-31 08:04:44 +02:00
|
|
|
{
|
|
|
|
return $this->is_development_server;
|
|
|
|
}
|
|
|
|
|
2020-09-06 09:32:56 +02:00
|
|
|
/**
|
|
|
|
* Read config values from ini file
|
|
|
|
*
|
|
|
|
* @param string $filename Path to ini file
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-09-08 00:05:38 +02:00
|
|
|
public function readFromIniFile(string $filename): void
|
2020-08-31 08:04:44 +02:00
|
|
|
{
|
|
|
|
if (!file_exists($filename) || !is_readable($filename)) {
|
|
|
|
throw new \Exception("Cannot read config file '$filename'");
|
|
|
|
}
|
|
|
|
|
|
|
|
$ini = parse_ini_file($filename);
|
2020-09-08 00:05:38 +02:00
|
|
|
$this->readFromArray($ini);
|
|
|
|
}
|
2020-08-31 08:04:44 +02:00
|
|
|
|
2020-09-08 00:05:38 +02:00
|
|
|
/**
|
|
|
|
* Read config values from array
|
|
|
|
*
|
|
|
|
* @param array $data Array of config values
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function readFromArray(array $params): void
|
|
|
|
{
|
2020-08-31 08:04:44 +02:00
|
|
|
$valid_keys = [
|
|
|
|
'host', 'port', 'hostname', 'tls_certfile',
|
2020-09-28 16:35:13 +02:00
|
|
|
'tls_keyfile', 'key_passphrase', 'log_file', 'log_level',
|
2020-09-08 00:05:38 +02:00
|
|
|
'root_dir', 'index_file', 'enable_directory_index',
|
2020-08-31 08:04:44 +02:00
|
|
|
];
|
|
|
|
|
2020-09-08 00:05:38 +02:00
|
|
|
foreach ($params as $key => $value) {
|
2020-08-31 08:04:44 +02:00
|
|
|
if (!in_array($key, $valid_keys)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->{$key} = $value;
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 00:57:01 +02:00
|
|
|
}
|