xref: /plugin/farmer/admin/config.php (revision 954c40ad15965c60e6a9a47948b3cd54d507ff25)
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    /** @var  helper_plugin_farmer */
17    protected $helper;
18
19    /**
20     * @return bool admin only!
21     */
22    public function forAdminOnly() {
23        return false;
24    }
25
26    /**
27     * admin_plugin_farmer_config constructor.
28     */
29    public function __construct() {
30        $this->helper = plugin_load('helper', 'farmer');
31    }
32
33    /**
34     * Should carry out any processing required by the plugin.
35     */
36    public function handle() {
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, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
55        send_redirect($self);
56    }
57
58    /**
59     * Render HTML output, e.g. helpful text and a form
60     */
61    public function html() {
62        $farmconf = $this->helper->getConfig();
63
64        $form = new Form(array('method' => 'post'));
65
66        $form->addFieldsetOpen($this->getLang('base'));
67        $form->addHTML('<label><span>'.$this->getLang('farm dir').'</span>'.DOKU_FARMDIR);
68        $form->addTextInput('farmconf[base][farmhost]', $this->getLang('farm host'))->val($farmconf['base']['farmhost']);
69        $form->addTextInput('farmconf[base][basedomain]', $this->getLang('base domain'))->val($farmconf['base']['basedomain']);
70        $form->addFieldsetClose();
71
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))->useInput(false);
77            if($val) $chk->attr('checked', 'checked');
78        }
79        $form->addFieldsetClose();
80
81
82        $options = array(
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'))->val($farmconf['notfound']['show']);
91        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
92        $form->addFieldsetClose();
93
94        $form->addButton('save', $this->getLang('save'));
95        echo $form->toHTML();
96    }
97
98    /**
99     * Simple function to create an ini file
100     *
101     * Does no escaping, but should suffice for our use case
102     *
103     * @link http://stackoverflow.com/a/5695202/172068
104     * @param array $data The data to transform
105     * @return string
106     */
107    public function createIni($data) {
108        $res = array();
109        foreach($data as $key => $val) {
110            if(is_array($val)) {
111                $res[] = '';
112                $res[] = "[$key]";
113                foreach($val as $skey => $sval) {
114                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
115                }
116            } else {
117                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
118            }
119        }
120        $res[] = '';
121        return join("\n", $res);
122    }
123}
124
125// vim:ts=4:sw=4:et:
126