xref: /plugin/farmer/helper.php (revision 4664a1d2ded1a7e5f540750968d7937fbb70f173)
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        $dir = dir(DOKU_PLUGIN);
186        $plugins = array();
187        while(false !== ($entry = $dir->read())) {
188            if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') {
189                continue;
190            }
191            if(!is_dir(DOKU_PLUGIN . "/$entry")) {
192                continue;
193            }
194            $plugins[] = $entry;
195        }
196        sort($plugins);
197        return $plugins;
198    }
199
200    /**
201     * Actiate a specific plugin in a specific animal
202     *
203     * @param string $plugin Name of the plugin to be activated
204     * @param string $animal Directory of the animal within DOKU_FARMDIR
205     */
206    public function activatePlugin($plugin, $animal) {
207        if(isset($this->allPlugins[$animal])) {
208            $plugins = $this->allPlugins[$animal];
209        } else {
210            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
211        }
212        if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
213            unset($plugins[$plugin]);
214            $this->writePluginConf($plugins, $animal);
215        }
216        $this->allPlugins[$animal] = $plugins;
217    }
218
219    /**
220     * @param string $plugin Name of the plugin to be deactivated
221     * @param string $animal Directory of the animal within DOKU_FARMDIR
222     */
223    public function deactivatePlugin($plugin, $animal) {
224        if(isset($this->allPlugins[$animal])) {
225            $plugins = $this->allPlugins[$animal];
226        } else {
227            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
228        }
229        if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
230            $plugins[$plugin] = 0;
231            $this->writePluginConf($plugins, $animal);
232        }
233        $this->allPlugins[$animal] = $plugins;
234    }
235
236    /**
237     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
238     *
239     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
240     * @param string $animal Directory of the animal within DOKU_FARMDIR
241     */
242    public function writePluginConf($plugins, $animal) {
243        $pluginConf = '<?php' . "\n";
244        foreach($plugins as $plugin => $status) {
245            $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n";
246        }
247        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
248        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
249    }
250}
251