-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetgistcard.php
62 lines (47 loc) · 1.42 KB
/
getgistcard.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
<?php
require(".env/github.php");
header("Content-Type: text/plain");
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
$opts = [
"http" => [
"method" => "GET",
'ignore_errors' => true,
"header" => "Authorization: token " . GITHUB_TOKEN . "\r\nUser-Agent: gitcard\r\n"
]
];
$context = stream_context_create($opts);
$username = urlencode(@$_GET["user"]);
$requesturl = "https://api.github.com/users/$username/gists";
$responseStr = file_get_contents($requesturl, false, $context);
$response = json_decode($responseStr, true);
if (array_key_exists("message", $response) && $response["message"] === "Bad credentials") {
http_response_code(403);
echo $response["message"];
exit();
}
if (array_key_exists("message", $response) && strpos($response["message"], "API rate limit exceeded") === 0) {
http_response_code(403);
echo $response["message"];
exit();
}
if (array_key_exists("message", $response) && $response["message"] === "Not Found") {
http_response_code(404);
echo $response["message"];
exit();
}
$raw_url = null;
foreach ($response as $gist) {
if (array_key_exists("gitcard.card", $gist["files"])) {
$raw_url = $gist["files"]["gitcard.card"]["raw_url"];
break;
}
}
if ($raw_url === null) {
http_response_code(404);
exit();
}
header("Cache-Control: public, max-age=900");
echo file_get_contents($raw_url, false, $context);