/
ApplicationController.php
294 lines (244 loc) · 7.28 KB
/
ApplicationController.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
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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_config
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Config\Administrator\Controller;
defined('_JEXEC') or die;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Client\ClientHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\Input\Input;
/**
* Controller for global configuration
*
* @since 1.5
*/
class ApplicationController extends BaseController
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
* Recognized key values include 'name', 'default_task', 'model_path', and
* 'view_path' (this list is not meant to be comprehensive).
* @param MVCFactoryInterface $factory The factory.
* @param CMSApplication $app The JApplication for the dispatcher
* @param Input $input Input
*
* @since 3.0
*/
public function __construct($config = array(), MVCFactoryInterface $factory = null, $app = null, $input = null)
{
parent::__construct($config, $factory, $app, $input);
// Map the apply task to the save method.
$this->registerTask('apply', 'save');
}
/**
* Cancel operation.
*
* @return void
*
* @since 3.0.0
*/
public function cancel()
{
$this->setRedirect(Route::_('index.php?option=com_cpanel'));
}
/**
* Saves the form
*
* @return mixed
*
* @since 4.0.0
*/
public function save()
{
// Check for request forgeries.
$this->checkToken();
// Check if the user is authorized to do this.
if (!$this->app->getIdentity()->authorise('core.admin'))
{
$this->setRedirect('index.php', Text::_('JERROR_ALERTNOAUTHOR'), 'error');
return false;
}
// Set FTP credentials, if given.
ClientHelper::setCredentialsFromRequest('ftp');
/** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
$model = $this->getModel('Application', 'Administrator');
$data = $this->input->post->get('jform', array(), 'array');
// Complete data array if needed
$oldData = $model->getData();
$data = array_replace($oldData, $data);
// Get request type
$saveFormat = $this->app->getDocument()->getType();
// Handle service requests
if ($saveFormat == 'json')
{
$form = $model->getForm();
$return = $model->validate($form, $data);
if ($return === false)
{
$this->app->setHeader('Status', 422, true);
return false;
}
return $model->save($return);
}
// Must load after serving service-requests
$form = $model->getForm();
// Validate the posted data.
$return = $model->validate($form, $data);
// Check for validation errors.
if ($return === false)
{
// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
{
if ($errors[$i] instanceof \Exception)
{
$this->app->enqueueMessage($errors[$i]->getMessage(), 'warning');
}
else
{
$this->app->enqueueMessage($errors[$i], 'warning');
}
}
// Save the posted data in the session.
$this->app->setUserState('com_config.config.global.data', $data);
// Redirect back to the edit screen.
$this->setRedirect(Route::_('index.php?option=com_config', false));
return false;
}
// Attempt to save the configuration.
$data = $return;
$return = $model->save($data);
// Check the return value.
if ($return === false)
{
/*
* The save method enqueued all messages for us, so we just need to redirect back.
*/
// Save failed, go back to the screen and display a notice.
$this->setRedirect(Route::_('index.php?option=com_config', false));
return false;
}
// Set the success message.
$this->app->enqueueMessage(Text::_('COM_CONFIG_SAVE_SUCCESS'), 'message');
// Set the redirect based on the task.
switch ($this->input->getCmd('task'))
{
case 'apply':
$this->setRedirect(Route::_('index.php?option=com_config', false));
break;
case 'save':
default:
$this->setRedirect(Route::_('index.php', false));
break;
}
}
/**
* Method to remove root in global configuration.
*
* @return boolean True on success.
*
* @since 3.2
*/
public function removeroot()
{
// Check for request forgeries.
if (!Session::checkToken('get'))
{
$this->setRedirect('index.php', Text::_('JINVALID_TOKEN'), 'error');
return false;
}
// Check if the user is authorized to do this.
if (!$this->app->getIdentity()->authorise('core.admin'))
{
$this->setRedirect('index.php', Text::_('JERROR_ALERTNOAUTHOR'), 'error');
return false;
}
// Initialise model.
/** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
$model = $this->getModel('Application', 'Administrator');
// Attempt to save the configuration and remove root.
try
{
$model->removeroot();
}
catch (\RuntimeException $e)
{
// Save failed, go back to the screen and display a notice.
$this->setRedirect('index.php', Text::_('JERROR_SAVE_FAILED', $e->getMessage()), 'error');
return false;
}
// Set the redirect based on the task.
$this->setRedirect(Route::_('index.php'), Text::_('COM_CONFIG_SAVE_SUCCESS'));
}
/**
* Method to send the test mail.
*
* @return string
*
* @since 3.5
*/
public function sendtestmail()
{
// Send json mime type.
$this->app->mimeType = 'application/json';
$this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
$this->app->sendHeaders();
// Check if user token is valid.
if (!Session::checkToken('get'))
{
$this->app->enqueueMessage(Text::_('JINVALID_TOKEN'), 'error');
echo new JsonResponse;
$this->app->close();
}
// Check if the user is authorized to do this.
if (!$this->app->getIdentity()->authorise('core.admin'))
{
$this->app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
echo new JsonResponse;
$this->app->close();
}
/** @var \Joomla\Component\Config\Administrator\Model\ApplicationModel $model */
$model = $this->getModel('Application', 'Administrator');
echo new JsonResponse($model->sendTestMail());
$this->app->close();
}
/**
* Method to GET permission value and give it to the model for storing in the database.
*
* @return boolean true on success, false when failed
*
* @since 3.5
*/
public function store()
{
// Send json mime type.
$this->app->mimeType = 'application/json';
$this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
$this->app->sendHeaders();
// Check if user token is valid.
if (!Session::checkToken('get'))
{
$this->app->enqueueMessage(Text::_('JINVALID_TOKEN'), 'error');
echo new JsonResponse;
$this->app->close();
}
/** @var \Joomla\Component\Config\Administrator\Model\Application $model */
$model = $this->getModel('Application', 'Administrator');
echo new JsonResponse($model->storePermissions());
$this->app->close();
}
}