CoreFiles by mySites.guru
UpdateController.php675 lines, 23,952 bytes, from Joomla_6.2.1-rc1-Release_Candidate-Full_Package.zip
Raw History Compare with 6.2.0-rc2
md5
4704286716bb3a8ea8034cc0c2e41e88
sha1
c0412ae917980480675682658ec1a04c428d8b40
sha256
f8639a404e47b3deb279b18a1c33c28ab2b444e62452520ff326f5c61502c7aa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
<?php

/**
 * @package     Joomla.Administrator
 * @subpackage  com_joomlaupdate
 *
 * @copyright   (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\Component\Joomlaupdate\Administrator\Controller;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Installer\Installer;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Updater\Updater;
use Joomla\Component\Joomlaupdate\Administrator\Enum\AutoupdateRegisterState;
use Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * The Joomla! update controller for the Update view
 *
 * @since  2.5.4
 */
class UpdateController extends BaseController
{
    /**
     * Performs the download of the update package
     *
     * @return  void
     *
     * @since   2.5.4
     */
    public function download()
    {
        $this->checkToken();

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');
        $user  = $this->app->getIdentity();

        // Make sure logging is working before continue
        try {
            Log::add('Test logging', Log::INFO, 'Update');
        } catch (\Throwable $e) {
            $message = Text::sprintf('COM_JOOMLAUPDATE_UPDATE_LOGGING_TEST_FAIL', $e->getMessage());
            $this->setRedirect('index.php?option=com_joomlaupdate', $message, 'error');
            return;
        }

        Log::add(Text::sprintf('COM_JOOMLAUPDATE_UPDATE_LOG_START', $user->id, $user->name, \JVERSION), Log::INFO, 'Update');

        $result = $model->download();
        $file   = $result['basename'];

        $message     = null;
        $messageType = null;

        // The versions mismatch (Use \JVERSION as target version when not set in case of reinstall core files)
        if ($result['version'] !== $this->input->get('targetVersion', \JVERSION, 'string')) {
            $message     = Text::_('COM_JOOMLAUPDATE_VIEW_UPDATE_VERSION_WRONG');
            $messageType = 'error';
            $url         = 'index.php?option=com_joomlaupdate';

            $this->app->setUserState('com_joomlaupdate.file', null);
            $this->setRedirect($url, $message, $messageType);

            Log::add($message, Log::ERROR, 'Update');

            return;
        }

        // The validation was not successful so stop.
        if ($result['check'] === false) {
            $message     = Text::_('COM_JOOMLAUPDATE_VIEW_UPDATE_CHECKSUM_WRONG');
            $messageType = 'error';
            $url         = 'index.php?option=com_joomlaupdate';

            $this->app->setUserState('com_joomlaupdate.file', null);
            $this->setRedirect($url, $message, $messageType);

            Log::add($message, Log::ERROR, 'Update');

            return;
        }

        if ($file) {
            $this->app->setUserState('com_joomlaupdate.file', $file);
            $url = 'index.php?option=com_joomlaupdate&task=update.install&' . $this->app->getSession()->getFormToken() . '=1';

            Log::add(Text::sprintf('COM_JOOMLAUPDATE_UPDATE_LOG_FILE', $file), Log::INFO, 'Update');
        } else {
            $this->app->setUserState('com_joomlaupdate.file', null);
            $url         = 'index.php?option=com_joomlaupdate';
            $message     = Text::_('COM_JOOMLAUPDATE_VIEW_UPDATE_DOWNLOADFAILED');
            $messageType = 'error';
        }

        $this->setRedirect($url, $message, $messageType);
    }

    /**
     * Start the installation of the new Joomla! version
     *
     * @return  void
     *
     * @since   2.5.4
     */
    public function install()
    {
        $this->checkToken('get');
        $this->app->setUserState('com_joomlaupdate.oldversion', JVERSION);

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        Log::add(Text::_('COM_JOOMLAUPDATE_UPDATE_LOG_INSTALL'), Log::INFO, 'Update');

        $file = $this->app->getUserState('com_joomlaupdate.file', null);
        $model->createUpdateFile($file);

        $this->display();
    }

