Your IP : 216.73.216.172


Current Path : /var/www/html/administrator/components/com_kunena/src/Controller/
Upload File :
Current File : /var/www/html/administrator/components/com_kunena/src/Controller/UserController.php

<?php

/**
 * Kunena Component
 *
 * @package         Kunena.Administrator
 * @subpackage      Controllers
 *
 * @copyright       Copyright (C) 2008 - 2026 Kunena Team. All rights reserved.
 * @license         https://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @link            https://www.kunena.org
 **/

namespace Kunena\Forum\Administrator\Controller;

\defined('_JEXEC') or die();

use Exception;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Session\Session;
use Joomla\Utilities\ArrayHelper;
use Kunena\Forum\Libraries\Access\KunenaAccess;
use Kunena\Forum\Libraries\Factory\KunenaFactory;
use Kunena\Forum\Libraries\Forum\Category\KunenaCategoryHelper;
use Kunena\Forum\Libraries\Forum\Message\KunenaMessageHelper;
use Kunena\Forum\Libraries\Route\KunenaRoute;
use Kunena\Forum\Libraries\User\KunenaUser;
use Kunena\Forum\Libraries\User\KunenaUserHelper;

/**
 * Kunena User Controller
 *
 * @since   Kunena 3.0
 */
class UserController extends FormController
{
    /**
     * @var     null|string
     * @since   Kunena 6.0
     */
    protected $baseurl = null;

    /**
     * Constructor.
     *
     * @param   MVCFactoryInterface|null  $factory  The factory.
     * @param   null                      $app      The CMSApplication for the dispatcher
     * @param   null                      $input    Input
     *
     * @param   array                     $config   An optional associative array of configuration settings.
     *
     * @throws Exception
     * @since   Kunena 2.0
     *
     * @see     BaseController
     */
    public function __construct($config = [], MVCFactoryInterface $factory = null, $app = null, $input = null)
    {
        parent::__construct($config, $factory, $app, $input);

        $this->baseurl = 'administrator/index.php?option=com_kunena&view=users';
    }

    /**
     * Method to save the form data.
     *
     * @param   null  $key     key
     * @param   null  $urlVar  url var
     *
     * @return  void
     *
     * @throws  Exception
     * @since   Kunena 2.0
     */
    public function save($key = null, $urlVar = null): void
    {
        if (!Session::checkToken()) {
            $this->app->enqueueMessage(Text::_('COM_KUNENA_ERROR_TOKEN'), 'error');
            $this->setRedirect(KunenaRoute::_($this->baseurl, false));

            return;
        }

        $this->saveInternal('save');

        $this->setRedirect(KunenaRoute::_($this->baseurl, false));
    }

    /**
     * Internal method to save an user
     *
     * @param   string  $type  type
     *
     * @return  void
     *
     * @throws Exception
     * @since   Kunena 6.0
     */
    protected function saveInternal(string $type)
    {
        $newView      = $this->app->getInput()->getString('newView');
        $newRank      = $this->app->getInput()->getString('newRank');
        $signature    = $this->app->getInput()->getString('signature');
        $deleteSig    = $this->app->getInput()->getInt('deleteSig');
        $moderator    = $this->app->getInput()->getInt('moderator');
        $uid          = $this->app->getInput()->getInt('uid');
        $deleteAvatar = $this->app->getInput()->getInt('deleteAvatar');
        $newOrder     = $this->app->getInput()->getInt('newOrder');
        $modCatids    = $moderator ? $this->app->getInput()->get('catid', [], 'array') : [];
        $modCatids    = ArrayHelper::toInteger($modCatids);
        KunenaFactory::loadLanguage('com_kunena.controllers', 'admin');

        if ($uid) {
            $user = KunenaFactory::getUser($uid);

            // Prepare variables
            if ($deleteSig === 1) {
                $user->signature = '';
            } else {
                $user->signature = $signature;
            }

            $user->personalText = $this->app->getInput()->getString('personalText');
            $birthdate          = $this->app->getInput()->getString('birthdate');

            if ($birthdate) {
                $date = Factory::getDate($birthdate);

                $birthdate = $date->format('Y-m-d');
            } else {
                $birthdate = '1000-01-01';
            }

            $user->birthdate    = $birthdate;
            $user->location     = trim($this->app->getInput()->getString('location'));
            $user->gender       = $this->app->getInput()->getInt('gender', '');
            $user->websitename  = $this->app->getInput()->getString('websitename');
            $user->websiteurl   = $this->app->getInput()->getString('websiteurl');
            $user->hideEmail    = $this->app->getInput()->getInt('hidemail');
            $user->showOnline   = $this->app->getInput()->getInt('showonline');
            $user->canSubscribe = $this->app->getInput()->getInt('cansubscribe');
            $user->userListtime = $this->app->getInput()->getInt('userlisttime');
            $user->view         = $newView;
            $user->ordering     = $newOrder;
            $user->rank         = $newRank;

            if ($deleteAvatar === 1) {
                $user->avatar = '';
            }

            if (!$user->save()) {
                $this->app->enqueueMessage(Text::_('COM_KUNENA_USER_PROFILE_SAVED_FAILED'), 'error');
            } else {
                $this->app->enqueueMessage(Text::_('COM_KUNENA_USER_PROFILE_SAVED_SUCCESSFULLY'), 'success');
            }

            if ($type === 'save') {
                $this->setModerate($user, $modCatids);
            } else {
                // Update moderator rights
                $categories = KunenaCategoryHelper::getCategories(false, false, 'admin');

                foreach ($categories as $category) {
                    $category->setModerator($user, \in_array($category->id, $modCatids, true));
                }

                // Global moderator is a special case
                if (KunenaUserHelper::getMyself()->isAdmin()) {
                    KunenaAccess::getInstance()->setModerator(0, $user, \in_array(0, $modCatids, true));
                }

                $this->setRedirect(KunenaRoute::_("administrator/index.php?option=com_kunena&view=user&layout=edit&userid={$uid}", false));
            }
        }
    }

