xref: /plugin/farmer/helper.php (revision af1c6dd8b9c1e35385ddfb7549af221f4070c191)
1<?php
2/**
3 * DokuWiki Plugin farmer (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 * @author  Andreas Gohr <gohr@cosmocode.de>
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13class helper_plugin_farmer extends DokuWiki_Plugin {
14
15    protected $defaultPluginState = null;
16    protected $animalPluginState  = array();
17
18    /**
19     * Returns the name of the current animal if any, false otherwise
20     *
21     * @return string|false
22     */
23    public function getAnimal() {
24        if(!isset($GLOBALS['FARMCORE'])) return false;
25        return $GLOBALS['FARMCORE']->getAnimal();
26    }
27
28    /**
29     * Get the farm config
30     *
31     * @return array
32     */
33    public function getConfig() {
34        if(!isset($GLOBALS['FARMCORE'])) return array();
35        return $GLOBALS['FARMCORE']->getConfig();
36    }
37
38    /**
39     * Was the current animal requested by host?
40     *
41     * @return bool
42     */
43    public function isHostbased() {
44        if(!isset($GLOBALS['FARMCORE'])) return false;
45        return $GLOBALS['FARMCORE']->isHostbased();
46    }
47
48    /**
49     * Was an animal requested that could not be found?
50     *
51     * @return bool
52     */
53    public function wasNotfound() {
54        if(!isset($GLOBALS['FARMCORE'])) return false;
55        return $GLOBALS['FARMCORE']->wasNotfound();
56    }
57
58    /**
59     * Guess the URL for an animal
60     *
61     * @param $animal
62     * @return string
63     */
64    public function getAnimalURL($animal) {
65        $config = $this->getConfig();
66
67        if(strpos($animal, '.') !== false) {
68            return 'http://' . $animal;
69        } elseif($config['base']['basedomain']) {
70            return 'http://' . $animal . '.' . $config['base']['basedomain'];
71        } else {
72            return DOKU_URL . '!' . $animal . '/';
73        }
74    }
75
76    /**
77     * List of all animals, i.e. directories within DOKU_FARMDIR without the template.
78     *
79     * @return array
80     */
81    public function getAllAnimals() {
82        $animals = array();
83        $list = glob(DOKU_FARMDIR . '*/conf/', GLOB_ONLYDIR);
84        foreach($list as $path) {
85            $animal = basename(dirname($path));
86            if($animal == '_animal') continue; // old template
87            $animals[] = $animal;
88        }
89        sort($animals);
90        return $animals;
91    }
92
93    /**
94     * checks wether $path is in under $container
95     *
96     * Also returns false if $path and $container are the same directory
97     *
98     * @param string $path
99     * @param string $container
100     * @return bool
101     */
102    public function isInPath($path, $container) {
103        $path = fullpath($path);
104        $container = fullpath($container);
105        if($path == $container) return false;
106        return (strpos($path, $container) === 0);
107    }
108
109    /**
110     * Check if the farm is correctly configured for this farmer plugin
111     *
112     * @return bool
113     */
114    public function checkFarmSetup() {
115        return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']);
116    }
117
118    /**
119     * @param string $animalname
120     *
121     * @return bool
122     */
123    public function validateAnimalName($animalname) {
124        return preg_match("/^[a-z0-9]+([\\.\\-][a-z0-9]+)*$/i", $animalname) === 1;
125    }
126
127    /**
128     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
129     *
130     * @todo: needs tests
131     *
132     * @author      Aidan Lister <aidan@php.net>
133     * @author      Michael Große <grosse@cosmocode.de>
134     * @author      Andreas Gohr <gohr@cosmocode.de>
135     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
136     *
137     * @param string $source Source path
138     * @param string $destination Destination path
139     * @param string $exclude Regular expression to exclude files or directories (complete with delimiters)
140     * @return bool Returns TRUE on success, FALSE on failure
141     */
142    function io_copyDir($source, $destination, $exclude = '') {
143        if($exclude && preg_match($exclude, $source)) {
144            return true;
145        }
146
147        if(is_link($source)) {
148            io_lock($destination);
149            $result = symlink(readlink($source), $destination);
150            io_unlock($destination);
151            return $result;
152        }
153
154        if(is_file($source)) {
155            io_lock($destination);
156            $result = copy($source, $destination);
157            io_unlock($destination);
158            return $result;
159        }
160
161        if(!is_dir($destination)) {
162            io_mkdir_p($destination);
163        }
164
165        $dir = @dir($source);
166        if($dir === false) return false;
167        while(false !== ($entry = $dir->read())) {
168            if($entry == '.' || $entry == '..') {
169                continue;
170            }
171
172            // recurse into directories
173            $this->io_copyDir("$source/$entry", "$destination/$entry", $exclude);
174        }
175
176        $dir->close();
177        return true;
178    }
179
180    /**
181     * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not.
182     *
183     * @param bool $all get all plugins, even disabled ones
184     * @return array
185     */
186    public function getAllPlugins($all = true) {
187
188        /** @var Doku_Plugin_Controller $plugin_controller */
189        global $plugin_controller;
190
191        $plugins = $plugin_controller->getList('', $all);
192
193        // filter out a few plugins
194        $plugins = array_filter(
195            $plugins, function ($item) {
196            if($item == 'farmer') return false;
197            if($item == 'extension') return false;
198            if($item == 'testing') return false;
199            return true;
200        }
201        );
202
203        sort($plugins);
204        return $plugins;
205    }
206
207    /**
208     * Get the plugin states configured locally in the given animal
209     *
210     * Response is cached
211     *
212     * @param $animal
213     * @return array
214     */
215    public function getAnimalPluginLocalStates($animal) {
216        if(isset($this->animalPluginState[$animal])) return $this->animalPluginState[$animal];
217
218        $localfile = DOKU_FARMDIR . $animal . '/conf/plugins.local.php';
219        $plugins = array();
220        if(file_exists($localfile)) {
221            include($localfile);
222        }
223
224        $this->animalPluginState[$animal] = $plugins;
225        return $plugins;
226    }
227
228    /**
229     * Return the default state plugins would have in animals
230     *
231     * Response is cached
232     *
233     * @return array
234     */
235    public function getDefaultPluginStates() {
236        if(!is_null($this->defaultPluginState)) return $this->defaultPluginState;
237
238        $farmconf = $this->getConfig();
239        $all = $this->getAllPlugins();
240
241        $plugins = array();
242        foreach($all as $one) {
243            if($farmconf['inherit']['plugins']) {
244                $plugins[$one] = !plugin_isdisabled($one);
245            } else {
246                $plugins[$one] = true; // default state is enabled
247            }
248        }
249
250        $this->defaultPluginState = $plugins;
251        return $plugins;
252    }
253
254    /**
255     * Return a structure giving detailed info about the state of all plugins in an animal
256     *
257     * @param $animal
258     * @return array
259     */
260    public function getAnimalPluginRealState($animal) {
261        $info = array();
262
263        $defaults = $this->getDefaultPluginStates();
264        $local = $this->getAnimalPluginLocalStates($animal);
265
266        foreach($defaults as $plugin => $set) {
267            $current = array(
268                'name' => $plugin,
269                'default' => $set,
270                'actual' => $set,
271                'isdefault' => true
272            );
273
274            if(isset($local[$plugin])) {
275                $current['actual'] = (bool) $local[$plugin];
276                $current['isdefault'] = false;
277            }
278
279            $info[] = $current;
280        }
281
282        return $info;
283    }
284
285    /**
286     * Set the state of a plugin in an animal
287     *
288     * @param string $plugin
289     * @param string $animal
290     * @param int $state -1 = default, 1 = enabled, 0 = disabled
291     */
292    public function setPluginState($plugin, $animal, $state) {
293        $state = (int) $state;
294
295        $plugins = $this->getAnimalPluginLocalStates($animal);
296        if($state < 0) {
297            if(isset($plugins[$plugin])) unset($plugins[$plugin]);
298        } else {
299            $plugins[$plugin] = $state;
300        }
301
302        $this->writePluginConf($plugins, $animal);
303    }
304
305    /**
306     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
307     *
308     * updates the plugin state cache
309     *
310     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
311     * @param string $animal Directory of the animal within DOKU_FARMDIR
312     */
313    public function writePluginConf($plugins, $animal) {
314        $pluginConf = '<?php' . "\n# plugins enabled and disabled by the farmer plugin\n";
315        foreach($plugins as $plugin => $status) {
316            $pluginConf .= '$plugins[\'' . $plugin . '\'] = ' . $status . ";\n";
317        }
318        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
319        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
320
321        $this->animalPluginState[$animal] = $plugins;
322    }
323}
324