-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleCheck.module.php
More file actions
159 lines (128 loc) · 5.36 KB
/
ModuleCheck.module.php
File metadata and controls
159 lines (128 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
#--------------------------------------------------
# Module: ModuleCheck
# Author: Pixel Solutions
# Copyright: (C) 2025 Pixel Solutions, info@pixelsolutions.biz
# Licence: GNU General Public License version 3
# See LICENSE for full license information.
#--------------------------------------------------
if (!defined('CMS_VERSION')) exit;
class ModuleCheck extends CMSModule
{
const MANAGE_PERM = 'manage_modulecheck';
public function GetName() { return 'ModuleCheck'; }
public function GetFriendlyName() { return $this->Lang('friendlyname'); }
public function GetVersion() { return '1.0.0'; }
public function MinimumCMSVersion() { return '2.2.1'; }
public function GetAdminDescription() { return $this->Lang('admindescription'); }
public function IsPluginModule() { return FALSE; }
public function HasAdmin() { return TRUE; }
public function GetAuthor() { return 'Pixel Solutions'; }
public function GetAuthorEmail() { return 'info@pixelsolutions.biz'; }
public function GetAdminSection() { return 'extensions'; }
public function GetDependencies() { return []; }
public function UninstallPreMessage() { return $this->Lang('ask_uninstall'); }
public function VisibleToAdminUser()
{
return $this->CheckPermission(self::MANAGE_PERM);
}
public function __construct()
{
spl_autoload_register([$this, '_autoloader']);
parent::__construct();
}
private function _autoloader($classname)
{
$parts = explode("\\", $classname);
$classname = end($parts);
$base = $this->GetModulePath() . DIRECTORY_SEPARATOR . 'lib';
$filename = 'class.' . $classname . '.php';
$fn = $base . DIRECTORY_SEPARATOR . $filename;
if (file_exists($fn)) { require_once($fn); return; }
$fn = $base . DIRECTORY_SEPARATOR . 'checks' . DIRECTORY_SEPARATOR . $filename;
if (file_exists($fn)) { require_once($fn); }
}
public function GetHelp()
{
$fn = __DIR__ . '/doc/help.inc';
return (is_file($fn)) ? @file_get_contents($fn) : '';
}
public function GetChangeLog()
{
$fn = __DIR__ . '/doc/changelog.inc';
return (is_file($fn)) ? @file_get_contents($fn) : '';
}
public function InitializeAdmin() {}
public function InitializeFrontend() {}
/**
* Get ordered list of check class basenames.
*/
public function GetCheckClasses(): array
{
$files = glob(__DIR__ . '/lib/checks/class.Check_*.php');
$list = [];
foreach ($files as $file) {
$list[] = str_replace('class.', '', basename($file, '.php'));
}
return $list;
}
/**
* Run a single check class against a module. Returns findings array.
*/
public function RunSingleCheck(string $check_class, string $module_name, array $categories = [], array $types = []): array
{
$module_path = CMS_ROOT_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $module_name;
if (!is_dir($module_path)) return [];
$fqcn = "ModuleCheck\\{$check_class}";
if (!class_exists($fqcn)) return [];
$check = new $fqcn($this);
if (!empty($categories) && !array_intersect($check->getCategories(), $categories)) return [];
$mod = \cms_utils::get_module($module_name);
$results = $check->run($module_name, $module_path, $mod);
if (!empty($types)) {
$results = array_values(array_filter($results, fn($r) => in_array($r['type'], $types)));
}
return $results;
}
/**
* Get list of installed modules available for checking.
*/
public function GetInstalledModules(): array
{
$modops = \ModuleOperations::get_instance();
$modules = $modops->GetInstalledModules();
$result = [];
foreach ($modules as $module_name) {
$mod = \cms_utils::get_module($module_name);
if (!is_object($mod)) continue;
$result[$module_name] = $mod;
}
ksort($result);
return $result;
}
/**
* Run all checks on a given module.
*/
public function RunChecks(string $module_name, array $categories = [], array $types = []): array
{
$module_path = CMS_ROOT_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $module_name;
if (!is_dir($module_path)) {
return [['type' => 'error', 'code' => 'module_not_found', 'message' => $this->Lang('error_module_not_found', $module_name), 'severity' => 9]];
}
$mod = \cms_utils::get_module($module_name);
$results = [];
foreach (glob(__DIR__ . '/lib/checks/class.Check_*.php') as $file) {
$class_name = "ModuleCheck\\" . str_replace('class.', '', basename($file, '.php'));
if (!class_exists($class_name)) continue;
$check = new $class_name($this);
if (!empty($categories) && !array_intersect($check->getCategories(), $categories)) continue;
$check_results = $check->run($module_name, $module_path, $mod);
if (!empty($types)) {
$check_results = array_filter($check_results, fn($r) => in_array($r['type'], $types));
}
$results = array_merge($results, $check_results);
}
usort($results, fn($a, $b) => ($b['severity'] ?? 0) <=> ($a['severity'] ?? 0));
return $results;
}
}