From 33edd851aa33ca292276c7a1561fac43a347a55a Mon Sep 17 00:00:00 2001 From: Austin Best Date: Tue, 30 Jan 2024 23:42:04 -0500 Subject: [PATCH] Add a settings page Fix a bunch of paths for docker --- root/app/www/public/ajax/branches.php | 2 +- root/app/www/public/ajax/code.php | 3 +- root/app/www/public/ajax/commits.php | 2 +- root/app/www/public/ajax/common.php | 2 +- root/app/www/public/ajax/contributors.php | 2 +- root/app/www/public/ajax/overview.php | 2 +- root/app/www/public/ajax/settings.php | 58 ++++++++++++++++++++++ root/app/www/public/ajax/shared.php | 13 ++++- root/app/www/public/classes/Git.php | 6 +-- root/app/www/public/functions/common.php | 16 +++++- root/app/www/public/includes/constants.php | 3 +- root/app/www/public/includes/footer.php | 4 +- root/app/www/public/includes/header.php | 6 +++ root/app/www/public/index.php | 8 ++- root/app/www/public/js/common.js | 34 ++++++++++++- root/app/www/public/loader.php | 33 ++++++++---- 16 files changed, 166 insertions(+), 28 deletions(-) create mode 100644 root/app/www/public/ajax/settings.php diff --git a/root/app/www/public/ajax/branches.php b/root/app/www/public/ajax/branches.php index b2f9c6d..d072c44 100644 --- a/root/app/www/public/ajax/branches.php +++ b/root/app/www/public/ajax/branches.php @@ -10,7 +10,7 @@ require 'shared.php'; if ($_POST['m'] == 'init') { - ?>

Branches ./' . $repository ?>


Branches


diff --git a/root/app/www/public/ajax/code.php b/root/app/www/public/ajax/code.php index 6468701..273a3b3 100644 --- a/root/app/www/public/ajax/code.php +++ b/root/app/www/public/ajax/code.php @@ -10,7 +10,8 @@ require 'shared.php'; if ($_POST['m'] == 'init') { - ?>

Code ./' . $repository ?>


Code


$fileTypeData) { diff --git a/root/app/www/public/ajax/commits.php b/root/app/www/public/ajax/commits.php index a708e64..7ba8b4c 100644 --- a/root/app/www/public/ajax/commits.php +++ b/root/app/www/public/ajax/commits.php @@ -10,7 +10,7 @@ require 'shared.php'; if ($_POST['m'] == 'init') { - ?>

Commits ./' . $repository ?>


Commits


(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}/'; diff --git a/root/app/www/public/ajax/common.php b/root/app/www/public/ajax/common.php index 0c0c5cd..91c70fb 100644 --- a/root/app/www/public/ajax/common.php +++ b/root/app/www/public/ajax/common.php @@ -41,7 +41,7 @@ if ($_POST['m'] == 'cloneRepository') { echo '

Clone result

'; - if (is_dir(RELATIVE_PATH . REPOSITORY_PATH . $_POST['folder'])) { + if (is_dir(REPOSITORY_PATH . $_POST['folder'])) { $error = 'The path ' . REPOSITORY_PATH . $_POST['folder'] . ' is already used'; } diff --git a/root/app/www/public/ajax/contributors.php b/root/app/www/public/ajax/contributors.php index b73386b..ab6f625 100644 --- a/root/app/www/public/ajax/contributors.php +++ b/root/app/www/public/ajax/contributors.php @@ -10,7 +10,7 @@ require 'shared.php'; if ($_POST['m'] == 'init') { - ?>

Contributors ./' . $repository ?>


Contributors


Overview ./' . $repository ?>


Overview


(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}~{(?(.*))}/'; diff --git a/root/app/www/public/ajax/settings.php b/root/app/www/public/ajax/settings.php new file mode 100644 index 0000000..f574695 --- /dev/null +++ b/root/app/www/public/ajax/settings.php @@ -0,0 +1,58 @@ +

Settings


+ + + + + + + + + + + + + + + + + + + +
Global
SettingValueDescription
Repository locationThis is the location of all git repositories to load and use
+ $val) { + if ($key == 'm') { + continue; + } + + $field = $key; + if (str_contains($field, '-')) { + list($cat, $field) = explode('-', $key); + } + + if ($cat) { + $settings[$cat][$field] = $val; + } else { + $settings[$field] = $val; + } + } + + file_put_contents(SETTINGS_FILE, json_encode($settings)); +} diff --git a/root/app/www/public/ajax/shared.php b/root/app/www/public/ajax/shared.php index 0fcf816..813bc10 100644 --- a/root/app/www/public/ajax/shared.php +++ b/root/app/www/public/ajax/shared.php @@ -7,7 +7,16 @@ ---------------------------------- */ -require '../loader.php'; +if (file_exists('loader.php')) { + define('ABSOLUTE_PATH', './'); +} +if (file_exists('../loader.php')) { + define('ABSOLUTE_PATH', '../'); +} +if (file_exists('../../loader.php')) { + define('ABSOLUTE_PATH', '../../'); +} +require ABSOLUTE_PATH . 'loader.php'; $repository = $_POST['repository']; $git = new Git($repository); @@ -31,7 +40,7 @@ sort($fileParts, SORT_NUMERIC); $linesOfCode += intval($fileParts[1]); - if (str_contains($fileParts[0], '.') && $fileParts[0][0] != '.' && !is_dir(RELATIVE_PATH . $repository . '.' . $fileParts[0][0])) { + if (str_contains($fileParts[0], '.') && $fileParts[0][0] != '.' && !is_dir(ABSOLUTE_PATH . $repository . '.' . $fileParts[0][0])) { $filePathParts = explode('.', $fileParts[0]); $extension = trim(end($filePathParts)); diff --git a/root/app/www/public/classes/Git.php b/root/app/www/public/classes/Git.php index 32cb32b..fe955f2 100644 --- a/root/app/www/public/classes/Git.php +++ b/root/app/www/public/classes/Git.php @@ -8,7 +8,7 @@ */ //-- BRING IN THE TRAITS -$traits = RELATIVE_PATH . 'classes/traits/Git/'; +$traits = ABSOLUTE_PATH . 'classes/traits/Git/'; $traitsDir = opendir($traits); while ($traitFile = readdir($traitsDir)) { if (str_contains($traitFile, '.php')) { @@ -30,7 +30,7 @@ class Git public function __construct($repository) { $this->repository = $repository; - $this->cd = 'cd ' . RELATIVE_PATH . $this->repository .' && '; + $this->cd = 'cd ' . ABSOLUTE_PATH . $this->repository .' && '; } public function __toString() @@ -56,7 +56,7 @@ public function pull() public function clone($repository, $folder) { - $cmd = 'git clone "' . $repository . '" ' . RELATIVE_PATH . REPOSITORY_PATH . $folder . ' 2>&1'; + $cmd = 'git clone "' . $repository . '" ' . REPOSITORY_PATH . $folder . ' 2>&1'; $shell = shell_exec($cmd); return ['cmd' => $cmd, 'shell' => $shell]; diff --git a/root/app/www/public/functions/common.php b/root/app/www/public/functions/common.php index 1987f68..807ecfd 100644 --- a/root/app/www/public/functions/common.php +++ b/root/app/www/public/functions/common.php @@ -7,6 +7,20 @@ ---------------------------------- */ +function startup() +{ + if (!file_exists(SETTINGS_FILE)) { + file_put_contents(SETTINGS_FILE, '{}'); + } + + createDirectoryTree(REPOSITORY_PATH); +} + +function createDirectoryTree($tree) +{ + system('mkdir -p ' . $tree); +} + function getRepositoryList() { $repositories = []; @@ -71,4 +85,4 @@ function byteConversion($bytes, $measurement = false, $dec = 2) case 'TiB': return round($bytes / pow(1024, 4), $dec); } -} \ No newline at end of file +} diff --git a/root/app/www/public/includes/constants.php b/root/app/www/public/includes/constants.php index 65cab9b..d818a70 100644 --- a/root/app/www/public/includes/constants.php +++ b/root/app/www/public/includes/constants.php @@ -7,4 +7,5 @@ ---------------------------------- */ -define('REPOSITORY_PATH', 'repositories/'); +define('APP_DATA_PATH', '/config/'); +define('SETTINGS_FILE', APP_DATA_PATH . 'settings.json'); diff --git a/root/app/www/public/includes/footer.php b/root/app/www/public/includes/footer.php index 71b7c9e..5adecb2 100644 --- a/root/app/www/public/includes/footer.php +++ b/root/app/www/public/includes/footer.php @@ -37,7 +37,7 @@ - - + + diff --git a/root/app/www/public/includes/header.php b/root/app/www/public/includes/header.php index a6b7262..1a13e75 100644 --- a/root/app/www/public/includes/header.php +++ b/root/app/www/public/includes/header.php @@ -80,6 +80,12 @@ Code +
  • Theme by bootstrapdash diff --git a/root/app/www/public/index.php b/root/app/www/public/index.php index ad045aa..a7014dd 100644 --- a/root/app/www/public/index.php +++ b/root/app/www/public/index.php @@ -17,11 +17,15 @@
    -
    +
    No repositories found.
    -
    Please clone/add them to
    + +
    Please clone/add them to
    + +
    Please clone/add them to or open the Settings and specify a path (such as /config/repositories for docker)
    +
    diff --git a/root/app/www/public/js/common.js b/root/app/www/public/js/common.js index f491bbb..7bd5b79 100644 --- a/root/app/www/public/js/common.js +++ b/root/app/www/public/js/common.js @@ -5,7 +5,7 @@ $(document).ready(function () { function loadPage(page) { const repository = $('#active-repository').val(); - if (repository == '0') { + if (repository == '0' && page != 'settings') { toast('Active Repository', 'Please select a repository before trying to view information', 'error'); return; } @@ -22,6 +22,33 @@ function loadPage(page) }); } // --------------------------------------------------------------------------------------------- +function saveSettings() +{ + let params = ''; + $.each($('.setting'), function () { + const field = $(this).attr('id'); + + let val = ''; + if ($(this).is(':checkbox') || $(this).is(':radio')) { + val = $(this).prop('checked') ? 1 : 0; + } else { + val = $(this).val(); + } + + params += '&' + field + '=' + val; + }); + + $.ajax({ + type: 'POST', + url: 'ajax/settings.php', + data: '&m=saveSettings' + params, + success: function (resultData) { + toast('Settings', 'Settings have been updated', 'info'); + reload(); + } + }); +} +// --------------------------------------------------------------------------------------------- function cloneRepository() { const url = $('#clone-url').val(); @@ -248,3 +275,8 @@ function dialogClose(elm) } // ------------------------------------------------------------------------------------------- +function reload() +{ + window.location.href='/'; +} +// ------------------------------------------------------------------------------------------- diff --git a/root/app/www/public/loader.php b/root/app/www/public/loader.php index d869db1..98bcd98 100644 --- a/root/app/www/public/loader.php +++ b/root/app/www/public/loader.php @@ -10,20 +10,32 @@ // This will NOT report uninitialized variables error_reporting(E_ERROR | E_PARSE); -//-- DETERMINE LOCATION -if (file_exists('loader.php')) { - define('RELATIVE_PATH', './'); -} elseif (file_exists('../loader.php')) { - define('RELATIVE_PATH', '../'); -} elseif (file_exists('../../loader.php')) { - define('RELATIVE_PATH', '../../'); +if (!defined('ABSOLUTE_PATH')) { + if (file_exists('loader.php')) { + define('ABSOLUTE_PATH', './'); + } + if (file_exists('../loader.php')) { + define('ABSOLUTE_PATH', '../'); + } + if (file_exists('../../loader.php')) { + define('ABSOLUTE_PATH', '../../'); + } } //-- INCLUDE DEFINES -require RELATIVE_PATH . 'includes/constants.php'; +require ABSOLUTE_PATH . 'includes/constants.php'; + +//-- GRAB SETTINGS FILE DATA +$settings = json_decode(file_get_contents(SETTINGS_FILE), true); +if ($settings['global']['repositoryPath']) { + if (substr($settings['global']['repositoryPath'], -1) != '/') { + $settings['global']['repositoryPath'] .= '/'; + } +} +define('REPOSITORY_PATH', ($settings['global']['repositoryPath'] ? $settings['global']['repositoryPath'] : '/config/repositories/')); //-- INCLUDE FUNCTIONS -$dir = RELATIVE_PATH . 'functions'; +$dir = ABSOLUTE_PATH . 'functions'; $handle = opendir($dir); while ($file = readdir($handle)) { if ($file[0] != '.' && !is_dir($dir . '/' . $file)) { @@ -33,7 +45,7 @@ closedir($handle); //-- INCLUDE CLASSES -$dir = RELATIVE_PATH . 'classes'; +$dir = ABSOLUTE_PATH . 'classes'; $handle = opendir($dir); while ($file = readdir($handle)) { if ($file[0] != '.' && !is_dir($dir . '/' . $file)) { @@ -42,4 +54,5 @@ } closedir($handle); +startup(); $repositories = getRepositoryList();