xref: /plugin/farmer/admin/setup.php (revision 9a6163bc8c5e1e1eaa3160c5b2e09f201cb168d4)
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
10if(!defined('DOKU_INC')) die();
11
12class admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin {
13
14    /** @var helper_plugin_farmer $helper */
15    private $helper;
16
17    /**
18     * @return bool admin only!
19     */
20    public function forAdminOnly() {
21        return true;
22    }
23
24    /**
25     * Should carry out any processing required by the plugin.
26     */
27    public function handle() {
28        global $INPUT;
29        global $ID;
30
31        if(!$INPUT->bool('farmdir')) return;
32        if(!checkSecurityToken()) return;
33
34        $this->helper = plugin_load('helper', 'farmer');
35
36        $farmdir = trim($INPUT->str('farmdir', ''));
37        if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
38        $farmdir = fullpath($farmdir);
39
40        $errors = array();
41        if($farmdir === '') {
42            $errors[] = $this->getLang('farmdir_missing');
43        } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
44            $errors[] = $this->getLang('farmdir_in_dokuwiki');
45        } elseif(!io_mkdir_p($farmdir)) {
46            $errors[] = $this->getLang('farmdir_uncreatable');
47        } elseif(!is_writeable($farmdir)) {
48            $errors[] = $this->getLang('farmdir_unwritable');
49        } elseif(count(scandir($farmdir)) > 2) {
50            $errors[] = $this->getLang('farmdir_notEmpty');
51        }
52
53        if($INPUT->str('serversetup', '', true) === '') {
54            $errors[] = $this->getLang('serversetup_missing');
55        }
56
57        if($errors) {
58            foreach($errors as $error) {
59                msg($error, -1);
60            }
61            return;
62        }
63
64        // create the files
65        $ok = $this->createPreloadPHP($farmdir);
66        if($ok && $INPUT->str('serversetup') == 'htaccess') $ok &= $this->createHtaccess();
67        $ok &= $this->createFarmIni($farmdir);
68
69        if($ok) {
70            msg($this->getLang('preload creation success'), 1);
71            $link = wl($ID, array('do' => 'admin', 'page' => 'farmer'), true, '&');
72            send_redirect($link);
73        } else {
74            msg($this->getLang('preload creation error'), -1);
75        }
76    }
77
78    /**
79     * Render HTML output, e.g. helpful text and a form
80     */
81    public function html() {
82        // Is preload.php already enabled?
83        if(file_exists(DOKU_INC . 'inc/preload.php')) {
84            msg($this->getLang('overwrite_preload'), -1);
85        }
86
87        $form = new \dokuwiki\Form\Form();
88        $form->addClass('plugin_farmer');
89        $form->addFieldsetOpen($this->getLang('preloadPHPForm'));
90        $form->addTextInput('farmdir', $this->getLang('farm dir'))->addClass('block edit');
91
92        $form->addRadioButton('serversetup', $this->getLang('subdomain setup'))->val('subdomain')->attr('type', 'radio')->addClass('block edit')->id('subdomain__setup');
93        $form->addRadioButton('serversetup', $this->getLang('htaccess setup'))->val('htaccess')->attr('type', 'radio')->addClass('block edit')->attr('checked', true)->id('htaccess__setup');
94
95        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
96        $form->addFieldsetClose();
97        echo $form->toHTML();
98
99        echo sprintf($this->locale_xhtml('preload'), dirname(DOKU_REL) . '/farm/');
100
101    }
102
103    /**
104     * Creates the preload that loads our farm controller
105     * @return bool true if saving was successful
106     */
107    protected function createPreloadPHP() {
108        $content = "<?php\n";
109        $content .= "# farm setup by farmer plugin\n";
110        $content .= "include(fullpath(dirname(__FILE__)).'/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
111        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
112    }
113
114    /**
115     * Appends the needed config to the main .htaccess for htaccess type setups
116     *
117     * @return bool true if saving was successful
118     */
119    protected function createHtaccess() {
120        $content = "\n\n# Options added for farm setup by farmer plugin:\n";
121        $content .= "RewriteEngine On\n";
122        $content .= 'RewriteRule ^/?!([^/]+)/(.*)  ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n";
123        $content .= 'RewriteRule ^/?!([^/]+)$      ' . DOKU_REL . '?animal=$1 [QSA]' . "\n";
124        $content .= 'Options +FollowSymLinks'."\n";
125        return io_saveFile(DOKU_INC . '.htaccess', $content, true);
126    }
127
128    /**
129     * Creates the initial configuration
130     *
131     * @param $animalpath
132     * @return bool true if saving was successful
133     */
134    protected function createFarmIni($animalpath) {
135        $content = "; farm config created by the farmer plugin\n\n";
136        $content .= "[base]\n";
137        $content .= "farmdir = $animalpath\n";
138        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
139        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
140    }
141}
142
143// vim:ts=4:sw=4:et:
144