xref: /plugin/farmer/admin/setup.php (revision c4c8e953c2820a8d49bd83ac58883b01f220312b)
1632c5618SAndreas Gohr<?php
2632c5618SAndreas Gohr/**
3632c5618SAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
4632c5618SAndreas Gohr *
5632c5618SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6632c5618SAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
7632c5618SAndreas Gohr */
8632c5618SAndreas Gohr
9632c5618SAndreas Gohr// must be run within Dokuwiki
10632c5618SAndreas Gohrif(!defined('DOKU_INC')) die();
11632c5618SAndreas Gohr
12632c5618SAndreas Gohrclass admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin {
13632c5618SAndreas Gohr
14632c5618SAndreas Gohr    /** @var helper_plugin_farmer $helper */
15632c5618SAndreas Gohr    private $helper;
16632c5618SAndreas Gohr
17632c5618SAndreas Gohr    /**
18632c5618SAndreas Gohr     * @return bool admin only!
19632c5618SAndreas Gohr     */
20632c5618SAndreas Gohr    public function forAdminOnly() {
21632c5618SAndreas Gohr        return true;
22632c5618SAndreas Gohr    }
23632c5618SAndreas Gohr
24632c5618SAndreas Gohr    /**
25632c5618SAndreas Gohr     * Should carry out any processing required by the plugin.
26632c5618SAndreas Gohr     */
27632c5618SAndreas Gohr    public function handle() {
28632c5618SAndreas Gohr        global $INPUT;
29632c5618SAndreas Gohr        global $ID;
30632c5618SAndreas Gohr
31632c5618SAndreas Gohr        if(!$INPUT->bool('farmdir')) return;
32632c5618SAndreas Gohr        if(!checkSecurityToken()) return;
33632c5618SAndreas Gohr
34632c5618SAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
35632c5618SAndreas Gohr
36632c5618SAndreas Gohr        $farmdir = trim($INPUT->str('farmdir', ''));
37632c5618SAndreas Gohr        if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
38632c5618SAndreas Gohr        $farmdir = fullpath($farmdir);
39632c5618SAndreas Gohr
40632c5618SAndreas Gohr        $errors = array();
41632c5618SAndreas Gohr        if($farmdir === '') {
42632c5618SAndreas Gohr            $errors[] = $this->getLang('farmdir_missing');
43632c5618SAndreas Gohr        } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
44632c5618SAndreas Gohr            $errors[] = $this->getLang('farmdir_in_dokuwiki');
45632c5618SAndreas Gohr        } elseif(!io_mkdir_p($farmdir)) {
46632c5618SAndreas Gohr            $errors[] = $this->getLang('farmdir_uncreatable');
47632c5618SAndreas Gohr        } elseif(!is_writeable($farmdir)) {
48632c5618SAndreas Gohr            $errors[] = $this->getLang('farmdir_unwritable');
49632c5618SAndreas Gohr        } elseif(count(scandir($farmdir)) > 2) {
50632c5618SAndreas Gohr            $errors[] = $this->getLang('farmdir_notEmpty');
51632c5618SAndreas Gohr        }
52632c5618SAndreas Gohr
53632c5618SAndreas Gohr        if($errors) {
54632c5618SAndreas Gohr            foreach($errors as $error) {
55632c5618SAndreas Gohr                msg($error, -1);
56632c5618SAndreas Gohr            }
57632c5618SAndreas Gohr            return;
58632c5618SAndreas Gohr        }
59632c5618SAndreas Gohr
60a646d519SAndreas Gohr        // create the files
61*c4c8e953SAndreas Gohr        $ok = $this->createPreloadPHP();
62*c4c8e953SAndreas Gohr        if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
63a646d519SAndreas Gohr        $ok &= $this->createFarmIni($farmdir);
64a646d519SAndreas Gohr
65a646d519SAndreas Gohr        if($ok) {
66632c5618SAndreas Gohr            msg($this->getLang('preload creation success'), 1);
67*c4c8e953SAndreas Gohr            $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
68632c5618SAndreas Gohr            send_redirect($link);
69632c5618SAndreas Gohr        } else {
70632c5618SAndreas Gohr            msg($this->getLang('preload creation error'), -1);
71632c5618SAndreas Gohr        }
72632c5618SAndreas Gohr    }
73632c5618SAndreas Gohr
74632c5618SAndreas Gohr    /**
75632c5618SAndreas Gohr     * Render HTML output, e.g. helpful text and a form
76632c5618SAndreas Gohr     */
77632c5618SAndreas Gohr    public function html() {
78632c5618SAndreas Gohr        // Is preload.php already enabled?
79632c5618SAndreas Gohr        if(file_exists(DOKU_INC . 'inc/preload.php')) {
80632c5618SAndreas Gohr            msg($this->getLang('overwrite_preload'), -1);
81632c5618SAndreas Gohr        }
82632c5618SAndreas Gohr
83632c5618SAndreas Gohr        $form = new \dokuwiki\Form\Form();
84632c5618SAndreas Gohr        $form->addClass('plugin_farmer');
85632c5618SAndreas Gohr        $form->addFieldsetOpen($this->getLang('preloadPHPForm'));
86*c4c8e953SAndreas Gohr        $form->addTextInput('farmdir', $this->getLang('farm dir'));
87*c4c8e953SAndreas Gohr        $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked');
88632c5618SAndreas Gohr        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
89632c5618SAndreas Gohr        $form->addFieldsetClose();
90632c5618SAndreas Gohr        echo $form->toHTML();
91632c5618SAndreas Gohr
92632c5618SAndreas Gohr        echo sprintf($this->locale_xhtml('preload'), dirname(DOKU_REL) . '/farm/');
93632c5618SAndreas Gohr
94632c5618SAndreas Gohr    }
95632c5618SAndreas Gohr
96632c5618SAndreas Gohr    /**
97a646d519SAndreas Gohr     * Creates the preload that loads our farm controller
98a646d519SAndreas Gohr     * @return bool true if saving was successful
99632c5618SAndreas Gohr     */
100a646d519SAndreas Gohr    protected function createPreloadPHP() {
101632c5618SAndreas Gohr        $content = "<?php\n";
102632c5618SAndreas Gohr        $content .= "# farm setup by farmer plugin\n";
103*c4c8e953SAndreas Gohr        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
104*c4c8e953SAndreas Gohr        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
105*c4c8e953SAndreas Gohr        $content .= "}\n";
106632c5618SAndreas Gohr        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
107632c5618SAndreas Gohr    }
108632c5618SAndreas Gohr
109632c5618SAndreas Gohr    /**
110632c5618SAndreas Gohr     * Appends the needed config to the main .htaccess for htaccess type setups
111632c5618SAndreas Gohr     *
112632c5618SAndreas Gohr     * @return bool true if saving was successful
113632c5618SAndreas Gohr     */
114632c5618SAndreas Gohr    protected function createHtaccess() {
115a646d519SAndreas Gohr        $content = "\n\n# Options added for farm setup by farmer plugin:\n";
116632c5618SAndreas Gohr        $content .= "RewriteEngine On\n";
117632c5618SAndreas Gohr        $content .= 'RewriteRule ^/?!([^/]+)/(.*)  ' . DOKU_REL . '$2?animal=$1 [QSA]' . "\n";
118632c5618SAndreas Gohr        $content .= 'RewriteRule ^/?!([^/]+)$      ' . DOKU_REL . '?animal=$1 [QSA]' . "\n";
119632c5618SAndreas Gohr        $content .= 'Options +FollowSymLinks'."\n";
120632c5618SAndreas Gohr        return io_saveFile(DOKU_INC . '.htaccess', $content, true);
121632c5618SAndreas Gohr    }
122632c5618SAndreas Gohr
123a646d519SAndreas Gohr    /**
124a646d519SAndreas Gohr     * Creates the initial configuration
125a646d519SAndreas Gohr     *
126a646d519SAndreas Gohr     * @param $animalpath
127a646d519SAndreas Gohr     * @return bool true if saving was successful
128a646d519SAndreas Gohr     */
129a646d519SAndreas Gohr    protected function createFarmIni($animalpath) {
130a646d519SAndreas Gohr        $content = "; farm config created by the farmer plugin\n\n";
131a646d519SAndreas Gohr        $content .= "[base]\n";
132a646d519SAndreas Gohr        $content .= "farmdir = $animalpath\n";
133a646d519SAndreas Gohr        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
134a646d519SAndreas Gohr        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
135a646d519SAndreas Gohr    }
136632c5618SAndreas Gohr}
137632c5618SAndreas Gohr
138632c5618SAndreas Gohr// vim:ts=4:sw=4:et:
139