This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
135 lines (111 loc) · 4 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
127
128
129
130
131
132
133
134
135
<?php
class Fonts {
// server variables
protected $server;
// font-display mode
protected $mode = '';
// response data
protected $body = '';
protected $encoding = '';
public function __construct($server, $mode = 'swap') {
$this->server = $server;
$this->setMode($mode);
}
public function setMode($mode) {
if (!in_array($mode, array('auto', 'block', 'swap', 'fallback', 'optional'))) {
$mode = '';
}
$this->mode = $mode;
}
public function getRequestHeaders() {
$headers = array();
foreach ($this->server as $key => $value) {
// find all request headers in server variables
if (substr($key, 0, 5) == 'HTTP_' && $key != 'HTTP_HOST' && $key != 'HTTP_COOKIE') {
// don't request brotli encoding if server can't handle it
if ($key == 'HTTP_ACCEPT_ENCODING' && !extension_loaded('brotli')) {
$value = str_replace(', br', '', $value);
}
$headers[] = str_replace('_', '-', substr($key, 5)) . ': ' . $value;
}
}
return $headers;
}
// decode a string according to specified encoding
protected function decode($string) {
return $this->transcode($string, array(
'gzip' => 'gzdecode',
'br' => 'brotli_uncompress',
));
}
// encode a string according to specified encoding
protected function encode($string) {
return $this->transcode($string, array(
'gzip' => 'gzencode',
'br' => 'brotli_compress',
));
}
// perform a string function according to encoding
protected function transcode($string, array $funcs) {
if (isset($funcs[$this->encoding])) {
$func = $funcs[$this->encoding];
return $func($string);
}
return $string;
}
// add font-display declaration if required
protected function transform($string) {
if (!$this->mode) {
return $string;
}
// Google announced support; do nothing
if (strpos($string, 'font-display:')) {
return $string;
}
return str_replace('}', " font-display: $this->mode;\n}", $string);
}
// capture response headers from curl request (one call per header field)
protected function writeCurlHeader($ch, $header) {
// store the encoding for later
if (substr($header, 0, 16) == 'Content-Encoding') {
$this->encoding = trim(substr($header, 17));
}
header($header);
return strlen($header);
}
// capture response body from curl request (can arrive in multiple chunks)
protected function writeCurlBody($ch, $chunk) {
$this->body .= $chunk;
return strlen($chunk);
}
// make the font request
public function request() {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://fonts.googleapis.com/css?' . $this->server['QUERY_STRING'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 1,
CURLOPT_TIMEOUT => 5,
CURLOPT_HTTPHEADER => $this->getRequestHeaders(),
CURLOPT_HEADER => false,
CURLOPT_BUFFERSIZE => 8192,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADERFUNCTION => array($this, 'writeCurlHeader'),
CURLOPT_WRITEFUNCTION => array($this, 'writeCurlBody'),
));
curl_exec($ch);
curl_close($ch);
return $this->output();
}
// output the transformed content
protected function output() {
if (!$this->mode) {
return $this->body; // save transcoding for nothing
}
return $this->encode($this->transform($this->decode($this->body)));
}
public function __toString() {
return $this->request();
}
}
echo new Fonts($_SERVER, isset($_GET['display']) ? $_GET['display'] : '');