| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
JLoader::import('adapter.mvc.controllers.admin'); |
| 15 |
|
| 16 |
/** |
| 17 |
* VikBooking plugin Override controller. |
| 18 |
* |
| 19 |
* @since 1.6.5 |
| 20 |
*/ |
| 21 |
class VikBookingControllerOverride extends JControllerAdmin |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Task used to save the record data set in the request. |
| 25 |
* After saving, the user is redirected to the management |
| 26 |
* page of the record that has been saved. |
| 27 |
* |
| 28 |
* @return boolean |
| 29 |
*/ |
| 30 |
public function save() |
| 31 |
{ |
| 32 |
$app = JFactory::getApplication(); |
| 33 |
|
| 34 |
$ajax = wp_doing_ajax(); |
| 35 |
|
| 36 |
if (!JSession::checkToken()) |
| 37 |
{ |
| 38 |
// missing CSRF-proof token |
| 39 |
if ($ajax) |
| 40 |
{ |
| 41 |
// ajax request |
| 42 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN')); |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
// post request |
| 47 |
throw new Exception(JText::translate('JINVALID_TOKEN'), 403); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// make sure the user is authorised to manage overrides |
| 52 |
if (!JFactory::getUser()->authorise('core.admin', 'com_vikbooking')) |
| 53 |
{ |
| 54 |
// action denied |
| 55 |
if ($ajax) |
| 56 |
{ |
| 57 |
// ajax request |
| 58 |
VBOHttpDocument::getInstance($app)->close(403, JText::translate('JERROR_ALERTNOAUTHOR')); |
| 59 |
} |
| 60 |
else |
| 61 |
{ |
| 62 |
// post request |
| 63 |
throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), 403); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// get selected file and override |
| 68 |
$client = $app->input->getString('client', 'site'); |
| 69 |
$status = $app->input->getString('status', ''); |
| 70 |
$file = $app->input->getBase64('selectedfile', ''); |
| 71 |
$override = $app->input->getBase64('overridefile', ''); |
| 72 |
|
| 73 |
// build return URL |
| 74 |
$url = sprintf( |
| 75 |
'admin.php?page=vikbooking&view=overrides&client=%s&selectedfile=%s&overridefile=%s', |
| 76 |
$client, |
| 77 |
$file, |
| 78 |
$override |
| 79 |
); |
| 80 |
|
| 81 |
if ($status !== '') |
| 82 |
{ |
| 83 |
$url .= '&status=' . (int) $status; |
| 84 |
} |
| 85 |
|
| 86 |
// register redirect URL |
| 87 |
$this->setRedirect($url); |
| 88 |
|
| 89 |
// build save data |
| 90 |
$data = []; |
| 91 |
$data['file'] = base64_decode($override); |
| 92 |
$data['code'] = $app->input->get('code', '', 'raw'); |
| 93 |
|
| 94 |
/** |
| 95 |
* Preserve the current status. |
| 96 |
* |
| 97 |
* @since 1.6.10 |
| 98 |
*/ |
| 99 |
$data['published'] = $status; |
| 100 |
|
| 101 |
if (empty($data['code'])) |
| 102 |
{ |
| 103 |
$error = __('No code received', 'vikbooking'); |
| 104 |
|
| 105 |
// missing code data |
| 106 |
if ($ajax) |
| 107 |
{ |
| 108 |
// ajax request |
| 109 |
VBOHttpDocument::getInstance($app)->close(400, $error); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
// post request, enqueue message and do redirect |
| 114 |
$app->enqueueMessage($error, 'error'); |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// load overrides model |
| 121 |
$overridesModel = $this->getModel('overrides', 'VikBookingModel'); |
| 122 |
|
| 123 |
// make sure the override we are going to create/update is supported |
| 124 |
$supported = $overridesModel->isSupported($client, $data['file']); |
| 125 |
|
| 126 |
if (!$supported) |
| 127 |
{ |
| 128 |
$error = sprintf(__('Cannot use the file as destination: [%s]', 'vikbooking'), $data['file']); |
| 129 |
|
| 130 |
// path not supported |
| 131 |
if ($ajax) |
| 132 |
{ |
| 133 |
// ajax request |
| 134 |
VBOHttpDocument::getInstance($app)->close(500, $error); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
// post request, enqueue message and do redirect |
| 139 |
$app->enqueueMessage($error, 'error'); |
| 140 |
|
| 141 |
return false; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// dispatch model to save the item |
| 146 |
if (!$this->model->save($data)) |
| 147 |
{ |
| 148 |
// get string error |
| 149 |
$error = $this->model->getError(null, false); |
| 150 |
|
| 151 |
if (!$error instanceof Exception) |
| 152 |
{ |
| 153 |
$error = new Exception($error ?: 'Unknown', 500); |
| 154 |
} |
| 155 |
|
| 156 |
$string = JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error->getMessage()); |
| 157 |
|
| 158 |
if ($ajax) |
| 159 |
{ |
| 160 |
// ajax request |
| 161 |
VBOHttpDocument::getInstance($app)->close($error->getCode() ?: 500, $string); |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
// post request, enqueue message and do redirect |
| 166 |
$app->enqueueMessage($string, 'error'); |
| 167 |
|
| 168 |
return false; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if ($ajax) |
| 173 |
{ |
| 174 |
// exit in case of AJAX request |
| 175 |
VBOHttpDocument::getInstance($app)->json(1); |
| 176 |
} |
| 177 |
|
| 178 |
// display generic successful message |
| 179 |
$app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS')); |
| 180 |
|
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Deletes a list of records set in the request. |
| 186 |
* |
| 187 |
* @return boolean |
| 188 |
*/ |
| 189 |
public function delete() |
| 190 |
{ |
| 191 |
$app = JFactory::getApplication(); |
| 192 |
|
| 193 |
if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 194 |
{ |
| 195 |
// missing CSRF-proof token |
| 196 |
throw new Exception(JText::translate('JINVALID_TOKEN'), 403); |
| 197 |
} |
| 198 |
|
| 199 |
// make sure the user is authorised to manage overrides |
| 200 |
if (!JFactory::getUser()->authorise('core.admin', 'com_vikbooking')) |
| 201 |
{ |
| 202 |
// action denied |
| 203 |
throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR', 403)); |
| 204 |
} |
| 205 |
|
| 206 |
// get selected file and override |
| 207 |
$client = $app->input->getString('client', 'site'); |
| 208 |
$status = $app->input->getString('status', ''); |
| 209 |
$file = $app->input->getBase64('selectedfile', ''); |
| 210 |
$override = $app->input->getBase64('overridefile', ''); |
| 211 |
|
| 212 |
// build return URL |
| 213 |
$url = sprintf( |
| 214 |
'admin.php?page=vikbooking&view=overrides&client=%s&selectedfile=%s&overridefile=%s', |
| 215 |
$client, |
| 216 |
$file, |
| 217 |
$override |
| 218 |
); |
| 219 |
|
| 220 |
if ($status !== '') |
| 221 |
{ |
| 222 |
$url .= '&status=' . (int) $status; |
| 223 |
} |
| 224 |
|
| 225 |
// register redirect URL |
| 226 |
$this->setRedirect($url); |
| 227 |
|
| 228 |
// fetch file PK |
| 229 |
$pk = base64_decode($override); |
| 230 |
|
| 231 |
// load overrides model |
| 232 |
$overridesModel = $this->getModel('overrides', 'VikBookingModel'); |
| 233 |
|
| 234 |
// make sure the override we are going to delete is supported |
| 235 |
$supported = $overridesModel->isSupported($client, $pk); |
| 236 |
|
| 237 |
if (!$supported) |
| 238 |
{ |
| 239 |
// invalid file |
| 240 |
$app->enqueueMessage(sprintf('The file to remove is not an override: [%s]', $pk), 'error'); |
| 241 |
|
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
// dispatch model to delete the item |
| 246 |
if ($this->model->delete($pk)) |
| 247 |
{ |
| 248 |
$app->enqueueMessage(__('1 item deleted.', 'vikbooking')); |
| 249 |
} |
| 250 |
|
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Task used to publish/unpublish an existing override. |
| 256 |
* |
| 257 |
* @return boolean |
| 258 |
*/ |
| 259 |
public function publish() |
| 260 |
{ |
| 261 |
$app = JFactory::getApplication(); |
| 262 |
|
| 263 |
if (!JSession::checkToken() && !JSession::checkToken('get')) |
| 264 |
{ |
| 265 |
// missing CSRF-proof token |
| 266 |
throw new Exception(JText::translate('JINVALID_TOKEN'), 403); |
| 267 |
} |
| 268 |
|
| 269 |
$state = $app->input->get('task') == 'unpublish' ? false : true; |
| 270 |
|
| 271 |
// make sure the user is authorised to manage overrides |
| 272 |
if (!JFactory::getUser()->authorise('core.admin', 'com_vikbooking')) |
| 273 |
{ |
| 274 |
// action denied |
| 275 |
throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR', 403)); |
| 276 |
} |
| 277 |
|
| 278 |
// get selected file and override |
| 279 |
$client = $app->input->getString('client', 'site'); |
| 280 |
$status = $app->input->getString('status', ''); |
| 281 |
$file = $app->input->getBase64('selectedfile', ''); |
| 282 |
$override = $app->input->getBase64('overridefile', ''); |
| 283 |
|
| 284 |
// build return URL |
| 285 |
$url = sprintf( |
| 286 |
'admin.php?page=vikbooking&view=overrides&client=%s&selectedfile=%s&overridefile=%s', |
| 287 |
$client, |
| 288 |
$file, |
| 289 |
$override |
| 290 |
); |
| 291 |
|
| 292 |
if ($status !== '') |
| 293 |
{ |
| 294 |
$url .= '&status=' . (int) $status; |
| 295 |
} |
| 296 |
|
| 297 |
// register redirect URL |
| 298 |
$this->setRedirect($url); |
| 299 |
|
| 300 |
// fetch file PK |
| 301 |
$pk = base64_decode($override); |
| 302 |
|
| 303 |
// load overrides model |
| 304 |
$overridesModel = $this->getModel('overrides', 'VikBookingModel'); |
| 305 |
|
| 306 |
// make sure the override we are going to toggle is supported |
| 307 |
$supported = $overridesModel->isSupported($client, $pk); |
| 308 |
|
| 309 |
if (!$supported) |
| 310 |
{ |
| 311 |
// invalid file |
| 312 |
$app->enqueueMessage(sprintf('The file to publish is not an override: [%s]', $pk), 'error'); |
| 313 |
|
| 314 |
return false; |
| 315 |
} |
| 316 |
|
| 317 |
// dispatch model to toggle the item |
| 318 |
$this->model->publish($pk, $state); |
| 319 |
|
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* AJAX end-point used to dismiss the breaking changes. |
| 325 |
* |
| 326 |
* @return void |
| 327 |
*/ |
| 328 |
public function dismissbc() |
| 329 |
{ |
| 330 |
$app = JFactory::getApplication(); |
| 331 |
|
| 332 |
// get a list of specified files, if any |
| 333 |
$files = $app->input->get('files', null, 'string'); |
| 334 |
|
| 335 |
// unregister breaking changes |
| 336 |
VikBookingInstaller::unregisterBreakingChanges($files); |
| 337 |
|
| 338 |
$this->sendJSON(1); |
| 339 |
} |
| 340 |
} |
| 341 |
|