1+ <?php
2+
3+ /*
4+ Copyright (c) 2010, Till Brehm, projektfarm Gmbh
5+ All rights reserved.
6+
7+ Redistribution and use in source and binary forms, with or without modification,
8+ are permitted provided that the following conditions are met:
9+
10+ * Redistributions of source code must retain the above copyright notice,
11+ this list of conditions and the following disclaimer.
12+ * Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+ * Neither the name of ISPConfig nor the names of its contributors
16+ may be used to endorse or promote products derived from this software without
17+ specific prior written permission.
18+
19+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+ */
30+
31+ class plugin {
32+
33+ private $ subscribed_events = array ();
34+ private $ debug = true ;
35+
36+
37+ /*
38+ This function is called to load the plugins from the plugins folder and update the plugin cache
39+ */
40+
41+ private function loadPluginCache () {
42+ global $ app ,$ conf ;
43+
44+
45+ if (isset ($ _SESSION ['s ' ]['plugin_cache ' ])) unset($ _SESSION ['s ' ]['plugin_cache ' ]);
46+
47+ $ plugins_dir = ISPC_LIB_PATH .FS_DIV .'plugins ' .FS_DIV ;
48+ $ _SESSION ['s ' ]['plugin_cache ' ] = array ();
49+ $ tmp_plugins = array ();
50+
51+ if (is_dir ($ plugins_dir )) {
52+ if ($ dh = opendir ($ plugins_dir )) {
53+ //** Go trough all files in the plugin dir
54+ while (($ file = readdir ($ dh )) !== false ) {
55+ if ($ file != '. ' && $ file != '.. ' && substr ($ file ,-8 ,8 ) == '.inc.php ' ) {
56+ $ plugin_name = substr ($ file ,0 ,-8 );
57+ $ tmp_plugins [$ plugin_name ] = $ file ;
58+ }
59+ }
60+ //** sort the plugins by name
61+ ksort ($ tmp_plugins );
62+
63+ //** load the plugins
64+ foreach ($ tmp_plugins as $ plugin_name => $ file ) {
65+ include_once ($ plugins_dir .$ file );
66+ if ($ this ->debug ) $ app ->log ("Loading Plugin: $ plugin_name " ,LOGLEVEL_DEBUG );
67+ $ app ->loaded_plugins [$ plugin_name ] = new $ plugin_name ;
68+ $ app ->loaded_plugins [$ plugin_name ]->onLoad ();
69+ }
70+ } else {
71+ $ app ->log ("Unable to open the plugin directory: $ plugins_dir " ,LOGLEVEL_ERROR );
72+ }
73+ } else {
74+ $ app ->log ("Plugin directory missing: $ plugins_dir " ,LOGLEVEL_ERROR );
75+ }
76+
77+ }
78+
79+ /*
80+ This function is called by the plugin to register for an event which is saved into the plugin cache
81+ for faster lookups without the need to load all plugins for every page.
82+ */
83+
84+ public function registerEvent ($ event_name ,$ plugin_name ,$ function_name ) {
85+ global $ app ;
86+
87+ $ _SESSION ['s ' ]['plugin_cache ' ][$ event_name ][] = array ('plugin ' => $ plugin_name , 'function ' => $ function_name );
88+ if ($ this ->debug ) $ app ->log ("Plugin ' $ plugin_name' has registered the function ' $ function_name' for the event ' $ event_name' " ,LOGLEVEL_DEBUG );
89+
90+ }
91+
92+ /*
93+ This function is called when a certian action occurs, e.g. a form gets saved or a user is logged in.
94+ */
95+
96+ public function raiseEvent ($ event_name ,$ data ) {
97+ global $ app ;
98+
99+ if (!isset ($ _SESSION ['s ' ]['plugin_cache ' ])) {
100+ $ this ->loadPluginCache ();
101+ if ($ this ->debug ) $ app ->log ("Loaded the plugin cache. " ,LOGLEVEL_DEBUG );
102+ }
103+
104+
105+ $ sub_events = explode (': ' ,$ event_name );
106+
107+ if (is_array ($ sub_events )) {
108+ if (count ($ sub_events ) == 3 ) {
109+ $ tmp_event = $ sub_events [2 ];
110+ if ($ this ->debug ) $ app ->log ("Called Event ' $ tmp_event' " ,LOGLEVEL_DEBUG );
111+ $ this ->callPluginEvent ($ tmp_event ,$ data );
112+ $ tmp_event = $ sub_events [0 ].': ' .$ sub_events [2 ];
113+ if ($ this ->debug ) $ app ->log ("Called Event ' $ tmp_event' " ,LOGLEVEL_DEBUG );
114+ $ this ->callPluginEvent ($ tmp_event ,$ data );
115+ $ tmp_event = $ sub_events [0 ].': ' .$ sub_events [1 ].': ' .$ sub_events [2 ];
116+ if ($ this ->debug ) $ app ->log ("Called Event ' $ tmp_event' " ,LOGLEVEL_DEBUG );
117+ $ this ->callPluginEvent ($ tmp_event ,$ data );
118+
119+ /*$sub_events = array_reverse($sub_events);
120+ $tmp_event = '';
121+ foreach($sub_events as $n => $sub_event) {
122+ $tmp_event = ($n == 0)?$sub_event:$sub_event.':'.$tmp_event;
123+ if($this->debug) $app->log("Called Event '$tmp_event'",LOGLEVEL_DEBUG);
124+ $this->callPluginEvent($tmp_event,$data);
125+ }
126+ */
127+ } else {
128+ if ($ this ->debug ) $ app ->log ("Called Event ' $ sub_events [0 ]' " ,LOGLEVEL_DEBUG );
129+ $ this ->callPluginEvent ($ sub_events [0 ],$ data );
130+ }
131+ }
132+
133+ } // end function raiseEvent
134+
135+ //* Internal function to load the plugin and call the event function in the plugin.
136+ private function callPluginEvent ($ event_name ,$ data ) {
137+ global $ app ;
138+
139+ //* execute the functions for the events
140+ if (is_array ($ _SESSION ['s ' ]['plugin_cache ' ][$ event_name ])) {
141+ foreach ($ _SESSION ['s ' ]['plugin_cache ' ][$ event_name ] as $ rec ) {
142+ $ plugin_name = $ rec ['plugin ' ];
143+ $ function_name = $ rec ['function ' ];
144+ $ plugin_file = ISPC_LIB_PATH .FS_DIV .'plugins ' .FS_DIV .$ plugin_name .'.inc.php ' ;
145+ if (is_file ($ plugin_file )) {
146+ if (!isset ($ app ->loaded_plugins [$ plugin_name ])) {
147+ include_once ($ plugin_file );
148+ $ app ->loaded_plugins [$ plugin_name ] = new $ plugin_name ;
149+ }
150+ if ($ this ->debug ) $ app ->log ("Called method: ' $ function_name' in plugin ' $ plugin_name' for event ' $ event_name' " ,LOGLEVEL_DEBUG );
151+ call_user_method ($ function_name ,$ app ->loaded_plugins [$ plugin_name ],$ event_name ,$ data );
152+ }
153+ }
154+
155+ }
156+ } // end functiom callPluginEvent
157+
158+
159+ }
160+
161+ ?>
0 commit comments