diff --git a/src/Std/Lib/Cache.php b/src/Std/Lib/Cache.php index ffe49b3..55ca34b 100644 --- a/src/Std/Lib/Cache.php +++ b/src/Std/Lib/Cache.php @@ -47,7 +47,7 @@ class Cache { protected int $dataDuration = 1800; // 30分 public function __construct(string $dataDir) { - $this->dataDir = ROOT.'/data/cache/openprovider/'.$dataDir.'/'; + $this->dataDir = ROOT.'/data/cache/'.$dataDir.'/'; } /** diff --git a/src/Std/Lib/Curl.php b/src/Std/Lib/Curl.php index b04ee76..38860f0 100644 --- a/src/Std/Lib/Curl.php +++ b/src/Std/Lib/Curl.php @@ -409,33 +409,22 @@ class Curl { $request .= "{$accept}\r\n"; $request .= "Connection: close\r\n"; + // 認証ヘッダー if (!empty($authHeader)) { $request .= $authHeader; } // ヘッダーを追加 - foreach ($this->headers as $name => $value) { - $request .= "{$name}: {$value}\r\n"; - } - - // リファラーが設定されていれば追加 - if (!empty($this->referer) && !isset($this->headers['Referer'])) { - $request .= "Referer: {$this->referer}\r\n"; - } - - // クッキーヘッダーを追加 - if (!empty($this->cookies) && !isset($this->headers['Cookie'])) { - $cookieStrings = []; - foreach ($this->cookies as $name => $value) { - $cookieStrings[] = $name.'='.urlencode($value); - } - $request .= 'Cookie: '.implode('; ', $cookieStrings)."\r\n"; + $ext = $this->buildHeaderString(); + if (!empty($ext)) { + $request .= $ext; } + // ヘッダー終了 $request .= "\r\n"; // POSTデータを追加 - if ($method === 'POST' || $method === 'PUT') { + if ($httpData !== '') { $request .= $httpData; } @@ -507,6 +496,7 @@ class Curl { // ヘッダーを解析 $this->responseHeaders = []; $this->responseCode = 0; + $redirectUrl = ''; foreach ($headerLines as $index => $header) { if ($index === 0) { @@ -520,6 +510,8 @@ class Curl { $name = trim($name); $value = trim($value); $this->responseHeaders[$name] = $value; + + $redirectUrl = $this->checkReds($name, $value, $currentUrl); } } @@ -541,20 +533,23 @@ class Curl { } // リダイレクトが必要な場合 - if (!empty($redirectUrl) && $redirectCount < $this->maxRedirects) { + if ($redirectUrl !== '' && $redirectCount < $this->maxRedirects) { $currentUrl = $redirectUrl; $redirectCount++; $this->info['redirect_count'] = $redirectCount; + $this->info['redirect_url'] = $redirectUrl; // 302や303リダイレクトはGETにメソッドを変更 - if ($this->responseCode == 302 || $this->responseCode == 303) { + if (in_array($this->responseCode, [302, 303], true)) { $this->method = 'GET'; $this->postRaw = ''; $this->postFields = []; } - } else { - break; + + continue; } + + break; } while (true); // リクエスト完了後、元のメソッドに戻す @@ -629,22 +624,20 @@ class Curl { * @return string リダイレクトURL、リダイレクトがない場合は空文字 */ private function checkReds(string $name, string $value, string $currentUrl): string { - $redirectUrl = ''; + if (!$this->followRedirects) return ''; + if (strtolower($name) !== 'location') return ''; + if ($this->responseCode < 300 || $this->responseCode >= 400) return ''; + $redirectUrl = $value; - if ($this->followRedirects && (strtolower($name) === 'location' - && $this->responseCode >= 300 && $this->responseCode < 400)) { - $redirectUrl = $value; - - if (strpos($redirectUrl, 'http') !== 0) { - if ($redirectUrl[0] === '/') { - $uri = new Uri($currentUrl); - $redirectUrl = $uri->getScheme().'://'.$uri->getHost() - .(NULL !== $uri->getPort() ? ':'.$uri->getPort() : '') - .$redirectUrl; - } else { - $redirectUrl = dirname($currentUrl).'/'.$redirectUrl; - } - } + if (strpos($redirectUrl, 'http') !== 0) { + if ($redirectUrl[0] === '/') { + $uri = new Uri($currentUrl); + $redirectUrl = $uri->getScheme().'://'.$uri->getHost() + .(NULL !== $uri->getPort() ? ':'.$uri->getPort() : '') + .$redirectUrl; + } else { + $redirectUrl = dirname($currentUrl).'/'.$redirectUrl; + } } return $redirectUrl; @@ -656,16 +649,16 @@ class Curl { * @return string 構築されたヘッダー文字列 */ private function buildHeaderString(): string { - $headers = []; + $request = ''; // ユーザー指定のヘッダーを追加 foreach ($this->headers as $name => $value) { - $headers[] = "{$name}: {$value}"; + $request .= "{$name}: {$value}\r\n"; } // リファラーが設定されていれば追加 if (!empty($this->referer) && !isset($this->headers['Referer'])) { - $headers[] = "Referer: {$this->referer}"; + $request .= "Referer: {$this->referer}\r\n"; } // 必要に応じてクッキーヘッダーを追加 @@ -675,10 +668,10 @@ class Curl { $cookieStrings[] = $name.'='.urlencode($value); } - $headers[] = 'Cookie: '.implode('; ', $cookieStrings); + $request .= 'Cookie: '.implode('; ', $cookieStrings)."\r\n"; } - return implode("\r\n", $headers)."\r\n"; + return $request; } /** diff --git a/src/Std/Lib/Openprovider.php b/src/Std/Lib/Openprovider.php index 3988c8f..7df4e89 100644 --- a/src/Std/Lib/Openprovider.php +++ b/src/Std/Lib/Openprovider.php @@ -52,9 +52,9 @@ class Openprovider { private string $ip = ''; private int $resellerId = 0; private int $lastAuth = 0; - private string $BASEURL = 'https://api.openprovider.eu/v1beta'; - // private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta' - // : 'https://api.openprovider.eu/v1beta'; + // private string $BASEURL = 'https://api.openprovider.eu/v1beta'; + private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta' + : 'https://api.openprovider.eu/v1beta'; public function __construct() { if (!OPENPROVIDER_ENABLED) return; @@ -107,6 +107,7 @@ class Openprovider { if ($curl->getResponseCode() != 200) { $err = json_decode($body, true); assert_not_null($err, "エラーの受け取りに失敗。"); + // kys($body); assert($curl->getResponseCode() == 200, $err['desc']); return Result::Error(); }