xref: /plugin/farmer/admin/config.php (revision a646d51957345f8381be2a22b73b0bf048cf637d)
1<?php
2/**
3 * DokuWiki Plugin farmer (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10use dokuwiki\Form\Form;
11
12if(!defined('DOKU_INC')) die();
13
14class admin_plugin_farmer_config extends DokuWiki_Admin_Plugin {
15
16    /**
17     * @return bool admin only!
18     */
19    public function forAdminOnly() {
20        return false;
21    }
22
23    /**
24     * Should carry out any processing required by the plugin.
25     */
26    public function handle() {
27        global $INPUT;
28        global $ID;
29        if(!$INPUT->has('farmconf')) return;
30        if(!checkSecurityToken()) return;
31
32        global $FARMCORE;
33        $farmconf = $FARMCORE->getConfig();
34        $farmconf = array_merge($farmconf, $INPUT->arr('farmconf'));
35
36        $ini = DOKU_INC . 'conf/farm.ini';
37        $data = "; Farm config created by the farmer plugin\n";
38        $data .= $this->createIni($farmconf);
39        io_saveFile($ini, $data);
40
41        $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
42        send_redirect($self);
43    }
44
45    /**
46     * Render HTML output, e.g. helpful text and a form
47     */
48    public function html() {
49        global $FARMCORE;
50        $farmconf = $FARMCORE->getConfig();
51
52        $form = new Form(array('method' => 'post'));
53        $form->addFieldsetOpen($this->getLang('conf_inherit'));
54        foreach($farmconf['inherit'] as $key => $val) {
55            $form->setHiddenField("farmconf[inherit][$key]", 0);
56            $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false);
57            if($val) $chk->attr('checked', 'checked');
58        }
59        $form->addFieldsetClose();
60
61        $form->addFieldsetOpen($this->getLang('conf_notfound'));
62        $form->addTagOpen('select')->attr('name', 'farmconf[notfound][show]');
63        foreach(array('farmer', '404', 'list', 'redirect') as $key) {
64            $opt = $form->addTagOpen('option')->attr('value', $key);
65            if($farmconf['notfound']['show'] == $key) $opt->attr('selected', 'selected');
66            $form->addHTML($this->getLang('conf_notfound_' . $key));
67            $form->addTagClose('option');
68        }
69        $form->addTagClose('select');
70        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
71        $form->addFieldsetClose();
72
73        $form->addButton('save', $this->getLang('save'));
74        echo $form->toHTML();
75    }
76
77    /**
78     * Simple function to create an ini file
79     *
80     * Does no escaping, but should suffice for our use case
81     *
82     * @link http://stackoverflow.com/a/5695202/172068
83     * @param array $data The data to transform
84     * @return string
85     */
86    public function createIni($data) {
87        $res = array();
88        foreach($data as $key => $val) {
89            if(is_array($val)) {
90                $res[] = '';
91                $res[] = "[$key]";
92                foreach($val as $skey => $sval) {
93                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
94                }
95            } else {
96                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
97            }
98        }
99        $res[] = '';
100        return join("\n", $res);
101    }
102}
103
104// vim:ts=4:sw=4:et:
105