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