forked from liuyunzhuge/php_weixin_proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
106 lines (93 loc) · 2.26 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
<?php
function is_HTTPS()
{
if (!isset($_SERVER['HTTPS'])) return FALSE;
if ($_SERVER['HTTPS'] === 1) { //Apache
return TRUE;
} elseif ($_SERVER['HTTPS'] === 'on') { //IIS
return TRUE;
} elseif ($_SERVER['SERVER_PORT'] == 443) { //其他
return TRUE;
}
return FALSE;
}
function getDomain()
{
$server_name = $_SERVER['SERVER_NAME'];
if (strpos($server_name, 'www.') !== false) {
return substr($server_name, 4);
}
return $server_name;
}
$appid = '';
$scope = 'snsapi_login';
$state = '';
$code = '';
$redirect_uri = '';
$device = '';
$protocol = '';
if (is_HTTPS()) {
$protocol = 'https';
} else {
$protocol = 'http';
}
if (isset($_GET['device'])) {
$device = $_GET['device'];
}
if (isset($_GET['appid'])) {
$appid = $_GET['appid'];
}
if (isset($_GET['state'])) {
$state = $_GET['state'];
}
if (isset($_GET['redirect_uri'])) {
$redirect_uri = $_GET['redirect_uri'];
}
if (isset($_GET['code'])) {
$code = $_GET['code'];
}
if (isset($_GET['scope'])) {
$scope = $_GET['scope'];
}
if ($code == 'test') {
exit;
}
if (empty($code)) {
$authUrl = '';
if ($device == 'pc') {
$authUrl = 'https://open.weixin.qq.com/connect/qrconnect';
} else {
$authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize';
}
$options = [
$authUrl,
'?appid=' . $appid,
'&redirect_uri=' . urlencode($protocol . '://' . $_SERVER['HTTP_HOST'] . '/'),
'&response_type=code',
'&scope=' . $scope,
'&state=' . $state,
'#wechat_redirect'
];
//把redirect_uri先写到cookie
header(implode('', [
"Set-Cookie: redirect_uri=",
urlencode($redirect_uri),
"; path=/; domain=",
getDomain(),
"; expires=" . gmstrftime("%A, %d-%b-%Y %H:%M:%S GMT", time() + 60),
"; Max-Age=" + 60,
"; httponly"
]));
header('Location: ' . implode('', $options));
} else {
if (isset($_COOKIE['redirect_uri'])) {
$back_url = urldecode($_COOKIE['redirect_uri']);
header('Location: ' . implode('', [
$back_url,
strpos($back_url, '?') ? '&' : '?',
'code=' . $code,
'&state=' . $state
]));
}
}
?>