xref: /plugin/farmer/helper.php (revision 0336ab2ad75d98ea4879adb49ccd41584bdab109)
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    /**
57c4c8e953SAndreas Gohr     * Guess the URL for an animal
58c4c8e953SAndreas Gohr     *
59c4c8e953SAndreas Gohr     * @param $animal
60c4c8e953SAndreas Gohr     * @return string
61c4c8e953SAndreas Gohr     */
62c4c8e953SAndreas Gohr    public function getAnimalURL($animal) {
63c4c8e953SAndreas Gohr        $config = $this->getConfig();
64c4c8e953SAndreas Gohr
65c4c8e953SAndreas Gohr        if(strpos($animal, '.') !== false) {
66c4c8e953SAndreas Gohr            return 'http://'.$animal;
67c4c8e953SAndreas Gohr        } elseif($config['base']['basedomain']) {
68c4c8e953SAndreas Gohr            return 'http://'.$animal.'.'.$config['base']['basedomain'];
69c4c8e953SAndreas Gohr        } else {
70*0336ab2aSAndreas Gohr            return DOKU_URL.'!'.$animal.'/';
71c4c8e953SAndreas Gohr        }
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
17049f2871cSAndreas Gohr
17149f2871cSAndreas Gohr
17249f2871cSAndreas Gohr
17349f2871cSAndreas Gohr
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
1950b96e6d7SMichael Große
19616bbfe4bSMichael Große    /**
19716bbfe4bSMichael Große     * Actiate a specific plugin in a specific animal
19816bbfe4bSMichael Große     *
19916bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be activated
20016bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
20116bbfe4bSMichael Große     */
202fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
203fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
204fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
205fcbe16a4SMichael Große        } else {
206fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
207fcbe16a4SMichael Große        }
208fcbe16a4SMichael Große        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
209fcbe16a4SMichael Große            unset($plugins[$plugin]);
210fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
211fcbe16a4SMichael Große        }
212fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
213fcbe16a4SMichael Große    }
214fcbe16a4SMichael Große
215a0fc814bSMichael Große    /**
21616bbfe4bSMichael Große     * @param string $plugin Name of the plugin to be deactivated
21716bbfe4bSMichael Große     * @param string $animal Directory of the animal within DOKU_FARMDIR
218a0fc814bSMichael Große     */
219fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
220fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
221fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
222fcbe16a4SMichael Große        } else {
223fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
224fcbe16a4SMichael Große        }
225fcbe16a4SMichael Große        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
226fcbe16a4SMichael Große            $plugins[$plugin] = 0;
227fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
228fcbe16a4SMichael Große        }
229fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
230fcbe16a4SMichael Große    }
231fcbe16a4SMichael Große
23216bbfe4bSMichael Große    /**
23316bbfe4bSMichael Große     * Write the list of (deactivated) plugins as plugin configuration of an animal to file
23416bbfe4bSMichael Große     *
23516bbfe4bSMichael Große     * @param array  $plugins associative array with the key being the plugin name and the value 0 or 1
23616bbfe4bSMichael Große     * @param string $animal  Directory of the animal within DOKU_FARMDIR
23716bbfe4bSMichael Große     */
238fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
239fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
240fcbe16a4SMichael Große        foreach ($plugins as $plugin => $status) {
241fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
242fcbe16a4SMichael Große        }
243fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
244fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
245fcbe16a4SMichael Große    }
246fcbe16a4SMichael Große
24716bbfe4bSMichael Große    /**
24816bbfe4bSMichael Große     * Show a message for all errors which occured during form validation
24916bbfe4bSMichael Große     *
25016bbfe4bSMichael Große     * @param \dokuwiki\Form\Form $form        The form to which the errors should be added.
25116bbfe4bSMichael Große     * @param array               $errorArray  An associative array with the key being the name of the element at fault
25216bbfe4bSMichael Große     *                                         and the value being the associated error message.
25316bbfe4bSMichael Große     */
254a12b96c0SMichael Große    public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) {
255628ea26fSMichael Große        foreach ($errorArray as $elementName => $errorMessage) {
256628ea26fSMichael Große            $offset = 0;
257628ea26fSMichael Große            msg($errorMessage, -1);
258628ea26fSMichael Große            while ($form->findPositionByAttribute('name',$elementName, $offset)) {
259628ea26fSMichael Große                $offset = $form->findPositionByAttribute('name',$elementName, $offset);
260628ea26fSMichael Große                $form->getElementAt($offset)->addClass('error');
261628ea26fSMichael Große                ++$offset;
262a12b96c0SMichael Große            }
263a12b96c0SMichael Große        }
264a12b96c0SMichael Große    }
265a12b96c0SMichael Große
26616bbfe4bSMichael Große    /**
26716bbfe4bSMichael Große     * @param string|null $page load adminpage $page, reload the current page if $page is ommited or null
26816bbfe4bSMichael Große     */
2694d120480SMichael Große    public function reloadAdminPage($page = null) {
2704d120480SMichael Große        global $ID;
2714d120480SMichael Große        $get = $_GET;
2724d120480SMichael Große        if(isset($get['id'])) unset($get['id']);
2734d120480SMichael Große        if ($page !== null ) {
2744d120480SMichael Große            $get['page'] = $page;
2754d120480SMichael Große        }
2764d120480SMichael Große        $self = wl($ID, $get, false, '&');
2774d120480SMichael Große        send_redirect($self);
2784d120480SMichael Große    }
2794d120480SMichael Große
28016bbfe4bSMichael Große    /**
28116bbfe4bSMichael Große     * Download and extract the animal template
28216bbfe4bSMichael Große     *
28316bbfe4bSMichael Große     * @param string $animalpath
28416bbfe4bSMichael Große     *
28516bbfe4bSMichael Große     * @throws \splitbrain\PHPArchive\ArchiveIOException
28616bbfe4bSMichael Große     */
28779435a6fSMichael Große    public function downloadTemplate($animalpath) {
28879435a6fSMichael Große        file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r'));
289c9fd7b89SMichael Große        $zip = new splitbrain\PHPArchive\Zip();
29079435a6fSMichael Große        $zip->open($animalpath.'/_animal.zip');
291c9fd7b89SMichael Große        $zip->extract($animalpath);
29279435a6fSMichael Große        $zip->close();
29379435a6fSMichael Große        unlink($animalpath.'/_animal.zip');
29479435a6fSMichael Große    }
29579435a6fSMichael Große
296d9ec4524SMichael Große
29780ef674eSMichael Große    /**
29880ef674eSMichael Große     * The subdomain must contain at least two dots
29980ef674eSMichael Große     *
30080ef674eSMichael Große     * @link http://stackoverflow.com/questions/17986371/regular-expression-to-validate-fqdn-in-c-sharp-and-javascript
30180ef674eSMichael Große     *
30280ef674eSMichael Große     * @param string $subdomain
30380ef674eSMichael Große     *
30480ef674eSMichael Große     * @return bool
30580ef674eSMichael Große     */
30680ef674eSMichael Große    public function validateSubdomain ($subdomain) {
30780ef674eSMichael 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;
30880ef674eSMichael Große    }
30980ef674eSMichael Große
310909f2ff6SMichael Große
31149f2871cSAndreas Gohr
31249f2871cSAndreas Gohr
313c66a21e9SMichael Große
314bc461538SMichael Große}
315