xref: /plugin/farmer/helper.php (revision d6737b72db56e277e0161cf41f7ea46b172ceb4c)
1<?php
2/**
3 * DokuWiki Plugin farmer (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_farmer extends DokuWiki_Plugin {
13
14    private $allPlugins = array();
15
16    /**
17     * Copy a file, or recursively copy a folder and its contents. Adapted for DokuWiki.
18     *
19     * @todo: needs tests
20     *
21     * @author      Aidan Lister <aidan@php.net>
22     * @author      Michael Große <grosse@cosmocode.de>
23     * @version     1.0.1
24     * @link        http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
25     *
26     * @param       string $source Source path
27     * @param       string $destination      Destination path
28     *
29     * @return      bool     Returns TRUE on success, FALSE on failure
30     */
31    function io_copyDir($source, $destination) {
32        if (is_link($source)) {
33            io_lock($destination);
34            $result=symlink(readlink($source), $destination);
35            io_unlock($destination);
36            return $result;
37        }
38
39        if (is_file($source)) {
40            io_lock($destination);
41            $result=copy($source, $destination);
42            io_unlock($destination);
43            return $result;
44        }
45
46        if (!is_dir($destination)) {
47            io_mkdir_p($destination);
48        }
49
50        $dir = dir($source);
51        while (false !== ($entry = $dir->read())) {
52            if ($entry == '.' || $entry == '..') {
53                continue;
54            }
55
56            // recurse into directories
57            $this->io_copyDir("$source/$entry", "$destination/$entry");
58        }
59
60        $dir->close();
61        return true;
62    }
63
64
65
66    public function getAllPlugins() {
67        $dir = dir(DOKU_PLUGIN);
68        $plugins = array();
69        while (false !== ($entry = $dir->read())) {
70            if($entry == '.' || $entry == '..' || $entry == 'testing' || $entry == 'farmer') {
71                continue;
72            }
73            if (!is_dir(DOKU_PLUGIN ."/$entry")) {
74                continue;
75            }
76            $plugins[] = $entry;
77        }
78        sort($plugins);
79        return $plugins;
80    }
81
82    public function getAllAnimals() {
83        $animals = array();
84
85        $dir = dir(DOKU_FARMDIR);
86        while (false !== ($entry = $dir->read())) {
87            if ($entry == '.' || $entry == '..' || $entry == '_animal' || $entry == '.htaccess') {
88                continue;
89            }
90            if (!is_dir(DOKU_FARMDIR . $entry)) {
91                continue;
92            }
93            $animals[] = $entry;
94        }
95        $dir->close();
96        return $animals;
97    }
98
99    public function activatePlugin($plugin, $animal) {
100        if (isset($this->allPlugins[$animal])) {
101            $plugins = $this->allPlugins[$animal];
102        } else {
103            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
104        }
105        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
106            unset($plugins[$plugin]);
107            $this->writePluginConf($plugins, $animal);
108        }
109        $this->allPlugins[$animal] = $plugins;
110    }
111
112    /**
113     * @param $plugin {string} Name of the plugin to deactivate
114     * @param $animal {string} directory of the animal within DOKU_FARMDIR
115     */
116    public function deactivatePlugin($plugin, $animal) {
117        if (isset($this->allPlugins[$animal])) {
118            $plugins = $this->allPlugins[$animal];
119        } else {
120            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
121        }
122        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
123            $plugins[$plugin] = 0;
124            $this->writePluginConf($plugins, $animal);
125        }
126        $this->allPlugins[$animal] = $plugins;
127    }
128
129    public function writePluginConf($plugins, $animal) {
130        $pluginConf = '<?php' . "\n";
131        foreach ($plugins as $plugin => $status) {
132            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
133        }
134        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
135        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
136    }
137
138    public function addErrorsToForm(\dokuwiki\Form\Form &$form, $errorArray) {
139        foreach ($errorArray as $elementName => $errorMessage) {
140            $offset = 0;
141            msg($errorMessage, -1);
142            while ($form->findPositionByAttribute('name',$elementName, $offset)) {
143                $offset = $form->findPositionByAttribute('name',$elementName, $offset);
144                $form->getElementAt($offset)->addClass('error');
145                ++$offset;
146            }
147        }
148    }
149
150    public function reloadAdminPage($page = null) {
151        global $ID;
152        $get = $_GET;
153        if(isset($get['id'])) unset($get['id']);
154        if ($page !== null ) {
155            $get['page'] = $page;
156        }
157        $self = wl($ID, $get, false, '&');
158        send_redirect($self);
159    }
160
161    public function downloadTemplate($animalpath) {
162        file_put_contents($animalpath . '/_animal.zip',fopen('https://www.dokuwiki.org/_media/dokuwiki_farm_animal.zip','r'));
163        $zip = new splitbrain\PHPArchive\Zip();
164        $zip->open($animalpath.'/_animal.zip');
165        $zip->extract($animalpath);
166        $zip->close();
167        unlink($animalpath.'/_animal.zip');
168    }
169
170    /**
171     * recursive function to test wether a (non-existing) path points into an existint path
172     *
173     * @param $path string
174     *
175     * @param $container string has to exist
176     *
177     * @throws BadMethodCallException
178     *
179     * @return bool
180     */
181    public function isInPath ($path, $container) {
182        if (!file_exists($container)) {
183            throw new BadMethodCallException('The Container has to exist and be accessable by realpath().');
184        }
185        if (realpath($path) === false) {
186            return $this->isInPath(dirname($path), $container);
187        }
188        if (strpos(realpath($path), realpath($container)) !== false) {
189            return true;
190        } else {
191            return false;
192        }
193    }
194
195    /**
196     * @return bool
197     */
198    public function checkFarmSetup () {
199        if(defined('DOKU_FARMDIR') && defined('DOKU_FARMTYPE')) {
200            if (DOKU_FARMTYPE == 'subdomain') {
201                return true;
202            } elseif (DOKU_FARMTYPE == 'htaccess') {
203                return defined('DOKU_FARMRELDIR');
204            } else {
205                return false;
206            }
207        }
208        return false;
209    }
210
211}
212