-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.php
126 lines (113 loc) · 3.86 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 7 |
// +----------------------------------------------------------------------+
// | Authors: abc1763613206 <[email protected]> |
// | cubercsl <[email protected]> |
// +----------------------------------------------------------------------+
//
// $Id:$
const COLORS = [
"Legendary Grandmaster" => "ff0000",
"International Grandmaster" => "ff0000",
"Grandmaster" => "ff0000",
"International Master" => "ff8c00",
"Master" => "ff8c00",
"Candidate Master" => "aa00aa",
"Expert" => "0000ff",
"Specialist" => "03a89e",
"Pupil" => "008000",
"Newbie" => "808080",
"Unrated" => "000000"
];
$badgeUrl = "http://localhost:8080/badge/"; // 使用本地搭建的Shields服务
$mainUrl = "http://codeforces.com/api/user.info?handles=";
function getdata($url)
{
error_log("Request url: " . $url, 0);
$headers = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
return $result;
}
function escapehandle($user)
{
$result = str_replace("_", "__", $user);
$result = str_replace("-", "--", $result);
return $result;
}
function getimage($handle, $rank, $color, $rating, $style, $link = false)
{
global $badgeUrl;
$url = $badgeUrl .
escapehandle($handle) . "-" .
rawurlencode($rank . " " . $rating . "-") .
$color . ".svg" .
$style .
"&logo=Codeforces" .
($link ? "&link=https://codeforces.com/profile/" . $handle : "");
error_log("Request url: " . $url, 0);
return file_get_contents($url);
}
header("Content-Type: image/svg+xml");
if ($_SERVER['REQUEST_METHOD'] !== "GET") {
http_response_code(405);
echo getimage("405", "method not allowed", "critical", null, $style);
die;
}
if (isset($_GET["style"])) { //是否使用自定义style
$style = "?longCache=true&style=" . $_GET["style"];
} else {
$style = "?longCache=true&style=flat";
}
if (isset($_GET["st"])) { //自定义style缩写,不可重用
switch ($_GET["st"]) {
case "f1":
$style = "?longCache=true&style=for-the-badge";
break;
case "f2":
$style = "?longCache=true&style=flat-square";
break;
default:
break;
}
}
$user = trim($_GET["user"]);
set_time_limit(600);
$timeo = 0;
if (!isset($_GET["user"]) || $_GET["user"] == "") {
http_response_code(404);
echo getimage("404", "user not found", "critical", null, $style);
die;
}
do { //发现真Unrated会返回OK....所以试下新操作
$response = getdata($mainUrl . rawurlencode($_GET["user"])); //获取json数据并转换为数组
$timeo = $timeo + 1;
} while (
$response["status"] !== "OK" and
!preg_match("/handles:.*/", $response["comment"]) and
$timeo !== 12
);
if ($response["status"] !== "OK") {
http_response_code(404);
error_log("Query " . $user . " failed in " . $timeo . " time(s).", 0);
echo getimage("404", "user not found", "critical", null, $style);
die;
}
error_log("Query " . $user . " success in " . $timeo . " time(s).", 0);
$handle = $response["result"][0]["handle"];
$rating = $response["result"][0]["rating"];
$rank = $response["result"][0]["rank"] == null ?
"Unrated" :
ucwords($response["result"][0]["rank"]);
$color = COLORS[$rank];
echo getimage($handle, $rank, $color, $rating, $style, true);