summaryrefslogtreecommitdiff
path: root/platform/www/lib/plugins/farmer/admin/setup.php
blob: f836ae59f23930d78d607b94d16c4570f6f8ac44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
 * DokuWiki Plugin farmer (Admin Component)
 *
 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
 * @author  Michael Große <grosse@cosmocode.de>
 * @author  Andreas Gohr <gohr@cosmocode.de>
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

/**
 * Setup the farm by creating preload.php etc
 */
class admin_plugin_farmer_setup extends DokuWiki_Admin_Plugin {

    /** @var helper_plugin_farmer $helper */
    private $helper;

    /**
     * @return bool admin only!
     */
    public function forAdminOnly() {
        return true;
    }

    /**
     * Should carry out any processing required by the plugin.
     */
    public function handle() {
        global $INPUT;
        global $ID;

        if(!$INPUT->bool('farmdir')) return;
        if(!checkSecurityToken()) return;

        $this->helper = plugin_load('helper', 'farmer');

        $farmdir = trim($INPUT->str('farmdir', ''));
        if($farmdir[0] !== '/') $farmdir = DOKU_INC . $farmdir;
        $farmdir = fullpath($farmdir);

        $errors = array();
        if($farmdir === '') {
            $errors[] = $this->getLang('farmdir_missing');
        } elseif($this->helper->isInPath($farmdir, DOKU_INC) !== false) {
            $errors[] = sprintf($this->getLang('farmdir_in_dokuwiki'), hsc($farmdir), hsc(DOKU_INC));
        } elseif(!io_mkdir_p($farmdir)) {
            $errors[] = sprintf($this->getLang('farmdir_uncreatable'), hsc($farmdir));
        } elseif(!is_writeable($farmdir)) {
            $errors[] = sprintf($this->getLang('farmdir_unwritable'), hsc($farmdir));
        } elseif(count(scandir($farmdir)) > 2) {
            $errors[] = sprintf($this->getLang('farmdir_notEmpty'), hsc($farmdir));
        }

        if($errors) {
            foreach($errors as $error) {
                msg($error, -1);
            }
            return;
        }

        // create the files
        $ok = $this->createPreloadPHP();
        if($ok && $INPUT->bool('htaccess')) $ok &= $this->createHtaccess();
        if($ok) $ok &= $this->createFarmIni($farmdir);

        if($ok) {
            msg($this->getLang('preload creation success'), 1);
            $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&');
            send_redirect($link);
        } else {
            msg($this->getLang('preload creation error'), -1);
        }
    }

    /**
     * Render HTML output, e.g. helpful text and a form
     */
    public function html() {
        // Is preload.php already enabled?
        if(file_exists(DOKU_INC . 'inc/preload.php')) {
            msg($this->getLang('overwrite_preload'), -1);
        }

        $form = new \dokuwiki\Form\Form();
        $form->addClass('plugin_farmer');
        $form->addFieldsetOpen($this->getLang('preloadPHPForm'));
        $form->addTextInput('farmdir', $this->getLang('farm dir'));
        $form->addCheckbox('htaccess', $this->getLang('htaccess setup'))->attr('checked', 'checked');
        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit');
        $form->addFieldsetClose();
        echo $form->toHTML();

    }

    /**
     * Creates the preload that loads our farm controller
     * @return bool true if saving was successful
     */
    protected function createPreloadPHP() {
        $content = "<?php\n";
        $content .= "# farm setup by farmer plugin\n";
        $content .= "if(file_exists(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php')) {\n";
        $content .= "    include(__DIR__ . '/../lib/plugins/farmer/DokuWikiFarmCore.php');\n";
        $content .= "}\n";
        return io_saveFile(DOKU_INC . 'inc/preload.php', $content);
    }

    /**
     * Prepends the needed config to the main .htaccess for htaccess type setups
     *
     * @return bool true if saving was successful
     */
    protected function createHtaccess() {
        // load existing or template
        if(file_exists(DOKU_INC . '.htaccess')) {
            $old = io_readFile(DOKU_INC . '.htaccess');
        } elseif(file_exists(DOKU_INC . '.htaccess.dist')) {
            $old = io_readFile(DOKU_INC . '.htaccess.dist');
        } else {
            $old = '';
        }

        $content = "# Options added for farm setup by farmer plugin:\n";
        $content .= "RewriteEngine On\n";
        $content .= 'RewriteRule ^!([^/]+)/(.*)  $2?animal=$1 [QSA,DPI]' . "\n";
        $content .= 'RewriteRule ^!([^/]+)$      ?animal=$1 [QSA,DPI]' . "\n";
        $content .= 'Options +FollowSymLinks' . "\n";
        $content .= '# end of farm configuration' . "\n\n";
        $content .= $old;
        return io_saveFile(DOKU_INC . '.htaccess', $content);
    }

    /**
     * Creates the initial configuration
     *
     * @param $animalpath
     * @return bool true if saving was successful
     */
    protected function createFarmIni($animalpath) {
        $content = "; farm config created by the farmer plugin\n\n";
        $content .= "[base]\n";
        $content .= "farmdir = $animalpath\n";
        $content .= "farmhost = {$_SERVER['HTTP_HOST']}\n";
        return io_saveFile(DOKU_INC . 'conf/farm.ini', $content);
    }
}

// vim:ts=4:sw=4:et: