2026-05-23 22:41:46 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
2026-06-15 05:19:13 -05:00
|
|
|
* @package MokoSuiteClient
|
|
|
|
|
* @subpackage com_mokosuiteclient
|
2026-05-23 22:41:46 -05:00
|
|
|
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
|
|
|
|
|
* @license GNU General Public License version 3 or later; see LICENSE
|
|
|
|
|
*/
|
|
|
|
|
|
2026-06-15 05:19:13 -05:00
|
|
|
namespace Moko\Component\MokoSuiteClient\Api\Controller;
|
2026-05-23 22:41:46 -05:00
|
|
|
|
|
|
|
|
defined('_JEXEC') or die;
|
|
|
|
|
|
|
|
|
|
use Joomla\CMS\Factory;
|
|
|
|
|
use Joomla\CMS\MVC\Controller\BaseController;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update check API controller.
|
|
|
|
|
*
|
2026-06-15 05:19:13 -05:00
|
|
|
* POST /api/index.php/v1/mokosuiteclient/update
|
2026-05-23 22:41:46 -05:00
|
|
|
*
|
|
|
|
|
* @since 1.0.0
|
|
|
|
|
*/
|
|
|
|
|
class UpdateController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Trigger Joomla update finder and return count of available updates.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*
|
|
|
|
|
* @since 1.0.0
|
|
|
|
|
*/
|
2026-06-06 06:25:18 -05:00
|
|
|
public function execute($task = 'update'): void
|
2026-05-23 22:41:46 -05:00
|
|
|
{
|
|
|
|
|
$app = Factory::getApplication();
|
|
|
|
|
|
|
|
|
|
if ($app->input->getMethod() !== 'POST')
|
|
|
|
|
{
|
|
|
|
|
$this->sendJson(405, ['error' => 'POST required']);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $app->getIdentity();
|
|
|
|
|
if (!$user->authorise('core.manage', 'com_installer'))
|
|
|
|
|
{
|
|
|
|
|
$this->sendJson(403, ['error' => 'Not authorized']);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
$db = Factory::getDbo();
|
|
|
|
|
|
|
|
|
|
$db->setQuery($db->getQuery(true)->delete($db->quoteName('#__updates')));
|
|
|
|
|
$db->execute();
|
|
|
|
|
|
|
|
|
|
\Joomla\CMS\Updater\Updater::getInstance()->findUpdates();
|
|
|
|
|
|
|
|
|
|
$db->setQuery(
|
|
|
|
|
$db->getQuery(true)
|
|
|
|
|
->select('COUNT(*)')
|
|
|
|
|
->from($db->quoteName('#__updates'))
|
|
|
|
|
->where($db->quoteName('extension_id') . ' != 0')
|
|
|
|
|
);
|
|
|
|
|
$count = (int) $db->loadResult();
|
|
|
|
|
|
|
|
|
|
$this->sendJson(200, [
|
|
|
|
|
'status' => 'ok',
|
|
|
|
|
'updates_found' => $count,
|
|
|
|
|
'message' => $count . ' update(s) available',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
catch (\Throwable $e)
|
|
|
|
|
{
|
|
|
|
|
$this->sendJson(500, [
|
|
|
|
|
'error' => 'Update check failed',
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $code HTTP status code
|
|
|
|
|
* @param array $payload Response data
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function sendJson(int $code, array $payload): void
|
|
|
|
|
{
|
|
|
|
|
$app = Factory::getApplication();
|
|
|
|
|
$app->setHeader('Content-Type', 'application/json', true);
|
|
|
|
|
$app->setHeader('Status', (string) $code, true);
|
|
|
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
|
|
|
|
$app->close();
|
|
|
|
|
}
|
|
|
|
|
}
|