-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
script.php
234 lines (205 loc) · 10.4 KB
/
script.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* @package Astroid Framework
* @author Astroid Framework Team https://astroidframe.work
* @copyright Copyright (C) 2024 AstroidFrame.work
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or Later
*/
// no direct access
use Joomla\CMS\Application\AdministratorApplication;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Installer\InstallerScriptInterface;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Exception\FilesystemException;
use Astroid\Helper\Overrides;
use Joomla\CMS\Installer\Installer;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
return new class () implements ServiceProviderInterface {
public function register(Container $container)
{
$container->set(
InstallerScriptInterface::class,
new class (
$container->get(AdministratorApplication::class),
$container->get(DatabaseInterface::class)
) implements InstallerScriptInterface {
private AdministratorApplication $app;
private DatabaseInterface $db;
public function __construct(AdministratorApplication $app, DatabaseInterface $db)
{
$this->app = $app;
$this->db = $db;
}
public function install(InstallerAdapter $parent): bool
{
$this->app->enqueueMessage('Astroid Library has been successful installed.');
return true;
}
public function update(InstallerAdapter $parent): bool
{
$this->app->enqueueMessage('Astroid Library has been successful updated.');
return true;
}
public function uninstall(InstallerAdapter $parent): bool
{
$this->app->enqueueMessage('Astroid Library has been successful uninstalled.');
return true;
}
public function preflight(string $type, InstallerAdapter $parent): bool
{
$plugin_dir = JPATH_LIBRARIES . '/' . 'astroid' . '/' . 'plugins' . '/';
$plugins = array_filter(glob($plugin_dir . '*'), 'is_dir');
foreach ($plugins as $plugin) {
if ($type == "uninstall") {
$this->uninstallPlugin($plugin, $plugin_dir);
}
}
$module_dir = JPATH_LIBRARIES . '/' . 'astroid' . '/' . 'modules' . '/';
$modules = array_filter(glob($module_dir . '*'), 'is_dir');
foreach ($modules as $module) {
if ($type == "uninstall") {
$this->uninstallModule($module, $module_dir);
}
}
return true;
}
public function postflight(string $type, InstallerAdapter $parent): bool
{
$plugin_dir = JPATH_LIBRARIES . '/' . 'astroid' . '/' . 'plugins' . '/';
$plugins = array_filter(glob($plugin_dir . '*'), 'is_dir');
foreach ($plugins as $plugin) {
if ($type == "install" || $type == "update") {
$this->installPlugin($plugin, $plugin_dir);
}
}
$module_dir = JPATH_LIBRARIES . '/' . 'astroid' . '/' . 'modules' . '/';
$modules = array_filter(glob($module_dir . '*'), 'is_dir');
foreach ($modules as $module) {
if ($type == "install" || $type == "update") {
$this->installModule($module, $module_dir);
}
}
if ($type == "update") {
Overrides::fix();
}
return true;
}
private function installPlugin($plugin, $plugin_dir)
{
$db = $this->db;
$plugin_name = str_replace($plugin_dir, '', $plugin);
$installer = new Installer;
if ($installer->install($plugin)) {
$this->app->enqueueMessage(Text::_(strtoupper($plugin_name)) . ' Plugin has been successfully installed.');
} else {
$this->app->enqueueMessage(Text::_(strtoupper($plugin_name)) . ' Plugin has been failed to install.');
}
$query = $db->getQuery(true);
$query->update('#__extensions');
$query->set($db->quoteName('enabled') . ' = 1');
$query->where($db->quoteName('element') . ' = ' . $db->quote($plugin_name));
$query->where($db->quoteName('type') . ' = ' . $db->quote('plugin'));
$db->setQuery($query);
$db->execute();
return true;
}
private function installModule($module, $module_dir)
{
$db = $this->db;
$module_name = str_replace($module_dir, '', $module);
$installer = new Installer;
if ($installer->install($module)) {
$this->app->enqueueMessage(Text::_(strtoupper($module_name)) . ' Module has been successfully installed.');
} else {
$this->app->enqueueMessage(Text::_(strtoupper($module_name)) . ' Module has been failed to install.');
}
$query = $db->getQuery(true);
$query->update('#__extensions');
$query->set($db->quoteName('enabled') . ' = 1');
$query->where($db->quoteName('element') . ' = ' . $db->quote($module_name));
$query->where($db->quoteName('type') . ' = ' . $db->quote('module'));
$db->setQuery($query);
$db->execute();
if ($module_name === 'mod_astroid_clear_cache') {
$query = $db->getQuery(true);
$query->update('#__modules');
$query->set($db->quoteName('published') . ' = 1');
$query->set($db->quoteName('position') . ' = ' . $db->quote('status'));
$query->set($db->quoteName('params') . ' = ' . $db->quote('{"layout":"_:default","moduleclass_sfx":"","style":"0","module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":""}'));
$query->where($db->quoteName('module') . ' = ' . $db->quote($module_name));
$db->setQuery($query);
$db->execute();
}
// Retrieve ID
$query = $db->getQuery(true);
$query->select($db->quoteName('id'));
$query->from($db->quoteName('#__modules'));
$query->where($db->quoteName('module') . ' = ' . $db->quote($module_name));
$db->setQuery($query);
$id = (int) $db->loadResult();
if ($id) {
$query = $db->getQuery(true);
$query->select($db->quoteName('moduleid'));
$query->from($db->quoteName('#__modules_menu'));
$query->where($db->quoteName('moduleid') . ' = ' . $id);
$db->setQuery($query);
if (!$db->loadResult()) {
$db->getQuery(true);
$db->setQuery("INSERT INTO #__modules_menu (`moduleid`,`menuid`) VALUES (".$id.", 0)");
$db->execute();
}
}
return true;
}
private function uninstallPlugin($plugin, $plugin_dir)
{
try {
$db = $this->db;
$plugin_name = str_replace($plugin_dir, '', $plugin);
$query = $db->getQuery(true);
$query->update('#__extensions');
$query->set($db->quoteName('enabled') . ' = 0');
$query->where($db->quoteName('element') . ' = ' . $db->quote($plugin_name));
$query->where($db->quoteName('type') . ' = ' . $db->quote('plugin'));
$db->setQuery($query);
if ($db->execute()) {
$this->app->enqueueMessage('Astroid Plugins has been successfully uninstalled.');
} else {
$this->app->enqueueMessage('Astroid Plugins has been failed to uninstall.');
}
} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
}
return true;
}
private function uninstallModule($module, $module_dir)
{
try {
$db = $this->db;
$module_name = str_replace($module_dir, '', $module);
$query = $db->getQuery(true);
$query->update('#__extensions');
$query->set($db->quoteName('enabled') . ' = 0');
$query->where($db->quoteName('element') . ' = ' . $db->quote($module_name));
$query->where($db->quoteName('type') . ' = ' . $db->quote('module'));
$db->setQuery($query);
if ($db->execute()) {
$this->app->enqueueMessage('Astroid Modules has been successfully uninstalled.');
} else {
$this->app->enqueueMessage('Astroid Modules has been failed to uninstall.');
}
} catch (\Exception $e) {
echo $e->getMessage() . '<br>';
}
return true;
}
}
);
}
};