Skip to content

Commit

Permalink
Add the settings page and some of the app settings to it
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Nov 9, 2024
1 parent e794ab2 commit f7b374d
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 3 deletions.
35 changes: 35 additions & 0 deletions root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
----------------------------------
------ Created: 110924 ------
------ Austin Best ------
----------------------------------
*/

if (!$_SESSION) {
session_start();
}

if (!$_SESSION['IN_UI']) {
exit('Invalid session, refresh the page');
}

require '../loader.php';

if ($_POST['m'] == 'saveSettings') {
$newSettings = [];
foreach ($_POST as $key => $val) {
if (str_equals_any($key, ['m', 'apikey'])) {
continue;
}

$newSettings[$key] = $val;
}

$proxyDb->setSettings($newSettings, $settingsTable);

if ($_POST['apikey'] && $_POST['apikey'] != APP_APIKEY) {
file_put_contents(APP_APIKEY_FILE, $_POST['apikey']);
}
}
3 changes: 0 additions & 3 deletions root/app/www/public/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
define('APP_API_ERROR', APP_NAME .': %s');
define('APP_URL', ($_SERVER['REQUEST_SCHEME'] ?: 'http') . '://'. $_SERVER['HTTP_HOST']);

define('LOG_AGE', 2); //-- DELETE AFTER THIS AMOUNT OF DAYS
define('BACKUP_AGE', 7); //-- DELETE AFTER THIS AMOUNT OF DAYS
define('STARR_BACKUP_AGE', 2); //-- DELETE AFTER THIS AMOUNT OF DAYS

//-- DATABASE
Expand Down Expand Up @@ -45,5 +43,4 @@
define('SYSTEM_LOG', LOGS_PATH . 'system/app.log');
define('MIGRATION_LOG', LOGS_PATH . 'system/migrations.log');
define('CRON_HOUSEKEEPER_LOG', LOGS_PATH . 'system/cron-housekeeper.log');
define('LOG_ROTATE_SIZE', 2); //-- MB UNTIL ROTATE
define('LOG_LINES_PER_PAGE', 1000);
25 changes: 25 additions & 0 deletions root/app/www/public/js/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function saveSettings()
{
let params = [];

$.each($('[id^=setting-]'), function() {
let val = '';
if ($(this).is(':checkbox') || $(this).is(':radio')) {
val = $(this).prop('checked') ? 1 : 0;
} else {
val = $(this).val();
}

params += '&' + $(this).attr('id').replace('setting-', '') + '=' + val;
});

$.ajax({
type: 'POST',
url: 'ajax/settings.php',
data: '&m=saveSettings' + params,
success: function (resultData) {
toast('Settings', 'The settings have been updated', 'success');
}
});
}
// -------------------------------------------------------------------------------------------
11 changes: 11 additions & 0 deletions root/app/www/public/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
}
}

//-- USED FOR MIGRATIONS, DEFINES SET BELOW
$LOG_ROTATE_SIZE = 2; //-- MB UNTIL ROTATE
$LOG_AGE = 2; //-- DELETE AFTER THIS AMOUNT OF DAYS
$BACKUP_AGE = 7; //-- DELETE AFTER THIS AMOUNT OF DAYS

//-- SETUP SOME SHARED VARIABLES
$page = $_GET['page'] ?: $_POST['page'];
$app = $_GET['app'] ?: $_POST['app'];
Expand Down Expand Up @@ -70,10 +75,16 @@
//-- LOAD THE TABLES
$starrsTable = $proxyDb->getStarrsTable();
$appsTable = $proxyDb->getAppsTable();
$settingsTable = $proxyDb->getSettings();
$usageTable = $usageDb->getUsageTable();

//-- SOMETIMES THE TABLE IS BUSY, RETRY
if (!$usageTable && ($app || !$page || $page == 'home')) {
sleep(1);
$usageTable = $usageDb->getUsageTable();
}

//-- CONSTANTS BASED ON DATABASE SETTINGS
define('LOG_ROTATE_SIZE', $settingsTable['logRotationSize'] ?: $LOG_ROTATE_SIZE);
define('LOG_AGE', $settingsTable['logRetentionLength'] ?: $LOG_AGE);
define('BACKUP_AGE', $settingsTable['backupRetentionLength'] ?: $BACKUP_AGE);
43 changes: 43 additions & 0 deletions root/app/www/public/migrations/002_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
----------------------------------
------ Created: 110924 ------
------ Austin Best ------
----------------------------------
*/

