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    /** @inheritdoc */
21    public function showInMenu()
22    {
23        return false;
24    }
25
26    /** @inheritdoc */
27    public function handle()
28    {
29        global $INPUT;
30        global $ID;
31
32        if (!$INPUT->bool('farmdir')) return;
33        if (!checkSecurityToken()) return;
34
35        $this->helper = plugin_load('helper', 'farmer');
36
37        $farmdir = trim($INPUT->str('farmdir', ''));
38        if ($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
39        $farmdir = fullpath($farmdir);
40
41        $errors = [];
42        if ($farmdir === '') {
43            $errors[] = $this->getLang('farmdir_missing');
44        } elseif ($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
45            $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC));
46        } elseif (!io_mkdir_p($farmdir)) {
47            $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir));
48        } elseif (!is_writable($farmdir)) {
49            $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir));
50        } elseif (count(scandir($farmdir)) > 2) {
51            $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir));
52        }
53
54        if ($errors) {
55            foreach ($errors as $error) {
56                msg($error, -1);
57            }
58            return;
59        }
60
61        // create the files
62        $ok = $this->createPreloadPHP();
63        if ($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
64        if ($ok) $ok &= $this->createFarmIni($farmdir);
65
66        if ($ok) {
67            msg($this->getLang('preload creation success'), 1);
68            $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'config'], true, '&');
69            send_redirect($link);
70        } else {
71            msg($this->getLang('preload creation error'), -1);
72        }
73    }
74
75    /** @inheritdoc */
76    public function html()
77    {
78        // Is preload.php already enabled?
79        if (file_exists(DOKU_INC . 'inc/preload.php')) {
80            msg($this->getLang('overwrite_preload'), -1);
81        }
82
83        $form = new Form();
84        $form->addClass('plugin_farmer');
85        $form->addFieldsetOpen($this->getLang('preloadPHPForm'));
86        $form->addTextInput('farmdir', $this->getLang('farm dir'));
87        $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked');
88        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
89        $form->addFieldsetClose();
90        echo $form->toHTML();
91    }
92
93    /**
94     * Creates the preload that loads our farm controller
95     * @return bool true if saving was successful
96     */
97    protected function createPreloadPHP()
98    {
99        $content = "<?php\n";
100        $content .= "# farm setup by farmer plugin\n";
101        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
102        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
103        $content .= "}\n";
104        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
105    }
106
107    /**
108     * Prepends the needed config to the main .htaccess for htaccess type setups
109     *
110     * @return bool true if saving was successful
111     */
112    protected function createHtaccess()
113    {
114        // load existing or template
115        if (file_exists(DOKU_INC . '.htaccess')) {
116            $old = io_readFile(DOKU_INC . '.htaccess');
117        } elseif (file_exists(DOKU_INC . '.htaccess.dist')) {
118            $old = io_readFile(DOKU_INC . '.htaccess.dist');
119        } else {
120            $old = '';
121        }
122
123        $content = "# Options added for farm setup by farmer plugin:\n";
124        $content .= "RewriteEngine On\n";
125        $content .= 'RewriteRule ^!([^/]+)/(.*)  $2?animal=$1 [QSA,DPI]' . "\n";
126        $content .= 'RewriteRule ^!([^/]+)$      ?animal=$1 [QSA,DPI]' . "\n";
127        $content .= 'Options +FollowSymLinks' . "\n";
128        $content .= '# end of farm configuration' . "\n\n";
129        $content .= $old;
130        return io_saveFile(DOKU_INC . '.htaccess', $content);
131    }
132
133    /**
134     * Creates the initial configuration
135     *
136     * @param $animalpath
137     * @return bool true if saving was successful
138     */
139    protected function createFarmIni($animalpath)
140    {
141        $content = "; farm config created by the farmer plugin\n\n";
142        $content .= "[base]\n";
143        $content .= "farmdir = $animalpath\n";
144        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
145        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
146    }
147}
148