xref: /plugin/farmer/helper.php (revision 0a5d2da2d569b70aa9be68458b77b2616f382e97)
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>
7*0a5d2da2SAndreas 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) {
11849f2871cSAndreas 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>
128bc461538SMichael Große     * @version     1.0.1
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
133bc461538SMichael Große     *
134bc461538SMichael Große     * @return      bool     Returns TRUE on success, FALSE on failure
135bc461538SMichael Große     */
136bc461538SMichael Große    function io_copyDir($source, $destination) {
137bc461538SMichael Große        if(is_link($source)) {
138bc461538SMichael Große            io_lock($destination);
139bc461538SMichael Große            $result = symlink(readlink($source), $destination);
140bc461538SMichael Große            io_unlock($destination);
141bc461538SMichael Große            return $result;
142bc461538SMichael Große        }
143bc461538SMichael Große
144bc461538SMichael Große        if(is_file($source)) {
145bc461538SMichael Große            io_lock($destination);
146bc461538SMichael Große            $result = copy($source, $destination);
147bc461538SMichael Große            io_unlock($destination);
148bc461538SMichael Große            return $result;
149bc461538SMichael Große        }
150bc461538SMichael Große
151bc461538SMichael Große        if(!is_dir($destination)) {
152bc461538SMichael Große            io_mkdir_p($destination);
153bc461538SMichael Große        }
154bc461538SMichael Große
15549f2871cSAndreas Gohr        $dir = @dir($source);
15649f2871cSAndreas Gohr        if($dir === false) return false;
157bc461538SMichael Große        while(false !== ($entry = $dir->read())) {
158bc461538SMichael Große            if($entry == '.' || $entry == '..') {
159bc461538SMichael Große                continue;
160bc461538SMichael Große            }
161bc461538SMichael Große
162bc461538SMichael Große            // recurse into directories
163bc461538SMichael Große            $this->io_copyDir("$source/$entry", "$destination/$entry");
164bc461538SMichael Große        }
165bc461538SMichael Große
166bc461538SMichael Große        $dir->close();
167bc461538SMichael Große        return true;
168bc461538SMichael Große    }
169bc461538SMichael Große
17016bbfe4bSMichael Große    /**
17116bbfe4bSMichael Große     * get a list of all Plugins installed in the farmer wiki, regardless whether they are active or not.
17216bbfe4bSMichael Große     *
17316bbfe4bSMichael Große     * @return array
17416bbfe4bSMichael Große     */
1750b96e6d7SMichael Große    public function getAllPlugins() {
1760b96e6d7SMichael Große        $dir = dir(DOKU_PLUGIN);
1770b96e6d7SMichael Große        $plugins = array();
1780b96e6d7SMichael Große        while(false !== ($entry = $dir->read())) {
1793949d8a1SMichael Große            if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') {
1800b96e6d7SMichael Große                continue;
1810b96e6d7SMichael Große            }
1820b96e6d7SMichael Große            if(!is_dir(DOKU_PLUGIN . "/$entry")) {
1830b96e6d7SMichael Große                continue;
1840b96e6d7SMichael Große            }
1850b96e6d7SMichael Große            $plugins[] = $entry;
1860b96e6d7SMichael Große        }
1876ec1ad8fSMichael Große        sort($plugins);
1880b96e6d7SMichael Große        return $plugins;
1890b96e6d7SMichael Große    }
1900b96e6d7SMichael Große
19116bbfe4bSMichael Große    /**
19216bbfe4bSMichael Große     * Actiate a specific plugin in a specific animal
19316bbfe4bSMichael Große     *
19416bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be activated
19516bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
19616bbfe4bSMichael Große     */
197fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
198fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
199fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
200fcbe16a4SMichael Große        } else {
201fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
202fcbe16a4SMichael Große        }
203fcbe16a4SMichael Große        if(isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
204fcbe16a4SMichael Große            unset($plugins[$plugin]);
205fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
206fcbe16a4SMichael Große        }
207fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
208fcbe16a4SMichael Große    }
209fcbe16a4SMichael Große
210a0fc814bSMichael Große    /**
21116bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be deactivated
21216bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
213a0fc814bSMichael Große     */
214fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
215fcbe16a4SMichael Große        if(isset($this->allPlugins[$animal])) {
216fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
217fcbe16a4SMichael Große        } else {
218fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
219fcbe16a4SMichael Große        }
220fcbe16a4SMichael Große        if(!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
221fcbe16a4SMichael Große            $plugins[$plugin] = 0;
222fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
223fcbe16a4SMichael Große        }
224fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
225fcbe16a4SMichael Große    }
226fcbe16a4SMichael Große
22716bbfe4bSMichael Große    /**
22816bbfe4bSMichael Große     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
22916bbfe4bSMichael Große     *
23016bbfe4bSMichael Große     * @param array $plugins associative array with the key being the plugin name and the value 0 or 1
23116bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
23216bbfe4bSMichael Große     */
233fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
234fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
235fcbe16a4SMichael Große        foreach($plugins as $plugin => $status) {
236fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin . '"] = ' . $status . ";\n";
237fcbe16a4SMichael Große        }
238fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
239fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
240fcbe16a4SMichael Große    }
241fcbe16a4SMichael Große
24216bbfe4bSMichael Große    /**
24316bbfe4bSMichael Große     * Show a message for all errors which occured during form validation
24416bbfe4bSMichael Große     *
24516bbfe4bSMichael Große     * @param \dokuwiki\Form\Form $form The form to which the errors should be added.
24616bbfe4bSMichael Große     * @param array $errorArray An associative array with the key being the name of the element at fault
24716bbfe4bSMichael Große     *                                         and the value being the associated error message.
24816bbfe4bSMichael Große     */
249a12b96c0SMichael Große    public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) {
250628ea26fSMichael Große        foreach($errorArray as $elementName => $errorMessage) {
251628ea26fSMichael Große            $offset = 0;
252628ea26fSMichael Große            msg($errorMessage, -1);
253628ea26fSMichael Große            while($form->findPositionByAttribute('name', $elementName, $offset)) {
254628ea26fSMichael Große                $offset = $form->findPositionByAttribute('name', $elementName, $offset);
255628ea26fSMichael Große                $form->getElementAt($offset)->addClass('error');
256628ea26fSMichael Große                ++$offset;
257a12b96c0SMichael Große            }
258a12b96c0SMichael Große        }
259a12b96c0SMichael Große    }
260a12b96c0SMichael Große
26116bbfe4bSMichael Große    /**
26216bbfe4bSMichael Große     * @param string|null $page load adminpage $page, reload the current page if $page is ommited or null
26316bbfe4bSMichael Große     */
2644d120480SMichael Große    public function reloadAdminPage($page = null) {
2654d120480SMichael Große        global $ID;
2664d120480SMichael Große        $get = $_GET;
2674d120480SMichael Große        if(isset($get['id'])) unset($get['id']);
2684d120480SMichael Große        if($page !== null) {
2694d120480SMichael Große            $get['page'] = $page;
2704d120480SMichael Große        }
2714d120480SMichael Große        $self = wl($ID, $get, false, '&');
2724d120480SMichael Große        send_redirect($self);
2734d120480SMichael Große    }
2744d120480SMichael Große
27516bbfe4bSMichael Große    /**
27616bbfe4bSMichael Große     * Download and extract the animal template
27716bbfe4bSMichael Große     *
27816bbfe4bSMichael Große     * @param string $animalpath
27916bbfe4bSMichael Große     *
28016bbfe4bSMichael Große     * @throws \splitbrain\PHPArchive\ArchiveIOException
28116bbfe4bSMichael Große     */
28279435a6fSMichael Große    public function downloadTemplate($animalpath) {
28379435a6fSMichael Große        file_put_contents($animalpath . '/_animal.zip', fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip', 'r'));
284c9fd7b89SMichael Große        $zip = new splitbrain\PHPArchive\Zip();
28579435a6fSMichael Große        $zip->open($animalpath . '/_animal.zip');
286c9fd7b89SMichael Große        $zip->extract($animalpath);
28779435a6fSMichael Große        $zip->close();
28879435a6fSMichael Große        unlink($animalpath . '/_animal.zip');
28979435a6fSMichael Große    }
29079435a6fSMichael Große
29180ef674eSMichael Große    /**
29280ef674eSMichael Große     * The subdomain must contain at least two dots
29380ef674eSMichael Große     *
29480ef674eSMichael Große     * @link http://stackoverflow.com/questions/17986371/regular-expression-to-validate-fqdn-in-c-sharp-and-javascript
29580ef674eSMichael Große     *
29680ef674eSMichael Große     * @param string $subdomain
29780ef674eSMichael Große     *
29880ef674eSMichael Große     * @return bool
29980ef674eSMichael Große     */
30080ef674eSMichael Große    public function validateSubdomain($subdomain) {
30180ef674eSMichael 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;
30280ef674eSMichael Große    }
30380ef674eSMichael Große
304bc461538SMichael Große}
305