urlsettings.php 8.75 KB
Newer Older
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
4
 *
5 6 7 8 9
 * Miscellaneous settings
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
10 11 12 13 14 15 16 17 18 19 20
 * 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/>.
21 22
 *
 * @category  Settings
23
 * @package   StatusNet
24 25
 * @author    Robin Millette <millette@status.net>
 * @author    Evan Prodromou <evan@status.net>
26
 * @copyright 2008-2009 StatusNet, Inc.
27
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28
 * @link      http://status.net/
29 30
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
32 33 34 35 36 37 38 39 40
    exit(1);
}

/**
 * Miscellaneous settings actions
 *
 * Currently this just manages URL shortening.
 *
 * @category Settings
41
 * @package  StatusNet
42 43
 * @author   Robin Millette <millette@status.net>
 * @author   Zach Copley <zach@status.net>
44
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45
 * @link     http://status.net/
46
 */
47
class UrlsettingsAction extends SettingsAction
48
{
49 50 51 52 53 54 55
    /**
     * Title of the page
     *
     * @return string Title of the page
     */
    function title()
    {
56
        // TRANS: Title of URL settings tab in profile settings.
57
        return _('URL settings');
58 59 60 61 62 63 64 65
    }

    /**
     * Instructions for use
     *
     * @return instructions for use
     */
    function getInstructions()
66
    {
67
        // TRANS: Instructions for tab "Other" in user profile settings.
68 69
        return _('Manage various other options.');
    }
70

71 72 73 74 75 76
    function showScripts()
    {
        parent::showScripts();
        $this->autofocus('urlshorteningservice');
    }

77 78 79 80 81 82 83 84
    /**
     * Content area of the page
     *
     * Shows a form for uploading an avatar.
     *
     * @return void
     */
    function showContent()
85
    {
86
        $user = common_current_user();
87

88
        $this->elementStart('form', array('method' => 'post',
sarven's avatar
sarven committed
89 90
                                          'id' => 'form_settings_other',
                                          'class' => 'form_settings',
91
                                          'action' =>
92
                                          common_local_url('urlsettings')));
sarven's avatar
sarven committed
93
        $this->elementStart('fieldset');
94
        $this->hidden('token', common_session_token());
95
        $this->elementStart('ul', 'form_data');
96

97 98
        $shorteners = array();

99
        Event::handle('GetUrlShorteners', array(&$shorteners));
100

101
        $services = array();
Evan Prodromou's avatar
Evan Prodromou committed
102 103

        foreach ($shorteners as $name => $value)
104
        {
Evan Prodromou's avatar
Evan Prodromou committed
105 106
            $services[$name] = $name;
            if ($value['freeService']) {
107 108 109
                // TRANS: Used as a suffix for free URL shorteners in a dropdown list in the tab "Other" of a
                // TRANS: user's profile settings. This message has one space at the beginning. Use your
                // TRANS: language's word separator here if it has one (most likely a single space).
Evan Prodromou's avatar
Evan Prodromou committed
110
                $services[$name] .= _(' (free service)');
Craig Andrews's avatar
Craig Andrews committed
111 112
            }
        }
Evan Prodromou's avatar
Evan Prodromou committed
113 114 115

        // Include default values

116
        // TRANS: Default value for URL shortening settings.
Evan Prodromou's avatar
Evan Prodromou committed
117
        $services['none']     = _('[none]');
118
        // TRANS: Default value for URL shortening settings.
Evan Prodromou's avatar
Evan Prodromou committed
119 120 121
        $services['internal'] = _('[internal]');

        if ($services) {
122
            asort($services);
123

124
            $this->elementStart('li');
125
            // TRANS: Label for dropdown with URL shortener services.
126
            $this->dropdown('urlshorteningservice', _('Shorten URLs with'),
127
                            // TRANS: Tooltip for for dropdown with URL shortener services.
128 129 130 131
                            $services, _('Automatic shortening service to use.'),
                            false, $user->urlshorteningservice);
            $this->elementEnd('li');
        }
132
        $this->elementStart('li');
133
        $this->input('maxurllength',
134
                     // TRANS: Field label in URL settings in profile.
135
                     _('URL longer than'),
136
                     (!is_null($this->arg('maxurllength'))) ?
137
                     $this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
138
                     // TRANS: Field title in URL settings in profile.
139
                     _('URLs longer than this will be shortened, 0 means always shorten.'));
140 141 142
        $this->elementEnd('li');
        $this->elementStart('li');
        $this->input('maxnoticelength',
143
                     // TRANS: Field label in URL settings in profile.
144
                     _('Text longer than'),
145
                     (!is_null($this->arg('maxnoticelength'))) ?
146
                     $this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
147
                     // TRANS: Field title in URL settings in profile.
148
                     _('URLs in notices longer than this will be shortened, 0 means always shorten.'));
149
        $this->elementEnd('li');
sarven's avatar
sarven committed
150
        $this->elementEnd('ul');
151 152
        // TRANS: Button text for saving "Other settings" in profile.
        $this->submit('save', _m('BUTTON','Save'));
sarven's avatar
sarven committed
153
        $this->elementEnd('fieldset');
154
        $this->elementEnd('form');
155
    }
156

157 158 159 160 161 162 163 164 165
    /**
     * Handle a post
     *
     * Saves the changes to url-shortening prefs and shows a success or failure
     * message.
     *
     * @return void
     */
    function handlePost()
166
    {
167
        // CSRF protection
168 169
        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
170
            // TRANS: Client error displayed when the session token does not match or is not given.
171 172
            $this->showForm(_('There was a problem with your session token. '.
                              'Try again, please.'));
173 174
            return;
        }
175

176
        $urlshorteningservice = $this->trimmed('urlshorteningservice');
177

178
        if (!is_null($urlshorteningservice) && strlen($urlshorteningservice) > 50) {
179
            // TRANS: Form validation error for form "Other settings" in user profile.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
180
            $this->showForm(_('URL shortening service is too long (maximum 50 characters).'));
181 182
            return;
        }
183

184 185 186
        $maxurllength = $this->trimmed('maxurllength');

        if (!Validate::number($maxurllength, array('min' => 0))) {
187 188
            // TRANS: Client exception thrown when the maximum URL settings value is invalid in profile URL settings.
            throw new ClientException(_('Invalid number for maximum URL length.'));
189 190 191 192 193
        }

        $maxnoticelength = $this->trimmed('maxnoticelength');

        if (!Validate::number($maxnoticelength, array('min' => 0))) {
194 195
            // TRANS: Client exception thrown when the maximum notice length settings value is invalid in profile URL settings.
            throw new ClientException(_('Invalid number for maximum notice length.'));
196 197
        }

198
        $user = common_current_user();
199

200
        assert(!is_null($user)); // should already be checked
201

202
        $user->query('BEGIN');
203

204
        $original = clone($user);
205

206
        $user->urlshorteningservice = $urlshorteningservice;
207

208
        $result = $user->update($original);
209

Evan Prodromou's avatar
TRUE  
Evan Prodromou committed
210
        if ($result === false) {
211
            common_log_db_error($user, 'UPDATE', __FILE__);
212
            // TRANS: Server error displayed when "Other" settings in user profile could not be updated on the server.
213
            $this->serverError(_('Could not update user.'));
214 215
            return;
        }
216

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        $prefs = User_urlshortener_prefs::getPrefs($user);
        $orig  = null;

        if (empty($prefs)) {
            $prefs = new User_urlshortener_prefs();

            $prefs->user_id = $user->id;
            $prefs->created = common_sql_now();
        } else {
            $orig = clone($prefs);
        }

        $prefs->urlshorteningservice = $urlshorteningservice;
        $prefs->maxurllength         = $maxurllength;
        $prefs->maxnoticelength      = $maxnoticelength;

        if (!empty($orig)) {
            $result = $prefs->update($orig);
        } else {
            $result = $prefs->insert();
        }

        if (!$result) {
240
            // TRANS: Server exception thrown in profile URL settings when preferences could not be saved.
241 242 243
            throw new ServerException(_('Error saving user URL shortening preferences.'));
        }

244
        $user->query('COMMIT');
245

246
        // TRANS: Confirmation message after saving preferences.
247
        $this->showForm(_('Preferences saved.'), true);
248
    }
249
}