add script for php-session cleanup
Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
31
install/scripts/php-sessionclean.php
Executable file
31
install/scripts/php-sessionclean.php
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file is part of the Froxlor project.
|
||||||
|
* Copyright (c) 2022 the Froxlor Team (see authors).
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the COPYING
|
||||||
|
* file that was distributed with this source code. You can also view the
|
||||||
|
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
||||||
|
*
|
||||||
|
* @copyright (c) the authors
|
||||||
|
* @author Froxlor team <team@froxlor.org> (2018-)
|
||||||
|
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||||
|
* @package Cron
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Check if we're in the CLI
|
||||||
|
if (@php_sapi_name() !== 'cli') {
|
||||||
|
die('This script will only work in the shell.');
|
||||||
|
}
|
||||||
|
|
||||||
|
require dirname(dirname(__DIR__)) . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
// give control to command line handler
|
||||||
|
try {
|
||||||
|
\Froxlor\Cli\PhpSessioncleanCmd::processParameters($argc, $argv);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
\Froxlor\Cli\PhpSessioncleanCmd::printerr($e->getMessage());
|
||||||
|
}
|
||||||
100
lib/Froxlor/Cli/Action/PhpSessioncleanAction.php
Normal file
100
lib/Froxlor/Cli/Action/PhpSessioncleanAction.php
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Froxlor\Cli\Action;
|
||||||
|
|
||||||
|
use Froxlor\Database\Database;
|
||||||
|
use Froxlor\Settings;
|
||||||
|
use Froxlor\Cli\PhpSessioncleanCmd;
|
||||||
|
|
||||||
|
class PhpSessioncleanAction extends \Froxlor\Cli\Action
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct($args)
|
||||||
|
{
|
||||||
|
parent::__construct($args);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
$this->validate();
|
||||||
|
|
||||||
|
if ((int) Settings::Get('phpfpm.enabled') == 1) {
|
||||||
|
if (isset($this->_args["max-lifetime"]) && is_numeric($this->_args["max-lifetime"]) && $this->_args["max-lifetime"] > 0) {
|
||||||
|
$this->cleanSessionfiles((int)$this->_args["max-lifetime"]);
|
||||||
|
} else {
|
||||||
|
// use default max-lifetime value
|
||||||
|
$this->cleanSessionfiles();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* validates the parsed command line parameters
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
private function validate()
|
||||||
|
{
|
||||||
|
global $lng;
|
||||||
|
|
||||||
|
$this->checkConfigParam(true);
|
||||||
|
$this->parseConfig();
|
||||||
|
|
||||||
|
require FROXLOR_INSTALL_DIR . '/lib/tables.inc.php';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function cleanSessionfiles(int $maxlifetime = 1440)
|
||||||
|
{
|
||||||
|
// store paths to clean up
|
||||||
|
$paths_to_clean = [];
|
||||||
|
// get all pool-config directories configured
|
||||||
|
$sel_stmt = Database::prepare("SELECT DISTINCT `config_dir` FROM `" . TABLE_PANEL_FPMDAEMONS . "`");
|
||||||
|
Database::pexecute($sel_stmt);
|
||||||
|
while ($fpmd = $sel_stmt->fetch(\PDO::FETCH_ASSOC)) {
|
||||||
|
$poolfiles = glob(\Froxlor\FileDir::makeCorrectFile($fpmd['config_dir'] . '/*.conf'));
|
||||||
|
foreach ($poolfiles as $cf) {
|
||||||
|
$contents = file_get_contents($cf);
|
||||||
|
$pattern = preg_quote('session.save_path', '/');
|
||||||
|
$pattern = "/" . $pattern . ".+?\=(.*)/";
|
||||||
|
if (preg_match_all($pattern, $contents, $matches)) {
|
||||||
|
$paths_to_clean[] = \Froxlor\FileDir::makeCorrectDir(trim($matches[1][0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// every path is just needed once
|
||||||
|
$paths_to_clean = array_unique($paths_to_clean);
|
||||||
|
|
||||||
|
if (count($paths_to_clean) > 0) {
|
||||||
|
foreach ($paths_to_clean as $ptc) {
|
||||||
|
// find all files older then maxlifetime and delete them
|
||||||
|
\Froxlor\FileDir::safe_exec("find -O3 \"" . $ptc . "\" -ignore_readdir_race -depth -mindepth 1 -name 'sess_*' -type f -cmin \"+" . $maxlifetime . "\" -delete");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private function parseConfig()
|
||||||
|
{
|
||||||
|
define('FROXLOR_INSTALL_DIR', $this->_args['froxlor-dir']);
|
||||||
|
if (!class_exists('\\Froxlor\\Database\\Database')) {
|
||||||
|
throw new \Exception("Could not find froxlor's Database class. Is froxlor really installed to '" . FROXLOR_INSTALL_DIR . "'?");
|
||||||
|
}
|
||||||
|
if (!file_exists(FROXLOR_INSTALL_DIR . '/lib/userdata.inc.php')) {
|
||||||
|
throw new \Exception("Could not find froxlor's userdata.inc.php file. You should use this script only with a fully installed and setup froxlor system.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkConfigParam($needed = false)
|
||||||
|
{
|
||||||
|
if ($needed) {
|
||||||
|
if (!isset($this->_args["froxlor-dir"])) {
|
||||||
|
$this->_args["froxlor-dir"] = \Froxlor\Froxlor::getInstallDir();
|
||||||
|
} elseif (!is_dir($this->_args["froxlor-dir"])) {
|
||||||
|
throw new \Exception("Given --froxlor-dir parameter is not a directory");
|
||||||
|
} elseif (!file_exists($this->_args["froxlor-dir"])) {
|
||||||
|
throw new \Exception("Given froxlor directory cannot be found ('" . $this->_args["froxlor-dir"] . "')");
|
||||||
|
} elseif (!is_readable($this->_args["froxlor-dir"])) {
|
||||||
|
throw new \Exception("Given froxlor directory cannot be read ('" . $this->_args["froxlor-dir"] . "')");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
64
lib/Froxlor/Cli/PhpSessioncleanCmd.php
Normal file
64
lib/Froxlor/Cli/PhpSessioncleanCmd.php
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Froxlor\Cli;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file is part of the Froxlor project.
|
||||||
|
* Copyright (c) 2022 the Froxlor Team (see authors).
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the COPYING
|
||||||
|
* file that was distributed with this source code. You can also view the
|
||||||
|
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
|
||||||
|
*
|
||||||
|
* @copyright (c) the authors
|
||||||
|
* @author Froxlor team <team@froxlor.org> (2018-)
|
||||||
|
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
|
||||||
|
* @package Cron
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class PhpSessioncleanCmd extends CmdLineHandler
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list of valid switches
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $switches = array(
|
||||||
|
'h'
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* list of valid parameters
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $params = array(
|
||||||
|
'froxlor-dir',
|
||||||
|
'max-lifetime',
|
||||||
|
'help'
|
||||||
|
);
|
||||||
|
|
||||||
|
public static $action_class = '\\Froxlor\\Cli\\Action\\PhpSessioncleanAction';
|
||||||
|
|
||||||
|
public static function printHelp()
|
||||||
|
{
|
||||||
|
self::println("");
|
||||||
|
self::println("Help / command line parameters:");
|
||||||
|
self::println("");
|
||||||
|
// commands
|
||||||
|
self::println("--froxlor-dir\t\tpath to froxlor installation");
|
||||||
|
self::println("\t\t\tExample: --froxlor-dir=/var/www/froxlor/");
|
||||||
|
self::println("");
|
||||||
|
self::println("--max-lifetime\t\tThe number of seconds after which data will be seen as 'garbage' and potentially cleaned up. Defaults to '1440'");
|
||||||
|
self::println("\t\t\tExample: --max-lifetime=2000");
|
||||||
|
self::println("");
|
||||||
|
self::println("--help\t\t\tshow help screen (this)");
|
||||||
|
self::println("");
|
||||||
|
// switches
|
||||||
|
self::println("-h\t\t\tsame as --help");
|
||||||
|
self::println("");
|
||||||
|
|
||||||
|
die(); // end of execution
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user