pathsadminpanel.php 15.8 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 25 26
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Paths administration panel
 *
 * 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  Settings
 * @package   StatusNet
 * @author    Evan Prodromou <evan@status.net>
 * @author    Zach Copley <zach@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
27
 * @copyright 2008-2011 StatusNet, Inc.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 * @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);
}

/**
 * Paths settings
 *
 * @category Admin
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @author   Zach Copley <zach@status.net>
 * @author   Sarven Capadisli <csarven@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 PathsadminpanelAction extends AdminPanelAction
{
    /**
     * Returns the page title
     *
     * @return string page title
     */

    function title()
    {
57
        // TRANS: Title for Paths admin panel.
58 59 60 61 62 63 64 65 66 67
        return _('Paths');
    }

    /**
     * Instructions for using this form.
     *
     * @return string instructions
     */
    function getInstructions()
    {
68
        // TRANS: Form instructions for Path admin panel.
69
        return _('Path and server settings for this StatusNet site');
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    }

    /**
     * Show the paths admin panel form
     *
     * @return void
     */
    function showForm()
    {
        $form = new PathsAdminPanelForm($this);
        $form->show();
        return;
    }

    /**
     * Save settings from the form
     *
     * @return void
     */
    function saveSettings()
    {
        static $settings = array(
92 93 94 95 96
                                 'site' => array('path', 'locale_path', 'ssl', 'sslserver'),
                                 'theme' => array('server', 'dir', 'path', 'sslserver', 'sslpath'),
                                 'avatar' => array('server', 'dir', 'path'),
                                 'attachments' => array('server', 'dir', 'path', 'sslserver', 'sslpath')
                                 );
97

98 99
        // XXX: If we're only going to have one boolean on thi page we
        // can remove some of the boolean processing code --Z
100

101
        static $booleans = array('site' => array('fancy'));
102

103 104 105 106 107 108 109 110
        $values = array();

        foreach ($settings as $section => $parts) {
            foreach ($parts as $setting) {
                $values[$section][$setting] = $this->trimmed("$section-$setting");
            }
        }

111 112 113 114 115 116
        foreach ($booleans as $section => $parts) {
            foreach ($parts as $setting) {
                $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
            }
        }

117 118 119 120 121 122 123 124 125 126 127 128 129 130
        $this->validate($values);

        // assert(all values are valid);

        $config = new Config();

        $config->query('BEGIN');

        foreach ($settings as $section => $parts) {
            foreach ($parts as $setting) {
                Config::save($section, $setting, $values[$section][$setting]);
            }
        }

131 132
        foreach ($booleans as $section => $parts) {
            foreach ($parts as $setting) {
133 134
                Config::save($section, $setting, $values[$section][$setting]);
            }
135
        }
136

137
        $config->query('COMMIT');
138 139 140 141 142 143 144 145 146 147 148 149 150 151

        return;
    }

    /**
     * Attempt to validate setting values
     *
     * @return void
     */
    function validate(&$values)
    {
        // Validate theme dir

        if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
152 153
            // TRANS: Client error in Paths admin panel.
            // TRANS: %s is the directory that could not be read from.
154
            $this->clientError(sprintf(_('Theme directory not readable: %s.'), $values['theme']['dir']));
155 156 157 158 159
        }

        // Validate avatar dir

        if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
160 161
            // TRANS: Client error in Paths admin panel.
            // TRANS: %s is the avatar directory that could not be written to.
162
            $this->clientError(sprintf(_('Avatar directory not writable: %s.'), $values['avatar']['dir']));
163 164 165 166 167 168 169
        }

        // Validate locales dir

        // XXX: What else do we need to validate for lacales path here? --Z

        if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
170 171
            // TRANS: Client error in Paths admin panel.
            // TRANS: %s is the locales directory that could not be read from.
172
            $this->clientError(sprintf(_('Locales directory not readable: %s.'), $values['site']['locale_path']));
173 174
        }

175 176
        // Validate SSL setup

177
        if (mb_strlen($values['site']['sslserver']) > 255) {
178 179
            // TRANS: Client error in Paths admin panel.
            // TRANS: %s is the SSL server URL that is too long.
180
            $this->clientError(_('Invalid SSL server. The maximum length is 255 characters.'));
181
        }
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    }
}

