xref: /plugin/farmer/helper.php (revision fc6a7cc1d84949eb291792ecf2620701eef4cc80)
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') {
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') {
88                continue;
89            }
90            $animals[] = $entry;
91        }
92        $dir->close();
93        return $animals;
94    }
95
96    public function activatePlugin($plugin, $animal) {
97        if (isset($this->allPlugins[$animal])) {
98            $plugins = $this->allPlugins[$animal];
99        } else {
100            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
101        }
102        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
103            unset($plugins[$plugin]);
104            $this->writePluginConf($plugins, $animal);
105        }
106        $this->allPlugins[$animal] = $plugins;
107    }
108
109    public function deactivatePlugin($plugin, $animal) {
110        if (isset($this->allPlugins[$animal])) {
111            $plugins = $this->allPlugins[$animal];
112        } else {
113            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
114        }
115        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
116            $plugins[$plugin] = 0;
117            $this->writePluginConf($plugins, $animal);
118        }
119        $this->allPlugins[$animal] = $plugins;
120    }
121
122    public function writePluginConf($plugins, $animal) {
123        dbglog($plugins);
124        $pluginConf = '<?php' . "\n";
125        foreach ($plugins as $plugin => $status) {
126            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
127        }
128        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
129        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
130    }
131
132}
133