1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Form\Form;
5
6/**
7 * DokuWiki Plugin farmer (Admin Component)
8 *
9 * Configuration Interface for farm.ini
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author  Michael Große <grosse@cosmocode.de>
13 * @author  Andreas Gohr <gohr@cosmocode.de>
14 */
15class admin_plugin_farmer_config extends AdminPlugin
16{
17    /** @var  helper_plugin_farmer */
18    protected $helper;
19
20    /** @inheritdoc */
21    public function showInMenu()
22    {
23        return false;
24    }
25
26    /**
27     * admin_plugin_farmer_config constructor.
28     */
29    public function __construct()
30    {
31        $this->helper = plugin_load('helper', 'farmer');
32    }
33
34    /** @inheritdoc */
35    public function handle()
36    {
37        global $INPUT;
38        global $ID;
39        if (!$INPUT->has('farmconf')) return;
40        if (!checkSecurityToken()) return;
41
42        $farmconf = $this->helper->getConfig();
43        $farmdir = $farmconf['base']['farmdir'];
44        $farmconf = array_merge($farmconf, $INPUT->arr('farmconf'));
45        $farmconf['base']['farmdir'] = $farmdir;
46
47        $farmconf['base']['basedomain'] = trim(trim($farmconf['base']['basedomain'], '.'));
48
49        $ini = DOKU_INC . 'conf/farm.ini';
50        $data = "; Farm config created by the farmer plugin\n";
51        $data .= $this->createIni($farmconf);
52        io_saveFile($ini, $data);
53
54        $self = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'config'], true, '&');
55        send_redirect($self);
56    }
57
58    /** @inheritdoc */
59    public function html()
60    {
61        $farmconf = $this->helper->getConfig();
62
63        $form = new Form(['method' => 'post']);
64
65        $form->addFieldsetOpen($this->getLang('base'));
66        $form->addHTML('<label><span>' . $this->getLang('farm dir') . '</span>' . DOKU_FARMDIR);
67        $form->addTextInput('farmconf[base][farmhost]', $this->getLang('farm host'))
68            ->val($farmconf['base']['farmhost']);
69        $form->addTextInput('farmconf[base][basedomain]', $this->getLang('base domain'))
70            ->val($farmconf['base']['basedomain']);
71        $form->addFieldsetClose();
72
73        $form->addFieldsetOpen($this->getLang('conf_inherit'));
74        foreach ($farmconf['inherit'] as $key => $val) {
75            $form->setHiddenField("farmconf[inherit][$key]", 0);
76            $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))
77                ->useInput(false);
78            if ($val) $chk->attr('checked', 'checked');
79        }
80        $form->addFieldsetClose();
81
82        $options = [
83            'farmer' => $this->getLang('conf_notfound_farmer'),
84            '404' => $this->getLang('conf_notfound_404'),
85            'list' => $this->getLang('conf_notfound_list'),
86            'redirect' => $this->getLang('conf_notfound_redirect')
87        ];
88
89        $form->addFieldsetOpen($this->getLang('conf_notfound'));
90        $form->addDropdown('farmconf[notfound][show]', $options, $this->getLang('conf_notfound'))
91            ->val($farmconf['notfound']['show']);
92        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))
93            ->val($farmconf['notfound']['url']);
94        $form->addFieldsetClose();
95
96        $form->addButton('save', $this->getLang('save'));
97        echo $form->toHTML();
98    }
99
100    /**
101     * Simple function to create an ini file
102     *
103     * Does no escaping, but should suffice for our use case
104     *
105     * @link http://stackoverflow.com/a/5695202/172068
106     * @param array $data The data to transform
107     * @return string
108     */
109    public function createIni($data)
110    {
111        $res = [];
112        foreach ($data as $key => $val) {
113            if (is_array($val)) {
114                $res[] = '';
115                $res[] = "[$key]";
116                foreach ($val as $skey => $sval) {
117                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
118                }
119            } else {
120                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
121            }
122        }
123        $res[] = '';
124        return implode("\n", $res);
125    }
126}
127