キャッシュとCURLライブラリーの修正

This commit is contained in:
2026-05-22 16:56:39 +09:00
parent 83e251cab3
commit b27063691f
3 changed files with 39 additions and 45 deletions

View File

@@ -47,7 +47,7 @@ class Cache {
protected int $dataDuration = 1800; // 30分 protected int $dataDuration = 1800; // 30分
public function __construct(string $dataDir) { public function __construct(string $dataDir) {
$this->dataDir = ROOT.'/data/cache/openprovider/'.$dataDir.'/'; $this->dataDir = ROOT.'/data/cache/'.$dataDir.'/';
} }
/** /**

View File

@@ -409,33 +409,22 @@ class Curl {
$request .= "{$accept}\r\n"; $request .= "{$accept}\r\n";
$request .= "Connection: close\r\n"; $request .= "Connection: close\r\n";
// 認証ヘッダー
if (!empty($authHeader)) { if (!empty($authHeader)) {
$request .= $authHeader; $request .= $authHeader;
} }
// ヘッダーを追加 // ヘッダーを追加
foreach ($this->headers as $name => $value) { $ext = $this->buildHeaderString();
$request .= "{$name}: {$value}\r\n"; if (!empty($ext)) {
} $request .= $ext;
// リファラーが設定されていれば追加
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";
} }
// ヘッダー終了
$request .= "\r\n"; $request .= "\r\n";
// POSTデータを追加 // POSTデータを追加
if ($method === 'POST' || $method === 'PUT') { if ($httpData !== '') {
$request .= $httpData; $request .= $httpData;
} }
@@ -507,6 +496,7 @@ class Curl {
// ヘッダーを解析 // ヘッダーを解析
$this->responseHeaders = []; $this->responseHeaders = [];
$this->responseCode = 0; $this->responseCode = 0;
$redirectUrl = '';
foreach ($headerLines as $index => $header) { foreach ($headerLines as $index => $header) {
if ($index === 0) { if ($index === 0) {
@@ -520,6 +510,8 @@ class Curl {
$name = trim($name); $name = trim($name);
$value = trim($value); $value = trim($value);
$this->responseHeaders[$name] = $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; $currentUrl = $redirectUrl;
$redirectCount++; $redirectCount++;
$this->info['redirect_count'] = $redirectCount; $this->info['redirect_count'] = $redirectCount;
$this->info['redirect_url'] = $redirectUrl;
// 302や303リダイレクトはGETにメソッドを変更 // 302や303リダイレクトはGETにメソッドを変更
if ($this->responseCode == 302 || $this->responseCode == 303) { if (in_array($this->responseCode, [302, 303], true)) {
$this->method = 'GET'; $this->method = 'GET';
$this->postRaw = ''; $this->postRaw = '';
$this->postFields = []; $this->postFields = [];
} }
} else {
break; continue;
} }
break;
} while (true); } while (true);
// リクエスト完了後、元のメソッドに戻す // リクエスト完了後、元のメソッドに戻す
@@ -629,10 +624,9 @@ class Curl {
* @return string リダイレクトURL、リダイレクトがない場合は空文字 * @return string リダイレクトURL、リダイレクトがない場合は空文字
*/ */
private function checkReds(string $name, string $value, string $currentUrl): string { private function checkReds(string $name, string $value, string $currentUrl): string {
$redirectUrl = ''; if (!$this->followRedirects) return '';
if (strtolower($name) !== 'location') return '';
if ($this->followRedirects && (strtolower($name) === 'location' if ($this->responseCode < 300 || $this->responseCode >= 400) return '';
&& $this->responseCode >= 300 && $this->responseCode < 400)) {
$redirectUrl = $value; $redirectUrl = $value;
if (strpos($redirectUrl, 'http') !== 0) { if (strpos($redirectUrl, 'http') !== 0) {
@@ -645,7 +639,6 @@ class Curl {
$redirectUrl = dirname($currentUrl).'/'.$redirectUrl; $redirectUrl = dirname($currentUrl).'/'.$redirectUrl;
} }
} }
}
return $redirectUrl; return $redirectUrl;
} }
@@ -656,16 +649,16 @@ class Curl {
* @return string 構築されたヘッダー文字列 * @return string 構築されたヘッダー文字列
*/ */
private function buildHeaderString(): string { private function buildHeaderString(): string {
$headers = []; $request = '';
// ユーザー指定のヘッダーを追加 // ユーザー指定のヘッダーを追加
foreach ($this->headers as $name => $value) { foreach ($this->headers as $name => $value) {
$headers[] = "{$name}: {$value}"; $request .= "{$name}: {$value}\r\n";
} }
// リファラーが設定されていれば追加 // リファラーが設定されていれば追加
if (!empty($this->referer) && !isset($this->headers['Referer'])) { 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); $cookieStrings[] = $name.'='.urlencode($value);
} }
$headers[] = 'Cookie: '.implode('; ', $cookieStrings); $request .= 'Cookie: '.implode('; ', $cookieStrings)."\r\n";
} }
return implode("\r\n", $headers)."\r\n"; return $request;
} }
/** /**

View File

@@ -52,9 +52,9 @@ class Openprovider {
private string $ip = ''; private string $ip = '';
private int $resellerId = 0; private int $resellerId = 0;
private int $lastAuth = 0; private int $lastAuth = 0;
private string $BASEURL = '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' private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta'
// : 'https://api.openprovider.eu/v1beta'; : 'https://api.openprovider.eu/v1beta';
public function __construct() { public function __construct() {
if (!OPENPROVIDER_ENABLED) return; if (!OPENPROVIDER_ENABLED) return;
@@ -107,6 +107,7 @@ class Openprovider {
if ($curl->getResponseCode() != 200) { if ($curl->getResponseCode() != 200) {
$err = json_decode($body, true); $err = json_decode($body, true);
assert_not_null($err, "エラーの受け取りに失敗。"); assert_not_null($err, "エラーの受け取りに失敗。");
// kys($body);
assert($curl->getResponseCode() == 200, $err['desc']); assert($curl->getResponseCode() == 200, $err['desc']);
return Result::Error(); return Result::Error();
} }