-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessModuleCleaner.module.php
More file actions
178 lines (157 loc) · 6.28 KB
/
ProcessModuleCleaner.module.php
File metadata and controls
178 lines (157 loc) · 6.28 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace ProcessWire;
/**
* ProcessModuleCleaner
*
* Lists and deletes module directories starting with a dot.
*/
class ProcessModuleCleaner extends Process implements Module
{
/**
* Returns the module information.
*
* @return array
*/
public static function getModuleInfo()
{
return [
'title' => __('Module Folder Cleaner'),
'summary' => __('Deletes old module directories (.ModuleName) directly.'),
'href' => 'https://github.com/markusthomas/ProcessModuleCleaner',
'version' => '010',
'author' => 'Markus Thomas',
'license' => 'MIT',
'icon' => 'trash',
'permission' => 'module-admin',
'page' => [
'name' => 'module-cleaner',
'parent' => 'setup',
'title' => __('Module Cleaner')
],
'requires' => 'ProcessWire>=3.0.0'
];
}
/**
* Main execution method.
* Lists orphaned module folders or shows a success message if none exist.
*
* @return string HTML output
*/
public function ___execute()
{
$modulesPath = $this->wire('config')->paths->siteModules;
$hiddenFolders = $this->findHiddenFolders($modulesPath);
if (empty($hiddenFolders)) {
return "<div class='uk-alert-success' uk-alert><p>" . $this->_('No orphaned module folders found.') . "</p></div>";
}
return $this->renderFolderTable($hiddenFolders);
}
/**
* Scans the modules directory for folders starting with a dot.
*
* @param string $path The path to scan
* @return array Array of folder information (name, modified date)
*/
protected function findHiddenFolders($path)
{
$folders = [];
if (!is_dir($path))
return $folders;
$dir = new \DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
$name = $fileinfo->getFilename();
if (strpos($name, '.') === 0) {
$folders[] = [
'name' => $name,
'modified' => date("d.m.Y H:i", $fileinfo->getMTime())
];
}
}
}
return $folders;
}
/**
* Renders the HTML table for the found folders.
* Includes AlpineJS for checkbox handling.
*
* @param array $folders List of folders
* @return string HTML output
*/
protected function renderFolderTable($folders)
{
$deleteUrl = $this->wire('page')->url . "delete/";
$tokenName = $this->wire('session')->CSRF->getTokenName();
$tokenValue = $this->wire('session')->CSRF->getTokenValue();
$out = "
<div class='uk-card uk-card-default uk-card-body' x-data='{ selectedFolders: [] }'>
<h3 class='uk-card-title'><i class='fa fa-folder-open-o'></i> " . $this->_('Delete Module Folders') . "</h3>
<form action='{$deleteUrl}' method='POST'>
<input type='hidden' name='{$tokenName}' value='{$tokenValue}'>
<table class='uk-table uk-table-divider uk-table-hover uk-table-small uk-table-middle'>
<thead>
<tr>
<th class='uk-table-shrink'>
<input class='uk-checkbox' type='checkbox' @change=\"if (\$el.checked) { selectedFolders = " . htmlspecialchars(json_encode(array_column($folders, 'name'))) . " } else { selectedFolders = [] }\">
</th>
<th>" . $this->_('Directory Name') . "</th>
<th>" . $this->_('Last Modified') . "</th>
</tr>
</thead>
<tbody>
";
foreach ($folders as $folder) {
$name = htmlspecialchars($folder['name']);
$out .= "
<tr>
<td><input class='uk-checkbox' type='checkbox' name='folders[]' value='{$name}' x-model='selectedFolders'></td>
<td><span class='uk-text-danger uk-text-bold font-mono'>{$name}</span></td>
<td class='uk-text-muted uk-text-small'>{$folder['modified']}</td>
</tr>";
}
$out .= "
</tbody>
</table>
<div class='uk-margin-top'>
<button type='submit' class='uk-button uk-button-danger' :disabled='selectedFolders.length === 0' onclick=\"return confirm('" . $this->_('Are you sure you want to permanently delete the selected folders?') . "')\">
<i class='fa fa-trash'></i> " . $this->_('Delete Selected') . " (<span x-text='selectedFolders.length'>0</span>)
</button>
</div>
</form>
</div>
<script src='https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js' defer></script>
";
return $out;
}
/**
* Handles the deletion of selected folders.
* Validates CSRF token and permissions.
*
* @return void Redirects back to the main page
*/
public function ___executeDelete()
{
$this->wire('session')->CSRF->validate();
$folders = $this->wire('input')->post->array('folders');
// Fallback für direkte Post-Daten
if (empty($folders) && isset($_POST['folders']))
$folders = $_POST['folders'];
$modulesPath = $this->wire('config')->paths->siteModules;
$successCount = 0;
if (empty($folders)) {
$this->error($this->_("No folders selected."));
$this->wire('session')->redirect("../");
}
foreach ($folders as $folderName) {
$folderName = trim($folderName);
if (strpos($folderName, '.') === 0 && strpos($folderName, '/') === false && strpos($folderName, '\\') === false) {
$fullPath = $modulesPath . $folderName;
if (is_dir($fullPath) && $this->wire('files')->rmdir($fullPath, true)) {
$successCount++;
}
}
}
$this->message(sprintf($this->_("Successfully deleted %d folders."), $successCount));
$this->wire('session')->redirect("../");
}
}