Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
178 lines (159 loc) · 4.81 KB
/
Copy pathindex.php
File metadata and controls
178 lines (159 loc) · 4.81 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
$hellosalut = array(
"code" => "none",
"hello" => "Hello"
);
function getIP() {
/*
Source: http://admincmd.blogspot.com/2007/08/php-get-client-ip-address.html
*/
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "UNKNOWN";
return $ip;
}
/*
Source: http://www.dyeager.org/blog/2008/10/getting-browser-default-language-php.html
*/
function getDefaultLanguage() {
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
return parseDefaultLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
else
return parseDefaultLanguage(NULL);
}
function parseDefaultLanguage($http_accept, $deflang = "en") {
if(isset($http_accept) && strlen($http_accept) > 1) {
# Split possible languages into array
$x = explode(",",$http_accept);
foreach ($x as $val) {
#check for q-value and create associative array. No q-value means 1 by rule
if(preg_match("/(.*);q=([0-1]{0,1}\.\d{0,4})/i",$val,$matches))
$lang[$matches[1]] = (float)$matches[2];
else
$lang[$val] = 1.0;
}
$qval = 0.0;
foreach ($lang as $key => $value) {
if ($value > $qval) {
$qval = (float)$value;
$deflang = $key;
}
}
}
return strtolower($deflang);
}
function getHelloFromLang($lang) {
$connection = mysqli_connect( getenv( "DB_HOST" ), getenv( "DB_USERNAME" ), getenv( "DB_PASSWORD" ), getenv( "DB_NAME" ) );
// $connection->set_charset("UTF-8");
if (mysqli_connect_errno()){
echo "Failed to connect to the database: " . mysqli_connect_error();
}
$query = "SELECT
cc.hello
FROM
ip2nationCountries cc
WHERE
cc.language = ?";
$stmt = $connection->prepare($query);
$stmt->bind_param("s", $lang);
$stmt->execute();
$stmt->bind_result($hello);
$stmt->fetch();
if (!is_null($hello)){
global $hellosalut;
$hellosalut["code"] = $lang;
$hellosalut["hello"] = $hello;
// $hellosalut["char_set"] = $connection->character_set_name();
}
}
function getHelloFromCountry($code) {
$connection = mysqli_connect( getenv( "DB_HOST" ), getenv( "DB_USERNAME" ), getenv( "DB_PASSWORD" ), getenv( "DB_NAME" ) );
// $connection->set_charset("UTF-8");
if (mysqli_connect_errno()){
echo "Failed to connect to the database: " . mysqli_connect_error();
}
$query = "SELECT
cc.hello
FROM
ip2nationCountries cc
WHERE
cc.code = ?";
$stmt = $connection->prepare($query);
$stmt->bind_param("s", $code);
$stmt->execute();
$stmt->bind_result($hello);
$stmt->fetch();
if (!is_null($hello)){
global $hellosalut;
$hellosalut["code"] = $code;
$hellosalut["hello"] = $hello;
}
}
function getHelloFromIP($ipRaw) {
$ip = sprintf("%u\n", ip2long($ipRaw));
$connection = mysqli_connect( getenv( "DB_HOST" ), getenv( "DB_USERNAME" ), getenv( "DB_PASSWORD" ), getenv( "DB_NAME" ) );
// $connection->set_charset("UTF-8");
if (mysqli_connect_errno()){
echo "Failed to connect to the database: " . mysqli_connect_error();
}
$query = "SELECT
cc.code,
cc.hello
FROM
ip2nationCountries cc,
ip2nation ip
WHERE
ip.ip < ?
AND
cc.code = ip.country
ORDER BY
ip.ip DESC
LIMIT 0,1";
$stmt = $connection->prepare($query);
$stmt->bind_param("i", $ip);
$stmt->execute();
$stmt->bind_result($code, $hello);
$stmt->fetch();
if (!is_null($hello)){
global $hellosalut;
$hellosalut["code"] = $code;
$hellosalut["hello"] = $hello;
}
}
if (!isset($_REQUEST['ip']) && !isset($_REQUEST['lang']) && !isset($_REQUEST['mode']) && !isset($_REQUEST['cc'])) {
echo "Please see <a href='https://stefanbohacek.com/project/hellosalut-api/'>https://stefanbohacek.com/project/hellosalut-api/</a> for details on how to use this service.";
}
else{
if (isset($_REQUEST['mode'])){
switch ($_REQUEST['mode']){
case "auto":
getHelloFromIP(getIP());
getHelloFromLang(getDefaultLanguage());
break;
}
}
else{
if (isset($_REQUEST['lang'])) {
getHelloFromLang($_REQUEST['lang']);
}
else{
if (isset($_REQUEST['ip'])) {
getHelloFromIP($_REQUEST['ip']);
}
else{
if (isset($_REQUEST['cc'])){
getHelloFromCountry($_REQUEST['cc']);
}
}
}
}
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
echo json_encode($hellosalut);
}