-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg.class.php
373 lines (318 loc) · 10.8 KB
/
cfg.class.php
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* Class cfg
*/
class cfg
{
const filename = 'rester.ini';
const query_version = 'v';
const query_module = 'm';
const query_proc = 'proc';
const version = 'version';
const module = 'module';
const proc = 'proc';
const method = 'method';
const request_body = 'request-body';
const request_body_token = 'token';
const common = 'common';
const common_timezone = 'timezone';
const common_debug_mode = 'debug_mode';
const common_host = 'host';
const cache = 'cache';
const cache_host = 'host';
const cache_port = 'port';
const cache_timeout = 'timeout';
const request = 'request';
const request_host = 'host';
const request_port = 'port';
const request_prefix = 'prefix';
const session = 'session';
const session_host = 'host';
const session_port = 'port';
const session_timeout = 'timeout';
const access_control = 'access_control';
const access_control_allows_origin = 'allows_origin';
const database = 'database';
const database_default = 'default';
const database_type = 'type';
const database_port = 'port';
const database_user = 'user';
const database_password = 'password';
const database_database = 'database';
// default configuration
private static $data = [
self::common=>[
self::common_debug_mode=>false,
self::common_timezone=>'Asia/Seoul',
],
self::session=>[
self::session_timeout=>86400
],
self::access_control=>[
self::access_control_allows_origin=>'*'
],
self::request_body=>[]
];
/**
* @return string
*/
public static function version() { return self::$data[self::version]; }
/**
* @return string
*/
public static function module() { return self::$data[self::module]; }
/**
* @return string
*/
public static function proc() { return self::$data[self::proc]; }
/**
* @return string
*/
public static function method() { return self::$data[self::method]; }
/**
* @return array
*/
public static function request_param() { return self::$data[self::request_body]; }
/**
* @return array
*/
public static function request_body() { return self::$data[self::request_body]; }
/**
* @return string
*/
public static function token() { return self::$data[self::request_body][self::request_body_token]; }
/**
* @return string
*/
public static function timezone() { return self::$data[self::common][self::common_timezone]; }
/**
* @return bool
*/
public static function debug_mode() { return self::$data[self::common][self::common_debug_mode]; }
/**
* @return string
*/
public static function host() { return self::$data[self::common][self::common_host]; }
/**
* @return array
*/
public static function cache() { return self::$data[self::cache]; }
/**
* @return string
*/
public static function cache_host() { return self::$data[self::cache][self::cache_host]; }
/**
* @return string
*/
public static function cache_port() { return self::$data[self::cache][self::cache_port]; }
/**
* @return int
*/
public static function cache_timeout() { return self::$data[self::cache][self::cache_timeout]; }
/**
* @return array
*/
public static function session() { return self::$data[self::session]; }
/**
* @return string
*/
public static function session_host() { return self::$data[self::session][self::session_host]; }
/**
* @return string
*/
public static function session_port() { return self::$data[self::session][self::session_port]; }
/**
* @return int
*/
public static function session_timeout() { return self::$data[self::session][self::session_timeout]; }
/**
* @param string $select
*
* @return array
*/
public static function request($select) { return self::$data[self::request][$select]; }
/**
* @param string $select
*
* @return string
*/
public static function request_host($select) { return self::$data[self::request][$select][self::request_host]; }
/**
* @param string $select
*
* @return string
*/
public static function request_port($select) { return self::$data[self::request][$select][self::request_port]; }
/**
* @param string $select
*
* @return bool
*/
public static function database($select=self::database_default)
{
$result = false;
if($v = self::$data[self::database][$select]) $result = $v;
return $result;
}
/**
* @return mixed
*/
public static function allows_origin() { return self::$data[self::access_control][self::access_control_allows_origin]; }
/**
* @return bool
*/
public static function check_origin()
{
$allows = self::allows_origin();
if($allows=='*') return true;
if(in_array(self::access_ip(),$allows)) return true;
return false;
}
/**
* Initialize default config
*
* @param bool $path
*
* @throws Exception
*/
public static function init($path=false)
{
// ---------------------------------------------------------------------
/// Load config
// ---------------------------------------------------------------------
if(!$path) $path = dirname(__FILE__).'/../cfg/'.self::filename;
if(!is_file($path))
throw new Exception("There is no config file.(".self::filename.")", rester_response::code_config);
$cfg = parse_ini_file($path,true, INI_SCANNER_TYPED);
// Extract access control
if($cfg[self::access_control])
{
$origin = $cfg[self::access_control][self::access_control_allows_origin];
if(strpos($origin,',')!==false)
{
$cfg[self::access_control][self::access_control_allows_origin] = explode(',', $origin);
}
}
array_walk_recursive($cfg, function(&$v) { $v = trim($v); });
foreach ($cfg as $section=>$values)
{
foreach($values as $kk=>$vv)
{
self::$data[$section][$kk] = $vv;
}
}
// ---------------------------------------------------------------------
/// Version
// ---------------------------------------------------------------------
if(preg_match('/^[0-9][0-9.]*$/i',$_GET[self::query_version],$matches))
{
self::$data[self::version] = $matches[0];
}
else
{
if($_GET[self::query_version]=='')
throw new Exception("Required version name.", rester_response::code_uri);
else
throw new Exception("Invalid version name.", rester_response::code_uri);
}
unset($_GET[self::query_version]);
// ---------------------------------------------------------------------
/// Module name
// ---------------------------------------------------------------------
if(preg_match('/^[a-z0-9-_]*$/i',strtolower($_GET[self::query_module]),$matches))
self::$data[self::module] = $matches[0];
else
throw new Exception("Invalid module name.", rester_response::code_uri);
unset($_GET[self::query_module]);
// ---------------------------------------------------------------------
/// Procedure name
// ---------------------------------------------------------------------
if(preg_match('/^[a-z0-9-_]*$/i',strtolower($_GET[self::query_proc]),$matches))
self::$data[self::proc] = $matches[0];
else
throw new Exception("Invalid procedure name.", rester_response::code_uri);
unset($_GET[self::query_proc]);
// ---------------------------------------------------------------------
/// Check method
// ---------------------------------------------------------------------
if($_SERVER['REQUEST_METHOD']=='POST' || $_SERVER['REQUEST_METHOD']=='GET')
{
self::$data[self::method] = strtolower($_SERVER['REQUEST_METHOD']);
}
else
{
throw new Exception("Invalid request METHOD.(Allowed POST)", rester_response::code_uri);
}
// ---------------------------------------------------------------------
/// Check ip
// ---------------------------------------------------------------------
$allows_origin = self::allows_origin();
if($allows_origin!='*')
{
$access_ip = self::access_ip();
if(!is_array($allows_origin)) $allows_origin = [$allows_origin];
if(!in_array($access_ip,$allows_origin))
throw new Exception("Access denied.(Not allowed ip address:{$access_ip})", rester_response::code_access_ip);
}
/**
* extract body parameter from json body, POST and GET
*
* json 으로 데이터가 넘어왔을 경우 (php://input)
* php://input 가 unset이 되지 않아 call_module 함수를 호출할 때에 파라미터 변경이 되지 않는 문게가 있었음
* get < json < post 순서로 덮어 씌우는 방식으로 해결함
*/
// Extract request parameter
// Json, POST, GET
$json = json_decode(file_get_contents('php://input'),true);
if(!$json) $json = [];
if(!is_array($_POST)) $_POST = [];
if(!is_array($_GET)) $_GET = [];
self::$data[self::request_body] = $_GET;
foreach($json as $k=>$v)
{
self::$data[self::request_body][$k] = $v;
}
foreach($_POST as $k=>$v)
{
self::$data[self::request_body][$k] = $v;
}
unset($_POST);
unset($_GET);
}
/**
* @return string
*/
protected static function access_ip()
{
// Check allows ip address
// Check ip from share internet
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$access_ip=$_SERVER['HTTP_CLIENT_IP'];
}
//to check ip is pass from proxy
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$access_ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$access_ip=$_SERVER['REMOTE_ADDR'];
}
return $access_ip;
}
/**
* return config
*
* @param string $section
* @param string $key
*
* @return array|string
*/
public static function get($section='', $key='')
{
if($section==='') return self::$data;
if($section && $key) return self::$data[$section][$key];
return self::$data[$section];
}
}