    /**
     * Set moderator rights on the user given
     *
     * @param   KunenaUser  $user       KunenaUser object
     * @param   array       $modCatids  modCatids
     *
     * @return  boolean
     *
     * @throws Exception
     * @since   Kunena 5.1
     */
    protected function setModerate(KunenaUser $user, array $modCatids): bool
    {
        // Update moderator rights
        $categories = KunenaCategoryHelper::getCategories(false, false, 'admin');

        foreach ($categories as $category) {
            $category->setModerator($user, \in_array($category->id, $modCatids));
        }

        // Global moderator is a special case
        if (KunenaUserHelper::getMyself()->isAdmin()) {
            KunenaAccess::getInstance()->setModerator(0, $user, \in_array(0, $modCatids));
        }

        return true;
    }

    /**
     * Apply
     *
     * @return  void
     *
     * @throws  Exception
     * @since   Kunena 2.0
     */
    public function applychanges(): void
    {
        if (!Session::checkToken('post')) {
            $this->app->enqueueMessage(Text::_('COM_KUNENA_ERROR_TOKEN'), 'error');

            return;
        }

        $this->saveInternal('apply');
    }

    /**
     * Move Messages
     *
     * @return  void
     *
     * @throws  Exception
     * @throws  null
     * @since   Kunena 2.0
     */
    public function moveMessages(): void
    {
        if (!Session::checkToken()) {
            $this->app->enqueueMessage(Text::_('COM_KUNENA_ERROR_TOKEN'), 'error');
            $this->setRedirect(KunenaRoute::_($this->baseurl, false));

            return;
        }

        $catid = $this->app->getInput()->getInt('catid');
        $uids  = (array) $this->app->getUserState('kunena.usermove.userids');

        $error = null;

        if ($uids) {
            foreach ($uids as $id) {
                list($total, $messages) = KunenaMessageHelper::getLatestMessages(false, 0, 0, ['starttime' => '-1', 'user' => $id]);

                foreach ($messages as $message) {
                    $topic = $message->getTopic();

                    try {
                        $message->isAuthorised('move');
                    } catch (Exception $e) {
                        $this->app->enqueueMessage($e->getMessage(), 'error');
                    }

                    $target = KunenaCategoryHelper::get($catid);

                    try {
                        $topic->move($target, false, false, '', false);
                    } catch (Exception $e) {
                        $this->app->enqueueMessage($e->getMessage(), 'error');
                    }
                }
            }
        } else {
            $this->app->enqueueMessage(Text::_('COM_KUNENA_PROFILE_NO_USER'), 'error');
            $this->setRedirect(KunenaRoute::_($this->baseurl, false));

            return;
        }

        $this->app->enqueueMessage(Text::_('COM_KUNENA_A_USERMES_MOVED_DONE'), 'success');

        $this->setRedirect(KunenaRoute::_($this->baseurl, false));
    }
}