/home/mjc1/public_html/xpay/lgdacom/XPayClient.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
<?php
class XPayClient
{
    var 
$ch;
    var 
$debug false;
    var    
$bTest false;
    var 
$error_msg;
    var 
$home_dir;
    var 
$mode;
    var 
$TX_ID;
    var    
$MID;
    var 
$Auth_Code;
    var 
$config;
    var 
$Post = array();
    var    
$response_json;
    var 
$response_array;
    var 
$response_code;
    var 
$response_msg;
    var    
$log_file;
    var 
$err_label = array("FATAL","ERROR","WARN ","INFO ","DEBUG");
    var 
$INFO = array("LGD_TXID","LGD_AUTHCODE","LGD_MID","LGD_OID","LGD_TXNAME","LGD_PAYKEY","LGD_RESPCODE","LGD_RESPMSG");
    var 
$DEBUG = array("LGD_TXID","LGD_AUTHCODE","LGD_MID","LGD_TID","LGD_OID","LGD_PAYTYPE","LGD_PAYDATE","LGD_TXNAME","LGD_PAYKEY","LGD_RESPCODE","LGD_RESPMSG");
    function 
IsAcceptLog($ParamName,$LogLevel)
    {
        if(
LGD_LOG_DEBUG == $LogLevel)
        {
            if (
in_array($ParamName,$this->DEBUG,true)) return true;
        }
        else if(
LGD_LOG_INFO == $LogLevel)
        {
            if (
in_array($ParamName,$this->INFO,true)) return true;
        }
        return 
false;
    }
    function 
XPayClient($home_dir,$mode="real")
    {
        if(!
function_exists('json_decode'))
        {
            function 
json_decode($content,$assoc=false)
            {
                require_once 
'JSON.php';
                if ( 
$assoc )
                {
                    
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
                }
                else 
                {
                    
$json = new Services_JSON;
                }
                return 
$json->decode($content);
            }
        }
        
define("LGD_USER_AGENT","XPayClient (4.0.0.0/PHP)");
        
define("LGD_LOG_FATAL",0);
        
define("LGD_LOG_ERROR",1);
        
define("LGD_LOG_WARN",2);
        
define("LGD_LOG_INFO",3);
        
define("LGD_LOG_DEBUG",4);
        
define("LGD_ERR_NO_HOME_DIR","10001");
        
define("LGD_ERR_NO_MALL_CONFIG","10002");
        
define("LGD_ERR_NO_LGDACOM_CONFIG","10003");
        
define("LGD_ERR_NO_MID","10004");
        
define("LGD_ERR_OUT_OF_MEMORY","10005");
        
define("LGD_ERR_NO_SECURE_PROTOCOLS","10007");
        
define("LGD_ERR_HTTP_URL","20001");
        
define("LGD_ERR_RESOLVE_HOST","20002");
        
define("LGD_ERR_RESOLVE_PROXY","20003");
        
define("LGD_ERR_CONNECT","20004");
        
define("LGD_ERR_WRITE","20005");
        
define("LGD_ERR_READ","20006");
        
define("LGD_ERR_SEND","20007");
        
define("LGD_ERR_RECV","20008");
        
define("LGD_ERR_TIMEDOUT","20009");
        
define("LGD_ERR_SSL","20101");
        
define("LGD_ERR_CURL","20201");
        
define("LGD_ERR_JSON_DECODE","40001");
        if(!isset(
$_SESSION))
        {
            
session_start();
        }
        
$this->home_dir $home_dir;
        if (!
file_exists($home_dir)) 
        {
            
$this->response_code LGD_ERR_NO_HOME_DIR;
            
$this->response_msg "home_dir [".$home_dir."] does not exist";
            
trigger_error($this->response_msgE_USER_ERROR);    
        }
        else if (!
file_exists($home_dir."/conf/mall.conf")) 
        {
            
$this->response_code LGD_ERR_NO_MALL_CONFIG;
            
$this->response_msg "config file [".$home_dir."/conf/mall.conf] does not exist";
            
trigger_error($this->response_msgE_USER_ERROR);    
        }
        else if (!
file_exists($home_dir."/conf/lgdacom.conf")) 
        {
            
$this->response_code LGD_ERR_NO_LGDACOM_CONFIG;
            
$this->response_msg "config file [".$home_dir."/conf/lgdacom.conf] does not exist";
            
trigger_error($this->response_msgE_USER_ERROR);    
        }
        
$array1 parse_ini_file($home_dir "/conf/mall.conf");
        foreach(
$array1 as $name => $value)
        {
            
$tempValue $name;
            if( 
strpos($tempValue,"MID_") !== FALSE )
            {
                
$tempValue substr($tempValue,4,strlen($tempValue));
                
$name $tempValue;
                
$temparray = array($name => $value);
                
$array1 += $temparray;
                break;
            }
        }

        
$array2 parse_ini_file($home_dir "/conf/lgdacom.conf");
        
$this->config $array1 $array2;

        
$this->config["log_dir"] = $home_dir."/log"// mall.conf에 절대경로로 명시된것을 이곳을 통해서 상대경로로 바꿈

        
$this->log_file $this->config["log_dir"] . "/log_" date("Ymd") . ".log";
        if (!
file_exists($this->config["log_dir"])) 
        {
            
mkdir($this->config["log_dir"], "0777"true);
        }
        
$this->log("XPayClient initialize [".$home_dir."] [".$mode."]"LGD_LOG_INFO);
        if (
strtolower($mode) == "test"
        {
            
$this->bTest true;
            
$this->debug false;
        }
        
$this->init();
    }
    function 
array_push_associative(&$arr
    {
        
$args func_get_args();
        foreach (
$args as $arg
        {
            if (
is_array($arg)) 
            {
                foreach (
$arg as $key => $value
                {
                    
$arr[$key] = $value;
                    
$ret++;
                }
            }
            else
            {
                
$arr[$arg] = "";
            }
        }
        return 
$ret;
    }
    function 
init()
    {
        
$this->ch curl_init();
        
curl_setopt($this->chCURLOPT_USERAGENTLGD_USER_AGENT);
        
curl_setopt($this->chCURLOPT_FOLLOWLOCATIONtrue);
        
curl_setopt($this->chCURLOPT_ENCODING 'gzip, deflate');
        if (
$this->config["verify_cert"] == 0
        {
            
curl_setopt($this->chCURLOPT_SSL_VERIFYPEER0);
            
$this->log("Do not verify server Certificate"LGD_LOG_WARN);
        }
        if (
$this->config["verify_host"] == 0
        {
            
curl_setopt($this->chCURLOPT_SSL_VERIFYHOST0);
            
$this->log("Do not verify host Domain"LGD_LOG_WARN);
        }
        
// TLS 설정
        //; 512 (TLS1.1) , 2048 (TLS1.2) , 2560 (TLS1.0)
        //CURL_SSLVERSION_DEFAULT (0), CURL_SSLVERSION_TLSv1 (1), CURL_SSLVERSION_SSLv2 (2), 
        //CURL_SSLVERSION_SSLv3 (3), CURL_SSLVERSION_TLSv1_0 (4), CURL_SSLVERSION_TLSv1_1 (5) or CURL_SSLVERSION_TLSv1_2 (6). 
        
if( $this->config['default_secure_protocols'] == "512" 
        {
            
curl_setopt($this->chCURLOPT_SSLVERSION );
            
//echo "default_secure_protocols = " . $this->config['default_secure_protocols'] . "<br>";
        
}
        else if ( 
$this->config['default_secure_protocols'] == "2048" 
        {
            
curl_setopt($this->chCURLOPT_SSLVERSION );
            
//echo "default_secure_protocols = " . $this->config['default_secure_protocols'] . "<br>";
        
}
        else if ( 
$this->config['default_secure_protocols'] == "2560" 
        {
            
curl_setopt($this->chCURLOPT_SSLVERSION );
            
//echo "default_secure_protocols = " . $this->config['default_secure_protocols'] . "<br>";
        
}
        else 
        {
            
curl_setopt($this->chCURLOPT_SSLVERSION 0);
            
//echo "default_secure_protocols = " . $this->config['default_secure_protocols'] . "<br>";
        
}
        
curl_setopt($this->chCURLOPT_CAINFO$this->home_dir."/conf/ca-bundle.crt");
    }
    function 
Init_TX($MID)
    {
        if (
$this->config[$MID] == null
        {
            
$this->response_code LGD_ERR_NO_MID;
            
$this->response_msg "Key for MID [".$MID."] does not exist in mall.conf";
            
$this->log($this->response_msgLGD_LOG_FATAL);
            return 
false;
        }
        
$this->TX_ID $this->Gen_TX_ID($MID);
        
$this->MID $MID;
        
$this->Auth_Code $this->Gen_Auth_Code($this->TX_ID$MID);
        
$this->Post = array("LGD_TXID" => $this->TX_ID,"LGD_AUTHCODE" => $this->Auth_Code,"LGD_MID" => $MID);
        return 
true;
    }
    function 
GenerateGUID()
    {
        if (
function_exists('com_create_guid'))
        {
            return 
com_create_guid();
        }
        else
        {
            
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            
$charid strtoupper(md5(uniqid(rand(), true)));
            
$hyphen chr(45);
            
//$uuid = chr(123).substr($charid, 0, 8).$hyphen.substr($charid, 8, 4).$hyphen.substr($charid,12, 4).$hyphen.substr($charid,16, 4).$hyphen.substr($charid,20,12).chr(125);
            
$uuid $charid;
            return 
$uuid;
        }
    }
    function 
Get_Unique()
    {
        if(isset(
$_SESSION['tx_counter'])) $_SESSION['tx_counter'] = $_SESSION['tx_counter'] + 1;
        else 
$_SESSION['tx_counter'] = 1;
        return 
session_id().$_SESSION['tx_counter'].$this->GenerateGUID();
    }
    function 
Gen_TX_ID($MID)
    {
        
$now date("YmdHis");
        
$header $MID "-" $this->config['server_id'] . $now;
        
$tx_id $header sha1($header.$this->Get_Unique());
        return 
$tx_id;
    }
    function 
Gen_Auth_Code($tx_id$MID)
    {
        
$auth_code sha1($tx_id $this->config[$MID]);
        return 
$auth_code;
    }
    function 
Set($name$value)
    {
        
$this->Post[$name] = $value;
    }
    function 
Check_Auto_Rollback($errno)
    {
        
//CURLE_OPERATION_TIMEDOUT 
        
if ($errno == 28) return true;
        else return 
false;
    }
    function 
set_curl_error()
    {
        switch (
curl_errno($this->ch)) 
        {
        case 
1// CURLE_UNSUPPORTED_PROTOCOL:
        
case 3// CURLE_URL_MALFORMAT:
            
$this->response_code LGD_ERR_HTTP_URL;
            
$this->response_msg "URL error";
            break;
        case 
6// CURLE_COULDNT_RESOLVE_HOST:
            
$this->response_code LGD_ERR_RESOLVE_HOST;
            
$this->response_msg "Resolve host error";
            break;
        case 
5// CURLE_COULDNT_RESOLVE_PROXY:
            
$this->response_code LGD_ERR_RESOLVE_PROXY;
            
$this->response_msg "Resolve proxy error";
            break;
        case 
7// CURLE_COULDNT_CONNECT:
            
$this->response_code LGD_ERR_CONNECT;
            
$this->response_msg "Could not connect error";
            break;
        case 
23// CURLE_WRITE_ERROR:
            
$this->response_code LGD_ERR_WRITE;
            
$this->response_msg "Write error";
            break;
        case 
26// CURLE_READ_ERROR:
            
$this->response_code LGD_ERR_READ;
            
$this->response_msg "Read error";
            break;
        case 
55// CURLE_SEND_ERROR:
            
$this->response_code LGD_ERR_SEND;
            
$this->response_msg "Send error";
            break;
        case 
56// CURLE_RECV_ERROR:
            
$this->response_code LGD_ERR_RECV;
            
$this->response_msg "Recv error";
            break;
        case 
27// CURLE_OUT_OF_MEMORY:
            
$this->response_code LGD_ERR_OUT_OF_MEMORY;
            
$this->response_msg "Out of memory error";
            break;
        case 
28// CURLE_OPERATION_TIMEDOUT :
            
$this->response_code LGD_ERR_TIMEDOUT;
            
$this->response_msg "Timeout error";
            break;
        case 
35// CURLE_SSL_CONNECT_ERROR:
        
case 51// CURLE_PEER_FAILED_VERIFICATION:
        
case 53// CURLE_SSL_ENGINE_NOTFOUND:
        
case 54// CURLE_SSL_ENGINE_SETFAILED:
        
case 58// CURLE_SSL_CERTPROBLEM:
        
case 59// CURLE_SSL_CIPHER:
        
case 60// CURLE_SSL_CACERT:
        
case 64// CURLE_USE_SSL_FAILED:
        
case 66// CURLE_SSL_ENGINE_INITFAILED:
        
case 77// CURLE_SSL_CACERT_BADFILE:
        
case 80// CURLE_SSL_SHUTDOWN_FAILED:
        
case 82// CURLE_SSL_CRL_BADFILE:
        
case 83// CURLE_SSL_ISSUER_ERROR:
            
$this->response_code LGD_ERR_SSL;
            
$this->response_msg "SSL error";
            break;
        default:
            
$this->response_code LGD_ERR_CURL;
            
$this->response_msg "CURL error";
            break;
        }
        
$this->response_msg .= "; cURL error code = ".curl_errno($this->ch)." msg = ".curl_error($this->ch);
    }
    function 
TX($bRollbackOnError true)
    {
        
$bTX true;
        
$bRollback false;
        
$strRollbackReason "";
        
$bReporting false;
        
$bCheckURL false;
        
$strReportStatus "";
        
$strReportMsg "";
        if (
$bRollbackOnError
        {
            if (
$this->config['auto_rollback'] == 0$bRollbackOnError false;
        }
        
        if (
$this->bTest$url $this->config['test_url'];
        else 
$url $this->config['url'];
        
        
$Protocol parse_url($urlPHP_URL_SCHEME);
        if(
$Protocol == ""
        {
            
$url "https://" $url;
            
$bCheckURL true;
        }
        else if(
$Protocol == "http"
        {
            
$bCheckURL false;
        }
        else if(
$Protocol == "https")
        {
            
$bCheckURL true;
        }
        if(
$bCheckURL == true)
        {
            
$result $this->send_post_data($url$this->Postnull$this->config['timeout']);
        }
        else
        {
            
$result false;
            
$bRollback false;
            
$bReporting false;
            
$this->response_code LGD_ERR_HTTP_URL;
            
$this->response_msg "http protocol not supported.";
            
$this->log("TX failed: res code = ".$this->response_code."; msg = "$this->response_msgLGD_LOG_FATAL);
        }
        if (
$result == false
        {
            if(
$bCheckURL == true)
            {
                
$bTX false;
                
$this->set_curl_error();
                
$this->log("TX failed: res code = ".$this->response_code."; msg = "$this->response_msgLGD_LOG_FATAL);
                if (
$bRollbackOnError && $this->Check_Auto_Rollback(curl_errno($this->ch))) 
                {
                    
$bRollback true;
                    
$strRollbackReason "Timeout";
                }
            }
            else
            {
                
$bTX false;
            }
        }
        else 
        {
            
$http_res_code curl_getinfo($this->chCURLINFO_HTTP_CODE);
            if (
$http_res_code 200 || $http_res_code >= 300
            {
                
// http response error
                
$bTX false;
                
$this->response_code "".(30000 $http_res_code);
                
$this->response_msg "HTTP response code = ".$http_res_code;
                
$this->log("TX failed: res code = ".$this->response_code."; msg = "$this->response_msgLGD_LOG_FATAL);
                
// report
                
$bReporting true;
                
$strReportStatus "HTTP response ".$http_res_code;
                
$strReportMsg $result;
                if (
$bRollbackOnError && $http_res_code >= 500
                {
                    
// rollback
                    
$bRollback true;
                    
$strRollbackReason "HTTP ".$http_res_code;
                }
            }
            else 
            {
                
$this->response_json $result;
                
$this->response_array json_decode($this->response_jsontrue);
                if ((
$this->response_array == false) || (strlen($this->response_array["LGD_RESPCODE"]) == 0)) 
                {
                    
// JSON decode failed
                    
$bTX false;
                    
$bReporting true;
                    
$strReportStatus "JSON decode fail";
                    
$strReportMsg $result;
                    if (
$bRollbackOnError
                    {
                        
$bRollback true;
                        
$strRollbackReason "JSON decode fail";
                    }
                    
$this->log("JSON Decode failed"LGD_LOG_ERROR);
                    
$this->response_array = array();
                    
$this->response_code LGD_ERR_JSON_DECODE;
                    
$this->response_msg "JSON Decode Failed";
                }
                else 
                {
                    
$this->response_code $this->response_array["LGD_RESPCODE"];
                    if (
$this->config['output_UTF8'] == 1$this->response_msg $this->response_array["LGD_RESPMSG"];
                    else 
$this->response_msg iconv("utf-8""euc-kr"$this->response_array["LGD_RESPMSG"]);
                    
$this->log("Response Code=[".$this->response_code."], Msg=[".iconv("utf-8""euc-kr"$this->response_array["LGD_RESPMSG"])."], Count=".$this->Response_Count(), LGD_LOG_INFO);
                    
$keys $this->Response_Names();
                    for (
$i 0$i $this->Response_Count(); $i++) 
                    {
                        foreach(
$keys as $name
                        {
                            if(
$this->IsAcceptLog($name,LGD_LOG_DEBUG))
                            {
                                
$this->log("Response (".$name.", ".$i.") = ".$this->Response($name$i), LGD_LOG_DEBUG);
                            }
                        }
                    }
                }
            }
        }
        if (
$bRollback
        {
            
// try auto-rollback
            
$tx_id $this->TX_ID;
            
$code $this->response_code;
            
$msg $this->response_msg;
            
$this->init();
            
$this->Rollback($strRollbackReason);
            
// restore previous info
            
$this->TX_ID $tx_id;
            
$this->response_code $code;
            
$this->response_msg $msg;
        }
        if (
$bReporting
        {
            
$this->Report($strReportStatus$strReportMsg);
        }
        return 
$bTX;
    }
    function 
Report_TX()
    {
        
$url $this->config['aux_url'];
        
$result $this->send_post_data($url$this->Postnull$this->config['timeout']);
        if (
$result == false
        {
            
set_curl_error();
            
$this->log("Reporting failed: res code = ".$this->response_code."; msg = "$this->response_msgLGD_LOG_ERROR);
            return 
false;
        }
        
$http_res_code curl_getinfo($this->chCURLINFO_HTTP_CODE);
        if (
$http_res_code 200 || $http_res_code >= 300
        {
            
// http response error
            
$this->log("Reporting failed: HTTP response code = ".$http_res_codeLGD_LOG_ERROR);
            return 
false;
        }
        
$response_array json_decode($resulttrue);
        if (
$response_array == false
        {
            
// JSON decode failed
            
$this->log("Report JSON Decode failed"LGD_LOG_ERROR);
            return 
false;
        }
        
$response_code $response_array["LGD_RESPCODE"];
        
$response_msg iconv("utf-8""euc-kr"$response_array["LGD_RESPMSG"]);
        
$this->log("Report Response Code=[".$response_code."], Msg=[".$response_msg."]"LGD_LOG_INFO);
        return 
true;
    }
    function 
Patch_TX($filename)
    {
        
$url $this->config['aux_url'];
        
$tmp_file tempnam($this->home_dir."/conf""tmp");
        
$fp fopen($tmp_file"w");
        
$result $this->post_into_file($url$this->Post$fpnull$this->config['timeout']);
        
fclose($fp);
        if (
$result == false
        {
            
unlink($tmp_file);
            
$this->log("Patch failed: file = ".$filenameLGD_LOG_ERROR);
            return 
false;
        }
        else 
        {
            
$this->log("Patch success: file = ".$filenameLGD_LOG_INFO);
            
$type curl_getinfo($this->chCURLINFO_CONTENT_TYPE);
            if (
strncasecmp($type"text/plain"10) == 0
            {
                
$this->log("Patch success: Content-Type = ".$typeLGD_LOG_INFO);
                
copy($tmp_file$this->home_dir."/conf/".$filename);
                
unlink($tmp_file);
                return 
true;            
            }
            else 
            {
                
// error
                
$fp fopen($tmp_file"r");
                
$content fread($fp512);
                
$this->Report("Patch error : ".$filename$content);
                
fclose($fp);
                
unlink($tmp_file);                
            }
            return 
false;
        }
    }
    function 
Rollback($reason)
    {
        
$RBTX $this->TX_ID;
        
$this->Init_TX($this->MID);
        
$this->Set("LGD_TXNAME""Rollback");
        
$this->Set("LGD_RB_TXID"$RBTX);
        
$this->Set("LGD_RB_REASON"$reason);
        if (
$this->TX(false) == false) return false;
        if (
$this->response_code == "0000") return true;
        return 
false;
    }
    function 
Report($status$msg)
    {
        if (
$this->config['report_error'] != 1) return false;
        
$this->Init_TX($this->MID);
        
$this->Set("LGD_TXNAME""Report");
        
$this->Set("LGD_STATUS"$status);
        
$this->Set("LGD_MSG"$msg);
        return 
$this->Report_TX();
    }
    function 
Patch($filename)
    {
        
$this->Init_TX($this->MID);
        
$this->Set("LGD_TXNAME""Patch");
        
$this->Set("LGD_FILE"$filename);
        return 
$this->Patch_TX($filename);
    }
    function 
Response_Json()
    {
        return (
$this->response_json);
    }
    function 
Response_Count()
    {
        if (
$this->response_array["LGD_RESPONSE"] == null) return 0;
        return 
count($this->response_array["LGD_RESPONSE"]);
    }
    function 
Response_Code()
    {
        return (
$this->response_code);
    }
    function 
Response_Msg()
    {
        return (
$this->response_msg);
    }
    function 
Response_Names()
    {
        if (
$this->Response_Count() == 0) return null;
        return 
array_keys($this->response_array["LGD_RESPONSE"][0]);
    }
    function 
Response($name$index=0)
    {
        if (
$this->config['output_UTF8'] == 1) return ($this->response_array["LGD_RESPONSE"][$index][$name]);
        else return (
iconv("utf-8""euc-kr"$this->response_array["LGD_RESPONSE"][$index][$name]));
    }
    function 
Log($msg$level=LGD_LOG_FATAL)
    {
        if (
$level $this->config["log_level"]) return;
        
$err_msg date("Y-m-d H:i:s")." [".$this->err_label[$level]."] [".$this->TX_ID."] ".$msg."\n";
        
error_log($err_msg3$this->log_file);
    }
    function 
set_credentials($username,$password)
    {
        
curl_setopt($this->chCURLOPT_USERPWD"$username:$password");
    }
    function 
set_referrer($referrer_url)
    {
        
curl_setopt($this->chCURLOPT_REFERER$referrer_url);
    }
    function 
set_user_agent($useragent)
    {
        
curl_setopt($this->chCURLOPT_USERAGENT$useragent);
    }
    function 
include_response_headers($value)
    {
        
curl_setopt($this->chCURLOPT_HEADER$value);
    }
    function 
set_proxy($proxy)
    {
        
curl_setopt($this->chCURLOPT_PROXY$proxy);
    }
    function 
send_post_data($url$postdata$ip=null$timeout=10)
    {
        
curl_setopt($this->chCURLOPT_URL$url);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFERtrue);
        if (
$ip)
        {
            if(
$this->debug)
            {
                echo 
"Binding to ip $ip\n";
            }
            
curl_setopt($this->chCURLOPT_INTERFACE$ip);
        }
        
curl_setopt($this->chCURLOPT_TIMEOUT$timeout);
        
curl_setopt($this->chCURLOPT_POSTtrue);
        
$post_array = array();
        if(
is_array($postdata))
        {        
            foreach(
$postdata as $key=>$value)
            {
                
$post_array[] = urlencode($key) . "=" urlencode($value);
                if(
$this->IsAcceptLog($key,LGD_LOG_DEBUG))
                {
                    
$this->log("Post [".$key."] = [".$value."]"LGD_LOG_DEBUG);
                }
            }
            
$post_string implode("&",$post_array);
        }
        else 
        {
            
$post_string $postdata;
        }
        
curl_setopt($this->chCURLOPT_POSTFIELDS$post_string);
        
$result curl_exec($this->ch);
        if(
curl_errno($this->ch))
        {
            if(
$this->debug)
            {
                echo 
"Error Occured in Curl\n";
                echo 
"Error number: " .curl_errno($this->ch) ."\n";
                echo 
"Error message: " .curl_error($this->ch)."\n";
            }
            return 
false;
        }
        else
        {
            return 
$result;
        }
    }
    function 
fetch_url($url$ip=null$timeout=5)
    {
        
curl_setopt($this->chCURLOPT_URL,$url);
        
curl_setopt($this->chCURLOPT_HTTPGET,true);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFER,true);
        if(
$ip)
        {
            if(
$this->debug)
            {
                echo 
"Binding to ip $ip\n";
            }
            
curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
        }
        
curl_setopt($this->chCURLOPT_TIMEOUT$timeout);
        
$result curl_exec($this->ch);
        if(
curl_errno($this->ch))
        {
            if(
$this->debug)
            {
                echo 
"Error Occured in Curl\n";
                echo 
"Error number: " .curl_errno($this->ch) ."\n";
                echo 
"Error message: " .curl_error($this->ch)."\n";
            }
            return 
false;
        }
        else
        {
            return 
$result;
        }
    }
    function 
fetch_into_file($url$fp$ip=null$timeout=5)
    {
        
curl_setopt($this->chCURLOPT_URL$url);
        
curl_setopt($this->chCURLOPT_HTTPGETtrue);
        
curl_setopt($this->chCURLOPT_FILE$fp);
        if(
$ip)
        {
            if(
$this->debug)
            {
                echo 
"Binding to ip $ip\n";
            }
            
curl_setopt($this->chCURLOPT_INTERFACE$ip);
        }
        
curl_setopt($this->chCURLOPT_TIMEOUT$timeout);
        
$result curl_exec($this->ch);
        if(
curl_errno($this->ch))
        {
            if(
$this->debug)
            {
                echo 
"Error Occured in Curl\n";
                echo 
"Error number: " .curl_errno($this->ch) ."\n";
                echo 
"Error message: " .curl_error($this->ch)."\n";
            }
            return 
false;
        }
        else
        {
            return 
true;
        }
    }
    function 
post_into_file($url$postdata$fp$ip=null$timeout=10)
    {
        
curl_setopt($this->chCURLOPT_URL$url);
        
curl_setopt($this->chCURLOPT_FILE$fp);
        if (
$ip)
        {
            if(
$this->debug)
            {
                echo 
"Binding to ip $ip\n";
            }
            
curl_setopt($this->chCURLOPT_INTERFACE$ip);
        }
        
curl_setopt($this->chCURLOPT_TIMEOUT$timeout);
        
curl_setopt($this->chCURLOPT_POSTtrue);
        
$post_array = array();
        if(
is_array($postdata))
        {        
            foreach(
$postdata as $key=>$value)
            {
                
$post_array[] = urlencode($key) . "=" urlencode($value);
                if(
IsAcceptLog($key,LGD_LOG_DEBUG))
                {
                    
$this->log("Post [".$key."] = [".$value."]"LGD_LOG_DEBUG);
                }
            }
            
$post_string implode("&",$post_array);
            if(
$this->debug)
            {
                echo 
"Url: $url\nPost String: $post_string\n";
            }
        }
        else 
        {
            
$post_string $postdata;
        }
        
curl_setopt($this->chCURLOPT_POSTFIELDS$post_string);
        
$result curl_exec($this->ch);
        if(
curl_errno($this->ch))
        {
            if(
$this->debug)
            {
                echo 
"Error Occured in Curl\n";
                echo 
"Error number: " .curl_errno($this->ch) ."\n";
                echo 
"Error message: " .curl_error($this->ch)."\n";
            }
            return 
false;
        }
        else
        {
            return 
$result;
        }
    }
    function 
send_multipart_post_data($url$postdata$file_field_array=array(), $ip=null$timeout=30)
    {
        
curl_setopt($this->chCURLOPT_URL$url);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFERtrue);
        if(
$ip)
        {
            if(
$this->debug)
            {
                echo 
"Binding to ip $ip\n";
            }
            
curl_setopt($this->ch,CURLOPT_INTERFACE,$ip);
        }
        
curl_setopt($this->chCURLOPT_TIMEOUT$timeout);
        
curl_setopt($this->chCURLOPT_POSTtrue);
        
$headers = array("Expect: ");
        
curl_setopt($this->chCURLOPT_HTTPHEADER$headers);
        
$result_post = array();
        
$post_array = array();
        
$post_string_array = array();
        if(!
is_array($postdata))
        {
            return 
false;
        }
        foreach(
$postdata as $key=>$value)
        {
            
$post_array[$key] = $value;
            
$post_string_array[] = urlencode($key)."=".urlencode($value);
        }
        
$post_string implode("&",$post_string_array);
        if(
$this->debug)
        {
            echo 
"Post String: $post_string\n";
        }
        if(!empty(
$file_field_array))
        {
            foreach(
$file_field_array as $var_name => $var_value)
            {
                if(
strpos(PHP_OS"WIN") !== false$var_value str_replace("/""\\"$var_value); // win hack
                
$file_field_array[$var_name] = "@".$var_value;
            }
        }
        
$result_post array_merge($post_array$file_field_array);
        
curl_setopt($this->chCURLOPT_POSTFIELDS$result_post);
        
$result curl_exec($this->ch);
        if(
curl_errno($this->ch))
        {
            if(
$this->debug)
            {
                echo 
"Error Occured in Curl\n";
                echo 
"Error number: " .curl_errno($this->ch) ."\n";
                echo 
"Error message: " .curl_error($this->ch)."\n";
            }
            return 
false;
        }
        else
        {
            return 
$result;
        }
    }
    function 
store_cookies($cookie_file)
    {
        
curl_setopt ($this->chCURLOPT_COOKIEJAR$cookie_file);
        
curl_setopt ($this->chCURLOPT_COOKIEFILE$cookie_file);
    }
    function 
set_cookie($cookie)
    {        
        
curl_setopt ($this->chCURLOPT_COOKIE$cookie);
    }
    function 
get_effective_url()
    {
        return 
curl_getinfo($this->chCURLINFO_EFFECTIVE_URL);
    }
    function 
get_http_response_code()
    {
        return 
curl_getinfo($this->chCURLINFO_HTTP_CODE);
    }
    function 
get_error_msg()
    {
        
$err "Error number: " .curl_errno($this->ch) ."\n";
        
$err .="Error message: " .curl_error($this->ch)."\n";
        return 
$err;
    }
    function 
close()
    {
        
curl_close($this->ch);
    }
        function 
GetTimeStamp()
    {
        
$Result "";
        
$Result date("YmdHis");
        return 
$Result;
    }

    function 
GetHashData($LGD_MID,$LGD_OID,$LGD_AMOUNT,$LGD_TIMESTAMP)
    {
        
$LGD_HASHDATA "";
        
$LGD_HASHDATA md5($LGD_MID.$LGD_OID.$LGD_AMOUNT.$LGD_TIMESTAMP.$this->config[$LGD_MID]);
        return 
$LGD_HASHDATA;
    }

    function 
GetHashDataOpenpay($LGD_VENDERNO,$LGD_MID,$LGD_OPENPAY_MER_UID,$LGD_OPENPAY_TOKEN,$LGD_TIMESTAMP)
    {
        
$LGD_HASHDATA "";            
        
$InputData "";
        
$InputData $LGD_MID $LGD_VENDERNO $LGD_OPENPAY_MER_UID;
        if(empty(
$LGD_OPENPAY_TOKEN)) $InputData $InputData $LGD_TIMESTAMP .$this->config[$LGD_MID];
        else 
$InputData $InputData $LGD_OPENPAY_TOKEN $LGD_TIMESTAMP .$this->config[$LGD_MID];
        
$LGD_HASHDATA hash('sha512',$InputData);
        return 
$LGD_HASHDATA;
    }
    
    function 
GetHashDataCas($LGD_MID,$LGD_OID,$LGD_AMOUNT,$LGD_RESPCODE,$LGD_TIMESTAMP)
    {
        
$LGD_HASHDATA "";
        
$LGD_HASHDATA md5($LGD_MID.$LGD_OID.$LGD_AMOUNT.$LGD_RESPCODE.$LGD_TIMESTAMP.$this->config[$LGD_MID]);
        return 
$LGD_HASHDATA;
    }
        
    
/**
    @brief MertKey Bytes 변환
    @param MertKey MertKey
    @date 2014.04.03
    @author SangmanPark (sangman.park@gmail.com)
    @remark MertKey 를 Bytes 로 변환한다.
    */
    
function StringToHex($MertKey)
    {
        
$szKey
        
$szMertKey str_split($MertKey,2);
        for (
$i $i 16 $i++)
        {
            
$szKey[$i] = Hex2Bin ($szMertKey[$i]);
        }
        
$szKeyString implode(""$szKey);

        
//for ($i = 0 ; $i < 16 ; $i++)
        //{
        //    echo Bin2Hex($szKeyString[$i])."<BR>";
        //}
        
        
return $szKeyString;
    }

    
/**
    @brief Micro time 
    @param MertKey MertKey
    @date 2014.04.03
    @author SangmanPark (sangman.park@gmail.com)
    @remark Micro Time 을 구한다.
    */
    
function getMicrotime()
    {
        if (
version_compare(PHP_VERSION'5.0.0''<'))
        {
            return 
array_sum(explode(' 'microtime()));
        }

        return 
microtime(true);
    }

    
/**
    @brief Encrypt And Encode
    @param MertKey MertKey
    @param PlainBuffer 원문
    @date 2014.04.03
    @author SangmanPark (sangman.park@gmail.com)
    @remark AES128/ECB/PKCS#5 로 암호화 한후 Base64 Encoding 한다.
    */
    
function EncryptAndEncode($MertKey,$PlainBuffer)
    {
        echo 
"EncryptAndEncode Start<BR>";    
        
$microsecond $this->getMicrotime();
        
$PlainBufferTemp sprintf("%lf"$microsecond);
        
$PlainBufferTemp substr($PlainBufferTemp,-3);
        
$LgdValue ":LGD:".$PlainBufferTemp;
        
$PlainBuffer .= $LgdValue;
        
$key $this->StringToHex($MertKey);
        
$EncrytBuffer mcrypt_ecb(MCRYPT_RIJNDAEL_128,$key,$PlainBuffer,MCRYPT_ENCRYPT);
        
$EncryptAndEncodeBuffer base64_encode($EncrytBuffer); 
        
$EncryptReturnValue $EncryptAndEncodeBuffer;
        echo 
"EncryptReturnValue = " $EncryptReturnValue "<BR>";
        echo 
"EncryptAndEncode Complete<BR>";
        return 
$EncryptReturnValue;
    }

    
/**
    @brief Decode And Decrypt
    @param MertKey MertKey
    @param EncryptAndEncodeBuffer Encrypt And Encode String
    @date 2014.04.03
    @author SangmanPark (sangman.park@gmail.com)
    @remark Base64 Decoding 한후 AES128/ECB/PKCS#5 로 복호화 한다.
    */
    
function DecodeAndDecrypt($MertKey,$EncryptAndEncodeBuffer)
    {
        echo 
"DecodeAndDecrypt Start<BR>";
        
$key $this->StringToHex($MertKey);
        
$DecodeBuffer base64_decode($EncryptAndEncodeBuffer);  
        
$DecryptBuffer mcrypt_ecb(MCRYPT_RIJNDAEL_128,$key,$DecodeBuffer,MCRYPT_DECRYPT);
        
$UnPaddString $this->pkcs5_unpad($DecryptBuffer);
        echo 
"UnPaddString : ".$UnPaddString "<BR>";
        echo 
"DecodeAndDecrypt Complete<BR>";
        
$ConvertCharset iconv("euc-kr""utf-8"$UnPaddString);    
        return 
$ConvertCharset;

    }

    
/**
    * PKCS5 패딩추가
    * @param string $text
    * @param int $blocksize
    * @return string
    */
    
function pkcs5_pad($text$blocksize
    { 
        
$pad $blocksize - (strlen($text) % $blocksize); 
        return 
$text str_repeat(chr($pad), $pad); 
    } 
    
    
/**
    * PKCS5 패딩제거
    * @param string $text
    * @return boolean|string
    */
    
function pkcs5_unpad($text
    { 
        
$pad ord($text{strlen($text)-1}); 
        if (
$pad strlen($text)) return false
        if (
strspn($textchr($pad), strlen($text) - $pad) != $pad) return false
        return 
substr($text0, -$pad); 
    }
}
?>