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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
<?php
/**
* Class regrouping basic methods for download page.
*
* @copyright 2009-2011 Romain d'Alverny <rda>
* @license GPL-3+
*
*/
class Downloads
{
/**
* @param string $country matches the URI base dir
* @param string $locale
*/
function __construct($locale = 'en', $cache = null)
{
$this->_locale = $locale;
}
/**
* @param string $ua
*
* @return array
*/
public static function get_platform($ua = null)
{
if ($ua == '')
return array(
'arch' => 'i586',
'system' => 'unknow',
'locale' => 'en',
'browser' => null
);
$locale = null;
if (preg_match_all('/([.^\(^\)]*) \((.*)\) (.*)/', $ua, $r))
{
$r = $r[2][0];
$r = explode(';', $r);
$r = explode(')', trim($r[3]));
if (strlen($r[0]) > 5)
$r = substr($r[0], 0, 5);
else
$r = $r[0];
$locale = $r;
}
$arch = 'i586';
if (strpos($ua, 'x86_64') !== false)
$arch = 'x86_64';
$sys = null;
if (strpos($ua, 'Windows') !== false)
$sys = 'win';
elseif (strpos($ua, 'Macintosh') !== false
|| strpos($ua, 'Mac OS X') !== false)
$sys = 'mac';
elseif (strpos($ua, 'Linux') !== false)
$sys = 'linux';
$browser = null;
if (strpos($ua, 'Firefox') !== false)
$browser = 'firefox';
elseif (strpos($ua, 'MSIE') !== false)
$browser = 'msie';
elseif (strpos($ua, 'Safari') !== false)
$browser = 'safari';
elseif (strpos($ua, 'Opera') !== false)
$browser = 'opera';
return array(
'arch' => $arch,
'system' => $sys,
'locale' => $locale,
'browser' => $browser
);
}
/**
*/
public static function get_all_mirrors()
{
$prod = true;
$cache_file = '../../lib/cached.list.php';
if ($prod) {
require $cache_file;
return $mirrors;
}
// @todo cache this!
$data = file('http://mirrors.mageia.org/api/foo.Cauldron.i586.list');
$mirrors3 = array();
foreach ($data as $line) {
$line = explode(',', trim($line));
foreach ($line as $val) {
$val = explode('=', trim($val));
$m[$val[0]] = $val[1];
}
$pu = parse_url($m['url']);
if (in_array($pu['scheme'], array('http', 'https', 'ftp')))
$mirrors3[$m['country']][] = array(
'city' => $m['city'],
'zone' => $m['zone'],
'url' => str_replace('/distrib/cauldron/i586', '', $m['url'])
);
}
/*
file_put_contents($cache_file,
sprintf('<?php $mirrors = %s; ?>',
var_export($mirrors3, true)));
//*/
return $mirrors3;
}
/**
*/
function get_mirror($country)
{
$mirs = self::get_all_mirrors();
if (array_key_exists($country, $mirs))
{
$mirs = $mirs[$country];
shuffle($mirs);
return array_shift($mirs);
}
else
{
shuffle($mirs);
$mirs = array_shift($mirs);
shuffle($mirs);
return array_shift($mirs);
}
}
/**
* Queries a GeoIP db for $ip to get the country.
*
* @param string $ip
*
* @return string
*
* If fail, use $country as a fallback to return.
* @see http://www.maxmind.com/app/php & http://www.maxmind.com/app/geoip_country
* It has limitations too & needs to be updated from time to time.
*
* @todo make a separate class for this.
* @todo unit tests for this!
*/
function get_country($ip)
{
error_log(sprintf('in get_country with %s.', $ip));
if ($ip == '127.0.0.1' || $ip == '::1')
return null;
if (!function_exists('geoip_open')) {
require_once '../../lib/maxmind/geoip/geoip.inc.php';
}
$gi = geoip_open(realpath('../../lib/maxmind/geoip/GeoIP.dat'),
GEOIP_STANDARD);
$loc = geoip_country_code_by_addr($gi, $ip);
geoip_close($gi);
if (trim($loc) == '' || is_null($loc))
return null;
error_log(sprintf('in get_country: %s', $loc));
return strtoupper($loc);
}
/**
* Setup session data about current visitor for downloads.
*
* @param string $version
* @param boolean $force
*
* @return array
*/
function prepare_download($version, $force = false)
{
$fuzzy_mirror = false;
if (isset($_GET['country']))
{
$country = $_GET['country'];
$force = true;
}
if (!$force && isset($_SESSION['dl-data']))
{
error_log(sprintf('Got session data: %s', print_r($_SESSION['dl-data'], true)));
$system = $_SESSION['dl-data']['system'];
if (isset($_GET['mirror']))
{
$mirror = array('url' => $_GET['mirror']);
$mirror['purl'] = parse_url($mirror['url']);
$_SESSION['dl-data']['mirror'] = $mirror;
$country = '';
}
else
{
$country = $_SESSION['dl-data']['country'];
$mirror = $_SESSION['dl-data']['mirror'];
}
}
else
{
error_log('getting platform');
$system = self::get_platform($_SERVER['HTTP_USER_AGENT']);
if (isset($_GET['mirror']))
{
$mirror = array('url' => $_GET['mirror']);
$mirror['purl'] = parse_url($mirror['url']);
$country = '';
}
else
{
error_log('no mirror set yet');
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])
&& $str = $_SERVER['HTTP_X_FORWARDED_FOR'])
{
$arr = explode(', ', $str);
$ip = $arr[0];
}
else
$ip = $_SERVER['REMOTE_ADDR'];
$_SESSION['ip'] = $ip;
if (isset($_GET['country']))
$country = $_GET['country'];
else
$country = self::get_country($ip);
$mirror = $this->get_mirror($country);
$fuzzy_mirror = is_null($country) ? true : false;
$mirror['purl'] = parse_url($mirror['url']);
// reassign country, as get_one_mirror() may have decided
// to return a mirror from another country than the one
// requested initially - @see get_one_mirror()
$country = $mirror['zone'];
if (is_null($mirror)) {
// @todo?
}
}
$_SESSION['dl-data'] = array(
'system' => $system,
'country' => $country,
'mirror' => $mirror
);
//echo "<pre>Got NEW session data:", print_r($_SESSION['dl-data'], true), '</pre>';
}
//
return array(
'arch' => $system['arch'],
'mirror_host' => $mirror['purl']['host'],
'mirror_scheme' => $mirror['purl']['scheme'],
'mirror_url' => $mirror['url'],
'country' => $country,
'fuzzy_mirror' => $fuzzy_mirror
);
}
}
|