]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/pathsadminpanel.php
Merge commit 'refs/merge-requests/157' of git://gitorious.org/statusnet/mainline...
[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-2010 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                                  'background' => array('server', 'dir', 'path', 'sslserver', 'sslpath'),
96                                  'attachments' => array('server', 'dir', 'path', 'sslserver', 'sslpath')
97                                  );
98
99         // XXX: If we're only going to have one boolean on thi page we
100         // can remove some of the boolean processing code --Z
101
102         static $booleans = array('site' => array('fancy'));
103
104         $values = array();
105
106         foreach ($settings as $section => $parts) {
107             foreach ($parts as $setting) {
108                 $values[$section][$setting] = $this->trimmed("$section-$setting");
109             }
110         }
111
112         foreach ($booleans as $section => $parts) {
113             foreach ($parts as $setting) {
114                 $values[$section][$setting] = ($this->boolean($setting)) ? 1 : 0;
115             }
116         }
117
118         $this->validate($values);
119
120         // assert(all values are valid);
121
122         $config = new Config();
123
124         $config->query('BEGIN');
125
126         foreach ($settings as $section => $parts) {
127             foreach ($parts as $setting) {
128                 Config::save($section, $setting, $values[$section][$setting]);
129             }
130         }
131
132         foreach ($booleans as $section => $parts) {
133             foreach ($parts as $setting) {
134                 Config::save($section, $setting, $values[$section][$setting]);
135             }
136         }
137
138         $config->query('COMMIT');
139
140         return;
141     }
142
143     /**
144      * Attempt to validate setting values
145      *
146      * @return void
147      */
148     function validate(&$values)
149     {
150         // Validate theme dir
151
152         if (!empty($values['theme']['dir']) && !is_readable($values['theme']['dir'])) {
153             // TRANS: Client error in Paths admin panel.
154             // TRANS: %s is the directory that could not be read from.
155             $this->clientError(sprintf(_('Theme directory not readable: %s.'), $values['theme']['dir']));
156         }
157
158         // Validate avatar dir
159
160         if (empty($values['avatar']['dir']) || !is_writable($values['avatar']['dir'])) {
161             // TRANS: Client error in Paths admin panel.
162             // TRANS: %s is the avatar directory that could not be written to.
163             $this->clientError(sprintf(_('Avatar directory not writable: %s.'), $values['avatar']['dir']));
164         }
165
166         // Validate background dir
167
168         if (empty($values['background']['dir']) || !is_writable($values['background']['dir'])) {
169             // TRANS: Client error in Paths admin panel.
170             // TRANS: %s is the background directory that could not be written to.
171             $this->clientError(sprintf(_('Background directory not writable: %s.'), $values['background']['dir']));
172         }
173
174         // Validate locales dir
175
176         // XXX: What else do we need to validate for lacales path here? --Z
177
178         if (!empty($values['site']['locale_path']) && !is_readable($values['site']['locale_path'])) {
179             // TRANS: Client error in Paths admin panel.
180             // TRANS: %s is the locales directory that could not be read from.
181             $this->clientError(sprintf(_('Locales directory not readable: %s.'), $values['site']['locale_path']));
182         }
183
184         // Validate SSL setup
185
186         if (mb_strlen($values['site']['sslserver']) > 255) {
187             // TRANS: Client error in Paths admin panel.
188             // TRANS: %s is the SSL server URL that is too long.
189             $this->clientError(_('Invalid SSL server. The maximum length is 255 characters.'));
190         }
191     }
192 }
193
194 class PathsAdminPanelForm extends AdminForm
195 {
196     /**
197      * ID of the form
198      *
199      * @return int ID of the form
200      */
201     function id()
202     {
203         return 'form_paths_admin_panel';
204     }
205
206     /**
207      * class of the form
208      *
209      * @return string class of the form
210      */
211     function formClass()
212     {
213         return 'form_settings';
214     }
215
216     /**
217      * Action of the form
218      *
219      * @return string URL of the action
220      */
221     function action()
222     {
223         return common_local_url('pathsadminpanel');
224     }
225
226     /**
227      * Data elements of the form
228      *
229      * @return void
230      */
231     function formData()
232     {
233         $this->out->elementStart('fieldset', array('id' => 'settings_paths_locale'));
234         // TRANS: Fieldset legend in Paths admin panel.
235         $this->out->element('legend', null, _('Site'), 'site');
236         $this->out->elementStart('ul', 'form_data');
237
238         $this->li();
239         $this->input('server',
240                      // TRANS: Field label in Paths admin panel.
241                      _('Server'),
242                      // TRANS: Field title in Paths admin panel.
243                      _('Site\'s server hostname.'));
244         $this->unli();
245
246         $this->li();
247         $this->input('path',
248                      // TRANS: Field label in Paths admin panel.
249                      _('Path'),
250                      // TRANS: Field title in Paths admin panel.
251                      _('Site path.'));
252         $this->unli();
253
254         $this->li();
255         $this->input('locale_path',
256                      // TRANS: Field label in Paths admin panel.
257                      _('Locale directory'),
258                      // TRANS: Field title in Paths admin panel.
259                      _('Directory path to locales.'),
260                      'site');
261         $this->unli();
262
263         $this->li();
264         $this->out->checkbox('fancy',
265                              // TRANS: Checkbox label in Paths admin panel.
266                              _('Fancy URLs'),
267                              (bool) $this->value('fancy'),
268                              // TRANS: Field title in Paths admin panel.
269                              _('Use fancy URLs (more readable and memorable)?'));
270         $this->unli();
271
272         $this->out->elementEnd('ul');
273         $this->out->elementEnd('fieldset');
274
275         $this->out->elementStart('fieldset', array('id' => 'settings_paths_theme'));
276         // TRANS: Fieldset legend in Paths admin panel.
277         $this->out->element('legend', null, _m('LEGEND','Theme'));
278
279         $this->out->elementStart('ul', 'form_data');
280
281         $this->li();
282         $this->input('server',
283                      // TRANS: Field label in Paths admin panel.
284                      _('Server'),
285                      // TRANS: Tooltip for field label in Paths admin panel.
286                      _('Server for themes.'),
287                      'theme');
288         $this->unli();
289
290         $this->li();
291         $this->input('path',
292                      // TRANS: Field label in Paths admin panel.
293                      _('Path'),
294                      // TRANS: Tooltip for field label in Paths admin panel.
295                      _('Web path to themes.'),
296                      'theme');
297         $this->unli();
298
299         $this->li();
300         $this->input('sslserver',
301                      // TRANS: Field label in Paths admin panel.
302                      _('SSL server'),
303                      // TRANS: Tooltip for field label in Paths admin panel.
304                      _('SSL server for themes (default: SSL server).'),
305                      'theme');
306         $this->unli();
307
308         $this->li();
309         $this->input('sslpath',
310                      // TRANS: Field label in Paths admin panel.
311                      _('SSL path'),
312                      // TRANS: Tooltip for field label in Paths admin panel.
313                      _('SSL path to themes (default: /theme/).'),
314                      'theme');
315         $this->unli();
316
317         $this->li();
318         $this->input('dir',
319                      // TRANS: Field label in Paths admin panel.
320                      _('Directory'),
321                      // TRANS: Tooltip for field label in Paths admin panel.
322                      _('Directory where themes are located.'),
323                      'theme');
324         $this->unli();
325
326         $this->out->elementEnd('ul');
327
328         $this->out->elementEnd('fieldset');
329         $this->out->elementStart('fieldset', array('id' => 'settings_avatar-paths'));
330         // TRANS: Fieldset legend in Paths admin panel.
331         $this->out->element('legend', null, _('Avatars'));
332
333         $this->out->elementStart('ul', 'form_data');
334
335         $this->li();
336         $this->input('server',
337                      // TRANS: Field label in Paths admin panel.
338                      _('Avatar server'),
339                      // TRANS: Tooltip for field label in Paths admin panel.
340                      _('Server for avatars.'),
341                      'avatar');
342         $this->unli();
343
344         $this->li();
345         $this->input('path',
346                      // TRANS: Field label in Paths admin panel.
347                      _('Avatar path'),
348                      // TRANS: Tooltip for field label in Paths admin panel.
349                      _('Web path to avatars.'),
350                      'avatar');
351         $this->unli();
352
353         $this->li();
354         $this->input('dir',
355                      // TRANS: Field label in Paths admin panel.
356                      _('Avatar directory'),
357                      // TRANS: Tooltip for field label in Paths admin panel.
358                      _('Directory where avatars are located.'),
359                      'avatar');
360         $this->unli();
361
362         $this->out->elementEnd('ul');
363
364         $this->out->elementEnd('fieldset');
365
366         $this->out->elementStart('fieldset', array('id' =>
367                                                    'settings_attachments-paths'));
368
369         // TRANS: Fieldset legens in Paths admin panel.
370         $this->out->element('legend', null, _('Attachments'));
371         $this->out->elementStart('ul', 'form_data');
372
373         $this->li();
374         $this->input('server',
375                      // TRANS: Field label in Paths admin panel.
376                      _('Server'),
377                      // TRANS: Tooltip for field label in Paths admin panel.
378                      _('Server for attachments.'),
379                      'attachments');
380         $this->unli();
381
382         $this->li();
383         $this->input('path',
384                      // TRANS: Field label in Paths admin panel.
385                      _('Path'),
386                      // TRANS: Tooltip for field label in Paths admin panel.
387                      _('Web path to attachments.'),
388                      'attachments');
389         $this->unli();
390
391         $this->li();
392         $this->input('sslserver',
393                      // TRANS: Field label in Paths admin panel.
394                      _('SSL server'),
395                      // TRANS: Tooltip for field label in Paths admin panel.
396                      _('Server for attachments on SSL pages.'),
397                      'attachments');
398         $this->unli();
399
400         $this->li();
401         $this->input('sslpath',
402                      // TRANS: Field label in Paths admin panel.
403                      _('SSL path'),
404                      // TRANS: Tooltip for field label in Paths admin panel.
405                      _('Web path to attachments on SSL pages.'),
406                      'attachments');
407         $this->unli();
408
409         $this->li();
410         $this->input('dir',
411                      // TRANS: Field label in Paths admin panel.
412                      _('Directory'),
413                      // TRANS: Tooltip for field label in Paths admin panel.
414                      _('Directory where attachments are located.'),
415                      'attachments');
416         $this->unli();
417
418         $this->out->elementEnd('ul');
419         $this->out->elementEnd('fieldset');
420
421         $this->out->elementStart('fieldset', array('id' => 'settings_admin_ssl'));
422         // TRANS: Fieldset legend in Paths admin panel.
423         $this->out->element('legend', null, _m('LEGEND','SSL'));
424         $this->out->elementStart('ul', 'form_data');
425         $this->li();
426
427         // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
428         $ssl = array('never' => _('Never'),
429                      // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
430                      'sometimes' => _('Sometimes'),
431                       // TRANS: Drop down option in Paths admin panel (option for "When to use SSL").
432                      'always' => _('Always'));
433
434         $this->out->dropdown('site-ssl',
435                              // TRANS: Drop down label in Paths admin panel.
436                              _('Use SSL'),
437                              // TRANS: Tooltip for field label in Paths admin panel.
438                              $ssl, _('When to use SSL.'),
439                              false,
440                              $this->value('ssl', 'site'));
441         $this->unli();
442
443         $this->li();
444         $this->input('sslserver',
445                      // TRANS: Field label in Paths admin panel.
446                      _('SSL server'),
447                      // TRANS: Tooltip for field label in Paths admin panel.
448                      _('Server to direct SSL requests to.'),
449                      'site');
450         $this->unli();
451         $this->out->elementEnd('ul');
452         $this->out->elementEnd('fieldset');
453     }
454
455     /**
456      * Action elements
457      *
458      * @return void
459      */
460     function formActions()
461     {
462         // TRANS: Button text to store form data in the Paths admin panel.
463         $this->out->submit('save', _m('BUTTON','Save'), 'submit',
464                            // TRANS: Button title text to store form data in the Paths admin panel.
465                            'save', _('Save paths'));
466     }
467
468     /**
469      * Utility to simplify some of the duplicated code around
470      * params and settings. Overriding the input() in the base class
471      * to handle a whole bunch of cases of settings with the same
472      * name under different sections.
473      *
474      * @param string $setting      Name of the setting
475      * @param string $title        Title to use for the input
476      * @param string $instructions Instructions for this field
477      * @param string $section      config section, default = 'site'
478      *
479      * @return void
480      */
481     function input($setting, $title, $instructions, $section='site')
482     {
483         $this->out->input("$section-$setting", $title, $this->value($setting, $section), $instructions);
484     }
485 }