1 | <?php
|
---|
2 | /**
|
---|
3 | * Class to encapsulate access to dokuwiki plugins
|
---|
4 | *
|
---|
5 | * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
---|
6 | * @author Christopher Smith <[email protected]>
|
---|
7 | */
|
---|
8 |
|
---|
9 | // plugin related constants
|
---|
10 | if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
|
---|
11 |
|
---|
12 | class Doku_Plugin_Controller {
|
---|
13 |
|
---|
14 | var $list_enabled = array();
|
---|
15 | var $list_disabled = array();
|
---|
16 | var $list_bytype = array();
|
---|
17 |
|
---|
18 | function Doku_Plugin_Controller() {
|
---|
19 | $this->_populateMasterList();
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Returns a list of available plugins of given type
|
---|
24 | *
|
---|
25 | * @param $type string, plugin_type name;
|
---|
26 | * the type of plugin to return,
|
---|
27 | * use empty string for all types
|
---|
28 | * @param $all bool;
|
---|
29 | * false to only return enabled plugins,
|
---|
30 | * true to return both enabled and disabled plugins
|
---|
31 | *
|
---|
32 | * @return array of plugin names
|
---|
33 | *
|
---|
34 | * @author Andreas Gohr <[email protected]>
|
---|
35 | */
|
---|
36 | function getList($type='',$all=false){
|
---|
37 |
|
---|
38 | // request the complete list
|
---|
39 | if (!$type) {
|
---|
40 | return $all ? array_merge($this->list_enabled,$this->list_disabled) : $this->list_enabled;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (!isset($this->list_bytype[$type]['enabled'])) {
|
---|
44 | $this->list_bytype[$type]['enabled'] = $this->_getListByType($type,true);
|
---|
45 | }
|
---|
46 | if ($all && !isset($this->list_bytype[$type]['disabled'])) {
|
---|
47 | $this->list_bytype[$type]['disabled'] = $this->_getListByType($type,false);
|
---|
48 | }
|
---|
49 |
|
---|
50 | return $all ? array_merge($this->list_bytype[$type]['enabled'],$this->list_bytype[$type]['disabled']) : $this->list_bytype[$type]['enabled'];
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Loads the given plugin and creates an object of it
|
---|
55 | *
|
---|
56 | * @author Andreas Gohr <[email protected]>
|
---|
57 | *
|
---|
58 | * @param $type string type of plugin to load
|
---|
59 | * @param $name string name of the plugin to load
|
---|
60 | * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
|
---|
61 | * @param $disabled bool true to load even disabled plugins
|
---|
62 | * @return objectreference the plugin object or null on failure
|
---|
63 | */
|
---|
64 | function &load($type,$name,$new=false,$disabled=false){
|
---|
65 | //we keep all loaded plugins available in global scope for reuse
|
---|
66 | global $DOKU_PLUGINS;
|
---|
67 |
|
---|
68 | list($plugin,$component) = $this->_splitName($name);
|
---|
69 |
|
---|
70 | // check if disabled
|
---|
71 | if(!$disabled && $this->isdisabled($plugin)){
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 |
|
---|
75 | //plugin already loaded?
|
---|
76 | if(!empty($DOKU_PLUGINS[$type][$name])){
|
---|
77 | if ($new || !$DOKU_PLUGINS[$type][$name]->isSingleton()) {
|
---|
78 | $class = $type.'_plugin_'.$name;
|
---|
79 | return class_exists($class) ? new $class : null;
|
---|
80 | } else {
|
---|
81 | return $DOKU_PLUGINS[$type][$name];
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | //try to load the wanted plugin file
|
---|
86 | $dir = $this->get_directory($plugin);
|
---|
87 | $file = $component ? "$type/$component.php" : "$type.php";
|
---|
88 |
|
---|
89 | if(!is_file(DOKU_PLUGIN."$dir/$file")){
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (!include_once(DOKU_PLUGIN."$dir/$file")) {
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | //construct class and instantiate
|
---|
98 | $class = $type.'_plugin_'.$name;
|
---|
99 | if (!class_exists($class)){
|
---|
100 | # the plugin might be in the wrong directory
|
---|
101 | $inf = confToHash(DOKU_PLUGIN."$dir/plugin.info.txt");
|
---|
102 | if($inf['base'] && $inf['base'] != $plugin){
|
---|
103 | msg("Plugin installed incorrectly. Rename plugin directory '".
|
---|
104 | hsc($plugin)."' to '".hsc($inf['base'])."'.",-1);
|
---|
105 | }
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 |
|
---|
109 | $DOKU_PLUGINS[$type][$name] = new $class;
|
---|
110 | return $DOKU_PLUGINS[$type][$name];
|
---|
111 | }
|
---|
112 |
|
---|
113 | function isdisabled($plugin) {
|
---|
114 | return (array_search($plugin, $this->list_enabled) === false);
|
---|
115 | }
|
---|
116 |
|
---|
117 | function enable($plugin) {
|
---|
118 | if (array_search($plugin, $this->list_disabled) !== false) {
|
---|
119 | return @unlink(DOKU_PLUGIN.$plugin.'/disabled');
|
---|
120 | }
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | function disable($plugin) {
|
---|
125 | if (array_search($plugin, $this->list_enabled) !== false) {
|
---|
126 | return @touch(DOKU_PLUGIN.$plugin.'/disabled');
|
---|
127 | }
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 |
|
---|
131 | function get_directory($plugin) {
|
---|
132 | return $plugin;
|
---|
133 | }
|
---|
134 |
|
---|
135 | function _populateMasterList() {
|
---|
136 | global $conf;
|
---|
137 | if ($dh = opendir(DOKU_PLUGIN)) {
|
---|
138 | while (false !== ($plugin = readdir($dh))) {
|
---|
139 | if ($plugin[0] == '.') continue; // skip hidden entries
|
---|
140 | if (is_file(DOKU_PLUGIN.$plugin)) continue; // skip files, we're only interested in directories
|
---|
141 |
|
---|
142 | if (substr($plugin,-9) == '.disabled') {
|
---|
143 | // the plugin was disabled by rc2009-01-26
|
---|
144 | // disabling mechanism was changed back very soon again
|
---|
145 | // to keep everything simple we just skip the plugin completely
|
---|
146 | }elseif(@file_exists(DOKU_PLUGIN.$plugin.'/disabled') ||
|
---|
147 | ($plugin === 'plugin' && isset($conf['pluginmanager']) &&
|
---|
148 | !$conf['pluginmanager'])){
|
---|
149 | $this->list_disabled[] = $plugin;
|
---|
150 | } else {
|
---|
151 | $this->list_enabled[] = $plugin;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | function _getListByType($type, $enabled) {
|
---|
158 | $master_list = $enabled ? $this->list_enabled : $this->list_disabled;
|
---|
159 |
|
---|
160 | $plugins = array();
|
---|
161 | foreach ($master_list as $plugin) {
|
---|
162 | $dir = $this->get_directory($plugin);
|
---|
163 |
|
---|
164 | if (@file_exists(DOKU_PLUGIN."$dir/$type.php")){
|
---|
165 | $plugins[] = $plugin;
|
---|
166 | } else {
|
---|
167 | if ($dp = @opendir(DOKU_PLUGIN."$dir/$type/")) {
|
---|
168 | while (false !== ($component = readdir($dp))) {
|
---|
169 | if (substr($component,0,1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
|
---|
170 | if (is_file(DOKU_PLUGIN."$dir/$type/$component")) {
|
---|
171 | $plugins[] = $plugin.'_'.substr($component, 0, -4);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | closedir($dp);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | return $plugins;
|
---|
180 | }
|
---|
181 |
|
---|
182 | function _splitName($name) {
|
---|
183 | if (array_search($name, $this->list_enabled + $this->list_disabled) === false) {
|
---|
184 | return explode('_',$name,2);
|
---|
185 | }
|
---|
186 |
|
---|
187 | return array($name,'');
|
---|
188 | }
|
---|
189 |
|
---|
190 | }
|
---|