xref: /plugin/farmer/helper.php (revision a0fc814b7dea1b280e6a23b5b71ba5abf75a338a)
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())) {
87*a0fc814bSMichael Große            if ($entry == '.' || $entry == '..' || $entry == '_animal' || $entry == '.htaccess') {
88*a0fc814bSMichael Große                continue;
89*a0fc814bSMichael Große            }
90*a0fc814bSMichael Große            if (!is_dir(DOKU_FARMDIR . $entry)) {
910b96e6d7SMichael Große                continue;
920b96e6d7SMichael Große            }
930b96e6d7SMichael Große            $animals[] = $entry;
940b96e6d7SMichael Große        }
950b96e6d7SMichael Große        $dir->close();
960b96e6d7SMichael Große        return $animals;
970b96e6d7SMichael Große    }
980b96e6d7SMichael Große
99fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
100fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
101fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
102fcbe16a4SMichael Große        } else {
103fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
104fcbe16a4SMichael Große        }
105fcbe16a4SMichael Große        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
106fcbe16a4SMichael Große            unset($plugins[$plugin]);
107fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
108fcbe16a4SMichael Große        }
109fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
110fcbe16a4SMichael Große    }
111fcbe16a4SMichael Große
112*a0fc814bSMichael Große    /**
113*a0fc814bSMichael Große     * @param $plugin {string} Name of the plugin to deactivate
114*a0fc814bSMichael Große     * @param $animal {string} directory of the animal within DOKU_FARMDIR
115*a0fc814bSMichael Große     */
116fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
117fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
118fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
119fcbe16a4SMichael Große        } else {
120fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
121fcbe16a4SMichael Große        }
122fcbe16a4SMichael Große        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
123fcbe16a4SMichael Große            $plugins[$plugin] = 0;
124fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
125fcbe16a4SMichael Große        }
126fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
127fcbe16a4SMichael Große    }
128fcbe16a4SMichael Große
129fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
130fcbe16a4SMichael Große        dbglog($plugins);
131fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
132fcbe16a4SMichael Große        foreach ($plugins as $plugin => $status) {
133fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
134fcbe16a4SMichael Große        }
135fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
136fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
137fcbe16a4SMichael Große    }
138fcbe16a4SMichael Große
139a12b96c0SMichael Große    public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) {
140a12b96c0SMichael Große        for ($position = 0; $position < $form->elementCount(); ++$position) {
141a12b96c0SMichael Große            if ($form->getElementAt($position) instanceof dokuwiki\Form\TagCloseElement) {
142a12b96c0SMichael Große                continue;
143a12b96c0SMichael Große            }
144a12b96c0SMichael Große            if ($form->getElementAt($position)->attr('name') == '') continue;
145a12b96c0SMichael Große            $elementName = $form->getElementAt($position)->attr('name');
146a12b96c0SMichael Große            if (!isset($errorArray[$elementName])) continue;
147a12b96c0SMichael Große            $form->getElementAt($position)->addClass('error');
148a12b96c0SMichael Große            $form->addTagOpen('div',$position+1)->addClass('error');
149a12b96c0SMichael Große            $form->addHTML($errorArray[$elementName],$position+2);
150a12b96c0SMichael Große            $form->addTagClose('div',$position+3);
151a12b96c0SMichael Große        }
152a12b96c0SMichael Große    }
153a12b96c0SMichael Große
1544d120480SMichael Große    public function reloadAdminPage($page = null) {
1554d120480SMichael Große        global $ID;
1564d120480SMichael Große        $get = $_GET;
1574d120480SMichael Große        if(isset($get['id'])) unset($get['id']);
1584d120480SMichael Große        if ($page !== null ) {
1594d120480SMichael Große            $get['page'] = $page;
1604d120480SMichael Große        }
1614d120480SMichael Große        $self = wl($ID, $get, false, '&');
1624d120480SMichael Große        send_redirect($self);
1634d120480SMichael Große    }
1644d120480SMichael Große
16579435a6fSMichael Große    public function downloadTemplate($animalpath) {
16679435a6fSMichael Große        file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r'));
167c9fd7b89SMichael Große        $zip = new splitbrain\PHPArchive\Zip();
16879435a6fSMichael Große        $zip->open($animalpath.'/_animal.zip');
169c9fd7b89SMichael Große        $zip->extract($animalpath);
17079435a6fSMichael Große        $zip->close();
17179435a6fSMichael Große        unlink($animalpath.'/_animal.zip');
17279435a6fSMichael Große    }
17379435a6fSMichael Große
174efa7af45SMichael Große    /**
175efa7af45SMichael Große     * recursive function to test wether a (non-existing) path points into an existint path
176efa7af45SMichael Große     *
177efa7af45SMichael Große     * @param $path string
178efa7af45SMichael Große     *
179efa7af45SMichael Große     * @param $container string has to exist
180efa7af45SMichael Große     *
181efa7af45SMichael Große     * @throws BadMethodCallException
182efa7af45SMichael Große     *
183efa7af45SMichael Große     * @return bool
184efa7af45SMichael Große     */
185efa7af45SMichael Große    public function isInPath ($path, $container) {
186efa7af45SMichael Große        if (!file_exists($container)) {
187efa7af45SMichael Große            throw new BadMethodCallException('The Container has to exist and be accessable by realpath().');
188efa7af45SMichael Große        }
189efa7af45SMichael Große        if (realpath($path) === false) {
190efa7af45SMichael Große            return $this->isInPath(dirname($path), $container);
191efa7af45SMichael Große        }
192efa7af45SMichael Große        if (strpos(realpath($path), realpath($container)) !== false) {
193efa7af45SMichael Große            return true;
194efa7af45SMichael Große        } else {
195efa7af45SMichael Große            return false;
196efa7af45SMichael Große        }
197efa7af45SMichael Große    }
198efa7af45SMichael Große
199bc461538SMichael Große}
200