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

93 lines
2.6 KiB
PHP
Raw Normal View History

2014-12-09 23:48:16 +01:00
<?php
/**
* User: sourcefabric
2021-10-11 16:10:47 +02:00
* Date: 08/12/14.
2014-12-09 23:48:16 +01:00
*
* Class GeneralSetup
*
* Wrapper class for validating and setting up general settings during the installation process
*/
2021-10-11 16:10:47 +02:00
class GeneralSetup extends Setup
{
// config file section header
2021-10-11 16:10:47 +02:00
protected static $_section = '[general]';
2014-12-09 23:48:16 +01:00
// Array of key->value pairs for the config file
2020-05-19 16:16:31 +02:00
protected static $_properties;
2014-12-09 23:48:16 +01:00
// Constant form field names for passing errors back to the front-end
2021-10-11 16:10:47 +02:00
public const GENERAL_PORT = 'generalPort';
public const GENERAL_HOST = 'generalHost';
public const CORS_URL = 'corsUrl';
2014-12-09 23:48:16 +01:00
2021-10-11 16:10:47 +02:00
public static $cors_url;
2014-12-09 23:48:16 +01:00
// Message and error fields to return to the front-end
2021-10-11 16:10:47 +02:00
public static $message;
public static $errors = [];
public function __construct($settings)
{
self::$_properties = [
'api_key' => $this->generateRandomString(),
'base_url' => $settings[self::GENERAL_HOST],
'base_port' => $settings[self::GENERAL_PORT],
'cors_url' => $settings[self::CORS_URL],
];
2020-05-19 16:16:31 +02:00
self::$cors_url = $settings[self::CORS_URL];
2014-12-09 23:48:16 +01:00
}
/**
* @return array associative array containing a display message and fields with errors
*/
2021-10-11 16:10:47 +02:00
public function runSetup()
{
2014-12-15 15:53:50 +01:00
// TODO Do we need to validate these settings?
2014-12-09 23:48:16 +01:00
if (count(self::$errors) <= 0) {
$this->writeToTemp();
}
2020-05-19 16:16:31 +02:00
if (strlen(self::$cors_url) == 0) {
2021-10-11 16:10:47 +02:00
} else {
2020-05-19 16:16:31 +02:00
$this->setupCorsUrl();
}
2021-10-11 16:10:47 +02:00
return [
'message' => self::$message,
'errors' => self::$errors,
];
2014-12-09 23:48:16 +01:00
}
2021-10-11 16:10:47 +02:00
2020-05-19 16:16:31 +02:00
/**
* If the user entered a CORS Url then add it to the system preferences
2021-10-11 16:10:47 +02:00
* TODO Make sure we check for existing CORS URLs and display them on initial form.
2020-05-19 16:16:31 +02:00
*/
2021-10-11 16:10:47 +02:00
public function setupCorsUrl()
{
2020-05-19 16:16:31 +02:00
try {
$_SERVER['LIBRETIME_CONFIG_FILEPATH'] = INSTALLER_CONFIG_FILEPATH;
Propel::init(PROPEL_CONFIG_FILEPATH);
2020-05-19 16:16:31 +02:00
$con = Propel::getConnection();
2021-10-11 16:10:47 +02:00
} catch (Exception $e) {
2020-05-19 16:16:31 +02:00
self::$message = "Failed to insert Cors URL; database isn't configured properly!";
self::$errors[] = self::CORS_URL;
2021-10-11 16:10:47 +02:00
2020-05-19 16:16:31 +02:00
return;
}
$this->runCorsUrlQuery($con);
}
2014-12-09 23:48:16 +01:00
2021-10-11 16:10:47 +02:00
public function runCorsUrlQuery($con)
{
2020-05-19 16:16:31 +02:00
try {
2021-10-11 16:10:47 +02:00
Application_Model_Preference::SetAllowedCorsUrls(self::$cors_url);
Propel::close();
2020-05-19 16:16:31 +02:00
} catch (Exception $e) {
2021-10-11 16:10:47 +02:00
self::$message = 'Failed to insert ' . self::$cors_url . ' into cc_pref' . $e;
2020-05-19 16:16:31 +02:00
self::$errors[] = self::CORS_URL;
}
}
2021-10-11 16:10:47 +02:00
}