xref: /plugin/farmer/helper.php (revision dfdaf33e5be77ffde44c74599bccb56fea95f9ea)
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();
82b96c66ccSAndreas 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     *
95*dfdaf33eSAndreas Gohr     * Also returns false if $path and $container are the same directory
96*dfdaf33eSAndreas Gohr     *
97b96c66ccSAndreas Gohr     * @param string $path
98b96c66ccSAndreas Gohr     * @param string $container
99b96c66ccSAndreas Gohr     * @return bool
100b96c66ccSAndreas Gohr     */
101b96c66ccSAndreas Gohr    public function isInPath($path, $container) {
102*dfdaf33eSAndreas Gohr        $path = fullpath($path);
103*dfdaf33eSAndreas Gohr        $container = fullpath($container);
104*dfdaf33eSAndreas Gohr        if($path == $container) return false;
105*dfdaf33eSAndreas 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() {
1850b96e6d7SMichael Große        $dir = dir(DOKU_PLUGIN);
1860b96e6d7SMichael Große        $plugins = array();
1870b96e6d7SMichael Große        while(false !== ($entry = $dir->read())) {
1883949d8a1SMichael Große            if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') {
1890b96e6d7SMichael Große                continue;
1900b96e6d7SMichael Große            }
1910b96e6d7SMichael Große            if(!is_dir(DOKU_PLUGIN . "/$entry")) {
1920b96e6d7SMichael Große                continue;
1930b96e6d7SMichael Große            }
1940b96e6d7SMichael Große            $plugins[] = $entry;
1950b96e6d7SMichael Große        }
1966ec1ad8fSMichael Große        sort($plugins);
1970b96e6d7SMichael Große        return $plugins;
1980b96e6d7SMichael Große    }
1990b96e6d7SMichael Große
20016bbfe4bSMichael Große    /**
20116bbfe4bSMichael Große     * Actiate a specific plugin in a specific animal
20216bbfe4bSMichael Große     *
20316bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be activated
20416bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
20516bbfe4bSMichael Große     */
206fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
207fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
208fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
209fcbe16a4SMichael Große        } else {
210fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
211fcbe16a4SMichael Große        }
212fcbe16a4SMichael Große        if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
213fcbe16a4SMichael Große            unset($plugins[$plugin]);
214fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
215fcbe16a4SMichael Große        }
216fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
217fcbe16a4SMichael Große    }
218fcbe16a4SMichael Große
219a0fc814bSMichael Große    /**
22016bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be deactivated
22116bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
222a0fc814bSMichael Große     */
223fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
224fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
225fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
226fcbe16a4SMichael Große        } else {
227fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
228fcbe16a4SMichael Große        }
229fcbe16a4SMichael Große        if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
230fcbe16a4SMichael Große            $plugins[$plugin] = 0;
231fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
232fcbe16a4SMichael Große        }
233fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
234fcbe16a4SMichael Große    }
235fcbe16a4SMichael Große
23616bbfe4bSMichael Große    /**
23716bbfe4bSMichael Große     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
23816bbfe4bSMichael Große     *
23916bbfe4bSMichael Große     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
24016bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
24116bbfe4bSMichael Große     */
242fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
243fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
244fcbe16a4SMichael Große        foreach($plugins as $plugin => $status) {
245fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n";
246fcbe16a4SMichael Große        }
247fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
248fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
249fcbe16a4SMichael Große    }
250bc461538SMichael Große}
251