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