1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
<?php
/*
@author dhtmlx.com
@license GPL, see license.txt
*/
/*! Class which allows to assign|fire events.
*/
class EventMaster{
private $events;//!< hash of event handlers
private $master;
private static $eventsStatic=array();
/*! constructor
*/
function __construct(){
$this->events=array();
$this->master = false;
}
/*! Method check if event with such name already exists.
@param name
name of event, case non-sensitive
@return
true if event with such name registered, false otherwise
*/
public function exist($name){
$name=strtolower($name);
return (isset($this->events[$name]) && sizeof($this->events[$name]));
}
/*! Attach custom code to event.
Only on event handler can be attached in the same time. If new event handler attached - old will be detached.
@param name
name of event, case non-sensitive
@param method
function which will be attached. You can use array(class, method) if you want to attach the method of the class.
*/
public function attach($name,$method=false){
//use class for event handling
if ($method === false){
$this->master = $name;
return;
}
//use separate functions
$name=strtolower($name);
if (!array_key_exists($name,$this->events))
$this->events[$name]=array();
$this->events[$name][]=$method;
}
public static function attach_static($name, $method){
$name=strtolower($name);
if (!array_key_exists($name,EventMaster::$eventsStatic))
EventMaster::$eventsStatic[$name]=array();
EventMaster::$eventsStatic[$name][]=$method;
}
public static function trigger_static($name, $method){
$arg_list = func_get_args();
$name=strtolower(array_shift($arg_list));
if (isset(EventMaster::$eventsStatic[$name]))
foreach(EventMaster::$eventsStatic[$name] as $method){
if (is_array($method) && !method_exists($method[0],$method[1]))
throw new Exception("Incorrect method assigned to event: ".$method[0].":".$method[1]);
if (!is_array($method) && !function_exists($method))
throw new Exception("Incorrect function assigned to event: ".$method);
call_user_func_array($method, $arg_list);
}
return true;
}
/*! Detach code from event
@param name
name of event, case non-sensitive
*/
public function detach($name){
$name=strtolower($name);
unset($this->events[$name]);
}
/*! Trigger event.
@param name
name of event, case non-sensitive
@param data
value which will be provided as argument for event function,
you can provide multiple data arguments, method accepts variable number of parameters
@return
true if event handler was not assigned , result of event hangler otherwise
*/
public function trigger($name,$data){
$arg_list = func_get_args();
$name=strtolower(array_shift($arg_list));
if (isset($this->events[$name]))
foreach($this->events[$name] as $method){
if (is_array($method) && !method_exists($method[0],$method[1]))
throw new Exception("Incorrect method assigned to event: ".$method[0].":".$method[1]);
if (!is_array($method) && !function_exists($method))
throw new Exception("Incorrect function assigned to event: ".$method);
call_user_func_array($method, $arg_list);
}
if ($this->master !== false)
if (method_exists($this->master, $name))
call_user_func_array(array($this->master, $name), $arg_list);
return true;
}
}
/*! Class which handles access rules.
**/
class AccessMaster{
private $rules,$local;
/*! constructor
Set next access right to "allowed" by default : read, insert, update, delete
Basically - all common data operations allowed by default
*/
function __construct(){
$this->rules=array("read" => true, "insert" => true, "update" => true, "delete" => true);
$this->local=true;
}
/*! change access rule to "allow"
@param name
name of access right
*/
public function allow($name){
$this->rules[$name]=true;
}
/*! change access rule to "deny"
@param name
name of access right
*/
public function deny($name){
$this->rules[$name]=false;
}
/*! change all access rules to "deny"
*/
public function deny_all(){
$this->rules=array();
}
/*! check access rule
@param name
name of access right
@return
true if access rule allowed, false otherwise
*/
public function check($name){
if ($this->local){
/*!
todo
add referrer check, to prevent access from remote points
*/
}
if (!isset($this->rules[$name]) || !$this->rules[$name]){
return false;
}
return true;
}
}
/*! Controls error and debug logging.
Class designed to be used as static object.
**/
class LogMaster{
private static $_log=false;//!< logging mode flag
private static $_output=false;//!< output error infor to client flag
private static $session="";//!< all messages generated for current request
/*! convert array to string representation ( it is a bit more readable than var_dump )
@param data
data object
@param pref
prefix string, used for formating, optional
@return
string with array description
*/
private static function log_details($data,$pref=""){
if (is_array($data)){
$str=array("");
foreach($data as $k=>$v)
array_push($str,$pref.$k." => ".LogMaster::log_details($v,$pref."\t"));
return implode("\n",$str);
}
return $data;
}
/*! put record in log
@param str
string with log info, optional
@param data
data object, which will be added to log, optional
*/
public static function log($str="",$data=""){
if (LogMaster::$_log){
$message = $str.LogMaster::log_details($data)."\n\n";
LogMaster::$session.=$message;
error_log($message,3,LogMaster::$_log);
}
}
/*! get logs for current request
@return
string, which contains all log messages generated for current request
*/
public static function get_session_log(){
return LogMaster::$session;
}
/*! error handler, put normal php errors in log file
@param errn
error number
@param errstr
error description
@param file
error file
@param line
error line
@param context
error cntext
*/
public static function error_log($errn,$errstr,$file,$line,$context){
LogMaster::log($errstr." at ".$file." line ".$line);
}
/*! exception handler, used as default reaction on any error - show execution log and stop processing
@param exception
instance of Exception
*/
public static function exception_log($exception){
LogMaster::log("!!!Uncaught Exception\nCode: " . $exception->getCode() . "\nMessage: " . $exception->getMessage());
if (LogMaster::$_output){
echo "<pre><xmp>\n";
echo LogMaster::get_session_log();
echo "\n</xmp></pre>";
}
die();
}
/*! enable logging
@param name
path to the log file, if boolean false provided as value - logging will be disabled
@param output
flag of client side output, if enabled - session log will be sent to client side in case of an error.
*/
public static function enable_log($name,$output=false){
LogMaster::$_log=$name;
LogMaster::$_output=$output;
if ($name){
set_error_handler(array("LogMaster","error_log"),E_ALL);
set_exception_handler(array("LogMaster","exception_log"));
LogMaster::log("\n\n====================================\nLog started, ".date("d/m/Y h:i:s")."\n====================================");
}
}
}
?>
|