source: branches/feature-module-paygent/data/downloads/module/mdl_paygent/jp/co/ks/merchanttool/connectmodule/util/HttpsRequestSender.php @ 15162

Revision 15162, 13.4 KB checked in by naka, 17 years ago (diff)

ペイジェント決済モジュール

Line 
1<?php
2/**
3 * PAYGENT B2B MODULE
4 * HttpsRequestSender.php
5 *
6 * Copyright (C) 2007 by PAYGENT Co., Ltd.
7 * All rights reserved.
8 */
9
10include_once("jp/co/ks/merchanttool/connectmodule/util/StringUtil.php");
11include_once("jp/co/ks/merchanttool/connectmodule/exception/PaygentB2BModuleConnectException.php");
12include_once("jp/co/ks/merchanttool/connectmodule/exception/PaygentB2BModuleException.php");
13include_once("jp/co/ks/merchanttool/connectmodule/util/PaygentB2BModuleLogger.php");
14
15/**
16 * httpsÍ×µá¤ò¤ª¤³¤Ê¤¦¥æ¡¼¥Æ¥£¥ê¥Æ¥£¥¯¥é¥¹¡£
17 *
18 * @vesrion $Revision: 1.5 $
19 * @author $Author: t-mori $
20 */
21 
22    // cURL ¥¨¥é¡¼¥³¡¼¥É
23    // http://curl.haxx.se/libcurl/c/libcurl-errors.html
24    define("HttpsRequestSender__CURLE_COULDNT_CONNECT", 7);
25    define("HttpsRequestSender__CURLE_SSL_CERTPROBLEM", 58);   
26    define("HttpsRequestSender__CURLE_SSL_CACERT", 60);
27    define("HttpsRequestSender__CURLE_SSL_CACERT_BADFILE", 77);
28    define("HttpsRequestSender__CURLE_HTTP_RETURNED_ERROR", 22);
29
30    /**
31     * HTTP POST ÄÌ¿®ÍѸÇÄêÃÍ
32     */
33    define("HttpsRequestSender__POST", "POST");
34
35    /**
36     * HTTP¥×¥í¥È¥³¥ë¤òɽ¤¹Äê¿ô
37     */
38    define("HttpsRequestSender__HTTP", "HTTP");
39
40    /**
41     * HTTP/1.0¤òɽ¤¹Äê¿ô
42     */
43    define("HttpsRequestSender__HTTP_1_0", "HTTP/1.0");
44
45    /**
46     * HTTPÄÌ¿®¤ÎÀ®¸ù¥³¡¼¥É
47     */
48    define("HttpsRequestSender__HTTP_1_0_200", "HTTP/1.0 200");
49   
50    /**
51     * HTTPÄÌ¿®¤ÎÀ®¸ù¥³¡¼¥É¡§200
52     */
53    define("HttpsRequestSender__HTTP_SUCCESS", 200);
54   
55    /**
56     * HTTPÄÌ¿®¤ÎÀ®¸ù¥³¡¼¥É¡§206
57     */
58    define("HttpsRequestSender__HTTP_PARTIAL_CONTENT", 206);
59
60    /**
61     * ÅÅʸĹ
62     */
63    define("HttpsRequestSender__TELEGRAM_LENGTH", 10240);
64
65    /**
66     * HTTPS Default Port
67     */
68    define("HttpsRequestSender__DEFAULT_PORT", 443);
69
70    /**
71     * ¥ê¥¯¥¨¥¹¥È¡¦¥ì¥¹¥Ý¥ó¥¹¤Î²þ¹Ô¥³¡¼¥É
72     */
73    define("HttpsRequestSender__CRLF", "\r\n");
74
75    /**
76     * ¥Ç¥Õ¥©¥ë¥È¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°
77     */
78    define("HttpsRequestSender__DEFAULT_ENCODING", "SJIS-win");
79
80    /**
81     * HTTP¥¹¥Æ¡¼¥¿¥¹¥³¡¼¥ÉÊÑ¿ô¤Î½é´üÃÍ
82     */
83    define("HttpsRequestSender__HTTP_STATUS_INIT_VALUE", -1);
84
85    /**
86     * ¥¹¥Æ¡¼¥¿¥¹¥³¡¼¥É¤ÎŤµ
87     */
88    define("HttpsRequestSender__REGEXPSTATUS_LEN", 3);
89
90    /**
91     * Content-Length
92     */
93    define("HttpsRequestSender__CONTENT_LENGTH", "Content-Length");
94
95    /**
96     * User-Agent
97     */
98    define("HttpsRequestSender__USER_AGENT", "User-Agent");
99
100    /**
101     * Content-Type
102     */
103    define("HttpsRequestSender__CONTENT_TYPE", "Content-Type=application/x-www-form-urlencoded");
104    define("HttpsRequestSender__HTTP_ENCODING", "charset=Windows-31J");
105   
106class HttpsRequestSender {
107    /**
108     * KeyStore Password
109     */
110    var $KEYSTORE_PASSWORD = "changeit";
111
112    /** ¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À */
113    var $responseHeader;
114
115    /** ¥ì¥¹¥Ý¥ó¥¹¥Ü¥Ç¥£ */
116    var $responseBody;
117
118    /** ¥¹¥Æ¡¼¥¿¥¹¥³¡¼¥É¡¡*/
119    var $statusCode;
120   
121    /** ÀܳÀè URL */
122    var $url;
123
124    /** ¥¯¥é¥¤¥¢¥ó¥È¾ÚÌÀ½ñ¥Ñ¥¹ */
125    var $clientCertificatePath;
126
127    /** ǧ¾Ú¶É¾ÚÌÀ½ñ¥Ñ¥¹ */
128    var $caCertificatePath;
129
130    /** SSLÄÌ¿®ÍÑ¥½¥±¥Ã¥È */
131    var $ch;
132
133    /** ¥È¥ó¥Í¥ë¥½¥±¥Ã¥È */
134    //var $tunnelSocket;
135
136    /** ¥¿¥¤¥à¥¢¥¦¥ÈÃÍ int */
137    var $timeout;
138
139    /** Proxy¥Û¥¹¥È̾ */
140    var $proxyHostName;
141
142    /** Proxy¥Ý¡¼¥ÈÈÖ¹æ int */
143    var $proxyPort;
144
145    /** ProxyÀܳ¥¿¥¤¥à¥¢¥¦¥ÈÃÍ */
146    var $proxyConnectTimeout;
147
148    /** ProxyÅÁÁ÷¥¿¥¤¥à¥¢¥¦¥ÈÃÍ */
149    var $proxyCommunicateTimeout;
150
151    /** Proxy»ÈÍÑȽÄê */
152    var $isUsingProxy = false;
153
154    /**
155     * ¥³¥ó¥¹¥È¥é¥¯¥¿<br>
156     * ÀܳÀèURL¤òÀßÄê
157     *
158     * @param url String
159     */
160    function HttpsRequestSender($url) {
161        $this->url = $url;
162        $this->proxyHostName = "";
163        $this->proxyPort = 0;
164       
165        $this->responseBody = null;
166        $this->responseHeader = null;
167    }
168
169    /**
170     * ¥¯¥é¥¤¥¢¥ó¥È¾ÚÌÀ½ñ¥Ñ¥¹¤òÀßÄê
171     *
172     * @param fileName String
173     */
174    function setClientCertificatePath($fileName) {
175        $this->clientCertificatePath = $fileName;
176    }
177
178    /**
179     * ǧ¾Ú¶É¾ÚÌÀ½ñ¥Ñ¥¹¤òÀßÄê
180     *
181     * @param fileName String
182     */
183    function setCaCertificatePath($fileName) {
184        $this->caCertificatePath = $fileName;
185    }
186
187    /**
188     * ¥¿¥¤¥à¥¢¥¦¥È¤òÀßÄê
189     *
190     * @param timeout int
191     */
192    function setTimeout($timeout) {
193        $this->timeout = $timeout;
194    }
195
196    /**
197     * ProxyÀܳ¥¿¥¤¥à¥¢¥¦¥È¤òÀßÄê
198     *
199     * @param proxyConnectTimeout int
200     */
201    function setProxyConnectTimeout($proxyConnectTimeout) {
202        $this->proxyConnectTimeout = $proxyConnectTimeout;
203    }
204
205    /**
206     * ProxyÅÁÁ÷¥¿¥¤¥à¥¢¥¦¥È¤òÀßÄê
207     *
208     * @param proxyCommunicateTimeout int
209     */
210    function setProxyCommunicateTimeout($proxyCommunicateTimeout) {
211        $this->proxyCommunicateTimeout = $proxyCommunicateTimeout;
212    }
213
214    /**
215     * ProxyHostName, ProxyPort ¤òÀßÄê
216     *
217     * @param proxyHostName String
218     * @param proxyPort int
219     */
220    function setProxyInfo($proxyHostName, $proxyPort) {
221        $this->proxyHostName = $proxyHostName;
222        $this->proxyPort = $proxyPort;
223        $this->isUsingProxy = false;
224
225        if (!StringUtil::isEmpty($this->proxyHostName) && 0 < $this->proxyPort) {
226            // Proxy¾ðÊó¤¬ÀßÄꤵ¤ì¤¿°Ù¡¢true ¤òÀßÄê
227            $this->isUsingProxy = true;
228        }
229    }
230
231    /**
232     * Post¤ò¼Â»Ü
233     *
234     * @param formData Map
235     * @return mixed TRUE:À®¸ù¡¢Â¾:¥¨¥é¡¼¥³¡¼¥É
236     */
237    function postRequestBody($formData) {
238
239        // ÄÌ¿®³«»Ï
240        $this->initCurl();
241
242        if ($this->isUsingProxy) {
243            // ¥×¥í¥­¥··Ðͳ¤ÇÄÌ¿®Àè¤ËÀܳ
244            $this->setProxy();
245        }
246
247        // ¥ê¥¯¥¨¥¹¥È¤òÁ÷¿®
248        $retCode = $this->send($formData);
249
250        // ¥ì¥¹¥Ý¥ó¥¹¤ò¼õ¿®
251        $this->closeCurl();
252
253        return $retCode;
254    }
255
256    /**
257     * ¼õ¿®¥Ç¡¼¥¿¤òÊÖ¤¹
258     *
259     * @return InputStream
260     */
261    function getResponseBody() {
262        return $this->responseBody;
263    }
264
265    /**
266     * ÅÅʸĹ¥Á¥§¥Ã¥¯
267     *
268     * @return boolean true=NotError false=Error
269     */
270    function isTelegramLength($formData) {
271        $rb = false;
272
273        // URL Length Check
274        $sb = $this->url;
275        $sb .= "?";
276        $sb .= $this->convertToUrlEncodedString($formData);
277
278        if (strlen($sb) <= HttpsRequestSender__TELEGRAM_LENGTH) {
279            $rb = true;
280        }
281
282        return $rb;
283    }
284
285    /**
286     * Í×µáÅÅʸ¤òºîÀ®
287     *
288     * @param formData Map Í×µáÅÅʸ
289     * @return String ºîÀ®¤·¤¿Í×µáÅÅʸ¡ÊURL¡Ë
290     */
291    function convertToUrlEncodedString($formData) {
292        $encodedString = "";
293        if ($formData == null) {
294            return "";
295        }
296
297        foreach($formData as $key => $value) {
298            $this->outputDebugLog("param: " . $key . " = \"" . $value . "\"");
299            $tmp = $key;
300            $encodedString .= urlencode($tmp);
301            $encodedString .= "=";
302            $tmp = $value;
303            $encodedString .= urlencode($tmp);
304            $encodedString .= "&";
305        }
306
307        $rs = "";
308
309        if (0 < strlen($encodedString)) {
310            $rs = substr($encodedString, 0, strlen($encodedString) - 1);
311        }
312
313        return $rs;
314
315    }
316   
317    /**
318     * ¥Ç¥Ð¥Ã¥°¥í¥°½ÐÎϥ᥽¥Ã¥É
319     * ¥í¥°½ÐÎÏ¥¯¥é¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹À¸À®¤Ë¼ºÇÔ¤·¤¿¤éɸ½à½ÐÎϤ˥¨¥é¡¼¥á¥Ã¥»¡¼¥¸¤ò
320     * ½ÐÎϤ¹¤ë¡£
321     *
322     * @param msg String ½ÐÎÏ¥á¥Ã¥»¡¼¥¸
323     */
324    function outputDebugLog($msg) {
325        if(StringUtil::isEmpty($msg)) return;
326
327        $inst = PaygentB2BModuleLogger::getInstance();
328        if (is_object($inst)) {
329            $inst->debug(get_class($this), $msg);
330        }
331    }
332
333    /**
334     * ProxyÀܳÍÑ
335     *
336     */
337    function setProxy() {
338        curl_setopt($this->ch, CURLOPT_HTTPPROXYTUNNEL, true);
339        curl_setopt($this->ch, CURLOPT_PROXY, "http://" . $this->proxyHostName . ":" . $this->proxyPort);
340
341    }
342
343    /**
344     * Àܳ¤Î¤¿¤á¤Î½é´ü²½½èÍý
345     *
346     */
347    function initCurl() {
348        $rslt = true;
349        // ½é´ü²½
350        $this->ch = curl_init($this->url);
351
352        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
353        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
354        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_POST, true);
355        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_HEADER, true);
356       
357        // ¾ÚÌÀ½ñ
358        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, true);
359        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
360        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_SSLCERT, $this->clientCertificatePath);
361        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_SSLKEYPASSWD, $this->KEYSTORE_PASSWORD);
362        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_CAINFO, $this->caCertificatePath);
363       
364        // ¥¿¥¤¥à¥¢¥¦¥È
365        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->timeout);
366        $rslt = $rslt && curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->proxyConnectTimeout);
367
368        return $rslt;
369    }
370
371    /**
372     * ¥ê¥¯¥¨¥¹¥ÈÀ¸À®¤ÈÁ÷¿®
373     *
374     * @param formData Map Í×µáÅÅʸ
375     * @return mixed TRUE:À®¸ù¡¢Â¾:¥¨¥é¡¼¥³¡¼¥É
376     */
377    function send($formData) {
378        // ¥ê¥¯¥¨¥¹¥È¤ò Map ¤«¤é String ¤ËÊÑ´¹
379
380        $query = $this->convertToUrlEncodedString($formData);
381
382        $header = array();
383        $header[] = HttpsRequestSender__CONTENT_TYPE;
384        $header[] = HttpsRequestSender__HTTP_ENCODING;
385        $header[] = HttpsRequestSender__CONTENT_LENGTH . ": "
386            . (StringUtil::isEmpty($query)? "0" : strlen($query));
387        $header[] = HttpsRequestSender__USER_AGENT . ": " . "curl_php";
388        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
389        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $query);
390       
391        $this->outputDebugLog("Query: " . $query);     
392        $str = curl_exec($this->ch);
393
394        if ($str === false && curl_errno($this->ch) != 0) {
395            return $this->procError($this->ch);
396        }
397       
398        $this->outputDebugLog("Response: " . $str);     
399        $data = $str;
400        $retCode = $this->parseResponse($data);
401
402        return $retCode;
403    }
404
405    /**
406     * Curl¤Î¥¨¥é¡¼½èÍý
407     * @return mixed True:ÌäÂê¤Ê¤·¡¢Â¾¡§¥¨¥é¡¼¥³¡¼¥É
408     */
409    function procError() {
410        $errorNo = curl_errno($this->ch);
411        $errorMsg = $errorNo . ": " . curl_error($this->ch);
412        $retCode = true;
413       
414        if ($errorNo <= HttpsRequestSender__CURLE_COULDNT_CONNECT) { // 7
415            // ÀܳÌäÂê
416            $retCode = PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
417            $this->outputDebugLog($errorMsg);
418        } else if ($errorNo == HttpsRequestSender__CURLE_COULDNT_CONNECT) { // 7
419            // ÀܳÌäÂê
420            $retCode = PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
421            $this->outputDebugLog($errorMsg);
422        } else if ($errorNo == HttpsRequestSender__CURLE_SSL_CERTPROBLEM) {
423            // ǧ¾ÚÌäÂê
424            $retCode = PaygentB2BModuleConnectException__CERTIFICATE_ERROR;
425            $this->outputDebugLog($errorMsg);
426        } else if ($errorNo == HttpsRequestSender__CURLE_SSL_CACERT) {
427            // ǧ¾ÚÌäÂê
428            $retCode = PaygentB2BModuleConnectException__CERTIFICATE_ERROR;
429            $this->outputDebugLog($errorMsg);
430        } else if ($errorNo == HttpsRequestSender__CURLE_SSL_CACERT_BADFILE) {  // CURLE_SSL_CACERT_BADFILE
431            // ǧ¾ÚÌäÂê
432            $retCode = PaygentB2BModuleConnectException__CERTIFICATE_ERROR;
433            $this->outputDebugLog($errorMsg);
434        } else if ($errorNo == HttpsRequestSender__CURLE_HTTP_RETURNED_ERROR) {
435            // HTTP Return code error
436            $retCode = PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
437            $this->outputDebugLog($errorMsg);
438        } else {
439            // ¤½¤Î¾¤Î¥¨¥é¡¼
440            $retCode = PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
441            $this->outputDebugLog($errorMsg);
442        }
443
444        trigger_error("$retCode: Http request ended with errors.", E_USER_WARNING);
445        return $retCode;
446    }
447
448    /**
449     * ¥ì¥¹¥Ý¥ó¥¹¤ò¼õ¿®¡£
450     *
451     * @param $data ¥ì¥¹¥Ý¥ó¥¹Ê¸»úÎó
452     * @return mixed TRUE:À®¸ù¡¢Â¾:¥¨¥é¡¼¥³¡¼¥É
453     */
454    function parseResponse($data) {
455
456        // ¥ì¥¹¥Ý¥ó¥¹¼õ¿®
457        $line = null;
458        $retCode = HttpsRequestSender__HTTP_STATUS_INIT_VALUE;
459        $bHeaderOver = false;
460        $resBodyStart = 0;
461   
462        $lines = mb_split(HttpsRequestSender__CRLF, $data);
463        // ¥Ø¥Ã¥À¤Þ¤Ç¤òÆɤ߹þ¤à
464        foreach($lines as $i => $line) {
465           
466            if (StringUtil::isEmpty($line)) {
467                 break;
468            }
469            $resBodyStart += strlen($line) + strlen(HttpsRequestSender__CRLF);
470           
471            if ($retCode === HttpsRequestSender__HTTP_STATUS_INIT_VALUE) {
472                // ¥¹¥Æ¡¼¥¿¥¹¤Î²òÀÏ
473                $retCode = $this->parseStatusLine($line);
474                if ($retCode === true) {
475                    continue;
476                }
477                $this->outputDebugLog("Cannot get http return code.");
478                return $retCode;
479            }
480
481            // ¥Ø¥Ã¥À¤Î²òÀÏ
482            if (!$this->parseResponseHeader($line)) {
483                continue;
484            }
485        }
486        $resBodyStart += strlen(HttpsRequestSender__CRLF);
487        $this->responseBody = substr($data, $resBodyStart);
488
489        return true;
490    }
491
492    /**
493     * ¥¹¥Æ¡¼¥¿¥¹¥é¥¤¥ó¤ò²òÀÏ
494     * (HTTP-Version SP Status-Code SP Reason-Phrase CRLF)
495     *
496     * @param line String ¥¹¥Æ¡¼¥¿¥¹¥é¥¤¥ó
497     * @return mixed TRUE:À®¸ù¡¢Â¾:¥¨¥é¡¼¥³¡¼¥É
498     */
499    function parseStatusLine($line) {
500
501        if (StringUtil::isEmpty($line)) {
502               
503            // ÉÔÀµ¤Ê¥¹¥Æ¡¼¥¿¥¹¥³¡¼¥É¤ò¼õ¤±¼è¤Ã¤¿
504            return PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
505        }
506
507        $statusLine = StringUtil::split($line, " ", 3);
508
509        if (StringUtil::isNumeric($statusLine[1])) {
510            $this->statusCode = intVal($statusLine[1]);
511        } else {
512           
513            // ÉÔÀµ¤Ê¥¹¥Æ¡¼¥¿¥¹¥³¡¼¥É¤ò¼õ¤±¼è¤Ã¤¿
514            return PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
515        }
516
517        if (strpos($statusLine[0], HttpsRequestSender__HTTP . "/") != 0
518                || !StringUtil::isNumericLength($statusLine[1], HttpsRequestSender__REGEXPSTATUS_LEN)) {
519
520            // ÉÔÀµ¤Ê¥¹¥Æ¡¼¥¿¥¹¥³¡¼¥É¤ò¼õ¤±¼è¤Ã¤¿
521            return PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
522        }
523       
524        if (!((HttpsRequestSender__HTTP_SUCCESS <= $this->statusCode)
525            && ($this->statusCode <= HttpsRequestSender__HTTP_PARTIAL_CONTENT))) {
526
527            // HTTP Status ¤¬ Success Code (200 - 206) ¤Ç¤Ê¤¤¾ì¹ç
528            return PaygentB2BModuleConnectException__KS_CONNECT_ERROR;
529        }
530
531        return true;
532    }
533
534    /**
535     * ¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤ò°ì¹Ô²òÀϤ·¤Æ¡¢ÆâÉô¤Ë³ÊǼ¡£<br>
536     * ¥ì¥¹¥Ý¥ó¥¹¥Ø¥Ã¥À¤ÎÃͤ¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¤Ï¡¢null¤òÀßÄê¡£
537     *
538     * @param line String ¥µ¡¼¥Ð¤«¤é¼õ¤±¼è¤Ã¤¿¥ì¥¹¥Ý¥ó¥¹¹Ô
539     * @return boolean true=¥Ø¥Ã¥À²òÀÏ¡¦³ÊǼ´°Î», false=¥Ø¥Ã¥À¤Ç¤Ï¤Ê¤¤¡Ê¥Ø¥Ã¥ÀÉô½ªÎ»¡Ë
540     */
541    function parseResponseHeader($line) {
542        if (StringUtil::isEmpty($line)) {
543            // HEADER½ªÎ»
544            return false;
545        }
546
547        // HEADER
548        $headerStr = StringUtil::split($line, ":", 2);
549        if ($this->responseHeader == null) {
550            $this->responseHeader = array();
551        }
552
553        if (count($headerStr) == 1 || strlen(trim($headerStr[1])) == 0) {
554            // Ãͤ¬Â¸ºß¤·¤Ê¤¤ or Ãͤ¬¶õʸ»úÎó
555            $this->responseHeader[$headerStr[0]] = null;
556        } else {
557            $this->responseHeader[$headerStr[0]] = trim($headerStr[1]);
558        }
559
560        return true;
561    }
562
563    /**
564     * Close curl
565     *
566     */
567    function closeCurl() {
568        // ¥×¥í¥­¥·¥½¥±¥Ã¥ÈCLOSE
569        if ($this->ch != null) {
570            curl_close($this->ch);
571            $this->ch = null;
572        }
573    }
574
575}
576
577?>
Note: See TracBrowser for help on using the repository browser.