-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitcheditor.install.php
More file actions
272 lines (254 loc) · 8.43 KB
/
switcheditor.install.php
File metadata and controls
272 lines (254 loc) · 8.43 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* @package Switch Editor
* @copyright Copyright (C) 2025 ConseilGouz. All rights reserved.
* From anything-digital.com Switch Editor
* @license GNU/GPLv2
*/
// no direct access
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseInterface;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
class pkg_SwitchEditorInstallerScript
{
protected $db;
private $min_joomla_version = '4.0.0';
private $min_php_version = '8.0';
private $newlib_version = '';
private $dir = null;
private $installerName = 'switcheditorinstaller';
public function __construct()
{
$this->dir = __DIR__;
$this->db = Factory::getContainer()->get(DatabaseInterface::class);
}
function preflight($type, $parent)
{
if ( ! $this->passMinimumJoomlaVersion())
{
$this->uninstallInstaller();
return false;
}
if ( ! $this->passMinimumPHPVersion())
{
$this->uninstallInstaller();
return false;
}
$obsloteFiles = ["SwitchEditorHelper.php"];
foreach ($obsloteFiles as $file) {
$f = JPATH_ADMINISTRATOR . '/modules/mod_switcheditor/src/Helper/' . $file;
if (@is_file($f)) {
File::delete($f);
}
$f = JPATH_ROOT . '/modules/mod_switcheditor/src/Helper/' . $file;
if (@is_file($f)) {
File::delete($f);
}
}
return true;
}
public function postflight($type, $parent)
{
if ('uninstall' == $type)
{
return;
}
if (!$this->checkLibrary('conseilgouz')) { // need library installation
$ret = $this->installPackage('lib_conseilgouz');
if ($ret) {
Factory::getApplication()->enqueueMessage('ConseilGouz Library ' . $this->newlib_version . ' installed', 'notice');
}
}
// delete obsolete version.php file
$this->delete([
JPATH_ADMINISTRATOR . '/modules/mod_switcheditor/src/Field/VersionField.php',
JPATH_SITE . '/modules/mod_switcheditor/src/Field/VersionField.php',
]);
// remove obsolete files
$obsloteFiles = ["helper.php","mod_switcheditor.php"];
foreach ($obsloteFiles as $file) {
$f = JPATH_ADMINISTRATOR . '/modules/mod_switcheditor/' . $file;
if (@is_file($f)) {
File::delete($f);
}
$f = JPATH_ROOT . '/modules/mod_switcheditor/' . $file;
if (@is_file($f)) {
File::delete($f);
}
}
// plugin not used anymore
$query = $this->db->getQuery(true)
->delete('#__extensions')
->where($this->db->quoteName('element') . ' like "switcheditor" AND '
.$this->db->quoteName('type'). 'like "plugin"');
$this->db->setQuery($query);
$this->db->execute();
$f = JPATH_ROOT . '/plugins/system/switcheditor';
if (@is_dir($f)) {
Folder::delete($f);
}
// update admin module if not enabled : access = registered, position = status, enable it
$query = $this->db->getQuery(true)
->select($this->db->quoteName('id'))
->from('#__modules')
->where($this->db->quoteName('module') . '="mod_switcheditor"')
->where($this->db->quoteName('client_id') . '=1')
->where($this->db->quoteName('published') . '=0'); // not yet published
$this->db->setQuery($query);
$id = $this->db->loadResult();
if ($id) {
$id = (int) $id;
// update the module position & publication
$query = $this->db->getQuery(true)
->update('#__modules')
->set($this->db->quoteName('published') . '=1')
->set($this->db->quoteName('position') . '= "status"')
->set($this->db->quoteName('access') . '=2') // registred mini
->where($this->db->quoteName('id') . '=' . $id);
$this->db->setQuery($query);
$this->db->execute();
// remove any previous module menu entries
$query = $this->db->getQuery(true)->delete('#__modules_menu')->where($this->db->quoteName('moduleid') . '=' . $id);
$this->db->setQuery($query);
$this->db->execute();
// insert a new module menu entry
$query = $this->db->getQuery(true)->insert('#__modules_menu')->values($id . ', 0');
$this->db->setQuery($query);
$this->db->execute();
}
// update site module if not enabled : access = registered, enable it
$query = $this->db->getQuery(true)
->select($this->db->quoteName('id'))
->from('#__modules')
->where($this->db->quoteName('module') . '="mod_switcheditor"')
->where($this->db->quoteName('client_id') . '=0')
->where($this->db->quoteName('published') . '=0'); // not yet published
$this->db->setQuery($query);
$id = $this->db->loadResult();
if ($id) {
$id = (int) $id;
// update the module position & publication
$query = $this->db->getQuery(true)
->update('#__modules')
->set($this->db->quoteName('published') . '=1')
->set($this->db->quoteName('access') . '=2') // registred mini
->where($this->db->quoteName('id') . '=' . $id);
$this->db->setQuery($query);
$this->db->execute();
// enable module on all menus
$menu = new stdClass();
$menu->menuid = 0;
$menu->moduleid=$id;
// Insert the object into the modules_menu table.
$result = $this->db->insertObject('#__modules_menu', $menu);
}
// SwitchEditor is now on Github
$query = $this->db->getQuery(true)
->delete('#__update_sites')
->where($this->db->quoteName('location') . ' like "%conseilgouz.com/updates/pkg_switcheditor%"');
$this->db->setQuery($query);
$this->db->execute();
}
// Check if Joomla version passes minimum requirement
private function passMinimumJoomlaVersion()
{
if (version_compare(JVERSION, $this->min_joomla_version, '<'))
{
Factory::getApplication()->enqueueMessage(
Text::sprintf(
'NOT_COMPATIBLE_UPDATE',
'<strong>' . JVERSION . '</strong>',
'<strong>' . $this->min_joomla_version . '</strong>'
),
'error'
);
return false;
}
return true;
}
// Check if PHP version passes minimum requirement
private function passMinimumPHPVersion()
{
if (version_compare(PHP_VERSION, $this->min_php_version, '<'))
{
Factory::getApplication()->enqueueMessage(
Text::sprintf(
'NOT_COMPATIBLE_PHP',
'<strong>' . PHP_VERSION . '</strong>',
'<strong>' . $this->min_php_version . '</strong>'
),
'error'
);
return false;
}
return true;
}
private function checkLibrary($library)
{
$file = $this->dir.'/lib_conseilgouz/conseilgouz.xml';
if (!is_file($file)) {// library not installed
return false;
}
$xml = simplexml_load_file($file);
$this->newlib_version = $xml->version;
$db = Factory::getContainer()->get(DatabaseInterface::class);
$conditions = array(
$db->qn('type') . ' = ' . $db->q('library'),
$db->qn('element') . ' = ' . $db->quote($library)
);
$query = $db->getQuery(true)
->select('manifest_cache')
->from($db->quoteName('#__extensions'))
->where($conditions);
$db->setQuery($query);
$manif = $db->loadObject();
if ($manif) {
$manifest = json_decode($manif->manifest_cache);
if ($manifest->version >= $this->newlib_version) { // compare versions
return true; // library ok
}
}
return false; // need library
}
private function installPackage($package)
{
$tmpInstaller = new Joomla\CMS\Installer\Installer();
$db = Factory::getContainer()->get(DatabaseInterface::class);
$tmpInstaller->setDatabase($db);
$installed = $tmpInstaller->install($this->dir . '/' . $package);
return $installed;
}
private function uninstallInstaller()
{
if ( ! is_dir(JPATH_PLUGINS . '/system/' . $this->installerName)) {
return;
}
$this->delete([
JPATH_PLUGINS . '/system/' . $this->installerName . '/language',
JPATH_PLUGINS . '/system/' . $this->installerName,
]);
$db = Factory::getContainer()->get(DatabaseInterface::class);
$query = $db->getQuery(true)
->delete('#__extensions')
->where($db->quoteName('element') . ' = ' . $db->quote($this->installerName))
->where($db->quoteName('folder') . ' = ' . $db->quote('system'))
->where($db->quoteName('type') . ' = ' . $db->quote('plugin'));
$db->setQuery($query);
$db->execute();
Factory::getCache()->clean('_system');
}
public function delete($files = [])
{
foreach ($files as $file) {
if (is_dir($file)) {
Folder::delete($file);
}
if (is_file($file)) {
File::delete($file);
}
}
}
}