xref: /plugin/farmer/admin/setup.php (revision d1dda524cb09d9f0de237206bae480d58fb70065)
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($errors) {
54            foreach($errors as $error) {
55                msg($error, -1);
56            }
57            return;
58        }
59
60        // create the files
61        $ok = $this->createPreloadPHP();
62        if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
63        $ok &= $this->createFarmIni($farmdir);
64
65        if($ok) {
66            msg($this->getLang('preload creation success'), 1);
67            $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
68            send_redirect($link);
69        } else {
70            msg($this->getLang('preload creation error'), -1);
71        }
72    }
73
74    /**
75     * Render HTML output, e.g. helpful text and a form
76     */
77    public function html() {
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 \dokuwiki\Form\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    }
95
96    /**
97     * Creates the preload that loads our farm controller
98     * @return bool true if saving was successful
99     */
100    protected function createPreloadPHP() {
101        $content = "<?php\n";
102        $content .= "# farm setup by farmer plugin\n";
103        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
104        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
105        $content .= "}\n";
106        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
107    }
108
109    /**
110     * Appends the needed config to the main .htaccess for htaccess type setups
111     *
112     * @return bool true if saving was successful
113     */
114    protected function createHtaccess() {
115        $content = "\n\n# Options added for farm setup by farmer plugin:\n";
116        $content .= "RewriteEngine On\n";
117        $content .= 'RewriteRule ^/?!([^/]+)/(.*)  ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n";
118        $content .= 'RewriteRule ^/?!([^/]+)$      ' . DOKU_REL . '?animal=$1 [QSA]' . "\n";
119        $content .= 'Options +FollowSymLinks'."\n";
120        return io_saveFile(DOKU_INC . '.htaccess', $content, true);
121    }
122
123    /**
124     * Creates the initial configuration
125     *
126     * @param $animalpath
127     * @return bool true if saving was successful
128     */
129    protected function createFarmIni($animalpath) {
130        $content = "; farm config created by the farmer plugin\n\n";
131        $content .= "[base]\n";
132        $content .= "farmdir = $animalpath\n";
133        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
134        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
135    }
136}
137
138// vim:ts=4:sw=4:et:
139