author | Dan |
Sat, 01 Mar 2008 23:02:05 -0500 | |
changeset 471 | 7906fb190fc1 |
parent 464 | 8063eace5b67 |
child 507 | 586fd7d3202d |
permissions | -rw-r--r-- |
1 | 1 |
<?php |
2 |
||
3 |
/* |
|
4 |
* Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between |
|
411 | 5 |
* Version 1.1.2 (Caoineag alpha 2) |
1 | 6 |
* Copyright (C) 2006-2007 Dan Fuhry |
7 |
* |
|
8 |
* This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 |
* as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 |
*/ |
|
14 |
||
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
15 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
16 |
* Class used to handle and process plugin requests and loading. Singleton. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
17 |
* @package Enano |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
18 |
* @author Dan Fuhry <dan@enanocms.org> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
19 |
* @copyright (C) 2006-2008 Enano Project |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
20 |
* @license GNU General Public License <http://enanocms.org/Special:GNU_General_Public_License> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
21 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
22 |
|
1 | 23 |
class pluginLoader { |
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
24 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
25 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
26 |
* The list of hooks registered. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
27 |
* @var array |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
28 |
* @access private |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
29 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
30 |
|
1 | 31 |
var $hook_list; |
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
32 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
33 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
34 |
* The list of plugins that should be loaded. Used only by common.php. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
35 |
* @var array |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
36 |
* @access private |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
37 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
38 |
|
1 | 39 |
var $load_list; |
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
40 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
41 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
42 |
* The list of plugins that are loaded currently. This is only used by the loaded() method which in turn is |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
43 |
* used by template files with the <!-- IFPLUGIN --> special tag. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
44 |
* @var array |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
45 |
* @access private |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
46 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
47 |
|
1 | 48 |
var $loaded_plugins; |
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
49 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
50 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
51 |
* The list of plugins that are always loaded because they're part of the Enano core. This cannot be modified |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
52 |
* by any external code because user plugins are loaded after the load_list is calculated. Can be useful in |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
53 |
* alternative administration panel frameworks that need the list of system plugins. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
54 |
* @var array |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
55 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
56 |
|
403 | 57 |
var $system_plugins = Array('SpecialUserFuncs.php','SpecialUserPrefs.php','SpecialPageFuncs.php','SpecialAdmin.php','SpecialCSS.php','SpecialUpdownload.php','SpecialSearch.php','PrivateMessages.php','SpecialGroups.php', 'SpecialRecentChanges.php'); |
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
58 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
59 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
60 |
* Name kept for compatibility. Effectively a constructor. Calculates the list of plugins that should be loaded |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
61 |
* and puts that list in the $load_list property. Plugin developers have absolutely no use for this whatsoever. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
62 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
63 |
|
1 | 64 |
function loadAll() |
65 |
{ |
|
66 |
$dir = ENANO_ROOT.'/plugins/'; |
|
67 |
||
68 |
$this->load_list = Array(); |
|
69 |
||
70 |
$plugins = Array(); |
|
71 |
||
72 |
// Open a known directory, and proceed to read its contents |
|
73 |
||
74 |
if (is_dir($dir)) |
|
75 |
{ |
|
76 |
if ($dh = opendir($dir)) |
|
77 |
{ |
|
78 |
while (($file = readdir($dh)) !== false) |
|
79 |
{ |
|
80 |
if(preg_match('#^(.*?)\.php$#is', $file)) |
|
81 |
{ |
|
82 |
if(getConfig('plugin_'.$file) == '1' || in_array($file, $this->system_plugins)) |
|
83 |
{ |
|
84 |
$this->load_list[] = $dir . $file; |
|
85 |
$plugid = substr($file, 0, strlen($file)-4); |
|
378
c1c7fa6b329f
Got Enano to load even if there are no plugins; added caching for decrypted session keys to significantly improve performance (in theory at least)
Dan
parents:
322
diff
changeset
|
86 |
$f = @file_get_contents($dir . $file); |
c1c7fa6b329f
Got Enano to load even if there are no plugins; added caching for decrypted session keys to significantly improve performance (in theory at least)
Dan
parents:
322
diff
changeset
|
87 |
if ( empty($f) ) |
c1c7fa6b329f
Got Enano to load even if there are no plugins; added caching for decrypted session keys to significantly improve performance (in theory at least)
Dan
parents:
322
diff
changeset
|
88 |
continue; |
1 | 89 |
$f = explode("\n", $f); |
90 |
$f = array_slice($f, 2, 7); |
|
91 |
$f[0] = substr($f[0], 13); |
|
92 |
$f[1] = substr($f[1], 12); |
|
93 |
$f[2] = substr($f[2], 13); |
|
94 |
$f[3] = substr($f[3], 8 ); |
|
95 |
$f[4] = substr($f[4], 9 ); |
|
96 |
$f[5] = substr($f[5], 12); |
|
97 |
$plugins[$plugid] = Array(); |
|
98 |
$plugins[$plugid]['name'] = $f[0]; |
|
99 |
$plugins[$plugid]['uri'] = $f[1]; |
|
100 |
$plugins[$plugid]['desc'] = $f[2]; |
|
101 |
$plugins[$plugid]['auth'] = $f[3]; |
|
102 |
$plugins[$plugid]['vers'] = $f[4]; |
|
103 |
$plugins[$plugid]['aweb'] = $f[5]; |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
closedir($dh); |
|
108 |
} |
|
109 |
} |
|
110 |
$this->loaded_plugins = $plugins; |
|
111 |
//die('<pre>'.htmlspecialchars(print_r($plugins, true)).'</pre>'); |
|
112 |
} |
|
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
113 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
114 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
115 |
* Name kept for compatibility. This method is used to add a new hook into the code somewhere. Plugins are encouraged |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
116 |
* to set hooks and hook into other plugins in a fail-safe way, this encourages reuse of code. Returns an array, whose |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
117 |
* values should be eval'ed. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
118 |
* @example <code> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
119 |
$code = $plugins->setHook('my_hook_name'); |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
120 |
foreach ( $code as $cmd ) |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
121 |
{ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
122 |
eval($cmd); |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
123 |
} |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
124 |
</code> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
125 |
* @param string The name of the hook. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
126 |
* @param array Deprecated. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
127 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
128 |
|
1 | 129 |
function setHook($name, $opts = Array()) { |
130 |
if(isset($this->hook_list[$name]) && is_array($this->hook_list[$name])) |
|
131 |
{ |
|
379
82b991bee797
Minor and hopefully non-breaking change to plugin loader to possibly double performance at hook points
Dan
parents:
378
diff
changeset
|
132 |
return array(implode("\n", $this->hook_list[$name])); |
1 | 133 |
} |
134 |
else |
|
135 |
{ |
|
136 |
return Array(); |
|
137 |
} |
|
138 |
} |
|
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
139 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
140 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
141 |
* Attaches to a hook effectively scheduling some code to be run at that point. You should try to keep hooks clean by |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
142 |
* making a function that has variables that need to be modified passed by reference. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
143 |
* @example Simple example: <code> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
144 |
$plugins->attachHook('render_wikiformat_pre', '$text = str_replace("Goodbye, Mr. Chips", "Hello, Mr. Carrots", $text);'); |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
145 |
</code> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
146 |
* @example More complicated example: <code> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
147 |
$plugins->attachHook('render_wikiformat_pre', 'myplugin_parser_ext($text);'); |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
148 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
149 |
// Notice that $text is passed by reference. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
150 |
function myplugin_parser_ext(&$text) |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
151 |
{ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
152 |
$text = str_replace("Goodbye, Mr. Chips", "Hello, Mr. Carrots", $text); |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
153 |
} |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
154 |
</code> |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
155 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
156 |
|
1 | 157 |
function attachHook($name, $code) { |
158 |
if(!isset($this->hook_list[$name])) |
|
159 |
{ |
|
160 |
$this->hook_list[$name] = Array(); |
|
161 |
} |
|
162 |
$this->hook_list[$name][] = $code; |
|
163 |
} |
|
464
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
164 |
|
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
165 |
/** |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
166 |
* Tell whether a plugin is loaded or not. |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
167 |
* @param string The filename of the plugin |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
168 |
* @return bool |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
169 |
*/ |
8063eace5b67
Documented includes/plugins.php to try and help plugin developers a little.
Dan
parents:
411
diff
changeset
|
170 |
|
1 | 171 |
function loaded($plugid) |
172 |
{ |
|
173 |
return isset( $this->loaded_plugins[$plugid] ); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
?> |