class PathsAdminPanelForm extends AdminForm
{
    /**
     * ID of the form
     *
     * @return int ID of the form
     */
    function id()
    {
        return 'form_paths_admin_panel';
    }

    /**
     * class of the form
     *
     * @return string class of the form
     */
    function formClass()
    {
        return 'form_settings';
    }

    /**
     * Action of the form
     *
     * @return string URL of the action
     */
    function action()
    {
        return common_local_url('pathsadminpanel');
    }

    /**
     * Data elements of the form
     *
     * @return void
     */
    function formData()
    {
224
        $this->out->elementStart('fieldset', array('id' => 'settings_paths_locale'));
225
        // TRANS: Fieldset legend in Paths admin panel.
226 227 228
        $this->out->element('legend', null, _('Site'), 'site');
        $this->out->elementStart('ul', 'form_data');

229
        $this->li();
230 231 232
        $this->input('server',
                     // TRANS: Field label in Paths admin panel.
                     _('Server'),
233
                     // TRANS: Field title in Paths admin panel.
234
                     _('Site\'s server hostname.'));
235 236
        $this->unli();

237
        $this->li();
238 239 240
        $this->input('path',
                     // TRANS: Field label in Paths admin panel.
                     _('Path'),
241
                     // TRANS: Field title in Paths admin panel.
242
                     _('Site path.'));
243 244 245
        $this->unli();

        $this->li();
246 247 248
        $this->input('locale_path',
                     // TRANS: Field label in Paths admin panel.
                     _('Locale directory'),
249
                     // TRANS: Field title in Paths admin panel.
250
                     _('Directory path to locales.'),
251
                     'site');
252 253
        $this->unli();

254
        $this->li();
255 256 257
        $this->out->checkbox('fancy',
                             // TRANS: Checkbox label in Paths admin panel.
                             _('Fancy URLs'),
258
                             (bool) $this->value('fancy'),
259 260
                             // TRANS: Field title in Paths admin panel.
                             _('Use fancy URLs (more readable and memorable)?'));
261
        $this->unli();
262

263 264 265 266
        $this->out->elementEnd('ul');
        $this->out->elementEnd('fieldset');

        $this->out->elementStart('fieldset', array('id' => 'settings_paths_theme'));
267 268
        // TRANS: Fieldset legend in Paths admin panel.
        $this->out->element('legend', null, _m('LEGEND','Theme'));
269 270 271 272

        $this->out->elementStart('ul', 'form_data');

        $this->li();
273 274 275 276 277 278
        $this->input('server',
                     // TRANS: Field label in Paths admin panel.
                     _('Server'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Server for themes.'),
                     'theme');
279 280 281
        $this->unli();

        $this->li();
282 283 284 285 286 287
        $this->input('path',
                     // TRANS: Field label in Paths admin panel.
                     _('Path'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Web path to themes.'),
                     'theme');
288 289 290
        $this->unli();

        $this->li();
291 292 293 294 295 296
        $this->input('sslserver',
                     // TRANS: Field label in Paths admin panel.
                     _('SSL server'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('SSL server for themes (default: SSL server).'),
                     'theme');
297 298 299
        $this->unli();

        $this->li();
300 301 302 303 304 305
        $this->input('sslpath',
                     // TRANS: Field label in Paths admin panel.
                     _('SSL path'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('SSL path to themes (default: /theme/).'),
                     'theme');
306 307 308
        $this->unli();

        $this->li();
309 310 311 312 313 314
        $this->input('dir',
                     // TRANS: Field label in Paths admin panel.
                     _('Directory'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Directory where themes are located.'),
                     'theme');
315 316 317 318 319 320
        $this->unli();

        $this->out->elementEnd('ul');

        $this->out->elementEnd('fieldset');
        $this->out->elementStart('fieldset', array('id' => 'settings_avatar-paths'));
321
        // TRANS: Fieldset legend in Paths admin panel.
322 323 324 325 326
        $this->out->element('legend', null, _('Avatars'));

        $this->out->elementStart('ul', 'form_data');

        $this->li();
327 328 329 330 331 332
        $this->input('server',
                     // TRANS: Field label in Paths admin panel.
                     _('Avatar server'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Server for avatars.'),
                     'avatar');
333 334 335
        $this->unli();

        $this->li();
336 337 338 339
        $this->input('path',
                     // TRANS: Field label in Paths admin panel.
                     _('Avatar path'),
                     // TRANS: Tooltip for field label in Paths admin panel.
340
                     _('Web path to avatars.'),
341
                     'avatar');
342 343 344
        $this->unli();

        $this->li();
345 346 347 348 349 350
        $this->input('dir',
                     // TRANS: Field label in Paths admin panel.
                     _('Avatar directory'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Directory where avatars are located.'),
                     'avatar');
351 352 353 354 355 356 357
        $this->unli();

        $this->out->elementEnd('ul');

        $this->out->elementEnd('fieldset');

        $this->out->elementStart('fieldset', array('id' =>
358
                                                   'settings_attachments-paths'));
359

360
        // TRANS: Fieldset legens in Paths admin panel.
361 362 363 364
        $this->out->element('legend', null, _('Attachments'));
        $this->out->elementStart('ul', 'form_data');

        $this->li();
365 366 367 368 369 370
        $this->input('server',
                     // TRANS: Field label in Paths admin panel.
                     _('Server'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Server for attachments.'),
                     'attachments');
371 372 373
        $this->unli();

        $this->li();
374 375 376 377 378 379
        $this->input('path',
                     // TRANS: Field label in Paths admin panel.
                     _('Path'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Web path to attachments.'),
                     'attachments');
380 381 382
        $this->unli();

        $this->li();
383 384 385 386 387 388
        $this->input('sslserver',
                     // TRANS: Field label in Paths admin panel.
                     _('SSL server'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Server for attachments on SSL pages.'),
                     'attachments');
389 390 391
        $this->unli();

        $this->li();
392 393 394 395 396 397
        $this->input('sslpath',
                     // TRANS: Field label in Paths admin panel.
                     _('SSL path'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Web path to attachments on SSL pages.'),
                     'attachments');
398 399 400
        $this->unli();

        $this->li();
401 402 403 404 405 406
        $this->input('dir',
                     // TRANS: Field label in Paths admin panel.
                     _('Directory'),
                     // TRANS: Tooltip for field label in Paths admin panel.
                     _('Directory where attachments are located.'),
                     'attachments');
407 408 409 410
        $this->unli();

        $this->out->elementEnd('ul');
        $this->out->elementEnd('fieldset');
411 412

        $this->out->elementStart('fieldset', array('id' => 'settings_admin_ssl'));
413
        // TRANS: Fieldset legend in Paths admin panel.
414
        $this->out->element('legend', null, _m('LEGEND','SSL'));
415 416
        $this->out->elementStart('ul', 'form_data');
        $this->li();
417

418
        // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
419
        $ssl = array('never' => _('Never'),
420
                      // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
421 422
                     'always' => _('Always'));

423
        $this->out->dropdown('site-ssl',
424
                             // TRANS: Drop down label in Paths admin panel.
425 426
                             _('Use SSL'),
                             // TRANS: Tooltip for field label in Paths admin panel.
427
                             $ssl, _('When to use SSL.'),
428 429
                             false,
                             $this->value('ssl', 'site'));
430 431 432
        $this->unli();

        $this->li();
433 434 435 436
        $this->input('sslserver',
                     // TRANS: Field label in Paths admin panel.
                     _('SSL server'),
                     // TRANS: Tooltip for field label in Paths admin panel.
437
                     _('Server to direct SSL requests to.'),
438
                     'site');
439 440 441
        $this->unli();
        $this->out->elementEnd('ul');
        $this->out->elementEnd('fieldset');
442 443 444 445 446 447 448 449 450
    }

    /**
     * Action elements
     *
     * @return void
     */
    function formActions()
    {
451 452 453
        // TRANS: Button text to store form data in the Paths admin panel.
        $this->out->submit('save', _m('BUTTON','Save'), 'submit',
                           // TRANS: Button title text to store form data in the Paths admin panel.
454
                           'save', _('Save path settings.'));
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    }

    /**
     * Utility to simplify some of the duplicated code around
     * params and settings. Overriding the input() in the base class
     * to handle a whole bunch of cases of settings with the same
     * name under different sections.
     *
     * @param string $setting      Name of the setting
     * @param string $title        Title to use for the input
     * @param string $instructions Instructions for this field
     * @param string $section      config section, default = 'site'
     *
     * @return void
     */
    function input($setting, $title, $instructions, $section='site')
    {
        $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
    }
}