Commit c574efae authored by Elias Häußler's avatar Elias Häußler 🐛

[FEATURE] Add hooks and console to clear cookie files and log files

parent 15f4bc0a
......@@ -60,6 +60,24 @@ matching this domain name will be printed, otherwise sub domains of the current
`*` will print all sub domains for all available domain names.
## Console
The project includes a console which can be used to execute several functions from the command line. It's located in
the root directory of the project:
```bash
# General usage
./console.php
# Clear expired cookie files
./console.php clear:cookie
./console.php clear:cookie --lifetime 604800
# Clear log files
./console.php clear:logfile
```
## License
[MIT License](LICENSE.md)
......@@ -14,7 +14,8 @@
"spomky-labs/otphp": "^9.1",
"php": "^7.1",
"ext-json": "*",
"ext-curl": "*"
"ext-curl": "*",
"symfony/console": "^4.2"
},
"autoload": {
"psr-4": {
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "cbb77881fd563b9b2a66172142bb2da4",
"content-hash": "513937f13096692b7795b507ae9ec5d6",
"packages": [
{
"name": "beberlei/assert",
......@@ -184,6 +184,205 @@
],
"time": "2018-11-24T11:14:38+00:00"
},
{
"name": "symfony/console",
"version": "v4.2.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
"reference": "1f0ad51dfde4da8a6070f06adc58b4e37cbb37a4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/1f0ad51dfde4da8a6070f06adc58b4e37cbb37a4",
"reference": "1f0ad51dfde4da8a6070f06adc58b4e37cbb37a4",
"shasum": ""
},
"require": {
"php": "^7.1.3",
"symfony/contracts": "^1.0",
"symfony/polyfill-mbstring": "~1.0"
},
"conflict": {
"symfony/dependency-injection": "<3.4",
"symfony/process": "<3.3"
},
"provide": {
"psr/log-implementation": "1.0"
},
"require-dev": {
"psr/log": "~1.0",
"symfony/config": "~3.4|~4.0",
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/event-dispatcher": "~3.4|~4.0",
"symfony/lock": "~3.4|~4.0",
"symfony/process": "~3.4|~4.0"
},
"suggest": {
"psr/log": "For using the console logger",
"symfony/event-dispatcher": "",
"symfony/lock": "",
"symfony/process": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Console\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
"time": "2019-01-25T14:35:16+00:00"
},
{
"name": "symfony/contracts",
"version": "v1.0.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/contracts.git",
"reference": "1aa7ab2429c3d594dd70689604b5cf7421254cdf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/contracts/zipball/1aa7ab2429c3d594dd70689604b5cf7421254cdf",
"reference": "1aa7ab2429c3d594dd70689604b5cf7421254cdf",
"shasum": ""
},
"require": {
"php": "^7.1.3"
},
"require-dev": {
"psr/cache": "^1.0",
"psr/container": "^1.0"
},
"suggest": {
"psr/cache": "When using the Cache contracts",
"psr/container": "When using the Service contracts",
"symfony/cache-contracts-implementation": "",
"symfony/service-contracts-implementation": "",
"symfony/translation-contracts-implementation": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Contracts\\": ""
},
"exclude-from-classmap": [
"**/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "A set of abstractions extracted out of the Symfony components",
"homepage": "https://symfony.com",
"keywords": [
"abstractions",
"contracts",
"decoupling",
"interfaces",
"interoperability",
"standards"
],
"time": "2018-12-05T08:06:11+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.10.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "c79c051f5b3a46be09205c73b80b346e4153e494"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/c79c051f5b3a46be09205c73b80b346e4153e494",
"reference": "c79c051f5b3a46be09205c73b80b346e4153e494",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.9-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"time": "2018-09-21T13:07:52+00:00"
},
{
"name": "vlucas/phpdotenv",
"version": "v2.5.1",
......@@ -242,9 +441,9 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^7.1",
"ext-json": "*",
"ext-curl": "*",
"php": "^7.1"
"ext-curl": "*"
},
"platform-dev": []
}
#!/usr/bin/env php
<?php
/**
* Copyright (c) 2019 Elias Häußler <mail@elias-haeussler.de>. All rights reserved.
*/
declare(strict_types=1);
define("ROOT_PATH", __DIR__);
define("VENDOR_PATH", ROOT_PATH . "/vendor");
require_once VENDOR_PATH . "/autoload.php";
use EliasHaeussler\CpanelRequests\Command\ClearCookieCommand;
use EliasHaeussler\CpanelRequests\Command\ClearLogFileCommand;
use EliasHaeussler\CpanelRequests\Utility;
use Symfony\Component\Console\Application;
// Create application
$app = new Application("cPanel Requests console", Utility::getCurrentVersion());
// Register commands
$app->add(new ClearCookieCommand());
$app->add(new ClearLogFileCommand());
// Run application
/** @noinspection PhpUnhandledExceptionInspection */
$app->run();
<?php
/**
* Copyright (c) 2019 Elias Häußler <mail@elias-haeussler.de>. All rights reserved.
*/
declare(strict_types=1);
namespace EliasHaeussler\CpanelRequests\Command;
use EliasHaeussler\CpanelRequests\Hooks;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* `clear:cookie` console command.
*
* @package EliasHaeussler\CpanelRequests\Command
* @author Elias Häußler <mail@elias-haeussler.de>
* @license MIT
*/
class ClearCookieCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName("clear:cookie")
->setDescription("Clear cookie files")
->setHelp("This command clears cookie files whose lifetime has expired.");
$this->addOption(
"lifetime",
"l",
InputOption::VALUE_OPTIONAL,
"Lifetime of cookie files in seconds"
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$lifetime = (int) $input->getOption("lifetime");
$cleared_cookies = Hooks::clearCookieFiles($lifetime);
$output->writeln(
sprintf("<info>Cleared %s cookie%s.</info>", $cleared_cookies, $cleared_cookies != 1 ? "s" : "")
);
}
}
<?php
/**
* Copyright (c) 2019 Elias Häußler <mail@elias-haeussler.de>. All rights reserved.
*/
declare(strict_types=1);
namespace EliasHaeussler\CpanelRequests\Command;
use EliasHaeussler\CpanelRequests\Hooks;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* `clear:logfile` console command.
*
* @package EliasHaeussler\CpanelRequests\Command
* @author Elias Häußler <mail@elias-haeussler.de>
* @license MIT
*/
class ClearLogFileCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName("clear:logfile")
->setDescription("Clear log files")
->setHelp("This command clears all log files.");
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$cleared_log_files = Hooks::clearLogFiles();
$output->writeln(
sprintf("<info>Cleared %s log file%s.</info>", $cleared_log_files, $cleared_log_files != 1 ? "s" : "")
);
}
}
<?php
/**
* Copyright (c) 2019 Elias Häußler <mail@elias-haeussler.de>. All rights reserved.
*/
declare(strict_types=1);
namespace EliasHaeussler\CpanelRequests;
/**
* Hooks to interact with the project.
*
* @package EliasHaeussler\CpanelRequests
* @author Elias Häußler <mail@elias-haeussler.de>
* @license MIT
*/
class Hooks
{
/**
* Clear expired cookie files by given lifetime.
*
* @param int $lifetime Lifetime of cookie files in seconds (defaults to 7 days)
* @return int Number of cleared cookie files
*/
public static function clearCookieFiles(int $lifetime = 604800): int
{
$cleared_cookies = 0;
$cookie_files = sprintf("%s/%s/*.txt", ROOT_PATH, Cpanel::COOKIE_DIRECTORY);
$target_time = time() - $lifetime;
foreach (glob($cookie_files) as $file) {
$mod_time = filemtime($file);
if ($mod_time <= $target_time) {
if (@unlink($file)) {
$cleared_cookies++;
}
}
}
return $cleared_cookies;
}
/**
* Clear log files.
*
* @return int Number of cleared log files
*/
public static function clearLogFiles(): int
{
$cleared_log_files = 0;
$log_files = sprintf("%s/%s/*.log", ROOT_PATH, Cpanel::LOG_DIRECTORY);
foreach (glob($log_files) as $file) {
if (@unlink($file)) {
$cleared_log_files++;
}
}
return $cleared_log_files;
}
}
......@@ -144,4 +144,15 @@ class Utility
return sprintf("%s://%s.%s", $protocol, preg_replace("/\.$/", "", $sub_domain), $domain);
}
/**
* Get current Git version of this project.
*
* @return string Current Git version of this project
*/
public static function getCurrentVersion(): string
{
$command = sprintf("git --git-dir=%s/.git describe --tags 2> /dev/null", ROOT_PATH);
return exec($command) ?: "";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment