<?php declare(strict_types=1);
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Delete account
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  API
 * @package   GNUsocial
 * @author    SENOO, Ken <develop@senooken.jp>
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://www.gnu.org/software/social/
 */

if (!defined('GNUSOCIAL')) { exit(1); }

/**
 * Delete a account with API.
 * 
 * Refer to DeleteuserAction.
 */
class ApiAccountDeleteAction extends ApiAuthAction
{
    var $user = null;

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
    protected function prepare(array $args=array())
    {
        if (!parent::prepare($args)) {
            return false;
        }

        assert($this->scoped instanceof Profile);

        $profile = $this->getTargetProfile($this->arg('id'));
        if (empty($profile)) {
            // TRANS: Client error displayed when trying delete who's profile could not be found.
            throw new ClientException(_('Could not delete user: user not found.'), 403);
        }
        $this->user = $profile->getUser();

        if ($this->user->id === $this->scoped->id) {
            throw new ClientException('Could not delete self for mistake.', 403);
        }

        if (!$this->scoped->hasRight(Right::DELETEUSER)) {
            // TRANS: Client error displayed when trying to delete a user without having the right to delete users.
            throw new AuthorizationException(_('You cannot delete users.'));
        }

        // Only administrators can delete other privileged users (such as others who have the right to silence).
        if ($this->scoped->isPrivileged() && !$this->scoped->hasRole(Profile_role::ADMINISTRATOR)) {
            // TRANS: Client error displayed when trying to delete a user that has been granted moderation privileges
            throw new AuthorizationException(_('You cannot delete other privileged users.'));
        }

        return true;
    }

    /**
     * Handle the request
     *
     * @param array $args $_REQUEST data (unused)
     *
     * @return void
     */
    protected function handle()
    {
        parent::handle();

        if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
            $this->handleDelete();
        }
    }

    /**
     * Actually delete a user.
     *
     * @return void
     */
    function handleDelete()
    {
        if (Event::handle('StartDeleteUser', array($this, $this->user))) {
            // Mark the account as deleted and shove low-level deletion tasks
            // to background queues. Removing a lot of posts can take a while...
            if (!$this->user->hasRole(Profile_role::DELETED)) {
                $this->user->grantRole(Profile_role::DELETED);
            }

            $qm = QueueManager::get();
            $qm->enqueue($this->user, 'deluser');

            Event::handle('EndDeleteUser', array($this, $this->user));
        }
    }
}
