xref: /plugin/farmer/helper.php (revision 8262a4cbfa75b904634931087aeac13ac2fa2168)
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
15fcbe16a4SMichael Große    private $allPlugins = array();
16fcbe16a4SMichael Große
17bc461538SMichael Große    /**
18632c5618SAndreas Gohr     * Returns the name of the current animal if any, false otherwise
19632c5618SAndreas Gohr     *
20632c5618SAndreas Gohr     * @return string|false
21632c5618SAndreas Gohr     */
22632c5618SAndreas Gohr    public function getAnimal() {
23b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return false;
24b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->getAnimal();
25632c5618SAndreas Gohr    }
26632c5618SAndreas Gohr
27632c5618SAndreas Gohr    /**
28b96c66ccSAndreas Gohr     * Get the farm config
29b96c66ccSAndreas Gohr     *
30b96c66ccSAndreas Gohr     * @return array
31b96c66ccSAndreas Gohr     */
32b96c66ccSAndreas Gohr    public function getConfig() {
33b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return array();
34b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->getConfig();
35b96c66ccSAndreas Gohr    }
36b96c66ccSAndreas Gohr
37b96c66ccSAndreas Gohr    /**
38b96c66ccSAndreas Gohr     * Was the current animal requested by host?
39b96c66ccSAndreas Gohr     *
40b96c66ccSAndreas Gohr     * @return bool
41b96c66ccSAndreas Gohr     */
42b96c66ccSAndreas Gohr    public function isHostbased() {
43b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return false;
44b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->isHostbased();
45b96c66ccSAndreas Gohr    }
46b96c66ccSAndreas Gohr
47b96c66ccSAndreas Gohr    /**
48b96c66ccSAndreas Gohr     * Was an animal requested that could not be found?
49b96c66ccSAndreas Gohr     *
50b96c66ccSAndreas Gohr     * @return bool
51b96c66ccSAndreas Gohr     */
52b96c66ccSAndreas Gohr    public function wasNotfound() {
53b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return false;
54b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->wasNotfound();
55b96c66ccSAndreas Gohr    }
56b96c66ccSAndreas Gohr
57b96c66ccSAndreas Gohr    /**
58c4c8e953SAndreas Gohr     * Guess the URL for an animal
59c4c8e953SAndreas Gohr     *
60c4c8e953SAndreas Gohr     * @param $animal
61c4c8e953SAndreas Gohr     * @return string
62c4c8e953SAndreas Gohr     */
63c4c8e953SAndreas Gohr    public function getAnimalURL($animal) {
64c4c8e953SAndreas Gohr        $config = $this->getConfig();
65c4c8e953SAndreas Gohr
66c4c8e953SAndreas Gohr        if(strpos($animal, '.') !== false) {
67c4c8e953SAndreas Gohr            return 'http://' . $animal;
68c4c8e953SAndreas Gohr        } elseif($config['base']['basedomain']) {
69c4c8e953SAndreas Gohr            return 'http://' . $animal . '.' . $config['base']['basedomain'];
70c4c8e953SAndreas Gohr        } else {
710336ab2aSAndreas Gohr            return DOKU_URL . '!' . $animal . '/';
72c4c8e953SAndreas Gohr        }
73c4c8e953SAndreas Gohr    }
74c4c8e953SAndreas Gohr
75c4c8e953SAndreas Gohr    /**
76b96c66ccSAndreas Gohr     * List of all animals, i.e. directories within DOKU_FARMDIR without the template.
77b96c66ccSAndreas Gohr     *
78b96c66ccSAndreas Gohr     * @return array
79b96c66ccSAndreas Gohr     */
80b96c66ccSAndreas Gohr    public function getAllAnimals() {
81b96c66ccSAndreas Gohr        $animals = array();
82*8262a4cbSAndreas Gohr        $list = glob(DOKU_FARMDIR . '*/conf/', GLOB_ONLYDIR);
83b96c66ccSAndreas Gohr        foreach($list as $path) {
84b96c66ccSAndreas Gohr            $animal = basename(dirname($path));
85b96c66ccSAndreas Gohr            if($animal == '_animal') continue; // old template
86b96c66ccSAndreas Gohr            $animals[] = $animal;
87b96c66ccSAndreas Gohr        }
88b96c66ccSAndreas Gohr        sort($animals);
89b96c66ccSAndreas Gohr        return $animals;
90b96c66ccSAndreas Gohr    }
91b96c66ccSAndreas Gohr
92b96c66ccSAndreas Gohr    /**
93b96c66ccSAndreas Gohr     * checks wether $path is in under $container
94b96c66ccSAndreas Gohr     *
95dfdaf33eSAndreas Gohr     * Also returns false if $path and $container are the same directory
96dfdaf33eSAndreas Gohr     *
97b96c66ccSAndreas Gohr     * @param string $path
98b96c66ccSAndreas Gohr     * @param string $container
99b96c66ccSAndreas Gohr     * @return bool
100b96c66ccSAndreas Gohr     */
101b96c66ccSAndreas Gohr    public function isInPath($path, $container) {
102dfdaf33eSAndreas Gohr        $path = fullpath($path);
103dfdaf33eSAndreas Gohr        $container = fullpath($container);
104dfdaf33eSAndreas Gohr        if($path == $container) return false;
105dfdaf33eSAndreas Gohr        return (strpos($path, $container) === 0);
106b96c66ccSAndreas Gohr    }
107b96c66ccSAndreas Gohr
108b96c66ccSAndreas Gohr    /**
109b96c66ccSAndreas Gohr     * Check if the farm is correctly configured for this farmer plugin
110b96c66ccSAndreas Gohr     *
111b96c66ccSAndreas Gohr     * @return bool
112b96c66ccSAndreas Gohr     */
113b96c66ccSAndreas Gohr    public function checkFarmSetup() {
114b96c66ccSAndreas Gohr        return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']);
115b96c66ccSAndreas Gohr    }
116b96c66ccSAndreas Gohr
11749f2871cSAndreas Gohr    /**
11849f2871cSAndreas Gohr     * @param string $animalname
11949f2871cSAndreas Gohr     *
12049f2871cSAndreas Gohr     * @return bool
12149f2871cSAndreas Gohr     */
12249f2871cSAndreas Gohr    public function validateAnimalName($animalname) {
12378c63d53SAndreas Gohr        return preg_match("/^[a-z0-9]+([\\.\\-][a-z0-9]+)*$/i", $animalname) === 1;
12449f2871cSAndreas Gohr    }
125b96c66ccSAndreas Gohr
126b96c66ccSAndreas Gohr    /**
127bc461538SMichael Große     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
128bc461538SMichael Große     *
129bc461538SMichael Große     * @todo: needs tests
130bc461538SMichael Große     *
131bc461538SMichael Große     * @author      Aidan Lister <aidan@php.net>
132bc461538SMichael Große     * @author      Michael Große <grosse@cosmocode.de>
133801ebaa1SAndreas Gohr     * @author      Andreas Gohr <gohr@cosmocode.de>
134bc461538SMichael Große     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
135bc461538SMichael Große     *
136bc461538SMichael Große     * @param string $source Source path
137bc461538SMichael Große     * @param string $destination Destination path
138801ebaa1SAndreas Gohr     * @param string $exclude Regular expression to exclude files or directories (complete with delimiters)
139bc461538SMichael Große     * @return bool Returns TRUE on success, FALSE on failure
140bc461538SMichael Große     */
141801ebaa1SAndreas Gohr    function io_copyDir($source, $destination, $exclude = '') {
142801ebaa1SAndreas Gohr        if($exclude && preg_match($exclude, $source)) {
143801ebaa1SAndreas Gohr            return true;
144801ebaa1SAndreas Gohr        }
145801ebaa1SAndreas Gohr
146bc461538SMichael Große        if(is_link($source)) {
147bc461538SMichael Große            io_lock($destination);
148bc461538SMichael Große            $result = symlink(readlink($source), $destination);
149bc461538SMichael Große            io_unlock($destination);
150bc461538SMichael Große            return $result;
151bc461538SMichael Große        }
152bc461538SMichael Große
153bc461538SMichael Große        if(is_file($source)) {
154bc461538SMichael Große            io_lock($destination);
155bc461538SMichael Große            $result = copy($source, $destination);
156bc461538SMichael Große            io_unlock($destination);
157bc461538SMichael Große            return $result;
158bc461538SMichael Große        }
159bc461538SMichael Große
160bc461538SMichael Große        if(!is_dir($destination)) {
161bc461538SMichael Große            io_mkdir_p($destination);
162bc461538SMichael Große        }
163bc461538SMichael Große
16449f2871cSAndreas Gohr        $dir = @dir($source);
16549f2871cSAndreas Gohr        if($dir === false) return false;
166bc461538SMichael Große        while(false !== ($entry = $dir->read())) {
167bc461538SMichael Große            if($entry == '.' || $entry == '..') {
168bc461538SMichael Große                continue;
169bc461538SMichael Große            }
170bc461538SMichael Große
171bc461538SMichael Große            // recurse into directories
172801ebaa1SAndreas Gohr            $this->io_copyDir("$source/$entry", "$destination/$entry", $exclude);
173bc461538SMichael Große        }
174bc461538SMichael Große
175bc461538SMichael Große        $dir->close();
176bc461538SMichael Große        return true;
177bc461538SMichael Große    }
178bc461538SMichael Große
17916bbfe4bSMichael Große    /**
18016bbfe4bSMichael Große     * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not.
18116bbfe4bSMichael Große     *
18216bbfe4bSMichael Große     * @return array
18316bbfe4bSMichael Große     */
1840b96e6d7SMichael Große    public function getAllPlugins() {
1853536d644SAndreas Gohr        /** @var Doku_Plugin_Controller $plugin_controller */
1863536d644SAndreas Gohr        global $plugin_controller;
1873536d644SAndreas Gohr
1883536d644SAndreas Gohr        $plugins = $plugin_controller->getList('', true);
1893536d644SAndreas Gohr
1903536d644SAndreas Gohr        // filter out a few plugins
1913536d644SAndreas Gohr        $plugins = array_filter($plugins, function($item) {
1923536d644SAndreas Gohr            if($item == 'farmer') return false;
1933536d644SAndreas Gohr            if($item == 'extension') return false;
1943536d644SAndreas Gohr            if($item == 'testing') return false;
1953536d644SAndreas Gohr            return true;
1963536d644SAndreas Gohr        });
1973536d644SAndreas Gohr
1986ec1ad8fSMichael Große        sort($plugins);
1990b96e6d7SMichael Große        return $plugins;
2000b96e6d7SMichael Große    }
2010b96e6d7SMichael Große
20216bbfe4bSMichael Große    /**
2033536d644SAndreas Gohr     * Activate a specific plugin in a specific animal
20416bbfe4bSMichael Große     *
20516bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be activated
20616bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
20716bbfe4bSMichael Große     */
208fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
209fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
210fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
211fcbe16a4SMichael Große        } else {
212114a05a7SAndreas Gohr            if(file_exists(DOKU_FARMDIR . $animal . '/conf/plugins.local.php')) {
213fcbe16a4SMichael Große                include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
214fcbe16a4SMichael Große            }
215114a05a7SAndreas Gohr        }
216fcbe16a4SMichael Große        if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
217fcbe16a4SMichael Große            unset($plugins[$plugin]);
218fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
219fcbe16a4SMichael Große        }
220fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
221fcbe16a4SMichael Große    }
222fcbe16a4SMichael Große
223a0fc814bSMichael Große    /**
22416bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be deactivated
22516bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
226a0fc814bSMichael Große     */
227fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
228fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
229fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
230fcbe16a4SMichael Große        } else {
231114a05a7SAndreas Gohr            if(file_exists(DOKU_FARMDIR . $animal . '/conf/plugins.local.php')) {
232fcbe16a4SMichael Große                include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
233fcbe16a4SMichael Große            }
234114a05a7SAndreas Gohr        }
235fcbe16a4SMichael Große        if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
236fcbe16a4SMichael Große            $plugins[$plugin] = 0;
237fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
238fcbe16a4SMichael Große        }
239fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
240fcbe16a4SMichael Große    }
241fcbe16a4SMichael Große
24216bbfe4bSMichael Große    /**
24316bbfe4bSMichael Große     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
24416bbfe4bSMichael Große     *
24516bbfe4bSMichael Große     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
24616bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
24716bbfe4bSMichael Große     */
248fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
249fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
250fcbe16a4SMichael Große        foreach($plugins as $plugin => $status) {
251fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n";
252fcbe16a4SMichael Große        }
253fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
254fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
255fcbe16a4SMichael Große    }
256bc461538SMichael Große}
257