229 lines
6.9 KiB
PHP
229 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace KTH;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
|
use Symfony\Component\Yaml\Exception\ParseException;
|
|
|
|
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 {
|
|
$menu = [];
|
|
foreach ($services as $service) {
|
|
if(!is_array($service)){
|
|
return $menu;
|
|
}
|
|
$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|null $file, string $type): string {
|
|
$defaultFile = 'assets/icons/missing.svg';
|
|
$permitType = ['favicons', 'big_favicons', 'screenshots', 'thumbs'];
|
|
|
|
if (empty($file) || is_null($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']);
|
|
$visibility = htmlspecialchars($_POST['visibility']);
|
|
$desc = htmlspecialchars($_POST['desc']);
|
|
|
|
if (isset($_POST['reimport'])) {
|
|
if (htmlspecialchars($_POST['reimport']) == '1') {
|
|
$_SESSION['reimport'] = true;
|
|
}
|
|
}
|
|
|
|
$userConfig = [
|
|
'colorScheme' => $colorScheme,
|
|
'view' => $view,
|
|
'title' => $title,
|
|
'visibility' => $visibility,
|
|
'desc' => $desc
|
|
];
|
|
|
|
$config = Yaml::dump($userConfig);
|
|
file_put_contents('../data/config.yaml', $config);
|
|
$this->clearCache();
|
|
}
|
|
|
|
/**
|
|
* Save services list
|
|
*
|
|
* @return void
|
|
*/
|
|
function saveServices(): array {
|
|
try {
|
|
$value = Yaml::parse(trim($_POST['services'], 2));
|
|
} catch (ParseException $exception) {
|
|
return ['status' => 'error', 'message' => $exception->getMessage(), 'content' => $_POST['services']];
|
|
}
|
|
|
|
file_put_contents('../data/services.yaml', $_POST['services']);
|
|
$this->clearCache();
|
|
return ['status' => 'success', 'message' => ''];
|
|
}
|
|
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|
|
|
|
public function getServices() {
|
|
if (file_exists('../data/services.yaml')) {
|
|
$services = Yaml::parseFile('../data/services.yaml');
|
|
} else {
|
|
$services = [
|
|
0 => [
|
|
'title' => 'Wikipedia',
|
|
'screenshot' => 'wikipedia.png',
|
|
'favicon' => 'wikipedia.png',
|
|
'link' => 'https://en.wikipedia.org/wiki/Dashboard_(computing)',
|
|
'appHome' => 'https://www.mediawiki.org/wiki/MediaWiki',
|
|
'location' => 'web',
|
|
'desc' => 'Wikipedia, the free encyclopedia',
|
|
'type' => 'webapp'
|
|
],
|
|
[
|
|
'title' => 'Awesome-Selfhosted',
|
|
'screenshot' => 'Awesome-Selfhosted.png',
|
|
'favicon' => 'Awesome-Selfhosted.ico',
|
|
'link' => 'https://awesome-selfhosted.net/',
|
|
'appHome' => 'https://www.mediawiki.org/wiki/MediaWiki',
|
|
'location' => 'web',
|
|
'desc' => 'A list of Free Software network services and web applications which can be hosted on your own servers',
|
|
'type' => 'webapp'
|
|
]
|
|
];
|
|
}
|
|
|
|
return $services;
|
|
}
|
|
}
|