Skip to content

Commit

Permalink
Fix Git-Requests, change to cURL
Browse files Browse the repository at this point in the history
  • Loading branch information
Bizarrus committed Sep 29, 2024
1 parent b2eb1f9 commit 31f4b9c
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions classes/Installer/Repository.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,24 @@ public function getRepository(int $id) {
}

public function getFile($repository, $file) : string | int | null {
$options = [];
$headers = [];
$url = $repository->url;
$branch = 'master';

// Load GitHub by RAW
if(preg_match('/github\.com\/([^\/]+)\/([^\/]+)$/Uis', rtrim($repository->url, '/'), $matches)) {
$user = rtrim($matches[1], '/');
$repo = rtrim($matches[2], '/');
$url = sprintf('https://raw.githubusercontent.com/%s/%s/%s', $user, $repo, $branch);
// Load by Git-Variables

// Load by Git-Variables
} else if(str_starts_with($url, 'git:')) {
$parts = explode(' ', $url);
$user = null;
$repo = null;
$token = $repository->token;
$branch = null;

foreach($parts as $part) {
if(str_starts_with($part, 'user:')) {
$user = str_replace('user:', '', $part);
Expand All @@ -143,7 +145,9 @@ public function getFile($repository, $file) : string | int | null {
$branch = str_replace('branch:', '', $part);
}
}

$url = sprintf('https://raw.githubusercontent.com/%s/%s/%s', $user, $repo, $branch);

if(!empty($token)) {
$headers['Authorization'] = sprintf('token %s', $token);
$headers['Accept'] = 'application/vnd.github.raw+json';
Expand All @@ -152,25 +156,35 @@ public function getFile($repository, $file) : string | int | null {
$url = sprintf('https://api.github.com/repos/%s/%s/contents', $user, $repo);
}
}

$request = curl_init();
curl_setopt($request, CURLOPT_URL, sprintf('%s/%s', $url, $file));
curl_setopt($request, CURLOPT_HEADER, false);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);

if(!empty($headers)) {
$h = '';
foreach($headers as $name => $value) {
$h .= sprintf('%s: %s%s', $name, $value, "\r\n");
$temp = [];

foreach($headers AS $name => $value) {
$temp[] = sprintf('%s: %s', $name, $value);
}
$options = [ "http" => [ "header" => $h ] ];

curl_setopt($request, CURLOPT_HTTPHEADER, $temp);
}
$context = stream_context_create($options);
$request = sprintf('%s/%s', $url, $file);
$content = @file_get_contents($request, false, $context);
if(empty($http_response_header)) {

$response = curl_exec($request);
$code = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);

if($code != 200) {
return self::BAD_RESPONSE;
} else if(count(preg_grep('/Forbidden/', $http_response_header)) == 1) {
} else if($code == 403) {
return self::FORBIDDEN;
} else if(empty($content)) {
} else if(empty($response)) {
return self::EMPTY;
}

return $content;
return $response;
}
}
?>

0 comments on commit 31f4b9c

Please sign in to comment.