1<?php
2/*
3 * e107 website system
4 *
5 * Copyright (C) 2008-2015 e107 Inc (e107.org)
6 * Released under the terms and conditions of the
7 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
8 *
9 */
10
11
12if (!defined('e107_INIT')) { exit; }
13
14
15
16class e107_event
17{
18	var $functions = array();
19	var $includes = array();
20
21	protected $coreEvents;
22
23	protected $oldCoreEvents = array(
24
25		'usersup'		=> 'user_signup_submitted',
26		'userveri'		=> 'user_signup_activated',
27		'flood'			=> 'user_ban_flood',
28		'subnews'		=> 'user_news_submit',
29		'fileupload'	=> 'user_file_upload',
30		'newspost'		=> 'admin_news_created',
31		'newsupd'		=> 'admin_news_updated',
32		'newsdel'		=> 'admin_news_deleted',
33		'userdatachanged' => 'user_profile_edit'
34	);
35
36
37	function __construct()
38	{
39
40
41
42	}
43
44
45	public function init()
46	{
47
48		$temp = e107::getAddonConfig('e_event');
49
50		if(!empty($temp))
51		{
52			foreach($temp as $plug=>$data)
53			{
54				foreach($data as $event)
55				{
56					$name = $event['name'];
57					$class = array($plug."_event", $event['function']);
58
59					if(!empty($name) && !empty($event['function']))
60					{
61						$this->register($name, $class);
62					}
63				}
64
65			}
66
67		}
68
69
70	}
71
72
73	function coreList()
74	{
75
76		$this->coreEvents = array( // used by e_notify admin area.
77
78			'session'	=> array(
79
80				'user_signup_submitted'		=> NU_LAN_2,
81				'user_signup_activated'		=> NU_LAN_3,
82				'login' 					=> NU_LAN_4,
83				'logout'					=> NU_LAN_5,
84				'user_xup_login'			=> NU_LAN_6,
85				'user_xup_signup'			=> NU_LAN_7,
86				'user_ban_flood'			=> NS_LAN_2,
87				'user_ban_failed_login'		=> NS_LAN_3,
88				'user_profile_display'      => NU_LAN_8,
89				'user_profile_edit'         => NU_LAN_9
90
91			),
92
93			'administrators'	=> array(
94				'admin_password_update'		=> NA_LAN_1,
95				'admin_user_created'		=> NA_LAN_2,
96				'admin_user_activated'		=> NA_LAN_3
97
98			),
99
100			'news'	=> array(
101
102				'admin_news_created'	=> NN_LAN_3,
103				'admin_news_updated'	=> NN_LAN_4,
104				'admin_news_deleted'	=> NN_LAN_5,
105				'admin_news_notify'     => NN_LAN_6,
106				'user_news_submit'		=> NN_LAN_2,
107
108			),
109
110			'mail'	=> array(
111
112				'maildone'			=> NM_LAN_2,
113			),
114
115			'file'	=> array(
116
117				//		'fileupload'		=> NF_LAN_2,
118				'user_file_upload'	=> NF_LAN_2,
119			),
120
121		);
122
123
124		return $this->coreEvents;
125	}
126
127	function oldCoreList()
128	{
129		return $this->oldCoreEvents;
130	}
131
132
133	/**
134	 * Register event
135	 *
136	 * @param string $eventname
137	 * @param array|string $function [class_name, method_name] or function name
138	 * @param string $include [optional] include path
139	 * @return void
140	 */
141	function register($eventname, $function, $include='')
142	{
143
144
145		if(!isset($this->functions[$eventname]) || !in_array($function, $this->functions[$eventname]))
146		{
147			if (!empty($include))
148			{
149				$this->includes[$eventname][] = $include;
150			}
151
152			$this->functions[$eventname][] = $function;
153		}
154	}
155
156
157
158	function debug()
159	{
160		$text = "<h3>Event Functions</h3>";
161		$text .= print_a($this->functions,true);
162		$text .= "<h3>Event Includes</h3>";
163		$text .= print_a($this->includes,true);
164
165		return $text;
166	}
167
168
169	/**
170	 * Trigger event
171	 *
172	 * @param string $eventname
173	 * @param mixed $data
174	 * @return mixed
175	 */
176	function trigger($eventname, $data='')
177	{
178		/*if (isset($this->includes[$eventname]))
179		{
180			foreach($this->includes[$eventname] as $evt_inc)
181			{
182				if (file_exists($evt_inc))
183				{
184					include_once($evt_inc);
185				}
186			}
187		}*/
188
189	//	echo ($this->debug());
190
191
192		if (isset($this->functions[$eventname]))
193		{
194			foreach($this->functions[$eventname] as $i => $evt_func)
195			{
196				$location = '';
197				if(isset($this->includes[$eventname][$i])) //no checks
198				{
199					$location = $this->includes[$eventname][$i];
200
201					e107_include_once($location);
202					unset($this->includes[$eventname][$i]);
203
204				}
205
206				if(is_array($evt_func)) //class, method
207				{
208					$class = $evt_func[0];
209					$method = $evt_func[1];
210
211					try
212					{
213
214						$tmp = new $class($eventname);
215						$ret = $tmp->{$method}($data, $eventname); //let callback know what event is calling it
216						unset($tmp);
217						if (!empty($ret))
218						{
219							break;
220						}
221					}
222					catch(Exception $e)
223					{
224						e107::getLog()->add('Event Trigger failed',array('name'=>$eventname,'location'=>$location,'class'=>$class,'method'=>$method,'error'=>$e),E_LOG_WARNING,'EVENT_01');
225						continue;
226					}
227				}
228				elseif (function_exists($evt_func))
229				{
230					$ret = $evt_func($data, $eventname); //let callback know what event is calling it
231					if (!empty($ret))
232					{
233						break;
234					}
235				}
236				else
237				{
238					e107::getLog()->add('Event Trigger failed',array('name'=>$eventname,'location'=>$location,'function'=>$evt_func), E_LOG_WARNING,'EVENT_01');
239				}
240
241			}
242		}
243		return (isset($ret) ? $ret : false);
244	}
245
246
247
248
249	/**
250	 * @Deprecated
251	 */
252	function triggerAdminEvent($type, $parms=null)
253	{
254		global $pref;
255		if(!is_array($parms))
256		{
257			parse_str($parms, $parms);
258		}
259		if(isset($pref['e_admin_events_list']) && is_array($pref['e_admin_events_list']))
260		{
261			// $called = getcachedvars('admin_events_called');
262			$called = e107::getRegistry('core/cachedvars/admin_events_called', false);
263			if(!is_array($called)) { $called = array(); }
264			foreach($pref['e_admin_events_list'] as $plugin)
265			{
266				if(e107::isInstalled($plugin))
267				{
268					$func = 'plugin_'.$plugin.'_admin_events';
269					if(!function_exists($func))
270					{
271						$fname = e_PLUGIN.$plugin.'/e_admin_events.php';
272						if(is_readable($fname)) { include_once($fname); }
273					}
274					if(function_exists($func))
275					{
276						$event_func = call_user_func($func, $type, $parms);
277						if ($event_func && function_exists($event_func) && !in_array($event_func, $called))
278						{
279							$called[] = $event_func;
280							// cachevars('admin_events_called', $called);
281							e107::setRegistry('core/cachedvars/admin_events_called', $called);
282							call_user_func($event_func);
283						}
284					}
285				}
286			}
287		}
288	}
289
290	/*
291	* triggerHook trigger a hooked in element
292	*   four methods are allowed hooks: form, create, update, delete
293	*   form : return array('caption'=>'', 'text'=>'');
294	*   create, update, delete : return string message
295	* @param array $data array containing
296	* @param string $method form,insert,update,delete
297	* @param string $table the table name of the calling plugin
298	* @param int $id item id of the record
299	* @param string $plugin identifier for the calling plugin
300	* @param string $function identifier for the calling function
301	* @return string $text string of rendered html, or message from db handler
302	*/
303	function triggerHook($data=array())
304	{
305		$text = null;
306		$e_event_list = e107::getPref('e_event_list');
307
308		if(is_array($e_event_list))
309		{
310			foreach($e_event_list as $hook)
311			{
312				if(e107::isInstalled($hook))
313				{
314					if(is_readable(e_PLUGIN.$hook."/e_event.php"))
315					{
316						require_once(e_PLUGIN.$hook."/e_event.php");
317						$name = "e_event_{$hook}";
318						if(class_exists($name))
319						{
320							$class = new $name();
321
322							switch($data['method'])
323							{
324								//returns array('caption'=>'', 'text'=>'');
325								case 'form':
326									if(method_exists($class, "event_{$data['method']}"))
327									{
328										$ret = $class->event_form($data);
329
330										if(!isset($ret[0]))
331										{
332											$text[$hook][0] = $ret;
333										}
334										else
335										{
336											$text[$hook] = $ret;
337										}
338
339
340									}
341									break;
342								//returns string message
343								case 'create':
344								case 'update':
345								case 'delete':
346									if(method_exists($class, "event_{$data['method']}"))
347									{
348										$text .= call_user_func(array($class, "event_{$data['method']}"), $data);
349									}
350									break;
351							}
352						}
353					}
354				}
355			}
356		}
357		return $text;
358	}
359}
360
361
362