//-- RESET THE LIST
$q = [];

//-- ALWAYS NEED TO BUMP THE MIGRATION ID
$q[] = "UPDATE " . SETTINGS_TABLE . "
SET value = '002'
WHERE name = 'migration'";

$settings = [
'backupRetentionLength' => $BACKUP_AGE,
'logRotationSize' => $LOG_ROTATE_SIZE,
'logRetentionLength' => $LOG_AGE,
];

$settingRows = [];
foreach ($settings as $key => $val) {
$settingRows[] = "('" . $key . "', '" . $val . "')";
}

$q[] = "INSERT INTO " . SETTINGS_TABLE . "
(`name`, `value`)
VALUES " . implode(', ', $settingRows);

foreach ($q as $query) {
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));

$proxyDb->query($query);

if ($proxyDb->error() != 'not an error') {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $proxyDb->error(), 'error');
} else {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
}
}
99 changes: 99 additions & 0 deletions root/app/www/public/pages/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,102 @@
if (!$_SESSION['IN_UI']) {
exit('Invalid session, refresh the page');
}

$migrations = '<option value="000">000_fresh_start</option>';
$dir = opendir(MIGRATIONS_PATH);
while ($migration = readdir($dir)) {
if (str_contains($migration, '.php')) {
$migrations .= '<option ' . ($settingsTable['migration'] == substr($migration, 0, 3) ? 'selected ' : '') . 'value="' . substr($migration, 0, 3) . '">' . str_replace('.php', '', $migration) . '</option>';
}
}
closedir($dir);

?>
<div class="w-100 mb-2">
<button class="btn btn-outline-success border-light" onclick="saveSettings()"><i class="far fa-save"></i> Save</button>
</div>
<div class="row">
<div class="col-sm-12">
<div class="card border-default mb-3">
<div class="card-header">System</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<td class="w-25">
API key<br>
<span class="text-small">File: <?= APP_APIKEY_FILE ?></span>
</td>
<td>
<div class="input-group mb-3 w-25">
<input type="text" class="form-control" aria-describedby="apikey-input" value="<?= APP_APIKEY ?>" id="setting-apikey">
<button title="Copy" class="btn btn-primary" type="button" id="apikey-input" onclick="clipboard('setting-apikey', 'val')"><i class="far fa-copy"></i></button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="card border-default mb-3">
<div class="card-header">Database</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<td class="w-25">
Migration<br>
<span class="text-small">Newest: <?= $proxyDb->getNewestMigration() ?></span>
</td>
<td>
<select class="form-select w-50" id="setting-migration"><?= $migrations ?></select>
<span class="text-small">If you change this, refresh the page after you click save.</span>
</td>
</tr>
<tr>
<td>
Backup retention length<br>
<span class="text-small">Default: <?= BACKUP_AGE ?></span>
</td>
<td><input type="number" class="form-control d-inline-block w-25" id="setting-backupRetentionLength" value="<?= BACKUP_AGE ?>"> days</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="card border-default mb-3">
<div class="card-header">Logging</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<td class="w-25">
Rotation size<br>
<span class="text-small">Default: <?= LOG_ROTATE_SIZE ?></span>
</td>
<td><input type="number" class="form-control d-inline-block w-25" id="setting-logRotationSize" value="<?= LOG_ROTATE_SIZE ?>">MiB</td>
</tr>
<tr>
<td>
Log retention length<br>
<span class="text-small">Default: <?= LOG_AGE ?></span>
</td>
<td><input type="number" class="form-control d-inline-block w-25" id="setting-logRetentionLength" value="<?= LOG_AGE ?>"> days</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<?php
4 changes: 4 additions & 0 deletions root/app/www/public/pages/starr.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
----------------------------------
*/

if (!$_SESSION) {
session_start();
}

if (!$_SESSION['IN_UI']) {
exit('Invalid session, refresh the page');
}
Expand Down

0 comments on commit f7b374d

Please sign in to comment.