xref: /plugin/farmer/admin/setup.php (revision 801ebaa1b6370a05d63b66cbbb24818101c96361)
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 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13/**
14 * Setup the farm by creating preload.php etc
15 */
16class admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin {
17
18    /** @var helper_plugin_farmer $helper */
19    private $helper;
20
21    /**
22     * @return bool admin only!
23     */
24    public function forAdminOnly() {
25        return true;
26    }
27
28    /**
29     * Should carry out any processing required by the plugin.
30     */
31    public function handle() {
32        global $INPUT;
33        global $ID;
34
35        if(!$INPUT->bool('farmdir')) return;
36        if(!checkSecurityToken()) return;
37
38        $this->helper = plugin_load('helper', 'farmer');
39
40        $farmdir = trim($INPUT->str('farmdir', ''));
41        if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
42        $farmdir = fullpath($farmdir);
43
44        $errors = array();
45        if($farmdir === '') {
46            $errors[] = $this->getLang('farmdir_missing');
47        } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
48            $errors[] = $this->getLang('farmdir_in_dokuwiki');
49        } elseif(!io_mkdir_p($farmdir)) {
50            $errors[] = $this->getLang('farmdir_uncreatable');
51        } elseif(!is_writeable($farmdir)) {
52            $errors[] = $this->getLang('farmdir_unwritable');
53        } elseif(count(scandir($farmdir)) > 2) {
54            $errors[] = $this->getLang('farmdir_notEmpty');
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();
66        if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
67        if($ok) $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', 'sub' => 'config'), 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'));
91        $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked');
92        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
93        $form->addFieldsetClose();
94        echo $form->toHTML();
95
96    }
97
98    /**
99     * Creates the preload that loads our farm controller
100     * @return bool true if saving was successful
101     */
102    protected function createPreloadPHP() {
103        $content = "<?php\n";
104        $content .= "# farm setup by farmer plugin\n";
105        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
106        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
107        $content .= "}\n";
108        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
109    }
110
111    /**
112     * Appends the needed config to the main .htaccess for htaccess type setups
113     *
114     * @return bool true if saving was successful
115     */
116    protected function createHtaccess() {
117        $content = "\n\n# Options added for farm setup by farmer plugin:\n";
118        $content .= "RewriteEngine On\n";
119        $content .= 'RewriteRule ^/?!([^/]+)/(.*)  ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n";
120        $content .= 'RewriteRule ^/?!([^/]+)$      ' . DOKU_REL . '?animal=$1 [QSA]' . "\n";
121        $content .= 'Options +FollowSymLinks' . "\n";
122        return io_saveFile(DOKU_INC . '.htaccess', $content, true);
123    }
124
125    /**
126     * Creates the initial configuration
127     *
128     * @param $animalpath
129     * @return bool true if saving was successful
130     */
131    protected function createFarmIni($animalpath) {
132        $content = "; farm config created by the farmer plugin\n\n";
133        $content .= "[base]\n";
134        $content .= "farmdir = $animalpath\n";
135        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
136        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
137    }
138}
139
140// vim:ts=4:sw=4:et:
141