joomla-component/admin/src/Controller/AjaxController.php
Robot 6acc130732
Stable release of v5.0.3
Fix canDelete to correctly use published. Add default 1 to version field to make sure the versioning feature works as expected.
2024-02-10 10:50:40 +02:00

299 lines
7.4 KiB
PHP

<?php
/*----------------------------------------------------------------------------------| io.vdm.dev |----/
Vast Development Method
/-------------------------------------------------------------------------------------------------------/
@package getBible.net
@created 3rd December, 2015
@author Llewellyn van der Merwe <https://getbible.net>
@git Get Bible <https://git.vdm.dev/getBible>
@github Get Bible <https://github.com/getBible>
@support Get Bible <https://git.vdm.dev/getBible/support>
@copyright Copyright (C) 2015. All Rights Reserved
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
/------------------------------------------------------------------------------------------------------*/
namespace TrueChristianChurch\Component\Getbible\Administrator\Controller;
use Joomla\CMS\Factory;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Session\Session;
use Joomla\Input\Input;
use Joomla\Utilities\ArrayHelper;
// No direct access to this file
\defined('_JEXEC') or die;
/**
* Getbible Ajax Base Controller
*
* @since 1.6
*/
class AjaxController 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 Application for the dispatcher
* @param ?Input $input Input
*
* @since 3.0
*/
public function __construct($config = [], ?MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null)
{
parent::__construct($config, $factory, $app, $input);
// make sure all json stuff are set
$this->app->getDocument()->setMimeEncoding( 'application/json' );
$this->app->setHeader('Content-Disposition','attachment;filename="getajax.json"');
$this->app->setHeader('Access-Control-Allow-Origin', '*');
// load the tasks
$this->registerTask('isNew', 'ajax');
$this->registerTask('isRead', 'ajax');
$this->registerTask('getWiki', 'ajax');
$this->registerTask('getVersion', 'ajax');
}
public function ajax()
{
// get the user for later use
$user = $this->app->getIdentity();
// get the input values
$jinput = $this->input ?? $this->app->input;
// check if we should return raw (DEFAULT TRUE SINCE J4)
$returnRaw = $jinput->get('raw', true, 'BOOLEAN');
// return to a callback function
$callback = $jinput->get('callback', null, 'CMD');
// Check Token!
$token = Session::getFormToken();
$call_token = $jinput->get('token', 0, 'ALNUM');
if($jinput->get($token, 0, 'ALNUM') || $token === $call_token)
{
// get the task
$task = $this->getTask();
switch($task)
{
case 'isNew':
try
{
$noticeValue = $jinput->get('notice', NULL, 'STRING');
if($noticeValue && $user->id != 0)
{
$ajaxModule = $this->getModel('ajax', 'Administrator');
if ($ajaxModule)
{
$result = $ajaxModule->isNew($noticeValue);
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
if($callback)
{
echo $callback . "(".json_encode($result).");";
}
elseif($returnRaw)
{
echo json_encode($result);
}
else
{
echo "(".json_encode($result).");";
}
}
catch(\Exception $e)
{
if($callback)
{
echo $callback."(".json_encode($e).");";
}
elseif($returnRaw)
{
echo json_encode($e);
}
else
{
echo "(".json_encode($e).");";
}
}
break;
case 'isRead':
try
{
$noticeValue = $jinput->get('notice', NULL, 'STRING');
if($noticeValue && $user->id != 0)
{
$ajaxModule = $this->getModel('ajax', 'Administrator');
if ($ajaxModule)
{
$result = $ajaxModule->isRead($noticeValue);
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
if($callback)
{
echo $callback . "(".json_encode($result).");";
}
elseif($returnRaw)
{
echo json_encode($result);
}
else
{
echo "(".json_encode($result).");";
}
}
catch(\Exception $e)
{
if($callback)
{
echo $callback."(".json_encode($e).");";
}
elseif($returnRaw)
{
echo json_encode($e);
}
else
{
echo "(".json_encode($e).");";
}
}
break;
case 'getWiki':
try
{
$nameValue = $jinput->get('name', NULL, 'WORD');
if($nameValue && $user->id != 0)
{
$ajaxModule = $this->getModel('ajax', 'Administrator');
if ($ajaxModule)
{
$result = $ajaxModule->getWiki($nameValue);
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
if($callback)
{
echo $callback . "(".json_encode($result).");";
}
elseif($returnRaw)
{
echo json_encode($result);
}
else
{
echo "(".json_encode($result).");";
}
}
catch(\Exception $e)
{
if($callback)
{
echo $callback."(".json_encode($e).");";
}
elseif($returnRaw)
{
echo json_encode($e);
}
else
{
echo "(".json_encode($e).");";
}
}
break;
case 'getVersion':
try
{
$versionValue = $jinput->get('version', NULL, 'INT');
if($versionValue && $user->id != 0)
{
$ajaxModule = $this->getModel('ajax', 'Administrator');
if ($ajaxModule)
{
$result = $ajaxModule->getVersion($versionValue);
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
}
else
{
$result = ['error' => 'There was an error! [149]'];
}
if($callback)
{
echo $callback . "(".json_encode($result).");";
}
elseif($returnRaw)
{
echo json_encode($result);
}
else
{
echo "(".json_encode($result).");";
}
}
catch(\Exception $e)
{
if($callback)
{
echo $callback."(".json_encode($e).");";
}
elseif($returnRaw)
{
echo json_encode($e);
}
else
{
echo "(".json_encode($e).");";
}
}
break;
}
}
else
{
// return to a callback function
if($callback)
{
echo $callback."(".json_encode(['error' => 'There was an error! [129]']).");";
}
elseif($returnRaw)
{
echo json_encode(['error' => 'There was an error! [129]']);
}
else
{
echo "(".json_encode(['error' => 'There was an error! [129]']).");";
}
}
}
}