2024-06-14 17:20:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace KTH;
|
|
|
|
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2024-07-16 10:19:59 +02:00
|
|
|
use Symfony\Component\Yaml\Exception\ParseException;
|
2024-06-14 17:20:01 +02:00
|
|
|
|
|
|
|
class App {
|
|
|
|
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the cache file exists.
|
|
|
|
*
|
|
|
|
* @return bool True if the cache file exists, false otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public function cacheExist(): bool {
|
2024-06-17 11:48:42 +02:00
|
|
|
if (file_exists('../cache/index.html')) {
|
2024-06-14 17:20:01 +02:00
|
|
|
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 {
|
2024-07-16 10:19:59 +02:00
|
|
|
$menu = [];
|
2024-06-14 17:20:01 +02:00
|
|
|
foreach ($services as $service) {
|
2024-07-16 10:19:59 +02:00
|
|
|
if(!is_array($service)){
|
|
|
|
return $menu;
|
|
|
|
}
|
2024-06-14 17:20:01 +02:00
|
|
|
$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.
|
|
|
|
*/
|
2024-07-16 10:19:59 +02:00
|
|
|
public function returnImg(string|null $file, string $type): string {
|
2024-06-14 17:20:01 +02:00
|
|
|
$defaultFile = 'assets/icons/missing.svg';
|
|
|
|
$permitType = ['favicons', 'big_favicons', 'screenshots', 'thumbs'];
|
|
|
|
|
2024-07-16 10:19:59 +02:00
|
|
|
if (empty($file) || is_null($file)) {
|
2024-06-14 17:20:01 +02:00
|
|
|
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']);
|
2024-07-02 11:50:00 +02:00
|
|
|
$visibility = htmlspecialchars($_POST['visibility']);
|
|
|
|
$desc = htmlspecialchars($_POST['desc']);
|
2024-06-14 17:20:01 +02:00
|
|
|
|
|
|
|
if (isset($_POST['reimport'])) {
|
|
|
|
if (htmlspecialchars($_POST['reimport']) == '1') {
|
|
|
|
$_SESSION['reimport'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$userConfig = [
|
|
|
|
'colorScheme' => $colorScheme,
|
|
|
|
'view' => $view,
|
2024-07-02 11:50:00 +02:00
|
|
|
'title' => $title,
|
|
|
|
'visibility' => $visibility,
|
|
|
|
'desc' => $desc
|
2024-06-14 17:20:01 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$config = Yaml::dump($userConfig);
|
|
|
|
file_put_contents('../data/config.yaml', $config);
|
|
|
|
$this->clearCache();
|
|
|
|
}
|
|
|
|
|
2024-07-16 10:19:59 +02:00
|
|
|
/**
|
|
|
|
* 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' => ''];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-14 17:20:01 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2024-06-17 11:48:42 +02:00
|
|
|
if (file_exists('../cache/index.html')) {
|
|
|
|
unlink('../cache/index.html');
|
2024-06-14 17:20:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
2024-07-16 10:19:59 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2024-06-14 17:20:01 +02:00
|
|
|
}
|