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