299 lines
9.3 KiB
PHP
299 lines
9.3 KiB
PHP
<?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;
|
|
|
|
/**
|
|
* 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 {
|
|
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)) {
|
|
$img = new ShowImg($params->type, $params->showImg, $conf->fileFormat);
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
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();
|
|
$inError = $conx->getInError();
|
|
$inQueue = $this->getInQueue();
|
|
$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';
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
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';
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
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'] = [];
|
|
}
|
|
|
|
if (!empty($post->fileFormat)) {
|
|
$uConfig['fileFormat'] = $post->fileFormat;
|
|
}
|
|
|
|
if (!empty($post->password)) {
|
|
$uConfig['password'] = password_hash($post->password, PASSWORD_DEFAULT);
|
|
}
|
|
|
|
if (!empty($post->key)) {
|
|
$uConfig['key'] = $post->key;
|
|
}
|
|
|
|
if (!empty($post->chromePath)) {
|
|
$uConfig['chromePath'] = $post->chromePath;
|
|
} else {
|
|
$uConfig['chromePath'] = '';
|
|
}
|
|
|
|
$uConfig->toFile($userConfig);
|
|
|
|
$newConfig = new Config($userConfig);
|
|
return (object)$newConfig->all();
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
if (glob(__DIR__ . '/../../cache/queue/*.json') != false) {
|
|
$fileCount = count(glob(__DIR__ . '/../../cache/queue/*.json'));
|
|
} else {
|
|
$fileCount = 0;
|
|
}
|
|
return $fileCount;
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
ob_start();
|
|
$token = Page::genToken();
|
|
require_once __DIR__ . '/../../tpl/login.php';
|
|
$content = ob_get_contents();
|
|
ob_end_clean();
|
|
return $content;
|
|
}
|
|
}
|