xref: /plugin/farmer/helper.php (revision fcbe16a41a592cca771448d3f38341a96779636c)
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
14*fcbe16a4SMichael Große    private $allPlugins = array();
15*fcbe16a4SMichael 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
96*fcbe16a4SMichael Große    public function activatePlugin($plugin, $animal) {
97*fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
98*fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
99*fcbe16a4SMichael Große        } else {
100*fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
101*fcbe16a4SMichael Große        }
102*fcbe16a4SMichael Große        if (isset($plugins[$plugin]) && $plugins[$plugin] === 0) {
103*fcbe16a4SMichael Große            unset($plugins[$plugin]);
104*fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
105*fcbe16a4SMichael Große        }
106*fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
107*fcbe16a4SMichael Große    }
108*fcbe16a4SMichael Große
109*fcbe16a4SMichael Große    public function deactivatePlugin($plugin, $animal) {
110*fcbe16a4SMichael Große        if (isset($this->allPlugins[$animal])) {
111*fcbe16a4SMichael Große            $plugins = $this->allPlugins[$animal];
112*fcbe16a4SMichael Große        } else {
113*fcbe16a4SMichael Große            include(DOKU_FARMDIR . $animal . '/conf/plugins.local.php');
114*fcbe16a4SMichael Große        }
115*fcbe16a4SMichael Große        if (!isset($plugins[$plugin]) || $plugins[$plugin] !== 0) {
116*fcbe16a4SMichael Große            $plugins[$plugin] = 0;
117*fcbe16a4SMichael Große            $this->writePluginConf($plugins, $animal);
118*fcbe16a4SMichael Große        }
119*fcbe16a4SMichael Große        $this->allPlugins[$animal] = $plugins;
120*fcbe16a4SMichael Große    }
121*fcbe16a4SMichael Große
122*fcbe16a4SMichael Große    public function writePluginConf($plugins, $animal) {
123*fcbe16a4SMichael Große        dbglog($plugins);
124*fcbe16a4SMichael Große        $pluginConf = '<?php' . "\n";
125*fcbe16a4SMichael Große        foreach ($plugins as $plugin => $status) {
126*fcbe16a4SMichael Große            $pluginConf .= '$plugins["' . $plugin  . '"] = ' . $status . ";\n";
127*fcbe16a4SMichael Große        }
128*fcbe16a4SMichael Große        io_saveFile(DOKU_FARMDIR . $animal . '/conf/plugins.local.php', $pluginConf);
129*fcbe16a4SMichael Große        touch(DOKU_FARMDIR . $animal . '/conf/local.php');
130*fcbe16a4SMichael Große    }
131*fcbe16a4SMichael Große
132bc461538SMichael Große}
133