-
Notifications
You must be signed in to change notification settings - Fork 3
/
inc.php
executable file
·648 lines (578 loc) · 17.1 KB
/
inc.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
<?php
function captcha($code=null){
if( 0 == func_num_args()){
require LIB.'/captcha/simple-php-captcha.php';
$_SESSION['captcha'] = simple_php_captcha();
return $_SESSION['captcha']['image_src'];
}else{
return strtolower($code) == strtolower($_SESSION['captcha']['code']);
}
}
function remove_dupchar($str,$char,$time=2){
return preg_replace_callback('#(['.preg_quote($char).']{'.$time.',})#',
function($m){
return $m[1]{1};
}
,$str);
}
function multi_require($files,$ext='php'){
$work_dir = empty(dirname(debug_backtrace()[0]['file'])) ? '' : dirname(debug_backtrace()[0]['file']).'/';
array_map(function($file)use($ext,$work_dir){
require $work_dir.$file.($ext? '.'.$ext : '');
},$files);
}
function html($file_name){
foreach((array)$file_name as $f){
include HTML.'/'.$f.'.html';
}
}
function array_pick($keys,$array){
foreach($keys as $k)
$pick[$k] = $array[$k];
return $pick;
}
function config($key='',$value=''){
$config = read_array(ROOT.'/config.php');
switch (func_num_args()){
case 0:
return $config;
break;
case 1:
return @$config[$key];
break;
case 2:
$config[$key] = $value;
array_save(ROOT.'/config.php',$config);
break;
}
}
function array_save($file_name,$array=[]){
$code = output_capture(function()use($array){
var_export($array);
});
file_put_contents($file_name,<<<PHP
<?php
return $code;
PHP
);
}
function read_array($file_name){
$array = include $file_name;
$array === 1 && $array = [];
return (array)$array;
}
function output_capture($output_code){
ob_start();
$output_code();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
function pdo(){
static $db = null;
!$db
&& ($db = new PDO('mysql:dbname='.config()['db_name'].';host='.config()['db_host'],config()['db_user'],config()['db_password']))
&& $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION)
&& $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
return $db;
}
function error($msg=''){
$msg = 'File: '.debug_backtrace()[0]['file'].' Line: '.debug_backtrace()[0]['line'];
echo is_ajax() ? json_encode(['status'=>0,'msg'=>$msg]) : $msg;
}
function db($prepare='',$params=[],$bool=0){
if(!is_array($params)){
$params = [$params];
}
if(!$prepare)
return pdo();
$q = db()->prepare($prepare);
$status = $q->execute($params);
if($bool == 0){
return $q;
}elseif($bool == 1){
return $status;
}elseif($bool == 2){
return pdo();
}
}
function is_ajax(){
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function db_clear(){
array_map(function($table_name){
db("delete from $table_name");
},array_column(db("show tables")->fetchAll(),'Tables_in_'.config('db_name')));
}
function times($time,$func){
for($i=0;$i<$time;$i++){
$func();
}
}
function bool2int($data){
if($data === true){
return 1;
}elseif($data === false){
return 0;
}elseif(is_array($data)){
foreach($data as $k=>$v){
if($v === true){
$data[$k] = 1;
}elseif($v === false){
$data[$k] = 0;
}elseif(is_array($v)){
$data[$k] = bool2int($v);
}
}
}
return $data;
}
function call_user_func_hash($function_name,$hash){
$arg = [];
foreach(is_array($function_name)
? (new ReflectionClass($function_name[0]))->getMethod($function_name[1])->getParameters()
:(new ReflectionFunction($function_name))->getParameters() as $param){
$arg[] = @$hash[$param->name];
}
return call_user_func_array($function_name,$arg);
}
function google_auth(){
require_once LIB.'/auth/PHPGangsta/GoogleAuthenticator.php';
return new PHPGangsta_GoogleAuthenticator();
}
function google_auth_create_secret(){
return google_auth()->createSecret();
}
function google_auth_qr_url(){
return preg_replace('#^https#','http',google_auth()->getQRCodeGoogleUrl(config('text_logo'),google_auth_current_user_secret()));
}
function google_auth_code(){
return google_auth()->getCode(google_auth_current_user_secret());
}
function google_auth_verify($code){
return google_auth()->verifyCode(google_auth_current_user_secret(),$code,2);
}
function google_auth_current_user_secret(){
return db("select * from user where id=?",[$_SESSION['user_id']])->fetch()['secret'];
}
function btc(){
static $btc = null;
require_once LIB.'/btc/bitcoin.inc';
$btc = $btc ?: new BitcoinClient(config('btc_protocol'),config('btc_user'),config('btc_password'),config('btc_host'),config('btc_port'),'',0);
return $btc;
}
function user_info($id=null){
if(!$id && empty($_SESSION['user_id']))
return;
$id = $id ?: $_SESSION['user_id'];
return array_hash_only(db("select * from user where id=?",[$id])->fetch());
}
function array_hash_only($array){
foreach($array as $k=>$v){
if(is_numeric($k))
unset($array[$k]);
}
return $array;
}
function receive_btc(){
db()->exec('lock tables user write');
if($received = btc()->getreceivedbyaddress(user_info()['address'],0) - user_info()['received']){
db("update user set btc=btc+?,received=received+? where id=?",[$received,$received,$_SESSION['user_id']],1);
}
db()->exec('unlock tables');
}
function db_lock($tables){
$tables = is_array($tables) ? $tables : [$tables];
$tables = join(',', array_map(function($t){
return $t.' write';
},$tables));
db()->exec('set autocommit=0');
db()->exec('lock tables '.$tables);
}
function db_unlock(){
db()->exec('commit');
db()->exec('unlock tables');
}
function db_rollback(){
db()->exec('rollback');
}
function pager($count,$per_page=20){
$cur_page = empty($_GET['p']) ? 1 : $_GET['p'];
$total_page = ceil($count/$per_page);
if($total_page <= 1)
return '';
$page_url = function($p){
$url = parse_url($_SERVER['REQUEST_URI']);
$query=[];
parse_str(@$url['query'],$query);
$query = array_merge($query,['p'=>$p]);
return $url['path'].'?'.http_build_query($query);
};
$pre_url = $cur_page > 1 ? $page_url($cur_page-1) : '';
$next_url = $cur_page < $total_page ? $page_url($cur_page+1) : '';
$ul_start = '<ul class="pager">';
$ul_end = '</ul>';
$html = $ul_start
.($pre_url ? '<li><a href="'.$pre_url.'">上一页</a></li>' : '<li class="disabled"><a href="#">上一页</a></li>')
.($next_url ? '<li><a href="'.$next_url.'">下一页</a></li>' : '<li class="disabled"><a href="#">下一页</a></li>')
.$ul_end;
return $html;
}
function run_deal(){
db_lock(['user','deal','trade']);
for(;;){
$buy = db("select * from trade where type=0 and status=0 order by price desc")->fetch();
$sell = db("select * from trade where type=1 and status=0 order by price asc")->fetch();
if($buy && $sell && floatval($buy['price']) - floatval($sell['price']) >= 0){
$amount = min($buy['amount']-$buy['deal'],$sell['amount']-$sell['deal']);
if(!(db("update trade set deal=deal+? where id in (?,?)",[$amount,$buy['id'],$sell['id']],1) &&
db("update user set rmb_frozen=rmb_frozen-?,btc=btc+? where id=?",[$amount*$buy['price'],$amount,$buy['user_id']],1) &&
db("update user set btc_frozen=btc_frozen-?,rmb=rmb+? where id=?",[$amount,$amount*$sell['price'],$sell['user_id']],1) &&
db("insert into deal(buy_trade,sell_trade,ctime,amount)values(?,?,?,?)",[$buy['id'],$sell['id'],time(),$amount],1) &&
db("update trade set status=1 where amount=deal",[],1))){
db_rollback();
}
}else{
break;
}
}
db_unlock();
}
function btcval($number){
return floatval(number_format(abs($number),config('btc_decimal'),'.',''));
}
function _btcval(&$number){
$number = btcval($number);
return $number;
}
function rmbval($number){
return floatval(number_format(abs($number),2,'.',''));
}
function _rmbval(&$number){
$number = rmbval($number);
return $number;
}
function ohlc(){
$ohlc = [];
$o = db("select price,floor(deal.ctime/60) min from deal inner join trade on deal.buy_trade=trade.id inner join (select min(id) id from deal group by floor(ctime/60)) b on deal.id=b.id")->fetchAll();
$h = db("select max(price) price,floor(deal.ctime/60) min from deal inner join trade on deal.buy_trade=trade.id group by floor(deal.ctime/60)")->fetchAll();
$l = db("select min(price) price,floor(deal.ctime/60) min from deal inner join trade on deal.buy_trade=trade.id group by floor(deal.ctime/60)")->fetchAll();
$c = db("select price,floor(deal.ctime/60) min from deal inner join trade on deal.buy_trade=trade.id inner join (select max(id) id from deal group by floor(ctime/60)) b on deal.id=b.id")->fetchAll();
$d = db("select sum(amount) amount,ctime,floor(ctime/60) min from deal group by floor(ctime/60)")->fetchAll();
$o = array_pk($o,'min');
$h = array_pk($h,'min');
$l = array_pk($l,'min');
$c = array_pk($c,'min');
$d = array_pk($d,'min');
foreach($o as $v){
$ohlc[] = [$d[$v['min']]['ctime']*1000,
$d[$v['min']]['amount'],
$o[$v['min']]['price'],
$h[$v['min']]['price'],
$l[$v['min']]['price'],
$c[$v['min']]['price']];
}
return $ohlc;
}
function array_pk($array,$pk){
$n_array = [];
foreach($array as $v){
$n_array[$v[$pk]] = $v;
}
return $n_array;
}
function send_mail($email,$title,$body){
require_once LIB.'/mail/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = config('smtp_host');
$mail->Port = config('smtp_port');
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = config('smtp_user');
$mail->Password = config('smtp_password');
$mail->setFrom(config('smtp_user'),config('text_logo'));
$mail->addAddress($email,$email);
$mail->Subject = $title;
$mail->msgHTML($body);
return $mail->send();
}
function array_remove_keys($array,$keys){
$keys = is_array($keys) ? $keys : [$keys];
foreach($array as $k=>$v){
if(in_array($k,$keys))
unset($array[$k]);
}
return $array;
}
function array_remove_number_keys($array){
foreach($array as $k=>$v){
if(preg_match('#^[0-9]+$#',$k))
unset($array[$k]);
}
return $array;
}
function array_trim($array){
foreach($array as $k=>$v){
$array[$k] = is_array($v) ? array_trim($v) : trim($v);
}
return $array;
}
function get_external_ip(){
return file_get_contents('http://ipecho.net/plain');
}
function date_full($timestamp){
return date('Ymd H:i:s',$timestamp);
}
function array_map_suf($arr,$suf){
return array_map(function($v)use($suf){
return $v.$suf;
},$arr);
}
function array_map_pre($arr,$pre){
return array_map(function($v)use($pre){
return $pre.$v;
},$arr);
}
function db_curd(){
array_map(function($t){
$read = <<<PHP
function $t(\$arr){
if(!is_array(\$arr)){
\$arr = [table_pk('$t')=>\$arr];
}
return db("select * from $t where ".implode(' and ',array_map_suf(array_keys(\$arr),'=?')),array_values(\$arr))->fetch();
}
PHP
;
eval($read);
$add = <<<PHP
function {$t}_add(\$arr){
if(db("insert into $t (".implode(',',array_keys(\$arr)).")values(".implode(',',array_fill(0,count(\$arr),'?')).")",array_values(\$arr),1)){
return db()->lastInsertId();
}
}
PHP
;
eval($add);
$edit = <<<PHP
function {$t}_edit(\$arr){
\$sql = "update $t set ".
rtrim(
implode(
'',array_map_suf(
array_map_pre(
array_keys(
array_remove_keys(
\$arr,table_pk('$t')
)
)
,' '),'=?,'
)
),','
)
." where ".table_pk('$t')."=?";
return db(\$sql,
array_merge(
array_values(
array_remove_keys(
\$arr,table_pk('$t'))),
[\$arr[table_pk('$t')]]),1);
}
PHP
;
eval($edit);
$del = <<<PHP
function {$t}_del(\$pk){
return db("delete from $t where ".
table_pk('$t').
'=?',[\$pk],1);
}
PHP
;
eval($del);
},array_column(db("show tables")->fetchAll(),'Tables_in_'.config('db_name')));
}
function table_pk($table_name){
foreach(db("show columns from $table_name")->fetchAll() as $v){
if($v['Key'] == 'PRI')
return $v['Field'];
}
}
function mid(){
return intval(@$_SESSION['user_id']);
}
function msg($msg=''){
$_SESSION['msg'] = $msg;
jump(url('msg'));
}
function jump($url=''){
$url = $url ?: $_SERVER['HTTP_REFERER'];
header('location:'.$url);
exit;
}
function db_page($sql,$arg=[],$per=20){
$GLOBALS['pager'] = pager(db(preg_replace('#select .*? from #','select count(*) as count from ',$sql),$arg)->fetch()['count'],$per);
return db($sql.' limit '.(intval(@$_REQUEST['p']?:1) - 1).','.$per,$arg)->fetchAll();
}
function url($act,$arg=[]){
if(is_array($act)){
$arg = $act;
$act = ACT;
}
ADMIN && ($arg['admin'] = 1);
return 'http://'.$_SERVER['HTTP_HOST'].'/?'. http_build_query(array_merge(['act'=>$act],$arg));
}
function form_text($arg){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
return <<<HTML
<div class="form-group">
<label class="col-md-{$arg['col'][0]} control-label" for="{$arg['id']}">
{$arg['label']}
</label>
<div class="col-md-{$arg['col'][1]}">
<input type="text" class="form-control" id="{$arg['id']}" name="{$arg['id']}" value="{$arg['value']}" />
</div>
</div>
HTML
;
}
function form_submit($arg){
if(!is_array($arg))
$arg = ['text'=>$arg];
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
$arg['text'] = $arg['text']?:'Submit';
return <<<HTML
<div class="form-group">
<div class="col-md-{$arg['col'][1]} col-md-offset-{$arg['col'][0]}">
<button class="btn btn-primary">
{$arg['text']}
</button>
</div>
</div>
HTML
;
}
function form_static($arg){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
return <<<HTML
<div class="form-group">
<label class="control-label col-md-{$arg['col'][0]}" for="{$arg['id']}">
{$arg['label']}
</label>
<div class="col-md-{$arg['col'][1]}">
<p class="form-control-static" id="{$arg['id']}">
{$arg['value']}
</p>
</div>
</div>
HTML
;
}
function form_password($arg){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
return <<<HTML
<div class="form-group">
<label class="col-md-{$arg['col'][0]} control-label" for="{$arg['id']}">
{$arg['label']}
</label>
<div class="col-md-{$arg['col'][1]}">
<input type="password" class="form-control" id="{$arg['id']}" value="{$arg['value']}" name="{$arg['id']}" />
</div>
</div>
HTML
;
}
function form_checkbox($arg){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
$html = <<<HTML
<div class="form-group">
<label class="col-md-{$arg['col'][0]} control-label">
{$arg['label']}
</label>
HTML
;
foreach($arg['item'] as $v){
$checked = $v['checked'] ? 'checked' : '';
$html .= <<<HTML
<label class="control-label">
<input type="checkbox" name="{$arg['id']}[]" value="{$v['value']}" $checked />
{$v['label']}
</label>
HTML
;
}
$html .= <<<HTML
</div>
HTML
;
return $html;
}
function form_textarea($arg){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
$arg['rows'] = $arg['rows'] ?: 3;
return <<<HTML
<div class="form-group">
<label class="control-label col-md-{$arg['col'][0]}" for="{$arg['id']}">
{$arg['label']}
</label>
<div class="col-md-{$arg['col'][1]}">
<textarea class="form-control" id="{$arg['id']}" name="{$arg['id']}" rows="{$arg['rows']}">{$arg['value']}</textarea>
</div>
</div>
HTML
;
}
function form_hidden($arg){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
return <<<HTML
<input type="hidden" name="{$arg['id']}" value="{$arg['value']}" id="{$arg['id']}" />
HTML
;
}
function form_captcha($arg=null){
$arg['col'] = $GLOBALS['COL'] = $arg['col']?:$GLOBALS['COL'];
$arg['captcha'] = $arg['captcha']?:captcha();
return <<<HTML
<div class="form-group">
<div class="col-md-{$arg['col'][1]} col-md-offset-{$arg['col'][0]}">
<img src="{$arg['captcha']}">
</div>
</div>
HTML
;
}
function output($content){
echo is_ajax() ? json_encode(bool2int($content)) : $content;
}
function v_email($email){
return 1 == preg_match('/[a-z0-9]+@[a-z0-9]+\.[a-z0-9]+/',$email);
}
function v_assert($validators){
$validators = is_array($validators) ? $validators : [$validators];
foreach($validators as $v){
if(is_callable($v)){
if(($msg = $v) !== true){
v_msg($msg);
}
}else{
if($v !== true){
v_msg($v);
}
}
}
}
function v_msg($msg){
output(['msg'=>$msg]);
exit;
}
function _trim(&$str,$char=" \t\n\r\0\x0B"){
$str = trim($str,$char);
return $str;
}
function _intval(&$number){
$number = intval($number);
return $number;
}