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