Nofu/app/App.php

171 lines
4.7 KiB
PHP

<?php
namespace KTH;
use Symfony\Component\Yaml\Yaml;
class App {
private $config;
/**
* Check if the cache file exists.
*
* @return bool True if the cache file exists, false otherwise.
*/
public function cacheExist(): bool {
if (file_exists('../cache/index.html')) {
return true;
}
return false;
}
/**
* Create a menu from an array of services.
*
* @param array $services An array of service data.
*
* @return array An array containing the unique types and locations of the services.
*/
public function makeMenu(array $services): array {
foreach ($services as $service) {
$menu['type'][] = $service['type'];
$menu['location'][] = $service['location'];
}
$menu['type'] = array_unique($menu['type'], SORT_LOCALE_STRING);
$menu['location'] = array_unique($menu['location'], SORT_LOCALE_STRING);
return $menu;
}
/**
* Return the path to an image file or a default image if the file does not exist.
*
* @param string $file The name of the image file.
* @param string $type The type of image (e.g., 'favicons', 'screenshots').
*
* @return string The path to the image file or a default image.
*/
public function returnImg(string $file, string $type): string {
$defaultFile = 'assets/icons/missing.svg';
$permitType = ['favicons', 'big_favicons', 'screenshots', 'thumbs'];
if (empty($file)) {
return $defaultFile;
}
if (!in_array($type, $permitType)) {
return $defaultFile;
}
if (filter_var($file, FILTER_VALIDATE_URL)) {
return $file;
}
if (file_exists('imgs/' . $type . '/' . $file . '.webp')) {
return 'imgs/' . $type . '/' . $file . '.webp';
}
return $defaultFile;
}
/**
* Check if the current IP address is permitted to bypass authentication.
*
* @param array|null $permitIP An array of permitted IP addresses or null if all IP addresses are permitted.
*
* @return bool True if the current IP address is permitted to bypass authentication, false otherwise.
*/
public function canByPassAuth(array|null $permitIP): bool {
if ($permitIP === null) {
return true;
}
if (in_array($_SERVER['REMOTE_ADDR'], $permitIP)) {
return true;
}
return false;
}
/**
* Save the user configuration settings.
*
* @return void
*/
function saveUserConfig(): void {
$colorScheme = htmlspecialchars($_POST['colorScheme']);
$view = htmlspecialchars($_POST['view']);
$title = htmlspecialchars($_POST['title']);
if (isset($_POST['reimport'])) {
if (htmlspecialchars($_POST['reimport']) == '1') {
$_SESSION['reimport'] = true;
}
}
$userConfig = [
'colorScheme' => $colorScheme,
'view' => $view,
'title' => $title
];
$config = Yaml::dump($userConfig);
file_put_contents('../data/config.yaml', $config);
$this->clearCache();
}
/**
* Get the configuration settings.
*
* @param array $defConfig The default configuration settings.
*
* @return array The configuration settings.
*/
public function getConfig(array $defConfig): array {
if (file_exists('../data/config.yaml')) {
$userConfig = Yaml::parseFile('../data/config.yaml');
if (empty($userConfig)) {
return $defConfig;
}
$config = array_merge($defConfig, $userConfig);
$this->config = $config;
return $config;
}
$this->config = $defConfig;
return $defConfig;
}
/**
* Clear the cache file.
*
* @return void
*/
public function clearCache(): void {
if (file_exists('../cache/index.html')) {
unlink('../cache/index.html');
}
}
/**
* Initialize the data directories.
*
* @return void
*/
static function initializeDataDir(): void {
if (!is_dir(('../data/assets/css'))) {
mkdir('../data/assets/css', 0775, true);
}
if (!is_dir(('../data/assets/icons'))) {
mkdir('../data/assets/icons', 0775, true);
}
if (!is_dir(('../data/assets/js'))) {
mkdir('../data/assets/js', 0775, true);
}
if (!is_dir(('../data/imgs/favicons'))) {
mkdir('../data/imgs/favicons', 0775, true);
}
if (!is_dir(('../data/imgs/screenshots'))) {
mkdir('../data/imgs/screenshots', 0775, true);
}
}
}