xref: /plugin/farmer/helper.php (revision 8262a4cbfa75b904634931087aeac13ac2fa2168)
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    private $allPlugins = array();
16
17    /**
18     * Returns the name of the current animal if any, false otherwise
19     *
20     * @return string|false
21     */
22    public function getAnimal() {
23        if(!isset($GLOBALS['FARMCORE'])) return false;
24        return $GLOBALS['FARMCORE']->getAnimal();
25    }
26
27    /**
28     * Get the farm config
29     *
30     * @return array
31     */
32    public function getConfig() {
33        if(!isset($GLOBALS['FARMCORE'])) return array();
34        return $GLOBALS['FARMCORE']->getConfig();
35    }
36
37    /**
38     * Was the current animal requested by host?
39     *
40     * @return bool
41     */
42    public function isHostbased() {
43        if(!isset($GLOBALS['FARMCORE'])) return false;
44        return $GLOBALS['FARMCORE']->isHostbased();
45    }
46
47    /**
48     * Was an animal requested that could not be found?
49     *
50     * @return bool
51     */
52    public function wasNotfound() {
53        if(!isset($GLOBALS['FARMCORE'])) return false;
54        return $GLOBALS['FARMCORE']->wasNotfound();
55    }
56
57    /**
58     * Guess the URL for an animal
59     *
60     * @param $animal
61     * @return string
62     */
63    public function getAnimalURL($animal) {
64        $config = $this->getConfig();
65
66        if(strpos($animal, '.') !== false) {
67            return 'http://' . $animal;
68        } elseif($config['base']['basedomain']) {
69            return 'http://' . $animal . '.' . $config['base']['basedomain'];
70        } else {
71            return DOKU_URL . '!' . $animal . '/';
72        }
73    }
74
75    /**
76     * List of all animals, i.e. directories within DOKU_FARMDIR without the template.
77     *
78     * @return array
79     */
80    public function getAllAnimals() {
81        $animals = array();
82        $list = glob(DOKU_FARMDIR . '*/conf/', GLOB_ONLYDIR);
83        foreach($list as $path) {
84            $animal = basename(dirname($path));
85            if($animal == '_animal') continue; // old template
86            $animals[] = $animal;
87        }
88        sort($animals);
89        return $animals;
90    }
91
92    /**
93     * checks wether $path is in under $container
94     *
95     * Also returns false if $path and $container are the same directory
96     *
97     * @param string $path
98     * @param string $container
99     * @return bool
100     */
101    public function isInPath($path, $container) {
102        $path = fullpath($path);
103        $container = fullpath($container);
104        if($path == $container) return false;
105        return (strpos($path, $container) === 0);
106    }
107
108    /**
109     * Check if the farm is correctly configured for this farmer plugin
110     *
111     * @return bool
112     */
113    public function checkFarmSetup() {
114        return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']);
115    }
116
117    /**
118     * @param string $animalname
119     *
120     * @return bool
121     */
122    public function validateAnimalName($animalname) {
123        return preg_match("/^[a-z0-9]+([\\.\\-][a-z0-9]+)*$/i", $animalname) === 1;
124    }
125
126    /**
127     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
128     *
129     * @todo: needs tests
130     *
131     * @author      Aidan Lister <aidan@php.net>
132     * @author      Michael Große <grosse@cosmocode.de>
133     * @author      Andreas Gohr <gohr@cosmocode.de>
134     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
135     *
136     * @param string $source Source path
137     * @param string $destination Destination path
138     * @param string $exclude Regular expression to exclude files or directories (complete with delimiters)
139     * @return bool Returns TRUE on success, FALSE on failure
140     */
141    function io_copyDir($source, $destination, $exclude = '') {
142        if($exclude && preg_match($exclude, $source)) {
143            return true;
144        }
145
146        if(is_link($source)) {
147            io_lock($destination);
148            $result = symlink(readlink($source), $destination);
149            io_unlock($destination);
150            return $result;
151        }
152
153        if(is_file($source)) {
154            io_lock($destination);
155            $result = copy($source, $destination);
156            io_unlock($destination);
157            return $result;
158        }
159
160        if(!is_dir($destination)) {
161            io_mkdir_p($destination);
162        }
163
164        $dir = @dir($source);
165        if($dir === false) return false;
166        while(false !== ($entry = $dir->read())) {
167            if($entry == '.' || $entry == '..') {
168                continue;
169            }
170
171            // recurse into directories
172            $this->io_copyDir("$source/$entry", "$destination/$entry", $exclude);
173        }
174
175        $dir->close();
176        return true;
177    }
178
179    /**
180     * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not.
181     *
182     * @return array
183     */
184    public function getAllPlugins() {
185        /** @var Doku_Plugin_Controller $plugin_controller */
186        global $plugin_controller;
187
188        $plugins = $plugin_controller->getList('', true);
189
190        // filter out a few plugins
191        $plugins = array_filter($plugins, function($item) {
192            if($item == 'farmer') return false;
193            if($item == 'extension') return false;
194            if($item == 'testing') return false;
195            return true;
196        });
197
198        sort($plugins);
199        return $plugins;
200    }
201
202    /**
203     * Activate a specific plugin in a specific animal
204     *
205     * @param string $plugin Name of the plugin to be activated
206     * @param string $animal Directory of the animal within DOKU_FARMDIR
207     */
208    public function activatePlugin($plugin, $animal) {
209        if(isset($this->allPlugins[$animal])) {
210            $plugins = $this->allPlugins[$animal];
211        } else {
212            if(file_exists(DOKU_FARMDIR . $animal . '/conf/plugins.local.php')) {
213                include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
214            }
215        }
216        if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
217            unset($plugins[$plugin]);
218            $this->writePluginConf($plugins, $animal);
219        }
220        $this->allPlugins[$animal] = $plugins;
221    }
222
223    /**
224     * @param string $plugin Name of the plugin to be deactivated
225     * @param string $animal Directory of the animal within DOKU_FARMDIR
226     */
227    public function deactivatePlugin($plugin, $animal) {
228        if(isset($this->allPlugins[$animal])) {
229            $plugins = $this->allPlugins[$animal];
230        } else {
231            if(file_exists(DOKU_FARMDIR . $animal . '/conf/plugins.local.php')) {
232                include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
233            }
234        }
235        if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
236            $plugins[$plugin] = 0;
237            $this->writePluginConf($plugins, $animal);
238        }
239        $this->allPlugins[$animal] = $plugins;
240    }
241
242    /**
243     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
244     *
245     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
246     * @param string $animal Directory of the animal within DOKU_FARMDIR
247     */
248    public function writePluginConf($plugins, $animal) {
249        $pluginConf = '<?php' . "\n";
250        foreach($plugins as $plugin => $status) {
251            $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n";
252        }
253        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
254        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
255    }
256}
257