manage.php
8.83 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* @package Joomla.Administrator
* @subpackage com_installer
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
JLoader::register('InstallerModel', __DIR__ . '/extension.php');
/**
* Installer Manage Model
*
* @since 1.5
*/
class InstallerModelManage extends InstallerModel
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @see JController
* @since 1.6
*/
public function __construct($config = array())
{
if (empty($config['filter_fields']))
{
$config['filter_fields'] = array(
'status',
'name',
'client_id',
'client', 'client_translated',
'type', 'type_translated',
'folder', 'folder_translated',
'package_id',
'extension_id',
);
}
parent::__construct($config);
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 1.6
*/
protected function populateState($ordering = 'name', $direction = 'asc')
{
$app = JFactory::getApplication();
// Load the filter state.
$this->setState('filter.search', $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
$this->setState('filter.client_id', $this->getUserStateFromRequest($this->context . '.filter.client_id', 'filter_client_id', null, 'int'));
$this->setState('filter.status', $this->getUserStateFromRequest($this->context . '.filter.status', 'filter_status', '', 'string'));
$this->setState('filter.type', $this->getUserStateFromRequest($this->context . '.filter.type', 'filter_type', '', 'string'));
$this->setState('filter.folder', $this->getUserStateFromRequest($this->context . '.filter.folder', 'filter_folder', '', 'string'));
$this->setState('message', $app->getUserState('com_installer.message'));
$this->setState('extension_message', $app->getUserState('com_installer.extension_message'));
$app->setUserState('com_installer.message', '');
$app->setUserState('com_installer.extension_message', '');
parent::populateState($ordering, $direction);
}
/**
* Enable/Disable an extension.
*
* @param array $eid Extension ids to un/publish
* @param int $value Publish value
*
* @return boolean True on success
*
* @since 1.5
*/
public function publish(&$eid = array(), $value = 1)
{
$user = JFactory::getUser();
if (!$user->authorise('core.edit.state', 'com_installer'))
{
JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
return false;
}
$result = true;
/*
* Ensure eid is an array of extension ids
* TODO: If it isn't an array do we want to set an error and fail?
*/
if (!is_array($eid))
{
$eid = array($eid);
}
// Get a table object for the extension type
$table = JTable::getInstance('Extension');
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_templates/tables');
// Enable the extension in the table and store it in the database
foreach ($eid as $i => $id)
{
$table->load($id);
if ($table->type == 'template')
{
$style = JTable::getInstance('Style', 'TemplatesTable');
if ($style->load(array('template' => $table->element, 'client_id' => $table->client_id, 'home' => 1)))
{
JError::raiseNotice(403, JText::_('COM_INSTALLER_ERROR_DISABLE_DEFAULT_TEMPLATE_NOT_PERMITTED'));
unset($eid[$i]);
continue;
}
}
if ($table->protected == 1)
{
$result = false;
JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'));
}
else
{
$table->enabled = $value;
}
$context = $this->option . '.' . $this->name;
JPluginHelper::importPlugin('extension');
JEventDispatcher::getInstance()->trigger('onExtensionChangeState', array($context, $eid, $value));
if (!$table->store())
{
$this->setError($table->getError());
$result = false;
}
}
// Clear the cached extension data and menu cache
$this->cleanCache('_system', 0);
$this->cleanCache('_system', 1);
$this->cleanCache('com_modules', 0);
$this->cleanCache('com_modules', 1);
$this->cleanCache('mod_menu', 0);
$this->cleanCache('mod_menu', 1);
return $result;
}
/**
* Refreshes the cached manifest information for an extension.
*
* @param int $eid extension identifier (key in #__extensions)
*
* @return boolean result of refresh
*
* @since 1.6
*/
public function refresh($eid)
{
if (!is_array($eid))
{
$eid = array($eid => 0);
}
// Get an installer object for the extension type
$installer = JInstaller::getInstance();
$result = 0;
// Uninstall the chosen extensions
foreach ($eid as $id)
{
$result |= $installer->refreshManifestCache($id);
}
return $result;
}
/**
* Remove (uninstall) an extension
*
* @param array $eid An array of identifiers
*
* @return boolean True on success
*
* @since 1.5
*/
public function remove($eid = array())
{
$user = JFactory::getUser();
if (!$user->authorise('core.delete', 'com_installer'))
{
JError::raiseWarning(403, JText::_('JERROR_CORE_DELETE_NOT_PERMITTED'));
return false;
}
/*
* Ensure eid is an array of extension ids in the form id => client_id
* TODO: If it isn't an array do we want to set an error and fail?
*/
if (!is_array($eid))
{
$eid = array($eid => 0);
}
// Get an installer object for the extension type
$installer = JInstaller::getInstance();
$row = JTable::getInstance('extension');
// Uninstall the chosen extensions
$msgs = array();
$result = false;
foreach ($eid as $id)
{
$id = trim($id);
$row->load($id);
$result = false;
$langstring = 'COM_INSTALLER_TYPE_TYPE_' . strtoupper($row->type);
$rowtype = JText::_($langstring);
if (strpos($rowtype, $langstring) !== false)
{
$rowtype = $row->type;
}
if ($row->type)
{
$result = $installer->uninstall($row->type, $id);
// Build an array of extensions that failed to uninstall
if ($result === false)
{
// There was an error in uninstalling the package
$msgs[] = JText::sprintf('COM_INSTALLER_UNINSTALL_ERROR', $rowtype);
continue;
}
// Package uninstalled successfully
$msgs[] = JText::sprintf('COM_INSTALLER_UNINSTALL_SUCCESS', $rowtype);
$result = true;
continue;
}
// There was an error in uninstalling the package
$msgs[] = JText::sprintf('COM_INSTALLER_UNINSTALL_ERROR', $rowtype);
}
$msg = implode('<br />', $msgs);
$app = JFactory::getApplication();
$app->enqueueMessage($msg);
$this->setState('action', 'remove');
$this->setState('name', $installer->get('name'));
$app->setUserState('com_installer.message', $installer->message);
$app->setUserState('com_installer.extension_message', $installer->get('extension_message'));
// Clear the cached extension data and menu cache
$this->cleanCache('_system', 0);
$this->cleanCache('_system', 1);
$this->cleanCache('com_modules', 0);
$this->cleanCache('com_modules', 1);
$this->cleanCache('com_plugins', 0);
$this->cleanCache('com_plugins', 1);
$this->cleanCache('mod_menu', 0);
$this->cleanCache('mod_menu', 1);
return $result;
}
/**
* Method to get the database query
*
* @return JDatabaseQuery The database query
*
* @since 1.6
*/
protected function getListQuery()
{
$query = $this->getDbo()->getQuery(true)
->select('*')
->select('2*protected+(1-protected)*enabled AS status')
->from('#__extensions')
->where('state = 0');
// Process select filters.
$status = $this->getState('filter.status');
$type = $this->getState('filter.type');
$clientId = $this->getState('filter.client_id');
$folder = $this->getState('filter.folder');
if ($status != '')
{
if ($status == '2')
{
$query->where('protected = 1');
}
elseif ($status == '3')
{
$query->where('protected = 0');
}
else
{
$query->where('protected = 0')
->where('enabled = ' . (int) $status);
}
}
if ($type)
{
$query->where('type = ' . $this->_db->quote($type));
}
if ($clientId != '')
{
$query->where('client_id = ' . (int) $clientId);
}
if ($folder != '')
{
$query->where('folder = ' . $this->_db->quote($folder == '*' ? '' : $folder));
}
// Process search filter (extension id).
$search = $this->getState('filter.search');
if (!empty($search) && stripos($search, 'id:') === 0)
{
$query->where('extension_id = ' . (int) substr($search, 3));
}
// Note: The search for name, ordering and pagination are processed by the parent InstallerModel class (in extension.php).
return $query;
}
}