1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4use dokuwiki\Form\Form;
5
6/**
7 * DokuWiki Plugin farmer (Admin Component)
8 *
9 * Setup the farm by creating preload.php etc
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author  Michael Große <grosse@cosmocode.de>
13 * @author  Andreas Gohr <gohr@cosmocode.de>
14 */
15class admin_plugin_farmer_setup extends AdminPlugin
16{
17    /** @var helper_plugin_farmer $helper */
18    private $helper;
19
20    /**
21     * @return bool admin only!
22     */
23    public function forAdminOnly()
24    {
25        return true;
26    }
27
28    /**
29     * Should carry out any processing required by the plugin.
30     */
31    public function handle()
32    {
33        global $INPUT;
34        global $ID;
35
36        if (!$INPUT->bool('farmdir')) return;
37        if (!checkSecurityToken()) return;
38
39        $this->helper = plugin_load('helper', 'farmer');
40
41        $farmdir = trim($INPUT->str('farmdir', ''));
42        if ($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
43        $farmdir = fullpath($farmdir);
44
45        $errors = [];
46        if ($farmdir === '') {
47            $errors[] = $this->getLang('farmdir_missing');
48        } elseif ($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
49            $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC));
50        } elseif (!io_mkdir_p($farmdir)) {
51            $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir));
52        } elseif (!is_writable($farmdir)) {
53            $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir));
54        } elseif (count(scandir($farmdir)) > 2) {
55            $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir));
56        }
57
58        if ($errors) {
59            foreach ($errors as $error) {
60                msg($error, -1);
61            }
62            return;
63        }
64
65        // create the files
66        $ok = $this->createPreloadPHP();
67        if ($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
68        if ($ok) $ok &= $this->createFarmIni($farmdir);
69
70        if ($ok) {
71            msg($this->getLang('preload creation success'), 1);
72            $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'config'], true, '&');
73            send_redirect($link);
74        } else {
75            msg($this->getLang('preload creation error'), -1);
76        }
77    }
78
79    /**
80     * Render HTML output, e.g. helpful text and a form
81     */
82    public function html()
83    {
84        // Is preload.php already enabled?
85        if (file_exists(DOKU_INC . 'inc/preload.php')) {
86            msg($this->getLang('overwrite_preload'), -1);
87        }
88
89        $form = new Form();
90        $form->addClass('plugin_farmer');
91        $form->addFieldsetOpen($this->getLang('preloadPHPForm'));
92        $form->addTextInput('farmdir', $this->getLang('farm dir'));
93        $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked');
94        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
95        $form->addFieldsetClose();
96        echo $form->toHTML();
97    }
98
99    /**
100     * Creates the preload that loads our farm controller
101     * @return bool true if saving was successful
102     */
103    protected function createPreloadPHP()
104    {
105        $content = "<?php\n";
106        $content .= "# farm setup by farmer plugin\n";
107        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
108        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
109        $content .= "}\n";
110        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
111    }
112
113    /**
114     * Prepends the needed config to the main .htaccess for htaccess type setups
115     *
116     * @return bool true if saving was successful
117     */
118    protected function createHtaccess()
119    {
120        // load existing or template
121        if (file_exists(DOKU_INC . '.htaccess')) {
122            $old = io_readFile(DOKU_INC . '.htaccess');
123        } elseif (file_exists(DOKU_INC . '.htaccess.dist')) {
124            $old = io_readFile(DOKU_INC . '.htaccess.dist');
125        } else {
126            $old = '';
127        }
128
129        $content = "# Options added for farm setup by farmer plugin:\n";
130        $content .= "RewriteEngine On\n";
131        $content .= 'RewriteRule ^!([^/]+)/(.*)  $2?animal=$1 [QSA,DPI]' . "\n";
132        $content .= 'RewriteRule ^!([^/]+)$      ?animal=$1 [QSA,DPI]' . "\n";
133        $content .= 'Options +FollowSymLinks' . "\n";
134        $content .= '# end of farm configuration' . "\n\n";
135        $content .= $old;
136        return io_saveFile(DOKU_INC . '.htaccess', $content);
137    }
138
139    /**
140     * Creates the initial configuration
141     *
142     * @param $animalpath
143     * @return bool true if saving was successful
144     */
145    protected function createFarmIni($animalpath)
146    {
147        $content = "; farm config created by the farmer plugin\n\n";
148        $content .= "[base]\n";
149        $content .= "farmdir = $animalpath\n";
150        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
151        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
152    }
153}
154