xref: /plugin/farmer/helper.php (revision 801ebaa1b6370a05d63b66cbbb24818101c96361)
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     *
95b96c66ccSAndreas Gohr     * @param string $path
96b96c66ccSAndreas Gohr     * @param string $container
97b96c66ccSAndreas Gohr     * @return bool
98b96c66ccSAndreas Gohr     */
99b96c66ccSAndreas Gohr    public function isInPath($path, $container) {
100b96c66ccSAndreas Gohr        return (strpos(fullpath($path), fullpath($container)) === 0);
101b96c66ccSAndreas Gohr    }
102b96c66ccSAndreas Gohr
103b96c66ccSAndreas Gohr    /**
104b96c66ccSAndreas Gohr     * Check if the farm is correctly configured for this farmer plugin
105b96c66ccSAndreas Gohr     *
106b96c66ccSAndreas Gohr     * @return bool
107b96c66ccSAndreas Gohr     */
108b96c66ccSAndreas Gohr    public function checkFarmSetup() {
109b96c66ccSAndreas Gohr        return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']);
110b96c66ccSAndreas Gohr    }
111b96c66ccSAndreas Gohr
11249f2871cSAndreas Gohr    /**
11349f2871cSAndreas Gohr     * @param string $animalname
11449f2871cSAndreas Gohr     *
11549f2871cSAndreas Gohr     * @return bool
11649f2871cSAndreas Gohr     */
11749f2871cSAndreas Gohr    public function validateAnimalName($animalname) {
11878c63d53SAndreas Gohr        return preg_match("/^[a-z0-9]+([\\.\\-][a-z0-9]+)*$/i", $animalname) === 1;
11949f2871cSAndreas Gohr    }
120b96c66ccSAndreas Gohr
121b96c66ccSAndreas Gohr    /**
122bc461538SMichael Große     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
123bc461538SMichael Große     *
124bc461538SMichael Große     * @todo: needs tests
125bc461538SMichael Große     *
126bc461538SMichael Große     * @author      Aidan Lister <aidan@php.net>
127bc461538SMichael Große     * @author      Michael Große <grosse@cosmocode.de>
128*801ebaa1SAndreas Gohr     * @author      Andreas Gohr <gohr@cosmocode.de>
129bc461538SMichael Große     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
130bc461538SMichael Große     *
131bc461538SMichael Große     * @param string $source Source path
132bc461538SMichael Große     * @param string $destination Destination path
133*801ebaa1SAndreas Gohr     * @param string $exclude Regular expression to exclude files or directories (complete with delimiters)
134bc461538SMichael Große     * @return bool Returns TRUE on success, FALSE on failure
135bc461538SMichael Große     */
136*801ebaa1SAndreas Gohr    function io_copyDir($source, $destination, $exclude = '') {
137*801ebaa1SAndreas Gohr        if($exclude && preg_match($exclude, $source)) {
138*801ebaa1SAndreas Gohr            return true;
139*801ebaa1SAndreas Gohr        }
140*801ebaa1SAndreas Gohr
141bc461538SMichael Große        if(is_link($source)) {
142bc461538SMichael Große            io_lock($destination);
143bc461538SMichael Große            $result = symlink(readlink($source), $destination);
144bc461538SMichael Große            io_unlock($destination);
145bc461538SMichael Große            return $result;
146bc461538SMichael Große        }
147bc461538SMichael Große
148bc461538SMichael Große        if(is_file($source)) {
149bc461538SMichael Große            io_lock($destination);
150bc461538SMichael Große            $result = copy($source, $destination);
151bc461538SMichael Große            io_unlock($destination);
152bc461538SMichael Große            return $result;
153bc461538SMichael Große        }
154bc461538SMichael Große
155bc461538SMichael Große        if(!is_dir($destination)) {
156bc461538SMichael Große            io_mkdir_p($destination);
157bc461538SMichael Große        }
158bc461538SMichael Große
15949f2871cSAndreas Gohr        $dir = @dir($source);
16049f2871cSAndreas Gohr        if($dir === false) return false;
161bc461538SMichael Große        while(false !== ($entry = $dir->read())) {
162bc461538SMichael Große            if($entry == '.' || $entry == '..') {
163bc461538SMichael Große                continue;
164bc461538SMichael Große            }
165bc461538SMichael Große
166bc461538SMichael Große            // recurse into directories
167*801ebaa1SAndreas Gohr            $this->io_copyDir("$source/$entry", "$destination/$entry", $exclude);
168bc461538SMichael Große        }
169bc461538SMichael Große
170bc461538SMichael Große        $dir->close();
171bc461538SMichael Große        return true;
172bc461538SMichael Große    }
173bc461538SMichael Große
17416bbfe4bSMichael Große    /**
17516bbfe4bSMichael Große     * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not.
17616bbfe4bSMichael Große     *
17716bbfe4bSMichael Große     * @return array
17816bbfe4bSMichael Große     */
1790b96e6d7SMichael Große    public function getAllPlugins() {
1800b96e6d7SMichael Große        $dir = dir(DOKU_PLUGIN);
1810b96e6d7SMichael Große        $plugins = array();
1820b96e6d7SMichael Große        while(false !== ($entry = $dir->read())) {
1833949d8a1SMichael Große            if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') {
1840b96e6d7SMichael Große                continue;
1850b96e6d7SMichael Große            }
1860b96e6d7SMichael Große            if(!is_dir(DOKU_PLUGIN . "/$entry")) {
1870b96e6d7SMichael Große                continue;
1880b96e6d7SMichael Große            }
1890b96e6d7SMichael Große            $plugins[] = $entry;
1900b96e6d7SMichael Große        }
1916ec1ad8fSMichael Große        sort($plugins);
1920b96e6d7SMichael Große        return $plugins;
1930b96e6d7SMichael Große    }
1940b96e6d7SMichael Große
19516bbfe4bSMichael Große    /**
19616bbfe4bSMichael Große     * Actiate a specific plugin in a specific animal
19716bbfe4bSMichael Große     *
19816bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be activated
19916bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
20016bbfe4bSMichael Große     */
201fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
202fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
203fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
204fcbe16a4SMichael Große        } else {
205fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
206fcbe16a4SMichael Große        }
207fcbe16a4SMichael Große        if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
208fcbe16a4SMichael Große            unset($plugins[$plugin]);
209fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
210fcbe16a4SMichael Große        }
211fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
212fcbe16a4SMichael Große    }
213fcbe16a4SMichael Große
214a0fc814bSMichael Große    /**
21516bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be deactivated
21616bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
217a0fc814bSMichael Große     */
218fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
219fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
220fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
221fcbe16a4SMichael Große        } else {
222fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
223fcbe16a4SMichael Große        }
224fcbe16a4SMichael Große        if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
225fcbe16a4SMichael Große            $plugins[$plugin] = 0;
226fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
227fcbe16a4SMichael Große        }
228fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
229fcbe16a4SMichael Große    }
230fcbe16a4SMichael Große
23116bbfe4bSMichael Große    /**
23216bbfe4bSMichael Große     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
23316bbfe4bSMichael Große     *
23416bbfe4bSMichael Große     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
23516bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
23616bbfe4bSMichael Große     */
237fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
238fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
239fcbe16a4SMichael Große        foreach($plugins as $plugin => $status) {
240fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n";
241fcbe16a4SMichael Große        }
242fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
243fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
244fcbe16a4SMichael Große    }
245bc461538SMichael Große}
246