xref: /plugin/farmer/admin/setup.php (revision 1da41c8bdb9ce0b590d5b69552c4ddb4d0aef860)
1632c5618SAndreas Gohr<?php
2*1da41c8bSAndreas Gohr
3*1da41c8bSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
4*1da41c8bSAndreas Gohruse dokuwiki\Form\Form;
5*1da41c8bSAndreas Gohr
6632c5618SAndreas Gohr/**
7632c5618SAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
8632c5618SAndreas Gohr *
9*1da41c8bSAndreas Gohr * Setup the farm by creating preload.php etc
10*1da41c8bSAndreas Gohr *
11632c5618SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12632c5618SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
130a5d2da2SAndreas Gohr * @author  Andreas Gohr <gohr@cosmocode.de>
14632c5618SAndreas Gohr */
15*1da41c8bSAndreas Gohrclass admin_plugin_farmer_setup extends AdminPlugin
16*1da41c8bSAndreas Gohr{
17632c5618SAndreas Gohr    /** @var helper_plugin_farmer $helper */
18632c5618SAndreas Gohr    private $helper;
19632c5618SAndreas Gohr
20632c5618SAndreas Gohr    /**
21632c5618SAndreas Gohr     * @return bool admin only!
22632c5618SAndreas Gohr     */
23*1da41c8bSAndreas Gohr    public function forAdminOnly()
24*1da41c8bSAndreas Gohr    {
25632c5618SAndreas Gohr        return true;
26632c5618SAndreas Gohr    }
27632c5618SAndreas Gohr
28632c5618SAndreas Gohr    /**
29632c5618SAndreas Gohr     * Should carry out any processing required by the plugin.
30632c5618SAndreas Gohr     */
31*1da41c8bSAndreas Gohr    public function handle()
32*1da41c8bSAndreas Gohr    {
33632c5618SAndreas Gohr        global $INPUT;
34632c5618SAndreas Gohr        global $ID;
35632c5618SAndreas Gohr
36632c5618SAndreas Gohr        if (!$INPUT->bool('farmdir')) return;
37632c5618SAndreas Gohr        if (!checkSecurityToken()) return;
38632c5618SAndreas Gohr
39632c5618SAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
40632c5618SAndreas Gohr
41632c5618SAndreas Gohr        $farmdir = trim($INPUT->str('farmdir', ''));
42632c5618SAndreas Gohr        if ($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
43632c5618SAndreas Gohr        $farmdir = fullpath($farmdir);
44632c5618SAndreas Gohr
45*1da41c8bSAndreas Gohr        $errors = [];
46632c5618SAndreas Gohr        if ($farmdir === '') {
47632c5618SAndreas Gohr            $errors[] = $this->getLang('farmdir_missing');
48632c5618SAndreas Gohr        } elseif ($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
49f4bc36c6SAndreas Gohr            $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC));
50632c5618SAndreas Gohr        } elseif (!io_mkdir_p($farmdir)) {
51f4bc36c6SAndreas Gohr            $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir));
52*1da41c8bSAndreas Gohr        } elseif (!is_writable($farmdir)) {
53f4bc36c6SAndreas Gohr            $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir));
54632c5618SAndreas Gohr        } elseif (count(scandir($farmdir)) > 2) {
55f4bc36c6SAndreas Gohr            $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir));
56632c5618SAndreas Gohr        }
57632c5618SAndreas Gohr
58632c5618SAndreas Gohr        if ($errors) {
59632c5618SAndreas Gohr            foreach ($errors as $error) {
60632c5618SAndreas Gohr                msg($error, -1);
61632c5618SAndreas Gohr            }
62632c5618SAndreas Gohr            return;
63632c5618SAndreas Gohr        }
64632c5618SAndreas Gohr
65a646d519SAndreas Gohr        // create the files
66c4c8e953SAndreas Gohr        $ok = $this->createPreloadPHP();
67c4c8e953SAndreas Gohr        if ($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
6823164e01SAndreas Gohr        if ($ok) $ok &= $this->createFarmIni($farmdir);
69a646d519SAndreas Gohr
70a646d519SAndreas Gohr        if ($ok) {
71632c5618SAndreas Gohr            msg($this->getLang('preload creation success'), 1);
72*1da41c8bSAndreas Gohr            $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'config'], true, '&');
73632c5618SAndreas Gohr            send_redirect($link);
74632c5618SAndreas Gohr        } else {
75632c5618SAndreas Gohr            msg($this->getLang('preload creation error'), -1);
76632c5618SAndreas Gohr        }
77632c5618SAndreas Gohr    }
78632c5618SAndreas Gohr
79632c5618SAndreas Gohr    /**
80632c5618SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
81632c5618SAndreas Gohr     */
82*1da41c8bSAndreas Gohr    public function html()
83*1da41c8bSAndreas Gohr    {
84632c5618SAndreas Gohr        // Is preload.php already enabled?
85632c5618SAndreas Gohr        if (file_exists(DOKU_INC . 'inc/preload.php')) {
86632c5618SAndreas Gohr            msg($this->getLang('overwrite_preload'), -1);
87632c5618SAndreas Gohr        }
88632c5618SAndreas Gohr
89*1da41c8bSAndreas Gohr        $form = new Form();
90632c5618SAndreas Gohr        $form->addClass('plugin_farmer');
91632c5618SAndreas Gohr        $form->addFieldsetOpen($this->getLang('preloadPHPForm'));
92c4c8e953SAndreas Gohr        $form->addTextInput('farmdir', $this->getLang('farm dir'));
93c4c8e953SAndreas Gohr        $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked');
94632c5618SAndreas Gohr        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
95632c5618SAndreas Gohr        $form->addFieldsetClose();
96632c5618SAndreas Gohr        echo $form->toHTML();
97632c5618SAndreas Gohr    }
98632c5618SAndreas Gohr
99632c5618SAndreas Gohr    /**
100a646d519SAndreas Gohr     * Creates the preload that loads our farm controller
101a646d519SAndreas Gohr     * @return bool true if saving was successful
102632c5618SAndreas Gohr     */
103*1da41c8bSAndreas Gohr    protected function createPreloadPHP()
104*1da41c8bSAndreas Gohr    {
105632c5618SAndreas Gohr        $content = "<?php\n";
106632c5618SAndreas Gohr        $content .= "# farm setup by farmer plugin\n";
107c4c8e953SAndreas Gohr        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
108c4c8e953SAndreas Gohr        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
109c4c8e953SAndreas Gohr        $content .= "}\n";
110632c5618SAndreas Gohr        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
111632c5618SAndreas Gohr    }
112632c5618SAndreas Gohr
113632c5618SAndreas Gohr    /**
1145881efbeSAndreas Gohr     * Prepends the needed config to the main .htaccess for htaccess type setups
115632c5618SAndreas Gohr     *
116632c5618SAndreas Gohr     * @return bool true if saving was successful
117632c5618SAndreas Gohr     */
118*1da41c8bSAndreas Gohr    protected function createHtaccess()
119*1da41c8bSAndreas Gohr    {
1205881efbeSAndreas Gohr        // load existing or template
1215881efbeSAndreas Gohr        if (file_exists(DOKU_INC . '.htaccess')) {
1225881efbeSAndreas Gohr            $old = io_readFile(DOKU_INC . '.htaccess');
1235881efbeSAndreas Gohr        } elseif (file_exists(DOKU_INC . '.htaccess.dist')) {
1245881efbeSAndreas Gohr            $old = io_readFile(DOKU_INC . '.htaccess.dist');
1255881efbeSAndreas Gohr        } else {
1265881efbeSAndreas Gohr            $old = '';
1275881efbeSAndreas Gohr        }
1285881efbeSAndreas Gohr
1295881efbeSAndreas Gohr        $content = "# Options added for farm setup by farmer plugin:\n";
130632c5618SAndreas Gohr        $content .= "RewriteEngine On\n";
1315881efbeSAndreas Gohr        $content .= 'RewriteRule ^!([^/]+)/(.*)  $2?animal=$1 [QSA,DPI]' . "\n";
1325881efbeSAndreas Gohr        $content .= 'RewriteRule ^!([^/]+)$      ?animal=$1 [QSA,DPI]' . "\n";
133632c5618SAndreas Gohr        $content .= 'Options +FollowSymLinks' . "\n";
1345881efbeSAndreas Gohr        $content .= '# end of farm configuration' . "\n\n";
1355881efbeSAndreas Gohr        $content .= $old;
1365881efbeSAndreas Gohr        return io_saveFile(DOKU_INC . '.htaccess', $content);
137632c5618SAndreas Gohr    }
138632c5618SAndreas Gohr
139a646d519SAndreas Gohr    /**
140a646d519SAndreas Gohr     * Creates the initial configuration
141a646d519SAndreas Gohr     *
142a646d519SAndreas Gohr     * @param $animalpath
143a646d519SAndreas Gohr     * @return bool true if saving was successful
144a646d519SAndreas Gohr     */
145*1da41c8bSAndreas Gohr    protected function createFarmIni($animalpath)
146*1da41c8bSAndreas Gohr    {
147a646d519SAndreas Gohr        $content = "; farm config created by the farmer plugin\n\n";
148a646d519SAndreas Gohr        $content .= "[base]\n";
149a646d519SAndreas Gohr        $content .= "farmdir = $animalpath\n";
150a646d519SAndreas Gohr        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
151a646d519SAndreas Gohr        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
152a646d519SAndreas Gohr    }
153632c5618SAndreas Gohr}
154