    /**
     * Finalise the upgrade by running the necessary scripts
     *
     * @return  void
     *
     * @since   2.5.4
     */
    public function finalise()
    {
        /*
         * Finalize with login page. Used for pre-token check versions
         * to allow updates without problems but with a maximum of security.
         */
        if (!Session::checkToken('get')) {
            $this->setRedirect('index.php?option=com_joomlaupdate&view=update&layout=finaliseconfirm');

            return;
        }

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        try {
            $model->finaliseUpgrade();
        } catch (\Throwable $e) {
            $model->collectError('finaliseUpgrade', $e);
        }

        // Reset update source from "Joomla Next" to "Default"
        $this->app->setUserState('com_joomlaupdate.update_channel_reset', $model->resetUpdateSource());

        // Check for update errors
        if ($model->getErrors()) {
            // The errors already should be logged at this point
            // Collect a messages to show them later in the complete page
            $errors = [];
            foreach ($model->getErrors() as $error) {
                $errors[] = $error->getMessage();
            }

            $this->app->setUserState('com_joomlaupdate.update_finished_with_error', true);
            $this->app->setUserState('com_joomlaupdate.update_errors', $errors);
        }

        // Check for captured output messages in the installer
        $msg = Installer::getInstance()->get('extension_message');
        if ($msg) {
            $this->app->setUserState('com_joomlaupdate.installer_message', $msg);
        }

        $url = 'index.php?option=com_joomlaupdate&task=update.cleanup&' . Session::getFormToken() . '=1';
        $this->setRedirect($url);
    }

    /**
     * Clean up after ourselves
     *
     * @return  void
     *
     * @since   2.5.4
     */
    public function cleanup()
    {
        /*
         * Cleanup with login page. Used for pre-token check versions to be able to update
         * from =< 3.2.7 to allow updates without problems but with a maximum of security.
         */
        if (!Session::checkToken('get')) {
            $this->setRedirect('index.php?option=com_joomlaupdate&view=update&layout=finaliseconfirm');

            return;
        }

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        try {
            $model->cleanUp();
        } catch (\Throwable $e) {
            $model->collectError('cleanUp', $e);
        }

        // Check for update errors
        if ($model->getErrors()) {
            // The errors already should be logged at this point
            // Collect a messages to show them later in the complete page
            $errors = $this->app->getUserState('com_joomlaupdate.update_errors', []);
            foreach ($model->getErrors() as $error) {
                $errors[] = $error->getMessage();
            }

            $this->app->setUserState('com_joomlaupdate.update_finished_with_error', true);
            $this->app->setUserState('com_joomlaupdate.update_errors', $errors);
        }

        $url = 'index.php?option=com_joomlaupdate&view=joomlaupdate&layout=complete';

        // In case for errored update, redirect to component view
        if ($this->app->getUserState('com_joomlaupdate.update_finished_with_error')) {
            $url .= '&tmpl=component';
        }

        $this->setRedirect($url);
    }

    /**
     * Purges updates.
     *
     * @return  void
     *
     * @since   3.0
     */
    public function purge()
    {
        // Check for request forgeries
        $this->checkToken('request');

        // Purge updates
        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');
        $model->purge();

        $url = 'index.php?option=com_joomlaupdate';
        $this->setRedirect($url, $model->_message);
    }

