2023-06-21 11:42:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
use App\DataBase\DataBase;
|
|
|
|
use App\Utils\Page;
|
|
|
|
use App\Utils\ShowImg;
|
|
|
|
use Noodlehaus\Config;
|
|
|
|
|
|
|
|
class Backend {
|
|
|
|
|
|
|
|
private $start = 0;
|
|
|
|
private $end = 15;
|
|
|
|
private $max = 15;
|
|
|
|
private $runningJob = false;
|
|
|
|
private $page = 'infos';
|
|
|
|
private $params;
|
|
|
|
private $conf;
|
|
|
|
private $title = 'Settings';
|
|
|
|
private $passwordRequired = false;
|
|
|
|
|
2024-03-15 12:17:10 +01:00
|
|
|
/**
|
|
|
|
* Handles the index action for the controller.
|
|
|
|
*
|
|
|
|
* This method performs various actions based on the provided parameters and
|
|
|
|
* configuration. It checks if the user is logged in, sets the page to display,
|
|
|
|
* checks if a job is running, and handles displaying images. It sets the
|
|
|
|
* configuration and parameters for the object. If the page is set to 'settings',
|
|
|
|
* it handles first run installation and option settings. Finally, it generates
|
|
|
|
* the HTML content for the page by requiring the navigation, infos or settings,
|
|
|
|
* and footer templates.
|
|
|
|
*
|
|
|
|
* @param object $params The parameters object.
|
|
|
|
* @param object $conf The configuration object.
|
|
|
|
*
|
|
|
|
* @return string The generated HTML content for the page.
|
|
|
|
*/
|
|
|
|
public function index(object $params, object $conf): string {
|
2023-06-21 11:42:36 +02:00
|
|
|
if (isset($params->loginPassword)) {
|
|
|
|
if (!Login::login($params->loginPassword, $conf->password)) {
|
|
|
|
$content = $this->showLogin();
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Login::isLogin()) {
|
|
|
|
$content = $this->showLogin();
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($params->page)) {
|
|
|
|
$this->page = $params->page;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(__DIR__ . '/../../cache/tmp/chrome-php-socket')) {
|
|
|
|
$this->runningJob = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($params->showImg)) {
|
2023-06-28 12:29:20 +02:00
|
|
|
$img = new ShowImg($params->type, $params->showImg, $conf->fileFormat);
|
2023-06-21 11:42:36 +02:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->conf = $conf;
|
|
|
|
$this->params = $params;
|
|
|
|
|
|
|
|
if ($this->page === 'settings') {
|
|
|
|
if (isset($_SESSION['firstRun']) && $_SESSION['firstRun'] === 1) {
|
|
|
|
$conf->webPage = false;
|
|
|
|
$this->title = 'Install';
|
|
|
|
$this->conf->key = bin2hex(random_bytes(12));
|
|
|
|
$this->passwordRequired = "required";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->params->token) && Page::verifToken($this->params->token)) {
|
|
|
|
$this->conf = $this->setOption($params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
require_once __DIR__ . '/../../tpl/nav.php';
|
|
|
|
|
|
|
|
if ($this->page === 'infos') {
|
|
|
|
$this->showInfos();
|
|
|
|
} else {
|
|
|
|
$this->showSettings();
|
|
|
|
}
|
|
|
|
require_once __DIR__ . '/../../tpl/footer.php';
|
|
|
|
$content = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:17:10 +01:00
|
|
|
/**
|
|
|
|
* Displays information about the generated data.
|
|
|
|
*
|
|
|
|
* This method sets the start and end index for the data to display based on the
|
|
|
|
* provided parameters. It retrieves the data list, total count, error count, and
|
|
|
|
* queue count from the `DataBase` class. It calculates the start, maximum, next,
|
|
|
|
* and previous index values for pagination. It then requires the 'infos.php'
|
|
|
|
* template to display the information.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-06-21 11:42:36 +02:00
|
|
|
private function showInfos() {
|
|
|
|
if (!empty($this->params->start)) {
|
|
|
|
$this->start = $this->params->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->params->end)) {
|
|
|
|
$this->end = $this->max + $this->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
$conx = new DataBase();
|
|
|
|
$genList = $conx->getList($this->start, $this->end);
|
|
|
|
$total = $conx->getTotal();
|
2023-06-29 10:08:37 +02:00
|
|
|
$inError = $conx->getInError();
|
|
|
|
$inQueue = $this->getInQueue();
|
2023-06-21 11:42:36 +02:00
|
|
|
$start = $this->start;
|
|
|
|
$max = $this->max;
|
|
|
|
$next = $start + $max;
|
|
|
|
$previous = $start - ($max);
|
|
|
|
|
|
|
|
if (count($genList) < $this->max) {
|
|
|
|
$next = $start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($previous < 0) {
|
|
|
|
$previous = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
require __DIR__ . '/../../tpl/infos.php';
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:17:10 +01:00
|
|
|
/**
|
|
|
|
* Displays the settings page.
|
|
|
|
*
|
|
|
|
* This method sets the start and end index for the data to display based on the
|
|
|
|
* provided parameters. It retrieves the data list and total count from the
|
|
|
|
* `DataBase` class. It calculates the start and maximum index values for
|
|
|
|
* pagination. It generates a CSRF token using the `Page::genToken` method. It
|
|
|
|
* then requires the 'settings.php' template to display the settings page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2023-06-21 11:42:36 +02:00
|
|
|
private function showSettings() {
|
|
|
|
if (!empty($this->params->start)) {
|
|
|
|
$this->start = $this->params->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->params->end)) {
|
|
|
|
$this->end = $this->max + $this->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->start < 0) {
|
|
|
|
$this->start = 0;
|
|
|
|
}
|
|
|
|
$conx = new DataBase();
|
|
|
|
$genList = $conx->getList($this->start, $this->end);
|
|
|
|
$total = $conx->getTotal();
|
|
|
|
$start = $this->start;
|
|
|
|
$max = $this->max;
|
|
|
|
$token = Page::genToken();
|
|
|
|
|
|
|
|
require __DIR__ . '/../../tpl/settings.php';
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:17:10 +01:00
|
|
|
/**
|
|
|
|
* Sets and saves user configuration options.
|
|
|
|
*
|
|
|
|
* This method checks if the configuration file exists and creates an empty one if
|
|
|
|
* it doesn't. It then loads the user configuration from the file using the `Config`
|
|
|
|
* class. It sets various configuration options based on the provided `$post`
|
|
|
|
* parameter, such as server settings, logging, PDF generation, icon size, cache
|
|
|
|
* expiration, maximum generations per batch, permit types, file format, password,
|
|
|
|
* key, and Chrome path. It saves the updated configuration to the file using the
|
|
|
|
* `Config` class. Finally, it loads the updated configuration from the file and
|
|
|
|
* returns it as an object.
|
|
|
|
*
|
|
|
|
* @param object $post The POST data containing the configuration options.
|
|
|
|
*
|
|
|
|
* @return object The updated configuration object.
|
|
|
|
*/
|
|
|
|
private function setOption(object $post): object {
|
2023-06-21 11:42:36 +02:00
|
|
|
if (!file_exists(__DIR__ . '/../../datas/config.json')) {
|
|
|
|
file_put_contents(__DIR__ . '/../../datas/config.json', json_encode([]));
|
|
|
|
unset($_SESSION['login']);
|
|
|
|
unset($_SESSION['firstRun']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$userConfig = __DIR__ . '/../../datas/config.json';
|
|
|
|
$uConfig = new Config($userConfig);
|
|
|
|
|
|
|
|
if (empty($post->onlyLocalServer)) {
|
|
|
|
$uConfig['onlyLocalServer'] = false;
|
|
|
|
} else {
|
|
|
|
$uConfig['onlyLocalServer'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($post->webPage)) {
|
|
|
|
$uConfig['webPage'] = false;
|
|
|
|
} else {
|
|
|
|
$uConfig['webPage'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($post->log)) {
|
|
|
|
$uConfig['log'] = false;
|
|
|
|
} else {
|
|
|
|
$uConfig['log'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($post->alwaysMakePdf)) {
|
|
|
|
$uConfig['alwaysMakePdf'] = false;
|
|
|
|
} else {
|
|
|
|
$uConfig['alwaysMakePdf'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($post->icoSize)) {
|
|
|
|
$uConfig['icoSize'] = (int)$post->icoSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($post->expireCache)) {
|
|
|
|
$uConfig['expireCache'] = (int)$post->expireCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($post->maxGenPerBatch)) {
|
|
|
|
$uConfig['maxGenPerBatch'] = (int)$post->maxGenPerBatch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($post->permitType)) {
|
|
|
|
$uConfig['permitType'] = $post->permitType;
|
|
|
|
} else {
|
|
|
|
$uConfig['permitType'] = [];
|
|
|
|
}
|
|
|
|
|
2023-06-28 12:29:20 +02:00
|
|
|
if (!empty($post->fileFormat)) {
|
|
|
|
$uConfig['fileFormat'] = $post->fileFormat;
|
|
|
|
}
|
|
|
|
|
2023-06-21 11:42:36 +02:00
|
|
|
if (!empty($post->password)) {
|
|
|
|
$uConfig['password'] = password_hash($post->password, PASSWORD_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($post->key)) {
|
|
|
|
$uConfig['key'] = $post->key;
|
|
|
|
}
|
|
|
|
|
2023-06-22 16:04:11 +02:00
|
|
|
if (!empty($post->chromePath)) {
|
|
|
|
$uConfig['chromePath'] = $post->chromePath;
|
2023-06-28 12:29:20 +02:00
|
|
|
} else {
|
|
|
|
$uConfig['chromePath'] = '';
|
2023-06-22 16:04:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 11:42:36 +02:00
|
|
|
$uConfig->toFile($userConfig);
|
|
|
|
|
|
|
|
$newConfig = new Config($userConfig);
|
|
|
|
return (object)$newConfig->all();
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:17:10 +01:00
|
|
|
/**
|
|
|
|
* Gets the number of files in the queue directory.
|
|
|
|
*
|
|
|
|
* This method uses the `glob` function to search for JSON files in the queue
|
|
|
|
* directory. If any files are found, it returns the count of files. Otherwise,
|
|
|
|
* it returns 0.
|
|
|
|
*
|
|
|
|
* @return int The number of files in the queue directory.
|
|
|
|
*/
|
|
|
|
private function getInQueue(): int {
|
2023-06-29 10:08:37 +02:00
|
|
|
if (glob(__DIR__ . '/../../cache/queue/*.json') != false) {
|
|
|
|
$fileCount = count(glob(__DIR__ . '/../../cache/queue/*.json'));
|
|
|
|
} else {
|
|
|
|
$fileCount = 0;
|
|
|
|
}
|
|
|
|
return $fileCount;
|
|
|
|
}
|
|
|
|
|
2024-03-15 12:17:10 +01:00
|
|
|
/**
|
|
|
|
* Displays the login page.
|
|
|
|
*
|
|
|
|
* This method generates a CSRF token using the `Page::genToken` method. It then
|
|
|
|
* requires the 'login.php' template to display the login page. Finally, it returns
|
|
|
|
* the generated HTML content as a string.
|
|
|
|
*
|
|
|
|
* @return string The generated HTML content for the login page.
|
|
|
|
*/
|
|
|
|
private function showLogin(): string {
|
2023-06-21 11:42:36 +02:00
|
|
|
ob_start();
|
|
|
|
$token = Page::genToken();
|
|
|
|
require_once __DIR__ . '/../../tpl/login.php';
|
|
|
|
$content = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
}
|