sintonia/legacy/public/setup/setup-functions.php

144 lines
3.8 KiB
PHP
Raw Normal View History

<?php
2014-12-09 23:48:16 +01:00
require_once dirname(__DIR__, 2) . '/application/preload.php';
/**
2021-10-11 16:10:47 +02:00
* Class Setup.
*
* @author sourcefabric
2021-10-11 16:10:47 +02:00
*
* Abstract superclass for the setup and installation process
*/
2021-10-11 16:10:47 +02:00
abstract class Setup
{
protected static $_section;
/**
* Array of key->value pairs for config.yml.
*
* @var array
*/
protected static $_properties;
2021-10-11 16:10:47 +02:00
abstract public function __construct($settings);
2021-10-11 16:10:47 +02:00
abstract public function runSetup();
/**
* Write new property values to a given section in airtime.temp.conf.
*/
2021-10-11 16:10:47 +02:00
protected function writeToTemp()
{
if (!file_exists(INSTALLER_CONFIG_FILEPATH)) {
copy(SAMPLE_CONFIG_FILEPATH, INSTALLER_CONFIG_FILEPATH);
2014-12-09 23:48:16 +01:00
}
// Logging::info(CONFIG_TEMP_FILEPATH);
$this->_write(INSTALLER_CONFIG_FILEPATH);
}
2021-10-11 16:10:47 +02:00
protected function _write($filePath)
{
$file = file($filePath);
2021-10-11 16:10:47 +02:00
$fileOutput = '';
2014-12-09 23:48:16 +01:00
$inSection = false;
foreach ($file as $line) {
if (strpos($line, static::$_section) !== false) {
2014-12-09 23:48:16 +01:00
$inSection = true;
2021-10-11 16:10:47 +02:00
} elseif (strpos($line, '[') !== false) {
2014-12-09 23:48:16 +01:00
$inSection = false;
}
2021-10-11 16:10:47 +02:00
if (substr(trim($line), 0, 1) == '#') {
/* Workaround to strip comments from config.yml.
* We need to do this because python's ConfigObj and PHP
* have different (and equally strict) rules about comment
* characters in configuration files.
*/
2021-10-11 16:10:47 +02:00
} elseif ($inSection) {
$key = trim(@substr($line, 0, strpos($line, '=')));
$fileOutput .= $key && isset(static::$_properties[$key]) ?
2021-10-11 16:10:47 +02:00
$key . ' = ' . static::$_properties[$key] . "\n" : $line;
2014-12-09 23:48:16 +01:00
} else {
$fileOutput .= $line;
}
}
file_put_contents($filePath, $fileOutput);
2014-12-09 23:48:16 +01:00
}
/**
* Generates a random string.
*
2021-10-11 16:10:47 +02:00
* @param int $p_len
* length of the output string
* @param string $p_chars
2021-10-11 16:10:47 +02:00
* characters to use in the output string
*
* @return string the generated random string
*/
2021-10-11 16:10:47 +02:00
protected function generateRandomString($p_len = 20, $p_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
{
$string = '';
2021-10-11 16:10:47 +02:00
for ($i = 0; $i < $p_len; ++$i) {
$pos = random_int(0, strlen($p_chars) - 1);
2021-10-11 16:10:47 +02:00
$string .= $p_chars[$pos];
}
2021-10-11 16:10:47 +02:00
return $string;
}
}
/**
2021-10-11 16:10:47 +02:00
* Class AirtimeDatabaseException.
*
* @author sourcefabric
*
* Exception class for database setup errors
*/
2021-10-11 16:10:47 +02:00
class AirtimeDatabaseException extends Exception
{
protected $message = 'Unknown Airtime database exception';
protected $errors = [];
2021-10-11 16:10:47 +02:00
public function __construct($message = null, $errors = [], $code = 0, Exception $previous = null)
{
parent::__construct($message, $code, $previous);
$this->errors = $errors;
}
2021-10-11 16:10:47 +02:00
public function getErrorFields()
{
return $this->errors;
}
}
2014-12-09 23:48:16 +01:00
// Import Setup subclasses
2021-10-11 16:10:47 +02:00
require_once 'database-setup.php';
require_once 'rabbitmq-setup.php';
require_once 'general-setup.php';
require_once 'media-setup.php';
// If config.yml exists, we shouldn't be here
if (!file_exists('/etc/libretime/config.yml')) {
2021-10-11 16:10:47 +02:00
if (isset($_GET['obj']) && $objType = $_GET['obj']) {
$obj = new $objType($_POST);
if ($obj instanceof Setup) {
try {
$response = $obj->runSetup();
} catch (AirtimeDatabaseException $e) {
2021-10-11 16:10:47 +02:00
$response = [
'message' => $e->getMessage(),
'errors' => $e->getErrorFields(),
];
}
2021-10-11 16:10:47 +02:00
echo json_encode($response);
}
}
}