]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/pathsadminpanel.php
Don't accept non-objects before testing with "instanceof".
[quix0rs-gnu-social.git] / actions / pathsadminpanel.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Paths administration panel
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2011 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET')) {
33     exit(1);
34 }
35
36 /**
37  * Paths settings
38  *
39  * @category Admin
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Zach Copley <zach@status.net>
43  * @author   Sarven Capadisli <csarven@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 PathsadminpanelAction extends AdminPanelAction
48 {
49     /**
50      * Returns the page title
51      *
52      * @return string page title
53      */
54
55     function title()
56     {
57         // TRANS: Title for Paths admin panel.
58         return _('Paths');
59     }
60
61     /**
62      * Instructions for using this form.
63      *
64      * @return string instructions
65      */
66     function getInstructions()
67     {
68         // TRANS: Form instructions for Path admin panel.
69         return _('Path and server settings for this StatusNet site');
70     }
71
72     /**
73      * Show the paths admin panel form
74      *
75      * @return void
76      */
77     function showForm()
78     {
79         $form = new PathsAdminPanelForm($this);
80         $form->show();
81         return;
82     }
83
84     /**
85      * Save settings from the form
86      *
87      * @return void
88      */
89     function saveSettings()
90     {
91         static $settings = array(
92                                  'site' => array('path', 'locale_path', 'ssl', 'sslserver'),
93                                  'theme' => array('server', 'dir', 'path', 'sslserver', 'sslpath'),
94                                  'avatar' => array('server', 'dir', 'path'),
95                                  'attachments' => array('server', 'dir', 'path', 'sslserver', 'sslpath')
96                                  );
97
98         // XXX: If we're only going to have one boolean on thi page we
99         // can remove some of the boolean processing code --Z
100
101         static $booleans = array('site' => array('fancy'));
102
103         $values = array();
104
105         foreach ($settings as $section => $parts) {
106             foreach ($parts as $setting) {
107                 $values[$section][$setting] = $this->trimmed("$section-$setting");
108             }
109         }
110
111         foreach ($booleans as $section => $parts) {
112             foreach ($parts as $setting) {
113                 $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
114             }
115         }
116
117         $this->validate($values);
118
119         // assert(all values are valid);
120
121         $config = new Config();
122
123         $config->query('BEGIN');
124
125         foreach ($settings as $section => $parts) {
126             foreach ($parts as $setting) {
127                 Config::save($section, $setting, $values[$section][$setting]);
128             }
129         }
130
131         foreach ($booleans as $section => $parts) {
132             foreach ($parts as $setting) {
133                 Config::save($section, $setting, $values[$section][$setting]);
134             }
135         }
136
137         $config->query('COMMIT');
138
139         return;
140     }
141
142     /**
143      * Attempt to validate setting values
144      *
145      * @return void
146      */
147     function validate(&$values)
148     {
149         // Validate theme dir
150
151         if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
152             // TRANS: Client error in Paths admin panel.
153             // 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         // Validate avatar dir
158
159         if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
160             // TRANS: Client error in Paths admin panel.
161             // 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         // Validate locales dir
166
167         // XXX: What else do we need to validate for lacales path here? --Z
168
169         if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
170             // TRANS: Client error in Paths admin panel.
171             // 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         // Validate SSL setup
176
177         if (mb_strlen($values['site']['sslserver']) > 255) {
178             // TRANS: Client error in Paths admin panel.
179             // 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 class PathsAdminPanelForm extends AdminForm
186 {
187     /**
188      * ID of the form
189      *
190      * @return int ID of the form
191      */
192     function id()
193     {
194         return 'form_paths_admin_panel';
195     }
196
197     /**
198      * class of the form
199      *
200      * @return string class of the form
201      */
202     function formClass()
203     {
204         return 'form_settings';
205     }
206
207     /**
208      * Action of the form
209      *
210      * @return string URL of the action
211      */
212     function action()
213     {
214         return common_local_url('pathsadminpanel');
215     }
216
217     /**
218      * Data elements of the form
219      *
220      * @return void
221      */
222     function formData()
223     {
224         $this->out->elementStart('fieldset', array('id' => 'settings_paths_locale'));
225         // TRANS: Fieldset legend in Paths admin panel.
226         $this->out->element('legend', null, _('Site'), 'site');
227         $this->out->elementStart('ul', 'form_data');
228
229         $this->li();
230         $this->input('server',
231                      // TRANS: Field label in Paths admin panel.
232                      _('Server'),
233                      // TRANS: Field title in Paths admin panel.
234                      _('Site\'s server hostname.'));
235         $this->unli();
236
237         $this->li();
238         $this->input('path',
239                      // TRANS: Field label in Paths admin panel.
240                      _('Path'),
241                      // TRANS: Field title in Paths admin panel.
242                      _('Site path.'));
243         $this->unli();
244
245         $this->li();
246         $this->input('locale_path',
247                      // TRANS: Field label in Paths admin panel.
248                      _('Locale directory'),
249                      // TRANS: Field title in Paths admin panel.
250                      _('Directory path to locales.'),
251                      'site');
252         $this->unli();
253
254         $this->li();
255         $this->out->checkbox('fancy',
256                              // TRANS: Checkbox label in Paths admin panel.
257                              _('Fancy URLs'),
258                              (bool) $this->value('fancy'),
259                              // TRANS: Field title in Paths admin panel.
260                              _('Use fancy URLs (more readable and memorable)?'));
261         $this->unli();
262
263         $this->out->elementEnd('ul');
264         $this->out->elementEnd('fieldset');
265
266         $this->out->elementStart('fieldset', array('id' => 'settings_paths_theme'));
267         // TRANS: Fieldset legend in Paths admin panel.
268         $this->out->element('legend', null, _m('LEGEND','Theme'));
269
270         $this->out->elementStart('ul', 'form_data');
271
272         $this->li();
273         $this->input('server',
274                      // TRANS: Field label in Paths admin panel.
275                      _('Server'),
276                      // TRANS: Tooltip for field label in Paths admin panel.
277                      _('Server for themes.'),
278                      'theme');
279         $this->unli();
280
281         $this->li();
282         $this->input('path',
283                      // TRANS: Field label in Paths admin panel.
284                      _('Path'),
285                      // TRANS: Tooltip for field label in Paths admin panel.
286                      _('Web path to themes.'),
287                      'theme');
288         $this->unli();
289
290         $this->li();
291         $this->input('sslserver',
292                      // TRANS: Field label in Paths admin panel.
293                      _('SSL server'),
294                      // TRANS: Tooltip for field label in Paths admin panel.
295                      _('SSL server for themes (default: SSL server).'),
296                      'theme');
297         $this->unli();
298
299         $this->li();
300         $this->input('sslpath',
301                      // TRANS: Field label in Paths admin panel.
302                      _('SSL path'),
303                      // TRANS: Tooltip for field label in Paths admin panel.
304                      _('SSL path to themes (default: /theme/).'),
305                      'theme');
306         $this->unli();
307
308         $this->li();
309         $this->input('dir',
310                      // TRANS: Field label in Paths admin panel.
311                      _('Directory'),
312                      // TRANS: Tooltip for field label in Paths admin panel.
313                      _('Directory where themes are located.'),
314                      'theme');
315         $this->unli();
316
317         $this->out->elementEnd('ul');
318
319         $this->out->elementEnd('fieldset');
320         $this->out->elementStart('fieldset', array('id' => 'settings_avatar-paths'));
321         // TRANS: Fieldset legend in Paths admin panel.
322         $this->out->element('legend', null, _('Avatars'));
323
324         $this->out->elementStart('ul', 'form_data');
325
326         $this->li();
327         $this->input('server',
328                      // TRANS: Field label in Paths admin panel.
329                      _('Avatar server'),
330                      // TRANS: Tooltip for field label in Paths admin panel.
331                      _('Server for avatars.'),
332                      'avatar');
333         $this->unli();
334
335         $this->li();
336         $this->input('path',
337                      // TRANS: Field label in Paths admin panel.
338                      _('Avatar path'),
339                      // TRANS: Tooltip for field label in Paths admin panel.
340                      _('Web path to avatars.'),
341                      'avatar');
342         $this->unli();
343
344         $this->li();
345         $this->input('dir',
346                      // TRANS: Field label in Paths admin panel.
347                      _('Avatar directory'),
348                      // TRANS: Tooltip for field label in Paths admin panel.
349                      _('Directory where avatars are located.'),
350                      'avatar');
351         $this->unli();
352
353         $this->out->elementEnd('ul');
354
355         $this->out->elementEnd('fieldset');
356
357         $this->out->elementStart('fieldset', array('id' =>
358                                                    'settings_attachments-paths'));
359
360         // TRANS: Fieldset legens in Paths admin panel.
361         $this->out->element('legend', null, _('Attachments'));
362         $this->out->elementStart('ul', 'form_data');
363
364         $this->li();
365         $this->input('server',
366                      // TRANS: Field label in Paths admin panel.
367                      _('Server'),
368                      // TRANS: Tooltip for field label in Paths admin panel.
369                      _('Server for attachments.'),
370                      'attachments');
371         $this->unli();
372
373         $this->li();
374         $this->input('path',
375                      // TRANS: Field label in Paths admin panel.
376                      _('Path'),
377                      // TRANS: Tooltip for field label in Paths admin panel.
378                      _('Web path to attachments.'),
379                      'attachments');
380         $this->unli();
381
382         $this->li();
383         $this->input('sslserver',
384                      // TRANS: Field label in Paths admin panel.
385                      _('SSL server'),
386                      // TRANS: Tooltip for field label in Paths admin panel.
387                      _('Server for attachments on SSL pages.'),
388                      'attachments');
389         $this->unli();
390
391         $this->li();
392         $this->input('sslpath',
393                      // TRANS: Field label in Paths admin panel.
394                      _('SSL path'),
395                      // TRANS: Tooltip for field label in Paths admin panel.
396                      _('Web path to attachments on SSL pages.'),
397                      'attachments');
398         $this->unli();
399
400         $this->li();
401         $this->input('dir',
402                      // TRANS: Field label in Paths admin panel.
403                      _('Directory'),
404                      // TRANS: Tooltip for field label in Paths admin panel.
405                      _('Directory where attachments are located.'),
406                      'attachments');
407         $this->unli();
408
409         $this->out->elementEnd('ul');
410         $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         $this->out->elementStart('ul', 'form_data');
416         $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                      'sometimes' => _('Sometimes'),
422                       // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
423                      'always' => _('Always'));
424
425         $this->out->dropdown('site-ssl',
426                              // TRANS: Drop down label in Paths admin panel.
427                              _('Use SSL'),
428                              // TRANS: Tooltip for field label in Paths admin panel.
429                              $ssl, _('When to use SSL.'),
430                              false,
431                              $this->value('ssl', 'site'));
432         $this->unli();
433
434         $this->li();
435         $this->input('sslserver',
436                      // TRANS: Field label in Paths admin panel.
437                      _('SSL server'),
438                      // TRANS: Tooltip for field label in Paths admin panel.
439                      _('Server to direct SSL requests to.'),
440                      'site');
441         $this->unli();
442         $this->out->elementEnd('ul');
443         $this->out->elementEnd('fieldset');
444     }
445
446     /**
447      * Action elements
448      *
449      * @return void
450      */
451     function formActions()
452     {
453         // TRANS: Button text to store form data in the Paths admin panel.
454         $this->out->submit('save', _m('BUTTON','Save'), 'submit',
455                            // TRANS: Button title text to store form data in the Paths admin panel.
456                            'save', _('Save path settings.'));
457     }
458
459     /**
460      * Utility to simplify some of the duplicated code around
461      * params and settings. Overriding the input() in the base class
462      * to handle a whole bunch of cases of settings with the same
463      * name under different sections.
464      *
465      * @param string $setting      Name of the setting
466      * @param string $title        Title to use for the input
467      * @param string $instructions Instructions for this field
468      * @param string $section      config section, default = 'site'
469      *
470      * @return void
471      */
472     function input($setting, $title, $instructions, $section='site')
473     {
474         $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
475     }
476 }