improve/inc/class.improve.php

506 lines
16 KiB
PHP
Raw Normal View History

2021-09-01 23:35:23 +00:00
<?php
/**
* @brief improve, a plugin for Dotclear 2
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @package Dotclear
2021-09-06 21:36:50 +00:00
* @subpackage Plugin
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @author Jean-Christian Denis and contributors
2021-11-01 21:17:44 +00:00
*
2021-09-01 23:35:23 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* This class manage all actions sub-class
*/
class Improve
{
2021-11-07 23:59:52 +00:00
/** @var array Allowed file extensions to open */
2021-09-01 23:35:23 +00:00
public static $readfile_extensions = [
'php', 'xml', 'js', 'css', 'csv', 'html', 'htm', 'txt', 'md'
];
2021-11-07 23:59:52 +00:00
/** @var dcCore dcCore instance */
2021-09-01 23:35:23 +00:00
private $core;
2021-11-07 23:59:52 +00:00
/** @var ImproveAction[] Loaded actions modules */
private $actions = [];
/** @var array<string> Disabled actions modules */
2021-11-05 21:52:46 +00:00
private $disabled = [];
2021-09-01 23:35:23 +00:00
2021-11-07 23:59:52 +00:00
/** @var array<string, array> Logs by actions modules */
private $logs = [];
/** @var array<string, boolean> Has log of given type */
private $has_log = ['success' => false, 'warning' => false, 'error' => false];
/**
* Constructor
*
* @param dcCore $core dcCore instance
*/
2021-09-01 23:35:23 +00:00
public function __construct(dcCore $core)
{
$core->blog->settings->addNamespace('improve');
2021-11-07 23:59:52 +00:00
$this->core = &$core;
$disabled = explode(';', (string) $core->blog->settings->improve->disabled);
$list = new arrayObject();
2021-09-01 23:35:23 +00:00
try {
$this->core->callBehavior('improveAddAction', $list, $this->core);
2021-11-01 21:17:44 +00:00
foreach ($list as $action) {
2021-11-07 23:59:52 +00:00
if (is_a($action, 'ImproveAction') && !isset($this->actions[$action->get('id')])) {
if (in_array($action->get('id'), $disabled)) {
$this->disabled[$action->get('id')] = $action->get('name');
2021-11-05 21:52:46 +00:00
} else {
2021-11-07 23:59:52 +00:00
$this->actions[$action->get('id')] = $action;
2021-11-05 21:52:46 +00:00
}
2021-09-01 23:35:23 +00:00
}
}
} catch (Exception $e) {
$core->error->add($e->getMessage());
}
uasort($this->actions, [$this, 'sortModules']);
}
public function getLogs(): array
{
return $this->logs;
}
public function hasLog(string $type): bool
{
return array_key_exists($type, $this->has_log) && $this->has_log[$type];
}
public function writeLogs(): string
{
if (empty($this->logs)) {
return '';
}
2021-11-01 21:17:44 +00:00
$cur = $this->core->con->openCursor($this->core->prefix . 'log');
$cur->log_msg = serialize($this->logs);
$cur->log_table = 'improve';
2021-11-01 21:17:44 +00:00
$id = $this->core->log->addLog($cur);
return $id;
}
2021-11-01 21:17:44 +00:00
public function readLogs(int $id): array
{
$rs = $this->core->log->getLogs(['log_table' => 'improve', 'log_id' => $id, 'limit' => 1]);
if ($rs->isEmpty()) {
return [];
}
$this->core->log->delLogs($rs->log_id);
2021-11-01 21:17:44 +00:00
return unserialize($rs->log_msg);
}
public function parselogs(int $id): array
{
$logs = $this->readLogs($id);
if (empty($logs)) {
return [];
}
$lines = [];
2021-11-01 21:17:44 +00:00
foreach ($logs['improve'] as $path => $tools) {
$l_types = [];
2021-11-01 21:17:44 +00:00
foreach (['success', 'warning', 'error'] as $type) {
$l_tools = [];
2021-11-01 21:17:44 +00:00
foreach ($tools as $tool) {
$l_msg = [];
if (!empty($logs[$tool][$type][$path])) {
2021-11-01 21:17:44 +00:00
foreach ($logs[$tool][$type][$path] as $msg) {
$l_msg[] = $msg;
}
}
if (!empty($l_msg)) {
$l_tools[$tool] = $l_msg;
}
}
if (!empty($l_tools)) {
$l_types[$type] = $l_tools;
}
}
if (!empty($l_types)) {
$lines[$path] = $l_types;
}
}
2021-11-01 21:17:44 +00:00
return $lines;
}
2021-11-07 23:59:52 +00:00
/**
* Get a loaded action module
*
* @param string $id Module id
*
* @return ImproveAction ImproveAction instance
*/
2021-09-01 23:35:23 +00:00
public function module(string $id): ?ImproveAction
{
if (empty($id)) {
2021-11-07 23:59:52 +00:00
return null;
2021-09-01 23:35:23 +00:00
}
2021-11-01 21:17:44 +00:00
2021-09-01 23:35:23 +00:00
return $this->actions[$id] ?? null;
}
2021-11-07 23:59:52 +00:00
/**
* Get all loaded action modules
*
* @return ImproveAction[] ImproveAction instance
*/
public function modules(): array
2021-09-01 23:35:23 +00:00
{
2021-11-07 23:59:52 +00:00
return $this->actions;
2021-09-01 23:35:23 +00:00
}
2021-11-07 23:59:52 +00:00
/**
* Get disabled action modules
*
* @return array Array of id/name modules
*/
2021-11-05 21:52:46 +00:00
public function disabled(): array
{
return $this->disabled;
}
public function fixModule(string $type, string $id, array $properties, array $actions): float
2021-09-01 23:35:23 +00:00
{
$time_start = microtime(true);
2021-11-01 21:17:44 +00:00
$module = ImproveDefinition::clean($type, $id, $properties);
2021-09-01 23:35:23 +00:00
$workers = [];
2021-11-01 21:17:44 +00:00
foreach ($actions as $action) {
2021-09-01 23:35:23 +00:00
if (isset($this->actions[$action]) && $this->actions[$action]->isConfigured()) {
$workers[] = $this->actions[$action];
}
}
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// trace all path and action in logs
2021-11-07 23:59:52 +00:00
$this->logs['improve'][__('Begin')][] = $action->get('id');
// info: set current module
$action->setModule($module);
$action->setPath(__('Begin'), '', true);
// action: open module
$action->openModule();
2021-09-01 23:35:23 +00:00
}
if (!isset($module['sroot']) || !$module['root_writable'] || !is_writable($module['sroot'])) {
throw new Exception(__('Module path is not writable'));
}
$tree = self::getModuleFiles($module['sroot']);
2021-11-01 21:17:44 +00:00
foreach ($tree as $file) {
2021-09-01 23:35:23 +00:00
if (!file_exists($file[0])) {
continue;
}
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// trace all path and action in logs
2021-11-07 23:59:52 +00:00
$this->logs['improve'][$file[0]][] = $action->get('id');
// info: set current path
$action->setPath($file[0], $file[1], $file[2]);
}
2021-09-01 23:35:23 +00:00
if (!$file[2]) {
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// action: open a directory. full path
$action->openDirectory();
2021-09-01 23:35:23 +00:00
}
} else {
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// action: before openning a file. full path, extension
$action->openFile();
2021-09-01 23:35:23 +00:00
}
if (in_array($file[1], self::$readfile_extensions)) {
if (false !== ($content = file_get_contents($file[0]))) {
$no_content = empty($content);
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// action: read a file content. full path, extension, content
$action->readFile($content);
2021-09-01 23:35:23 +00:00
if (empty($content) && !$no_content) {
throw new Exception(sprintf(
2021-11-01 21:17:44 +00:00
__('File content has been removed: %s by %s'),
$file[0],
2021-11-07 23:59:52 +00:00
$action->get('name')
2021-09-01 23:35:23 +00:00
));
}
}
files::putContent($file[0], $content);
}
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// action: after closing a file. full path, extension
$action->closeFile();
2021-09-01 23:35:23 +00:00
}
}
}
}
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
// trace all path and action in logs
2021-11-07 23:59:52 +00:00
$this->logs['improve'][__('End')][] = $action->get('id');
// info: set current module
$action->setPath(__('End'), '', true);
// action: close module
$action->closeModule();
2021-09-01 23:35:23 +00:00
}
// info: get acions reports
2021-11-01 21:17:44 +00:00
foreach ($workers as $action) {
2021-11-07 23:59:52 +00:00
$this->logs[$action->get('id')] = $action->getLogs();
2021-11-01 21:17:44 +00:00
foreach ($this->has_log as $type => $v) {
if ($action->hasLog($type)) {
$this->has_log[$type] = true;
}
2021-09-01 23:35:23 +00:00
}
}
2021-11-07 23:59:52 +00:00
return round(microtime(true) - $time_start, 5);
2021-09-01 23:35:23 +00:00
}
private static function getModuleFiles(string $path, string $dir = '', array $res = []): array
{
$path = path::real($path);
2021-11-07 23:59:52 +00:00
if (!$path) {
return [];
}
2021-09-01 23:35:23 +00:00
if (!is_dir($path) || !is_readable($path)) {
return [];
}
if (!$dir) {
$dir = $path;
}
$res[] = [$dir, '', false];
$files = files::scandir($path);
2021-11-01 21:17:44 +00:00
foreach ($files as $file) {
2021-09-01 23:35:23 +00:00
if (substr($file, 0, 1) == '.') {
continue;
}
if (is_dir($path . '/' . $file)) {
$res = self::getModuleFiles(
2021-11-01 21:17:44 +00:00
$path . '/' . $file,
$dir . '/' . $file,
2021-09-01 23:35:23 +00:00
$res
);
} else {
2021-11-07 23:59:52 +00:00
$res[] = [$dir . '/' . $file, files::getExtension($file), true];
2021-09-01 23:35:23 +00:00
}
}
2021-11-01 21:17:44 +00:00
2021-09-01 23:35:23 +00:00
return $res;
}
public function getURL(array $params = []): string
{
return $this->core->adminurl->get('admin.plugin.improve', $params, '&');
2021-09-01 23:35:23 +00:00
}
2021-11-07 23:59:52 +00:00
public static function cleanExtensions(string|array $in): array
2021-09-01 23:35:23 +00:00
{
$out = [];
if (!is_array($in)) {
$in = explode(',', $in);
}
if (!empty($in)) {
2021-11-01 21:17:44 +00:00
foreach ($in as $v) {
2021-09-01 23:35:23 +00:00
$v = trim(files::getExtension('a.' . $v));
if (!empty($v)) {
$out[] = $v;
}
}
}
2021-11-01 21:17:44 +00:00
2021-09-01 23:35:23 +00:00
return $out;
}
2021-11-07 23:59:52 +00:00
/**
* Sort modules by priority then name
*
* @param ImproveAction $a ImproveAction instance
* @param ImproveAction $b ImproveAction instance
*
* @return integer Is higher
*/
private function sortModules(ImproveAction $a, ImproveAction $b): int
2021-09-01 23:35:23 +00:00
{
2021-11-07 23:59:52 +00:00
if ($a->get('priority') == $b->get('priority')) {
return strcasecmp($a->get('name'), $b->get('name'));
2021-09-01 23:35:23 +00:00
}
2021-11-01 21:17:44 +00:00
2021-11-07 23:59:52 +00:00
return $a->get('priority') < $b->get('priority') ? -1 : 1;
2021-09-01 23:35:23 +00:00
}
}
class ImproveDefinition
{
2021-11-07 23:59:52 +00:00
/** @var array Current module properties */
private $properties = [];
2021-11-07 23:59:52 +00:00
/**
* Constructor
*
* @param string $type Module type, plugin or theme
* @param string $id Module id
* @param array $properties Module properties
*/
public function __construct(string $type, string $id, array $properties = [])
{
$this->loadDefine($id, $properties['root']);
$this->properties = array_merge($this->properties, self::sanitizeModule($type, $id, $properties));
}
2021-11-07 23:59:52 +00:00
/**
* Get module properties
*
* @return array The properties
*/
public function get(): array
{
return $this->properties;
}
2021-11-07 23:59:52 +00:00
/**
* Get clean properties of registered module
*
* @param string $type Module type, plugin or theme
* @param string $id Module id
* @param array $properties Module properties
*
* @return array Module properties
*/
public static function clean(string $type, string $id, array $properties): array
{
$p = new self($type, $id, $properties);
2021-11-01 21:17:44 +00:00
return $p->get();
}
2021-11-07 23:59:52 +00:00
/**
* Replicate dcModule::loadDefine
*
* @param string $id Module id
* @param string $root Module path
*
* @return boolean Success
*/
private function loadDefine(string $id, string $root): bool
{
if (file_exists($root . '/_define.php')) {
ob_start();
require $root . '/_define.php';
ob_end_clean();
}
2021-11-07 23:59:52 +00:00
return true;
}
2021-11-07 23:59:52 +00:00
/**
* Replicate dcModule::registerModule
*
* @param string $name The module name
* @param string $desc The module description
* @param string $author The module author
* @param string $version The module version
* @param string|array $properties The properties
*
* @return boolean Success
*/
private function registerModule(string $name, string $desc, string $author, string $version, string|array $properties = []): bool // @phpstan-ignore-line
{
if (!is_array($properties)) {
$args = func_get_args();
$properties = [];
if (isset($args[4])) {
$properties['permissions'] = $args[4];
}
if (isset($args[5])) {
2021-11-01 21:17:44 +00:00
$properties['priority'] = (int) $args[5];
}
}
$this->properties = array_merge(
[
'permissions' => null,
'priority' => 1000,
'standalone_config' => false,
'type' => null,
'enabled' => true,
'requires' => [],
'settings' => [],
'repository' => ''
2021-11-01 21:17:44 +00:00
],
$properties
);
2021-11-07 23:59:52 +00:00
return true;
}
2021-11-07 23:59:52 +00:00
/**
* Replicate adminModulesList::sanitizeModule
*
* @param string $type Module type
* @param string $id Module id
* @param array $properties Module properties
*
* @return array Sanitized module properties
*/
public static function sanitizeModule(string $type, string $id, array $properties): array
{
$label = empty($properties['label']) ? $id : $properties['label'];
$name = __(empty($properties['name']) ? $label : $properties['name']);
2021-11-01 21:17:44 +00:00
$oname = empty($properties['name']) ? $label : $properties['name'];
return array_merge(
# Default values
[
'desc' => '',
'author' => '',
'version' => 0,
'current_version' => 0,
'root' => '',
'root_writable' => false,
'permissions' => null,
'parent' => null,
'priority' => 1000,
'standalone_config' => false,
'support' => '',
'section' => '',
'tags' => '',
'details' => '',
'sshot' => '',
'score' => 0,
'type' => null,
2021-09-25 19:12:47 +00:00
'requires' => [],
'settings' => [],
'repository' => '',
'dc_min' => 0
],
# Module's values
$properties,
# Clean up values
[
'id' => $id,
'sid' => self::sanitizeString($id),
'type' => $type,
'label' => $label,
'name' => $name,
'oname' => $oname,
'sname' => self::sanitizeString($name),
'sroot' => path::real($properties['root'])
]
);
}
2021-11-07 23:59:52 +00:00
/**
* Replicate adminModulesList::sanitizeString
*
* @param string $str String to sanitize
*
* @return string Sanitized string
*/
public static function sanitizeString(string $str): string
{
2021-11-07 23:59:52 +00:00
return (string) preg_replace('/[^A-Za-z0-9\@\#+_-]/', '', strtolower($str));
}
2021-11-01 21:17:44 +00:00
}