    /**
     * Uploads an update package to the temporary directory, under a random name
     *
     * @return  void
     *
     * @since   3.6.0
     */
    public function upload()
    {
        // Check for request forgeries
        $this->checkToken();

        // Did a non Super User tried to upload something (a.k.a. pathetic hacking attempt)?
        $this->app->getIdentity()->authorise('core.admin') or jexit(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'));

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        // Make sure logging is working before continue
        try {
            Log::add('Test logging', Log::INFO, 'Update');
        } catch (\Throwable $e) {
            $message = Text::sprintf('COM_JOOMLAUPDATE_UPDATE_LOGGING_TEST_FAIL', $e->getMessage());
            $this->setRedirect('index.php?option=com_joomlaupdate', $message, 'error');
            return;
        }

        Log::add(Text::_('COM_JOOMLAUPDATE_UPDATE_LOG_UPLOAD'), Log::INFO, 'Update');

        try {
            $model->upload();
        } catch (\RuntimeException $e) {
            $url = 'index.php?option=com_joomlaupdate';
            $this->setRedirect($url, $e->getMessage(), 'error');

            return;
        }

        $token = Session::getFormToken();
        $url   = 'index.php?option=com_joomlaupdate&task=update.captive&' . $token . '=1';
        $this->setRedirect($url);
    }

    /**
     * Checks there is a valid update package and redirects to the captive view for super admin authentication.
     *
     * @return  void
     *
     * @since   3.6.0
     */
    public function captive()
    {
        // Check for request forgeries
        $this->checkToken('get');

        // Did a non Super User tried to upload something (a.k.a. pathetic hacking attempt)?
        if (!$this->app->getIdentity()->authorise('core.admin')) {
            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
        }

        // Do I really have an update package?
        $tempFile = $this->app->getUserState('com_joomlaupdate.temp_file', null);

        if (empty($tempFile) || !is_file($tempFile)) {
            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
        }

        $this->input->set('view', 'upload');
        $this->input->set('layout', 'captive');

        $this->display();
    }

    /**
     * Checks the admin has super administrator privileges and then proceeds with the update.
     *
     * @return  void
     *
     * @since   3.6.0
     */
    public function confirm()
    {
        // Check for request forgeries
        $this->checkToken();

        // Did a non Super User tried to upload something (a.k.a. pathetic hacking attempt)?
        if (!$this->app->getIdentity()->authorise('core.admin')) {
            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
        }

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        // Get the captive file before the session resets
        $tempFile = $this->app->getUserState('com_joomlaupdate.temp_file', null);

        // Do I really have an update package?
        if (!$model->captiveFileExists()) {
            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
        }

        // Try to log in
        $credentials = [
            'username'  => $this->input->post->get('username', '', 'username'),
            'password'  => $this->input->post->get('passwd', '', 'raw'),
            'secretkey' => $this->input->post->get('secretkey', '', 'raw'),
        ];

        $result = $model->captiveLogin($credentials);

        if (!$result) {
            $model->removePackageFiles();

            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
        }

        // Set the update source in the session
        $this->app->setUserState('com_joomlaupdate.file', basename($tempFile));

        try {
            Log::add(Text::sprintf('COM_JOOMLAUPDATE_UPDATE_LOG_FILE', $tempFile), Log::INFO, 'Update');
        } catch (\RuntimeException) {
            // Informational log only
        }

        // Redirect to the actual update page
        $url = 'index.php?option=com_joomlaupdate&task=update.install&' . Session::getFormToken() . '=1';
        $this->setRedirect($url);
    }

    /**
     * Method to display a view.
     *
     * @param   boolean  $cachable   If true, the view output will be cached
     * @param   array    $urlparams  An array of safe URL parameters and their variable types.
     *                   @see        \Joomla\CMS\Filter\InputFilter::clean() for valid values.
     *
     * @return  static  This object to support chaining.
     *
     * @since   2.5.4
     */
    public function display($cachable = false, $urlparams = [])
    {
        // Get the document object.
        $document = $this->app->getDocument();

        // Set the default view name and format from the Request.
        $vName   = $this->input->get('view', 'update');
        $vFormat = $document->getType();
        $lName   = $this->input->get('layout', 'default', 'string');

        // Get and render the view.
        if ($view = $this->getView($vName, $vFormat)) {
            // Get the model for the view.
            /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
            $model = $this->getModel('Update');

            // Push the model into the view (as default).
            $view->setModel($model, true);
            $view->setLayout($lName);

            // Push document object into the view.
            $view->document = $document;
            $view->display();
        }

        return $this;
    }

    /**
     * Checks the admin has super administrator privileges and then proceeds with the final & cleanup steps.
     *
     * @return  void
     *
     * @since   3.6.3
     */
    public function finaliseconfirm()
    {
        // Check for request forgeries
        $this->checkToken();

        // Did a non Super User try do this?
        if (!$this->app->getIdentity()->authorise('core.admin')) {
            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN'), 403);
        }

        // Get the model
        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        // Try to log in
        $credentials = [
            'username'  => $this->input->post->get('username', '', 'username'),
            'password'  => $this->input->post->get('passwd', '', 'raw'),
            'secretkey' => $this->input->post->get('secretkey', '', 'raw'),
        ];

        $result = $model->captiveLogin($credentials);

        // The login fails?
        if (!$result) {
            $this->setMessage(Text::_('JGLOBAL_AUTH_INVALID_PASS'), 'warning');
            $this->setRedirect('index.php?option=com_joomlaupdate&view=update&layout=finaliseconfirm');

            return;
        }

        // Redirect back to the actual finalise page
        $this->setRedirect('index.php?option=com_joomlaupdate&task=update.finalise&' . Session::getFormToken() . '=1');
    }

    /**
     * Determines the compatibility information for a number of extensions.
     *
     * Called by the Joomla Update JavaScript (PreUpdateChecker.checkNextChunk).
     *
     * @return  void
     *
     * @since   4.2.0
     */
    public function batchextensioncompatibility()
    {
        $joomlaTargetVersion  = $this->input->post->get('joomla-target-version', '', 'DEFAULT');
        $joomlaCurrentVersion = $this->input->post->get('joomla-current-version', JVERSION);
        $extensionInformation = $this->input->post->get('extensions', []);

        /** @var \Joomla\Component\Joomlaupdate\Administrator\Model\UpdateModel $model */
        $model = $this->getModel('Update');

        $extensionResults = [];
        $leftover         = [];
        $startTime        = microtime(true);

        foreach ($extensionInformation as $information) {
            // Only process an extension if we have spent less than 5 seconds already
            $currentTime = microtime(true);

            if ($currentTime - $startTime > 5.0) {
                $leftover[] = $information;

                continue;
            }

            // Get the extension information and fetch its compatibility information
            $extensionID                = $information['eid'] ?: '';
            $extensionVersion           = $information['version'] ?: '';
            $upgradeCompatibilityStatus = $model->fetchCompatibility($extensionID, $joomlaTargetVersion);
            $currentCompatibilityStatus = $model->fetchCompatibility($extensionID, $joomlaCurrentVersion);
            $upgradeUpdateVersion       = false;
            $currentUpdateVersion       = false;
            $upgradeWarning             = 0;

            if ($upgradeCompatibilityStatus->state == 1 && !empty($upgradeCompatibilityStatus->compatibleVersions)) {
                $upgradeUpdateVersion = end($upgradeCompatibilityStatus->compatibleVersions);
            }

            if ($currentCompatibilityStatus->state == 1 && !empty($currentCompatibilityStatus->compatibleVersions)) {
                $currentUpdateVersion = end($currentCompatibilityStatus->compatibleVersions);
            }

            if ($upgradeCompatibilityStatus->state === 3 || $currentCompatibilityStatus->state === 3) {
                // One of the update servers could not be checked.
                $resultGroup = 4;
            } elseif ($upgradeUpdateVersion !== false) {
                $upgradeOldestVersion = $upgradeCompatibilityStatus->compatibleVersions[0];

                if ($currentUpdateVersion !== false) {
                    // If there are updates compatible with both CMS versions use these
                    $bothCompatibleVersions = array_values(
                        array_intersect($upgradeCompatibilityStatus->compatibleVersions, $currentCompatibilityStatus->compatibleVersions)
                    );

                    if (!empty($bothCompatibleVersions)) {
                        $upgradeOldestVersion = $bothCompatibleVersions[0];
                        $upgradeUpdateVersion = end($bothCompatibleVersions);
                    }
                }

                if (version_compare($upgradeOldestVersion, $extensionVersion, '>')) {
                    // Installed version is empty or older than the oldest compatible update: Update required
                    $resultGroup = 2;
                } else {
                    // Current version is compatible
                    $resultGroup = 3;
                }

                if ($currentUpdateVersion !== false && version_compare($upgradeUpdateVersion, $currentUpdateVersion, '<')) {
                    // Special case warning when version compatible with target is lower than current
                    $upgradeWarning = 2;
                }
            } elseif ($currentUpdateVersion !== false) {
                // No compatible version for target version but there is a compatible version for current version
                $resultGroup = 1;
            } else {
                // No update server available
                $resultGroup = 1;
            }

            // Do we need to capture
            $extensionResults[] = [
                'id'                         => $extensionID,
                'upgradeCompatibilityStatus' => (object) [
                    'state'             => $upgradeCompatibilityStatus->state,
                    'compatibleVersion' => $upgradeUpdateVersion,
                ],
                'currentCompatibilityStatus' => (object) [
                    'state'             => $currentCompatibilityStatus->state,
                    'compatibleVersion' => $currentUpdateVersion,
                ],
                'resultGroup'    => $resultGroup,
                'upgradeWarning' => $upgradeWarning,
            ];
        }

        $this->app->mimeType = 'application/json';
        $this->app->charSet  = 'utf-8';
        $this->app->setHeader('Content-Type', $this->app->mimeType . '; charset=' . $this->app->charSet);
        $this->app->sendHeaders();

        try {
            $return = [
                'compatibility' => $extensionResults,
                'extensions'    => $leftover,
            ];

            echo new JsonResponse($return);
        } catch (\Exception $e) {
            echo $e;
        }

        $this->app->close();
    }

    /**
     * Fetch and report updates in \JSON format, for AJAX requests
     *
     * @return  void
     *
     * @since   3.10.10
     */
    public function ajax()
    {
        if (!Session::checkToken('get')) {
            $this->app->setHeader('status', 403, true);
            $this->app->sendHeaders();
            echo Text::_('JINVALID_TOKEN_NOTICE');
            $this->app->close();
        }

        /** @var UpdateModel $model */
        $model      = $this->getModel('Update');
        $updateInfo = $model->getUpdateInformation();

        $update   = [];
        $update[] = ['version' => $updateInfo['latest']];

        echo json_encode($update);

        $this->app->close();
    }

    /**
     * Fetch and report health status of the automated updates in \JSON format, for AJAX requests
     *
     * @return  void
     *
     * @since   5.4.0
     */
    public function healthstatus()
    {
        if (!Session::checkToken('get')) {
            $this->app->setHeader('status', 403, true);
            $this->app->sendHeaders();
            echo Text::_('JINVALID_TOKEN_NOTICE');
            $this->app->close();
        }

        $params = ComponentHelper::getParams('com_joomlaupdate');

        // Edge case: the current state requires the registration, i.e. because it's a new installation
        $registrationState = AutoupdateRegisterState::tryFrom($params->get('autoupdate_status', 0));

        if (
            $this->app->getIdentity()->authorise('core.admin', 'com_joomlaupdate')
            && $registrationState === AutoupdateRegisterState::Subscribe
        ) {
            /** @var UpdateModel $model */
            $model  = $this->getModel('Update');
            $result = $model->changeAutoUpdateRegistration($registrationState);

            $result = [
                'active'  => true,
                'healthy' => $result->value,
            ];

            echo json_encode($result);

            $this->app->close();
        }

        // Default case: connection already configured, check update source and date
        $lastCheck = date_create_from_format('Y-m-d H:i:s', $params->get('update_last_check', ''));

        $result = [
            'active' => (
                (int) $params->get('autoupdate')
                && $params->get('updatesource', 'default') === 'default'
                && (int) $params->get('minimum_stability', Updater::STABILITY_STABLE) === Updater::STABILITY_STABLE
            ),
            'healthy' => (int) ($lastCheck !== false && $lastCheck->diff(new \DateTime())->days < 4),
        ];

        echo json_encode($result);

        $this->app->close();
    }
}

The same content is in 12 releases, Joomla 6.1.3-rc1 to 6.2.1-rc1. See them all, or every version of this file.