xref: /plugin/farmer/helper.php (revision c9fd7b89f490dfbeacd17424b69eb9d5e9333e88)
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    /**
17bc461538SMichael Große     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
18bc461538SMichael Große     *
19bc461538SMichael Große     * @todo: needs tests
20bc461538SMichael Große     *
21bc461538SMichael Große     * @author      Aidan Lister <aidan@php.net>
22bc461538SMichael Große     * @author      Michael Große <grosse@cosmocode.de>
23bc461538SMichael Große     * @version     1.0.1
24bc461538SMichael Große     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
25bc461538SMichael Große     *
26bc461538SMichael Große     * @param       string $source Source path
27bc461538SMichael Große     * @param       string $destination      Destination path
28bc461538SMichael Große     *
29bc461538SMichael Große     * @return      bool     Returns TRUE on success, FALSE on failure
30bc461538SMichael Große     */
31bc461538SMichael Große    function io_copyDir($source, $destination) {
32bc461538SMichael Große        if (is_link($source)) {
33bc461538SMichael Große            io_lock($destination);
34bc461538SMichael Große            $result=symlink(readlink($source), $destination);
35bc461538SMichael Große            io_unlock($destination);
36bc461538SMichael Große            return $result;
37bc461538SMichael Große        }
38bc461538SMichael Große
39bc461538SMichael Große        if (is_file($source)) {
40bc461538SMichael Große            io_lock($destination);
41bc461538SMichael Große            $result=copy($source, $destination);
42bc461538SMichael Große            io_unlock($destination);
43bc461538SMichael Große            return $result;
44bc461538SMichael Große        }
45bc461538SMichael Große
46bc461538SMichael Große        if (!is_dir($destination)) {
47bc461538SMichael Große            io_mkdir_p($destination);
48bc461538SMichael Große        }
49bc461538SMichael Große
50bc461538SMichael Große        $dir = dir($source);
51bc461538SMichael Große        while (false !== ($entry = $dir->read())) {
52bc461538SMichael Große            if ($entry == '.' || $entry == '..') {
53bc461538SMichael Große                continue;
54bc461538SMichael Große            }
55bc461538SMichael Große
56bc461538SMichael Große            // recurse into directories
57bc461538SMichael Große            $this->io_copyDir("$source/$entry", "$destination/$entry");
58bc461538SMichael Große        }
59bc461538SMichael Große
60bc461538SMichael Große        $dir->close();
61bc461538SMichael Große        return true;
62bc461538SMichael Große    }
63bc461538SMichael Große
640b96e6d7SMichael Große
650b96e6d7SMichael Große
660b96e6d7SMichael Große    public function getAllPlugins() {
670b96e6d7SMichael Große        $dir = dir(DOKU_PLUGIN);
680b96e6d7SMichael Große        $plugins = array();
690b96e6d7SMichael Große        while (false !== ($entry = $dir->read())) {
706ec1ad8fSMichael Große            if($entry == '.' || $entry == '..' || $entry == 'testing') {
710b96e6d7SMichael Große                continue;
720b96e6d7SMichael Große            }
730b96e6d7SMichael Große            if (!is_dir(DOKU_PLUGIN ."/$entry")) {
740b96e6d7SMichael Große                continue;
750b96e6d7SMichael Große            }
760b96e6d7SMichael Große            $plugins[] = $entry;
770b96e6d7SMichael Große        }
786ec1ad8fSMichael Große        sort($plugins);
790b96e6d7SMichael Große        return $plugins;
800b96e6d7SMichael Große    }
810b96e6d7SMichael Große
820b96e6d7SMichael Große    public function getAllAnimals() {
830b96e6d7SMichael Große        $animals = array();
840b96e6d7SMichael Große
850b96e6d7SMichael Große        $dir = dir(DOKU_FARMDIR);
860b96e6d7SMichael Große        while (false !== ($entry = $dir->read())) {
870b96e6d7SMichael Große            if ($entry == '.' || $entry == '..' || $entry == '_animal') {
880b96e6d7SMichael Große                continue;
890b96e6d7SMichael Große            }
900b96e6d7SMichael Große            $animals[] = $entry;
910b96e6d7SMichael Große        }
920b96e6d7SMichael Große        $dir->close();
930b96e6d7SMichael Große        return $animals;
940b96e6d7SMichael Große    }
950b96e6d7SMichael Große
96fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
97fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
98fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
99fcbe16a4SMichael Große        } else {
100fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
101fcbe16a4SMichael Große        }
102fcbe16a4SMichael Große        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
103fcbe16a4SMichael Große            unset($plugins[$plugin]);
104fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
105fcbe16a4SMichael Große        }
106fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
107fcbe16a4SMichael Große    }
108fcbe16a4SMichael Große
109fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
110fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
111fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
112fcbe16a4SMichael Große        } else {
113fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
114fcbe16a4SMichael Große        }
115fcbe16a4SMichael Große        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
116fcbe16a4SMichael Große            $plugins[$plugin] = 0;
117fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
118fcbe16a4SMichael Große        }
119fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
120fcbe16a4SMichael Große    }
121fcbe16a4SMichael Große
122fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
123fcbe16a4SMichael Große        dbglog($plugins);
124fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
125fcbe16a4SMichael Große        foreach ($plugins as $plugin => $status) {
126fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
127fcbe16a4SMichael Große        }
128fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
129fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
130fcbe16a4SMichael Große    }
131fcbe16a4SMichael Große
132a12b96c0SMichael Große    public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) {
133a12b96c0SMichael Große        for ($position = 0; $position < $form->elementCount(); ++$position) {
134a12b96c0SMichael Große            if ($form->getElementAt($position) instanceof dokuwiki\Form\TagCloseElement) {
135a12b96c0SMichael Große                continue;
136a12b96c0SMichael Große            }
137a12b96c0SMichael Große            if ($form->getElementAt($position)->attr('name') == '') continue;
138a12b96c0SMichael Große            $elementName = $form->getElementAt($position)->attr('name');
139a12b96c0SMichael Große            if (!isset($errorArray[$elementName])) continue;
140a12b96c0SMichael Große            $form->getElementAt($position)->addClass('error');
141a12b96c0SMichael Große            $form->addTagOpen('div',$position+1)->addClass('error');
142a12b96c0SMichael Große            $form->addHTML($errorArray[$elementName],$position+2);
143a12b96c0SMichael Große            $form->addTagClose('div',$position+3);
144a12b96c0SMichael Große        }
145a12b96c0SMichael Große    }
146a12b96c0SMichael Große
1474d120480SMichael Große    public function reloadAdminPage($page = null) {
1484d120480SMichael Große        global $ID;
1494d120480SMichael Große        $get = $_GET;
1504d120480SMichael Große        if(isset($get['id'])) unset($get['id']);
1514d120480SMichael Große        if ($page !== null ) {
1524d120480SMichael Große            $get['page'] = $page;
1534d120480SMichael Große        }
1544d120480SMichael Große        $self = wl($ID, $get, false, '&');
1554d120480SMichael Große        send_redirect($self);
1564d120480SMichael Große    }
1574d120480SMichael Große
15879435a6fSMichael Große    public function downloadTemplate($animalpath) {
15979435a6fSMichael Große        file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r'));
160*c9fd7b89SMichael Große        $zip = new splitbrain\PHPArchive\Zip();
16179435a6fSMichael Große        $zip->open($animalpath.'/_animal.zip');
162*c9fd7b89SMichael Große        $zip->extract($animalpath);
16379435a6fSMichael Große        $zip->close();
16479435a6fSMichael Große        unlink($animalpath.'/_animal.zip');
16579435a6fSMichael Große    }
16679435a6fSMichael Große
167efa7af45SMichael Große    /**
168efa7af45SMichael Große     * recursive function to test wether a (non-existing) path points into an existint path
169efa7af45SMichael Große     *
170efa7af45SMichael Große     * @param $path string
171efa7af45SMichael Große     *
172efa7af45SMichael Große     * @param $container string has to exist
173efa7af45SMichael Große     *
174efa7af45SMichael Große     * @throws BadMethodCallException
175efa7af45SMichael Große     *
176efa7af45SMichael Große     * @return bool
177efa7af45SMichael Große     */
178efa7af45SMichael Große    public function isInPath ($path, $container) {
179efa7af45SMichael Große        if (!file_exists($container)) {
180efa7af45SMichael Große            throw new BadMethodCallException('The Container has to exist and be accessable by realpath().');
181efa7af45SMichael Große        }
182efa7af45SMichael Große        if (realpath($path) === false) {
183efa7af45SMichael Große            return $this->isInPath(dirname($path), $container);
184efa7af45SMichael Große        }
185efa7af45SMichael Große        if (strpos(realpath($path), realpath($container)) !== false) {
186efa7af45SMichael Große            return true;
187efa7af45SMichael Große        } else {
188efa7af45SMichael Große            return false;
189efa7af45SMichael Große        }
190efa7af45SMichael Große    }
191efa7af45SMichael Große
192bc461538SMichael Große}
193