summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/farmer/admin/config.php
blob: 7d799706ece9f696d5f8c5b7fae8c86e8adfe0f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
 * DokuWiki Plugin farmer (Admin Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Michael Große <grosse@cosmocode.de>
 * @author  Andreas Gohr <gohr@cosmocode.de>
 */
use dokuwiki\Form\Form;

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

/**
 * Configuration Interface for farm.ini
 */
class admin_plugin_farmer_config extends DokuWiki_Admin_Plugin {

    /** @var  helper_plugin_farmer */
    protected $helper;

    /**
     * @return bool admin only!
     */
    public function forAdminOnly() {
        return false;
    }

    /**
     * admin_plugin_farmer_config constructor.
     */
    public function __construct() {
        $this->helper = plugin_load('helper', 'farmer');
    }

    /**
     * Should carry out any processing required by the plugin.
     */
    public function handle() {
        global $INPUT;
        global $ID;
        if(!$INPUT->has('farmconf')) return;
        if(!checkSecurityToken()) return;

        $farmconf = $this->helper->getConfig();
        $farmdir = $farmconf['base']['farmdir'];
        $farmconf = array_merge($farmconf, $INPUT->arr('farmconf'));
        $farmconf['base']['farmdir'] = $farmdir;

        $farmconf['base']['basedomain'] = trim(trim($farmconf['base']['basedomain'], '.'));

        $ini = DOKU_INC . 'conf/farm.ini';
        $data = "; Farm config created by the farmer plugin\n";
        $data .= $this->createIni($farmconf);
        io_saveFile($ini, $data);

        $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
        send_redirect($self);
    }

    /**
     * Render HTML output, e.g. helpful text and a form
     */
    public function html() {
        $farmconf = $this->helper->getConfig();

        $form = new Form(array('method' => 'post'));

        $form->addFieldsetOpen($this->getLang('base'));
        $form->addHTML('<label><span>' . $this->getLang('farm dir') . '</span>' . DOKU_FARMDIR);
        $form->addTextInput('farmconf[base][farmhost]', $this->getLang('farm host'))->val($farmconf['base']['farmhost']);
        $form->addTextInput('farmconf[base][basedomain]', $this->getLang('base domain'))->val($farmconf['base']['basedomain']);
        $form->addFieldsetClose();

        $form->addFieldsetOpen($this->getLang('conf_inherit'));
        foreach($farmconf['inherit'] as $key => $val) {
            $form->setHiddenField("farmconf[inherit][$key]", 0);
            $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false);
            if($val) $chk->attr('checked', 'checked');
        }
        $form->addFieldsetClose();

        $options = array(
            'farmer' => $this->getLang('conf_notfound_farmer'),
            '404' => $this->getLang('conf_notfound_404'),
            'list' => $this->getLang('conf_notfound_list'),
            'redirect' => $this->getLang('conf_notfound_redirect')
        );

        $form->addFieldsetOpen($this->getLang('conf_notfound'));
        $form->addDropdown('farmconf[notfound][show]', $options, $this->getLang('conf_notfound'))->val($farmconf['notfound']['show']);
        $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']);
        $form->addFieldsetClose();

        $form->addButton('save', $this->getLang('save'));
        echo $form->toHTML();
    }

    /**
     * Simple function to create an ini file
     *
     * Does no escaping, but should suffice for our use case
     *
     * @link http://stackoverflow.com/a/5695202/172068
     * @param array $data The data to transform
     * @return string
     */
    public function createIni($data) {
        $res = array();
        foreach($data as $key => $val) {
            if(is_array($val)) {
                $res[] = '';
                $res[] = "[$key]";
                foreach($val as $skey => $sval) {
                    $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"');
                }
            } else {
                $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"');
            }
        }
        $res[] = '';
        return join("\n", $res);
    }
}

// vim:ts=4:sw=4:et: