xref: /plugin/farmer/helper.php (revision 49f2871cf739ba9d87eb5b5878cdd7066d4018b8)
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>
7bc461538SMichael Große */
8bc461538SMichael Große
9bc461538SMichael Große// must be run within Dokuwiki
10bc461538SMichael Großeif(!defined('DOKU_INC')) die();
11bc461538SMichael Große
12bc461538SMichael Großeclass helper_plugin_farmer extends DokuWiki_Plugin {
13bc461538SMichael Große
14fcbe16a4SMichael Große    private $allPlugins = array();
15fcbe16a4SMichael Große
16bc461538SMichael Große    /**
17632c5618SAndreas Gohr     * Returns the name of the current animal if any, false otherwise
18632c5618SAndreas Gohr     *
19632c5618SAndreas Gohr     * @return string|false
20632c5618SAndreas Gohr     */
21632c5618SAndreas Gohr    public function getAnimal() {
22b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return false;
23b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->getAnimal();
24632c5618SAndreas Gohr    }
25632c5618SAndreas Gohr
26632c5618SAndreas Gohr    /**
27b96c66ccSAndreas Gohr     * Get the farm config
28b96c66ccSAndreas Gohr     *
29b96c66ccSAndreas Gohr     * @return array
30b96c66ccSAndreas Gohr     */
31b96c66ccSAndreas Gohr    public function getConfig() {
32b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return array();
33b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->getConfig();
34b96c66ccSAndreas Gohr    }
35b96c66ccSAndreas Gohr
36b96c66ccSAndreas Gohr    /**
37b96c66ccSAndreas Gohr     * Was the current animal requested by host?
38b96c66ccSAndreas Gohr     *
39b96c66ccSAndreas Gohr     * @return bool
40b96c66ccSAndreas Gohr     */
41b96c66ccSAndreas Gohr    public function isHostbased() {
42b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return false;
43b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->isHostbased();
44b96c66ccSAndreas Gohr    }
45b96c66ccSAndreas Gohr
46b96c66ccSAndreas Gohr    /**
47b96c66ccSAndreas Gohr     * Was an animal requested that could not be found?
48b96c66ccSAndreas Gohr     *
49b96c66ccSAndreas Gohr     * @return bool
50b96c66ccSAndreas Gohr     */
51b96c66ccSAndreas Gohr    public function wasNotfound() {
52b96c66ccSAndreas Gohr        if(!isset($GLOBALS['FARMCORE'])) return false;
53b96c66ccSAndreas Gohr        return $GLOBALS['FARMCORE']->wasNotfound();
54b96c66ccSAndreas Gohr    }
55b96c66ccSAndreas Gohr
56b96c66ccSAndreas Gohr    /**
57b96c66ccSAndreas Gohr     * List of all animals, i.e. directories within DOKU_FARMDIR without the template.
58b96c66ccSAndreas Gohr     *
59b96c66ccSAndreas Gohr     * @return array
60b96c66ccSAndreas Gohr     */
61b96c66ccSAndreas Gohr    public function getAllAnimals() {
62b96c66ccSAndreas Gohr        $animals = array();
63b96c66ccSAndreas Gohr        $list = glob(DOKU_FARMDIR . '/*/conf/', GLOB_ONLYDIR);
64b96c66ccSAndreas Gohr        foreach($list as $path) {
65b96c66ccSAndreas Gohr            $animal = basename(dirname($path));
66b96c66ccSAndreas Gohr            if($animal == '_animal') continue; // old template
67b96c66ccSAndreas Gohr            $animals[] = $animal;
68b96c66ccSAndreas Gohr        }
69b96c66ccSAndreas Gohr        sort($animals);
70b96c66ccSAndreas Gohr        return $animals;
71b96c66ccSAndreas Gohr    }
72b96c66ccSAndreas Gohr
73b96c66ccSAndreas Gohr    /**
74b96c66ccSAndreas Gohr     * checks wether $path is in under $container
75b96c66ccSAndreas Gohr     *
76b96c66ccSAndreas Gohr     * @param string $path
77b96c66ccSAndreas Gohr     * @param string $container
78b96c66ccSAndreas Gohr     * @return bool
79b96c66ccSAndreas Gohr     */
80b96c66ccSAndreas Gohr    public function isInPath ($path, $container) {
81b96c66ccSAndreas Gohr        return (strpos(fullpath($path), fullpath($container)) === 0);
82b96c66ccSAndreas Gohr    }
83b96c66ccSAndreas Gohr
84b96c66ccSAndreas Gohr    /**
85b96c66ccSAndreas Gohr     * Check if the farm is correctly configured for this farmer plugin
86b96c66ccSAndreas Gohr     *
87b96c66ccSAndreas Gohr     * @return bool
88b96c66ccSAndreas Gohr     */
89b96c66ccSAndreas Gohr    public function checkFarmSetup () {
90b96c66ccSAndreas Gohr        return defined('DOKU_FARMDIR') && isset($GLOBALS['FARMCORE']);
91b96c66ccSAndreas Gohr    }
92b96c66ccSAndreas Gohr
93*49f2871cSAndreas Gohr    /**
94*49f2871cSAndreas Gohr     * @param string $animalname
95*49f2871cSAndreas Gohr     *
96*49f2871cSAndreas Gohr     * @return bool
97*49f2871cSAndreas Gohr     */
98*49f2871cSAndreas Gohr    public function validateAnimalName ($animalname) {
99*49f2871cSAndreas Gohr        return preg_match("/^[a-z0-9]+(\\.-[a-z0-9]+)*$/i",$animalname) === 1;
100*49f2871cSAndreas Gohr    }
101b96c66ccSAndreas Gohr
102b96c66ccSAndreas Gohr    /**
103bc461538SMichael Große     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
104bc461538SMichael Große     *
105bc461538SMichael Große     * @todo: needs tests
106bc461538SMichael Große     *
107bc461538SMichael Große     * @author      Aidan Lister <aidan@php.net>
108bc461538SMichael Große     * @author      Michael Große <grosse@cosmocode.de>
109bc461538SMichael Große     * @version     1.0.1
110bc461538SMichael Große     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
111bc461538SMichael Große     *
112bc461538SMichael Große     * @param       string $source       Source path
113bc461538SMichael Große     * @param       string $destination  Destination path
114bc461538SMichael Große     *
115bc461538SMichael Große     * @return      bool     Returns TRUE on success, FALSE on failure
116bc461538SMichael Große     */
117bc461538SMichael Große    function io_copyDir($source, $destination) {
118bc461538SMichael Große        if (is_link($source)) {
119bc461538SMichael Große            io_lock($destination);
120bc461538SMichael Große            $result=symlink(readlink($source), $destination);
121bc461538SMichael Große            io_unlock($destination);
122bc461538SMichael Große            return $result;
123bc461538SMichael Große        }
124bc461538SMichael Große
125bc461538SMichael Große        if (is_file($source)) {
126bc461538SMichael Große            io_lock($destination);
127bc461538SMichael Große            $result=copy($source, $destination);
128bc461538SMichael Große            io_unlock($destination);
129bc461538SMichael Große            return $result;
130bc461538SMichael Große        }
131bc461538SMichael Große
132bc461538SMichael Große        if (!is_dir($destination)) {
133bc461538SMichael Große            io_mkdir_p($destination);
134bc461538SMichael Große        }
135bc461538SMichael Große
136*49f2871cSAndreas Gohr        $dir = @dir($source);
137*49f2871cSAndreas Gohr        if($dir === false) return false;
138bc461538SMichael Große        while (false !== ($entry = $dir->read())) {
139bc461538SMichael Große            if ($entry == '.' || $entry == '..') {
140bc461538SMichael Große                continue;
141bc461538SMichael Große            }
142bc461538SMichael Große
143bc461538SMichael Große            // recurse into directories
144bc461538SMichael Große            $this->io_copyDir("$source/$entry", "$destination/$entry");
145bc461538SMichael Große        }
146bc461538SMichael Große
147bc461538SMichael Große        $dir->close();
148bc461538SMichael Große        return true;
149bc461538SMichael Große    }
150bc461538SMichael Große
151*49f2871cSAndreas Gohr
152*49f2871cSAndreas Gohr
153*49f2871cSAndreas Gohr
154*49f2871cSAndreas Gohr
15516bbfe4bSMichael Große    /**
15616bbfe4bSMichael Große     * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not.
15716bbfe4bSMichael Große     *
15816bbfe4bSMichael Große     * @return array
15916bbfe4bSMichael Große     */
1600b96e6d7SMichael Große    public function getAllPlugins() {
1610b96e6d7SMichael Große        $dir = dir(DOKU_PLUGIN);
1620b96e6d7SMichael Große        $plugins = array();
1630b96e6d7SMichael Große        while (false !== ($entry = $dir->read())) {
1643949d8a1SMichael Große            if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') {
1650b96e6d7SMichael Große                continue;
1660b96e6d7SMichael Große            }
1670b96e6d7SMichael Große            if (!is_dir(DOKU_PLUGIN ."/$entry")) {
1680b96e6d7SMichael Große                continue;
1690b96e6d7SMichael Große            }
1700b96e6d7SMichael Große            $plugins[] = $entry;
1710b96e6d7SMichael Große        }
1726ec1ad8fSMichael Große        sort($plugins);
1730b96e6d7SMichael Große        return $plugins;
1740b96e6d7SMichael Große    }
1750b96e6d7SMichael Große
1760b96e6d7SMichael Große
17716bbfe4bSMichael Große    /**
17816bbfe4bSMichael Große     * Actiate a specific plugin in a specific animal
17916bbfe4bSMichael Große     *
18016bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be activated
18116bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
18216bbfe4bSMichael Große     */
183fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
184fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
185fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
186fcbe16a4SMichael Große        } else {
187fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
188fcbe16a4SMichael Große        }
189fcbe16a4SMichael Große        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
190fcbe16a4SMichael Große            unset($plugins[$plugin]);
191fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
192fcbe16a4SMichael Große        }
193fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
194fcbe16a4SMichael Große    }
195fcbe16a4SMichael Große
196a0fc814bSMichael Große    /**
19716bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be deactivated
19816bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
199a0fc814bSMichael Große     */
200fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
201fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
202fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
203fcbe16a4SMichael Große        } else {
204fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
205fcbe16a4SMichael Große        }
206fcbe16a4SMichael Große        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
207fcbe16a4SMichael Große            $plugins[$plugin] = 0;
208fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
209fcbe16a4SMichael Große        }
210fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
211fcbe16a4SMichael Große    }
212fcbe16a4SMichael Große
21316bbfe4bSMichael Große    /**
21416bbfe4bSMichael Große     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
21516bbfe4bSMichael Große     *
21616bbfe4bSMichael Große     * @param array  $plugins associative array with the key being the plugin name and the value 0 or 1
21716bbfe4bSMichael Große     * @param string $animal  Directory of the animal within DOKU_FARMDIR
21816bbfe4bSMichael Große     */
219fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
220fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
221fcbe16a4SMichael Große        foreach ($plugins as $plugin => $status) {
222fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
223fcbe16a4SMichael Große        }
224fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
225fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
226fcbe16a4SMichael Große    }
227fcbe16a4SMichael Große
22816bbfe4bSMichael Große    /**
22916bbfe4bSMichael Große     * Show a message for all errors which occured during form validation
23016bbfe4bSMichael Große     *
23116bbfe4bSMichael Große     * @param \dokuwiki\Form\Form $form        The form to which the errors should be added.
23216bbfe4bSMichael Große     * @param array               $errorArray  An associative array with the key being the name of the element at fault
23316bbfe4bSMichael Große     *                                         and the value being the associated error message.
23416bbfe4bSMichael Große     */
235a12b96c0SMichael Große    public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) {
236628ea26fSMichael Große        foreach ($errorArray as $elementName => $errorMessage) {
237628ea26fSMichael Große            $offset = 0;
238628ea26fSMichael Große            msg($errorMessage, -1);
239628ea26fSMichael Große            while ($form->findPositionByAttribute('name',$elementName, $offset)) {
240628ea26fSMichael Große                $offset = $form->findPositionByAttribute('name',$elementName, $offset);
241628ea26fSMichael Große                $form->getElementAt($offset)->addClass('error');
242628ea26fSMichael Große                ++$offset;
243a12b96c0SMichael Große            }
244a12b96c0SMichael Große        }
245a12b96c0SMichael Große    }
246a12b96c0SMichael Große
24716bbfe4bSMichael Große    /**
24816bbfe4bSMichael Große     * @param string|null $page load adminpage $page, reload the current page if $page is ommited or null
24916bbfe4bSMichael Große     */
2504d120480SMichael Große    public function reloadAdminPage($page = null) {
2514d120480SMichael Große        global $ID;
2524d120480SMichael Große        $get = $_GET;
2534d120480SMichael Große        if(isset($get['id'])) unset($get['id']);
2544d120480SMichael Große        if ($page !== null ) {
2554d120480SMichael Große            $get['page'] = $page;
2564d120480SMichael Große        }
2574d120480SMichael Große        $self = wl($ID, $get, false, '&');
2584d120480SMichael Große        send_redirect($self);
2594d120480SMichael Große    }
2604d120480SMichael Große
26116bbfe4bSMichael Große    /**
26216bbfe4bSMichael Große     * Download and extract the animal template
26316bbfe4bSMichael Große     *
26416bbfe4bSMichael Große     * @param string $animalpath
26516bbfe4bSMichael Große     *
26616bbfe4bSMichael Große     * @throws \splitbrain\PHPArchive\ArchiveIOException
26716bbfe4bSMichael Große     */
26879435a6fSMichael Große    public function downloadTemplate($animalpath) {
26979435a6fSMichael Große        file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r'));
270c9fd7b89SMichael Große        $zip = new splitbrain\PHPArchive\Zip();
27179435a6fSMichael Große        $zip->open($animalpath.'/_animal.zip');
272c9fd7b89SMichael Große        $zip->extract($animalpath);
27379435a6fSMichael Große        $zip->close();
27479435a6fSMichael Große        unlink($animalpath.'/_animal.zip');
27579435a6fSMichael Große    }
27679435a6fSMichael Große
277d9ec4524SMichael Große
27880ef674eSMichael Große    /**
27980ef674eSMichael Große     * The subdomain must contain at least two dots
28080ef674eSMichael Große     *
28180ef674eSMichael Große     * @link http://stackoverflow.com/questions/17986371/regular-expression-to-validate-fqdn-in-c-sharp-and-javascript
28280ef674eSMichael Große     *
28380ef674eSMichael Große     * @param string $subdomain
28480ef674eSMichael Große     *
28580ef674eSMichael Große     * @return bool
28680ef674eSMichael Große     */
28780ef674eSMichael Große    public function validateSubdomain ($subdomain) {
28880ef674eSMichael Große        return preg_match("/^(?=.{1,254}$)((?=[a-z0-9-]{1,63}\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\.){2,}[a-z]{2,63}$/i",$subdomain) === 1;
28980ef674eSMichael Große    }
29080ef674eSMichael Große
291909f2ff6SMichael Große
292*49f2871cSAndreas Gohr
293*49f2871cSAndreas Gohr
294c66a21e9SMichael Große
295bc461538SMichael Große}
296