dcAdvancedCleaner/inc/class.advanced.cleaner.php

184 lines
4.5 KiB
PHP
Raw Normal View History

2021-10-28 20:17:38 +00:00
<?php
/**
* @brief dcAdvancedCleaner, a plugin for Dotclear 2
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
abstract class advancedCleaner
{
private static $exclude = [
2022-11-15 10:34:45 +00:00
'.', '..', '__MACOSX', '.svn', 'CVS', '.DS_Store', 'Thumbs.db',
2021-10-28 20:17:38 +00:00
];
private $properties = [
'id' => '',
'name' => '',
2022-11-15 10:34:45 +00:00
'desc' => '',
2021-10-28 20:17:38 +00:00
];
private $actions = [];
2022-11-15 10:34:45 +00:00
final public function __construct()
2021-10-28 20:17:38 +00:00
{
$this->init();
}
2022-11-15 10:34:45 +00:00
public static function create(arrayObject $o)
2021-10-28 20:17:38 +00:00
{
$c = get_called_class();
2022-11-15 10:34:45 +00:00
$o->append(new $c());
2021-10-28 20:17:38 +00:00
}
final public function __get(string $property)
{
return $this->getProperty($property);
}
final public function getProperty(string $property)
{
return $this->properties[$property] ?? null;
}
final protected function setProperties($property, $value = null): bool
{
$properties = is_array($property) ? $property : [$property => $value];
2021-11-06 15:19:49 +00:00
foreach ($properties as $k => $v) {
2021-10-28 20:17:38 +00:00
if (isset($this->properties[$k])) {
$this->properties[$k] = (string) $v;
}
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return true;
}
final public function getActions()
{
return $this->actions;
}
final protected function setActions($action, $name = null): bool
{
$actions = is_array($action) ? $action : [$action => $name];
2021-11-06 15:19:49 +00:00
foreach ($actions as $k => $v) {
2021-10-28 20:17:38 +00:00
$this->actions[$k] = (string) $v;
}
return true;
}
abstract protected function init(): bool;
abstract public function error($action): string;
abstract public function official(): array;
abstract public function get(): array;
abstract public function set($action, $ns): bool;
# helpers
protected static function getDirs($roots)
{
if (!is_array($roots)) {
$roots = [$roots];
}
$rs = [];
2021-11-06 15:19:49 +00:00
$i = 0;
2021-10-28 20:17:38 +00:00
foreach ($roots as $root) {
$dirs = files::scanDir($root);
2021-11-06 15:19:49 +00:00
foreach ($dirs as $k) {
if ('.' == $k || '..' == $k || !is_dir($root . '/' . $k)) {
2021-10-28 20:17:38 +00:00
continue;
}
2021-11-06 15:19:49 +00:00
$rs[$i]['key'] = $k;
2021-10-28 20:17:38 +00:00
$rs[$i]['value'] = count(self::scanDir($root . '/' . $k));
$i++;
}
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return $rs;
}
protected static function delDir($roots, $folder, $delfolder = true)
{
2021-11-06 15:19:49 +00:00
if (strpos($folder, '/')) {
2021-10-28 20:17:38 +00:00
return false;
}
if (!is_array($roots)) {
$roots = [$roots];
}
foreach ($roots as $root) {
2021-11-06 15:19:49 +00:00
if (file_exists($root . '/' . $folder)) {
2021-10-28 20:17:38 +00:00
return self::delTree($root . '/' . $folder, $delfolder);
2021-11-06 15:19:49 +00:00
}
2021-10-28 20:17:38 +00:00
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return false;
}
protected static function scanDir($path, $dir = '', $res = [])
{
$exclude = self::$exclude;
$path = path::real($path);
if (!is_dir($path) || !is_readable($path)) {
return [];
}
$files = files::scandir($path);
2021-11-06 15:19:49 +00:00
foreach ($files as $file) {
2021-10-28 20:17:38 +00:00
if (in_array($file, $exclude)) {
continue;
}
if (is_dir($path . '/' . $file)) {
$res[] = $file;
2021-11-06 15:19:49 +00:00
$res = self::scanDir($path . '/' . $file, $dir . '/' . $file, $res);
2021-10-28 20:17:38 +00:00
} else {
$res[] = empty($dir) ? $file : $dir . '/' . $file;
}
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return $res;
}
protected static function delTree($dir, $delroot = true)
{
if (!is_dir($dir) || !is_readable($dir)) {
return false;
}
2021-11-06 15:19:49 +00:00
if (substr($dir, -1) != '/') {
2021-10-28 20:17:38 +00:00
$dir .= '/';
}
if (($d = @dir($dir)) === false) {
return false;
}
while (($entryname = $d->read()) !== false) {
if ($entryname != '.' && $entryname != '..') {
if (is_dir($dir . '/' . $entryname)) {
2021-11-06 15:19:49 +00:00
if (!self::delTree($dir . '/' . $entryname)) {
return false;
2021-10-28 20:17:38 +00:00
}
} else {
if (!@unlink($dir . '/' . $entryname)) {
return false;
}
}
}
}
$d->close();
if ($delroot) {
return @rmdir($dir);
}
2021-11-06 15:19:49 +00:00
return true;
2021-10-28 20:17:38 +00:00
}
2021-11-06 15:19:49 +00:00
}