git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

apigroupcreate.php 6.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Create a group via the API
 *
 * 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   StatusNet
 * @author    Craig Andrews <candrews@integralblue.com>
Zach Copley's avatar
Zach Copley committed
25 26
 * @author    Evan Prodromou <evan@status.net>
 * @author    Jeffery To <jeffery.to@gmail.com>
27 28
 * @author    Zach Copley <zach@status.net>
 * @copyright 2009 StatusNet, Inc.
29
 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30 31 32 33 34 35 36 37 38 39 40 41 42
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

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

/**
 * Make a new group. Sets the authenticated user as the administrator of the group.
 *
 * @category API
 * @package  StatusNet
Zach Copley's avatar
Zach Copley committed
43 44 45
 * @author   Craig Andrews <candrews@integralblue.com>
 * @author   Evan Prodromou <evan@status.net>
 * @author   Jeffery To <jeffery.to@gmail.com>
46 47 48 49 50 51
 * @author   Zach Copley <zach@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
class ApiGroupCreateAction extends ApiAuthAction
{
52 53
    protected $needPost = true;

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    var $group       = null;
    var $nickname    = null;
    var $fullname    = null;
    var $homepage    = null;
    var $description = null;
    var $location    = null;
    var $aliasstring = null;
    var $aliases     = null;

    /**
     * Take arguments for running
     *
     * @param array $args $_REQUEST args
     *
     * @return boolean success flag
     */
70
    protected function prepare(array $args=array())
71 72 73
    {
        parent::prepare($args);

74
        $this->nickname    = Nickname::normalize($this->arg('nickname'), true);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        $this->fullname    = $this->arg('full_name');
        $this->homepage    = $this->arg('homepage');
        $this->description = $this->arg('description');
        $this->location    = $this->arg('location');
        $this->aliasstring = $this->arg('aliases');

        return true;
    }

    /**
     * Handle the request
     *
     * Save the new group
     *
     * @return void
     */
91
    protected function handle()
92
    {
93
        parent::handle();
94 95

        if (empty($this->user)) {
96
            // TRANS: Client error given when a user was not found (404).
97
            $this->clientError(_('No such user.'), 404);
98 99 100 101 102 103
        }

        if ($this->validateParams() == false) {
            return;
        }

104 105 106 107 108 109
        $group = User_group::register(array('nickname' => $this->nickname,
                                            'fullname' => $this->fullname,
                                            'homepage' => $this->homepage,
                                            'description' => $this->description,
                                            'location' => $this->location,
                                            'aliases'  => $this->aliases,
110 111 112
                                            'userid'   => $this->user->id,
                                            'local'    => true));

113 114 115 116 117 118 119 120
        switch($this->format) {
        case 'xml':
            $this->showSingleXmlGroup($group);
            break;
        case 'json':
            $this->showSingleJsonGroup($group);
            break;
        default:
121 122
            // TRANS: Client error displayed when coming across a non-supported API method.
            $this->clientError(_('API method not found.'), 404);
123 124 125 126 127 128 129 130 131 132
        }
    }

    /**
     * Validate params for the new group
     *
     * @return void
     */
    function validateParams()
    {
133
        if (!is_null($this->homepage)
134 135
                && strlen($this->homepage) > 0
                && !common_valid_http_url($this->homepage)) {
136 137 138
            // TRANS: Client error in form for group creation.
            $this->clientError(_('Homepage is not a valid URL.'), 403);

139
        } elseif (!is_null($this->fullname)
140 141 142 143
                && mb_strlen($this->fullname) > 255) {
            // TRANS: Client error in form for group creation.
            $this->clientError(_('Full name is too long (maximum 255 characters).'), 403);

144
        } elseif (User_group::descriptionTooLong($this->description)) {
145 146 147 148 149 150 151 152 153 154
            // TRANS: Client error shown when providing too long a description during group creation.
            // TRANS: %d is the maximum number of allowed characters.
            $this->clientError(sprintf(_m('Description is too long (maximum %d character).',
                                'Description is too long (maximum %d characters).',
                                User_group::maxDescription()), User_group::maxDescription()), 403);

        } elseif (!is_null($this->location)
                && mb_strlen($this->location) > 255) {
            // TRANS: Client error shown when providing too long a location during group creation.
            $this->clientError(_('Location is too long (maximum 255 characters).'), 403);
155 156 157 158
        }

        if (!empty($this->aliasstring)) {
            $this->aliases = array_map(
159
                array('Nickname', 'normalize'), // static call to Nickname::normalize
160
                array_unique(preg_split('/[\s,]+/', $this->aliasstring))
161 162 163 164 165 166
            );
        } else {
            $this->aliases = array();
        }

        if (count($this->aliases) > common_config('group', 'maxaliases')) {
167
            $this->clientError(sprintf(
Siebrand Mazeland's avatar
Siebrand Mazeland committed
168 169 170 171 172
                    // TRANS: Client error shown when providing too many aliases during group creation.
                    // TRANS: %d is the maximum number of allowed aliases.
                    _m('Too many aliases! Maximum %d allowed.',
                       'Too many aliases! Maximum %d allowed.',
                       common_config('group', 'maxaliases')),
173 174
                    common_config('group', 'maxaliases')),
                403);
175 176
        }

Siebrand Mazeland's avatar
Siebrand Mazeland committed
177
        // Everything looks OK
178 179 180 181

        return true;
    }
}