-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathlinb.php
484 lines (447 loc) · 13.7 KB
/
linb.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
if(function_exists('date_default_timezone_set')){
date_default_timezone_set('UTC');
}
/*
* phpLinb 1.2
* Copyright(c) 2008 Yingbo Li(linb.net, linb.net[at]gmail.com).
* GPL3 (http://www.opensource.org/licenses/gpl-3.0.html) licenses.
*/
/**
* __autoload class
* @author rambolee <linb.net@gmail.com>
**/
spl_autoload_register(function($class){
try{
$path = LINB::$DIR_LINB.LINB::$DIR_CLASS.str_replace('_', DIRECTORY_SEPARATOR, str_replace('.', DIRECTORY_SEPARATOR, $class)).'.php';
if(!file_exists($path)){
$path = LINB::$DIR_APP.LINB::$DIR_CLASS.str_replace('_', DIRECTORY_SEPARATOR, str_replace('.', DIRECTORY_SEPARATOR, $class)).'.php';
}
if(file_exists($path)){
include_once($path);
} else {
return eval("
class $class{
function __construct(\$a=0, \$b=0, \$c=0, \$d=0, \$e=0, \$f=0, \$g=0, \$h=0, \$i=0){
throw new LINB_E('Class $class could not be found.');
}
}
");
}
}catch (LINB_E $e){
throw new LINB_E($e->getMessage(), $e->getCode());
}
});
/**
* Exception class for LINB
* @author rambolee <linb.net@gmail.com>
**/
class LINB_E extends Exception
{
/**
* constructor
*
* @param string $message
* @param int $code
*
* @return LINB_E
*/
function __construct ($message = '', $code = 0, $file = '', $line = -1) {
parent::__construct($message, $code);
}
function _handle_exception(Exception $e) {
LINB::echoException($e->getCode(), $e->getMessage());
}
function _handle_error($errno, $errstr, $errfile, $errline) {
LINB::echoException($errno, $errstr, $errfile, $errline);
}
};
$err = new LINB_E();
set_error_handler (array ($err, '_handle_error'));
set_exception_handler(array($err, "_handle_exception"));
unset($err);
/**
* LINB base class
*
* @author rambolee <linb.net@gmail.com>
**/
class LINB
{
/**
* logic unit abstrct class name
*/
const UNIT = "Unit";
/**
* request data symbol: callback
*/
const SYM_CALLBACK = "callback";
/**
* request data symbol: hash
*/
const SYM_HASH = "hash";
/**
* request data symbol: data
* for exception in __autoload
*/
const SYM_DATA = "data";
/**
* request data symbol: error
*/
const SYM_ERR = "error";
/**
* request data symbol: error
*/
const SYM_ID = "id";
const SYM_MESSAGE = "message";
/**
* request data sub symbol: key
*
*/
const SYM_KEY = "key";
/**
* request data sub symbol: parameter
*
*/
const SYM_PARA = "paras";
const MAX_LEN = 800;
/**
* Request data backup
*
*/
public static $data;
/**
* for switch debug
*
*/
public static $debug;
/**
* The path of LINB.PHP
*/
public static $DIR_LINB;
/**
* path for main app
*/
public static $DIR_APP;
/**
* The root path of class files
*/
public static $DIR_CLASS;
/**
* JSON object
*/
public static $json;
/**
* object hash table for straight call
*/
private static $H = array();
/**
* type strict
*
* @param string $type
* @param mix $v
* @param mix $default
* @return mix
*/
public static function toStrict($type, $v, $default){
$map = array(
'string' => array('is_string',''),
'integer' => array('is_integer',0),
'double' => array('is_float',0.0),
'boolean' => array('is_bool',false),
'array' => array('is_array',array()),
'object' => array('is_object', new stdClass())
);
if(!in_array($type, array_keys($map))){
throw new LINB_E('$type is not a valid type.');
}
$r=$map[$type][1];
if(isset($v) || $map[$type][0]($v)){
$r=$v;
}else{
if(isset($default) || $map[$type][0]($default)){
$r = $default;
}
}
return $r;
}
public static function toType($v, $type){
switch ($type){
case 'string' :
return (string)$v;
case 'integer' :
return (integer)$v;
case 'double' :
return (double)$v;
case 'boolean' :
return (boolean)$v;
case 'array' :
return (array)$v;
case 'object' :
return (object)$v;
default:
throw new LINB_E('No such type: $type!');
}
}
/**
* check arguemnts
*
* @param arary $h
* @param array $a
* array(
* 'string' => array(
* 'id' => NULL,
* 'action' => ''
* )
* )
*/
public static function checkArgs(&$h, $a){
if(is_object($h)){
$o = (array)$h;
}else{
$o = & $h;
}
foreach ($a as $k=>$v){
foreach ($v as $k2=>$v2){
if(is_null($v2)){
if(isset($o[$k2])){
if(gettype($o[$k2]) != $k)
$o[$k2] = self::toType($o[$k2], $k);
}else
throw new LINB_E($k2." must be specified!");
}else{
$o[$k2] = self::toStrict($k, isset($o[$k2])?$o[$k2]:NULL, $v2 );
}
}
}
if(is_object($h)){
foreach ($o as $k=>$v){
$h->$k = $v;
}
}
}
/**
* Parse a template to string
*
* @param string $template
* tag: {tag}
* or
* tag pairs: {tag} string... {/tag}
* @param array $data
* array(
* 'a' => 'a',
* array(
* array('i'=>'i','j1'=>'j1'),
* array('i'=>'i','j2'=>'j2'),
* array('i'=>'i','j3'=>'j3')
* )
* )
* @return string
*/
public static function parseTemplate($template, $data, $tag_l = '{', $tag_r = '}'){
$str = $template;
if(is_object($data))
$data=(array)$data;
foreach ($data as $key => $val){
if ( ! is_array($val)){
$str = str_replace($tag_l.$key.$tag_r, $val, $str);
}else{
if (preg_match("|".$tag_l.$key.$tag_r."(.+)".$tag_l.'/'.$key.$tag_r."|s", $template, $match)) {
$str2 = '';
foreach ($val as $item){
$str2 .= LINB::parseTemplate($match['1'], $item);
}
$str = str_replace($match['0'], $str2, $str);
}
}
}
return $str;
}
/**
* Staight call
*
* @param string $key
* @return object
*/
public static function SC($key, $new=false){
if ($new || !isset(self::$H[$key])) {
try{
if ($new)
return new $key();
else
self::$H[$key] = new $key();
}catch(LINB_E $e){
throw $e;
}
}
return self::$H[$key];
}
/**
* stimulate
*
* @param object $hash
* @return object
*/
public static function stimulate(&$hash){
$key = self::SYM_KEY;
$paras = self::SYM_PARA;
if(!is_object($hash)){ throw new LINB_E('Input data format error!');}
if(!isset($hash->$key)){ throw new LINB_E('Input data must include key!');}
$key = $hash->$key;
$o = self::SC($key);
if(is_subclass_of($o,self::UNIT)){
return $o->stimulate($hash->$paras);
}else{ throw new LINB_E('$key is not a substantial class of Unit!'); }
}
/**
* handle http post or get request
* html get/post : key=xxx¶s={...}
* cgi get/pos :{key:xxx,paras:{...}}
* there must be a 'key' included.
*
*/
public static function handler(){
// try{
$httpdata=new stdClass;
$data = self::SYM_DATA;
$paras = self::SYM_PARA;
//"post" request
//post a=b$c=d
if(count($_POST)>0){
foreach ($_POST as $k=>$v)
$httpdata->$k = ini_set("magic_quotes_runtime",0)?stripslashes($v):$v;
//post {a:'b',c:'d'}
//or xmlhttp post
}else{
//get string post next
$request = file_get_contents('php://input');
if($request){
$request = LINB::$json->decode($request);
foreach ($request as $k=>$v)
$httpdata->$k = is_string($v)?ini_set("magic_quotes_runtime",0)?stripslashes($v):$v:$v;
}
}
//"get" request
$request = $_SERVER['QUERY_STRING'];
//get ?a=b$c=d
if($request){
if(strstr($request,'=')!==false){
foreach ($_GET as $k=>$v)
$httpdata->$k = ini_set("magic_quotes_runtime",0)?stripslashes($v):$v;
//get ?{a:'b',c:'d'}
}else{
$request = LINB::$json->decode(rawurldecode($request));
foreach ($request as $k=>$v)
$httpdata->$k = is_string($v)?ini_set("magic_quotes_runtime",0)?stripslashes($v):$v:$v;
}
}
if($_SERVER['QUERY_STRING']){
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
}
if(isset($httpdata->$paras)){
if(is_string($httpdata->$paras))
$httpdata->$paras = LINB::$json->decode($httpdata->$paras);
// for __autoload
LINB::$data = &$httpdata;
$d = self::stimulate($httpdata);
if(isset($d))
echo LINB::formatResponse($d);
}
// }catch(LINB_E $e){
// throw new LINB_E($e->getMessage(), $e->getCode());
// }
}
public static function formatResponse($d, $ok=true){
$data = self::SYM_DATA;
$hash = self::SYM_HASH ;
$callback = self::SYM_CALLBACK;
$err = self::SYM_ERR;
$key = self::SYM_KEY;
$paras = self::SYM_PARA;
$httpdata = &LINB::$data;
if(isset($httpdata->$callback))
$cb=$httpdata->$callback;
unset($httpdata->$key);
unset($httpdata->$paras);
unset($httpdata->$callback);
if($ok)
$httpdata->$data = $d;
else
$httpdata->$err = $d;
$output=LINB::$json->encode($httpdata);
//use script tag
if(isset($cb)){
if($cb=="window.name"){
$output="<script type='text' id='json'>".$output."</script><script type='text/javascript'>window.name=document.getElementById('json').innerHTML;</script>";
}else if(strpos($cb,":")){
$output="<script type='text' id='json'>".$output."</script><script type='text/javascript'>parent.postMessage(document.getElementById('json').innerHTML,'".$cb."'.replace( /([^:]+:\/\/[^\/]+).*/, '$1'));</script>";
}else{
$output = $cb.'('.$output.')';
}
}
return $output;
}
public static function echoException($eid, $e, $file='', $line=-1){
$id = LINB::SYM_ID;
$msg = LINB::SYM_MESSAGE;
if($e instanceof Exception){
$file = $e->getFile();
$line = $e->getLine();
$e = $e->getMessage();
}
if(LINB::$debug)
$e = $e." at ".$file."(".$line.")";
$d = array( $id => $eid, $msg => $e);
echo LINB::formatResponse($d,false );
//only the first error will return to browser
exit();
}
}
/**
* logic unit abstrct class
*
*/
abstract class Unit
{
/**
* for logic unit interface
*
* @param class $hash
*/
abstract public function stimulate(&$hash);
}
/**
* shortcut for LINB::SC
*
* @param string $key
* @return object
*/
function SC($key, $new=false){
return LINB::SC($key, $new);
}
///////////////
/////running functions
///////////////
// ini the object of LINB
LINB::$debug = true;
LINB::$DIR_LINB = dirname(__FILE__).DIRECTORY_SEPARATOR;
LINB::$DIR_APP = realpath('.').DIRECTORY_SEPARATOR;
LINB::$DIR_CLASS = 'phpClass'.DIRECTORY_SEPARATOR;
//for php 5.22 json enabled
if(function_exists("json_encode")){
class PHPJSON{
function encode($var){
return json_encode($var);
}
function decode($var){
return json_decode($var);
}
}
LINB::$json = new PHPJSON;
}else
LINB::$json = new JSON;
// handle http request
LINB::handler();
?>