aboutsummaryrefslogtreecommitdiffstats
path: root/en/downloads/get/lib.php
blob: fdae33c44d899cbd3423d87eb1df2387d0de61c8 (plain)
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
<?php
/**
 * PHP version 5.4
 *
 * @category Mageia
 * @package  Mageia\Web\www
 * @author   rda <rda@mageia.org>
 * @license  http://www.gnu.org/licenses/gpl-2.0.html GPL-2+
 * @link     http://www.mageia.org/
 *
*/
// note, we use geographical country names
$countries = array (
  'AR' => _r("Argentina"),
  'AU' => _r("Australia"),
  'BE' => _r("Belgique"),
  'BG' => _r("България - Bulgaria"),
  'BR' => _r("Brasil"),
  'BY' => _r("Беларусь - Belarus"),
  'CA' => _r("Canada"),
  'CH' => _r("Switzerland"),
  'CN' => _r("中国 - China"),
  'CZ' => _r("Česko"),
  'DE' => _r("Deutschland"),
  'DK' => _r("Danmark"),
  'EC' => _r("Ecuador"),
  'ES' => _r("España"),
  'FR' => _r("France"),
  'GB' => _r("Great Britain"),
  'GR' => _r("Ελλάδα - Greece"),
  'GT' => _r("Guatemala"),
  'HU' => _r("Hungary"),
  'ID' => _r("Indonesia"),
  'IL' => _r("Israel"),
  'IT' => _r("Italia"),
  'JP' => _r("日本国 - Japan"),
  'NC' => _r("Nouvelle-Calédonie"),
  'NL' => _r("Nederlands"),
  'PH' => _r("Philipines"),
  'PL' => _r("Polska"),
  'RU' => _r("Россия - Russia"),
  'SE' => _r("Sverige"),
  'TR' => _r("Türkiye"),
  'TW' => _r("臺灣 - Taiwan"),
  'UA' => _r("Ukraine"),
  'UK' => _r("the UK"),
  'US' => _r("the USA"),
  'VN' => _r("Vietnam"),
  'ZA' => _r("South Africa"),
);

$cities_i18n = array (
  'Adelaide' => _r("Adelaide"),
  'Amsterdam' => _r("Amsterdam"),
  'Ankara' => _r("Ankara"),
  'Beauharnois' => _r("Beauharnois"),
  'Beijing' => _r("Beijing"), // '北京', // .cn
  'Brasilia' => _r("Brasilia"),
  'Brisbane' => _r("Brisbane"),
  'Brno' => _r("Brno"),
  'Brussels' => _r("Brussels"),
  'Canterbury' => _r("Canterbury"),
  'Cebu' => _r("Cebu"),
  'Chungli' => _r("Chungli"),
  'Cuenca' => _r("Cuenca"),
  'DTU' => _r("DTU"),
  'Durham' => _r("Durham"),
  'Düsseldorf' => _r("Düsseldorf"),
  'Enschede' => _r("Enschede"),
  'Erlangen' => _r("Erlangen"),
  'Falkenstein/Vogtl.' => _r("Falkenstein/Vogtl."),
  'Göttingen' => _r("Göttingen"),
  'Heraklion' => _r("Heraklion"), // 'Ηράκλειο', // .gr
  'HsinChu' => _r("HsinChu"), // '新竹市', // .tw
  'Lenoir' => _r("Lenoir"),
  'Lyon' => _r("Lyon"),
  'Milan' => _r("Milan"),
  'Minsk' => _r("Minsk"), // 'Мінск', // .by
  'Miskolc' => _r("Miskolc"),
  'Moscow' => _r("Moscow"), // 'Москва', // .ru
  'Paris' => _r("Paris"),
  'Prague' => _r("Prague"), // 'Praha', // .cz
  'Princeton' => _r("Princeton"),
  'Schneverdingen' => _r("Schneverdingen"),
  'Shanghai' => _r("Shanghai"),
  'Sofia' => _r("Sofia"),
  'Stellenbosch' => _r("Stellenbosch"),
  'Taipei' => _r("Taipei"),
  'Tsukuba' => _r("Tsukuba"),
  'Umeå' => _r("Umeå"),
  'Vinnytsia' => _r("Vinnytsia"),
  'Warszawa' => _r("Warszawa"),
  'Yonezawa' => _r("Yonezawa"), // '米沢市', // .jp
  'ruchmond' => _r("Richmond"),
  'Краснодар' => _r("Краснодар"),
);


/**
 * Rewrite city name in the local official language.
 *
 * @param string $name city name
 *
 * @return string
*/
function rewrite_city($name, $cities_for_rewrite)
{
    if (array_key_exists($name, $cities_for_rewrite)) {
        return $cities_for_rewrite[$name];
    }

    return $name;
}

/**
 * Return $_GET value for $s key if it exists.
 *
 * @param string $s key
 *
 * @return mixed
*/
function get($s)
{
    if (isset($_GET[$s])) {
        return strip_tags(trim($_GET[$s]));
    }

    return null;
}

class NoProductFoundError extends Exception {}
class NoMirrorFoundError extends Exception {}

/**
 * TODO use aliases, so that downloads asking for alpha3
 * get redirected to beta1 for instance? (on migration)
 *
 * @param array $product array definition
 * @param string $def_file definition file
 *
 * @return array
*/
function get_info_for_product($product, $def_file = null)
{
    $def_file = is_null($def_file) ? 'definitions.ini' : $def_file;

    $defs = parse_ini_file($def_file, true);

    if (array_key_exists($product, $defs)) {
        return $defs[$product];
    }

    throw new NoProductFoundError;
}

/**
 * Return mirrors for $file.
 * First mirror returned is the preferred one for auto redirection.
 *
 * @param string $file    id of the file to download/find a mirror for
 * @param string $locale  hint for selecting a mirror
 * @param string $country hint for selecting a mirror
 *
 * @return array
 * mirror(product):
 *   name
 *   host
 *   country
 *   city
 *   speed
 *   link
*/
function get_mirrors_for($file, $locale = null, $country = null, $prod = true, $documentation = false)
{
    //include '../../../lib/Downloads.php';

    $mirrors = Downloads::get_all_mirrors($prod, $documentation);
    $wsd     = new Downloads();
    $one     = $wsd->prepare_download(true, $country, $prod, $documentation);

    return array($one, $mirrors);
}

/**
 * Simplifies things.
 *
 * @param array   $product           _one_ product definition array
 * @param boolean $torrent_preferred do we prefer to get a torrent, if available?
 *
 * @return string
*/
function get_download_link($product, $torrent_preferred = false)
{
    if ($torrent_preferred === true
        && isset($product['torrent'])
        && strlen($product['torrent']) > 0
    ) {

        $path = $product['torrent'];
    } else {
        $path = $product['path'] . '/' . $product['file'];
    }

    return '$MIRROR/' . $path;
}

/**
 * Builds human readable list from array with l10n option
 *
 * @param array  $array             to build the string from
 * @param string $last_separator    flexible (l10n) last separator
 * @param string $nonlast_separator flexible (l10n) other than last separators
 *
 * @return string
*/
function array_to_list($array, $last_separator = ' and ', $nonlast_separator = ', ')
{
    $num_of_values  = count($array);
    $output_string = '';
    $separator = $nonlast_separator;
    foreach ($array as $value) {
        $output_string .= $value;
        if ($num_of_values == 2) {
            $separator = $last_separator;
        } else if ($num_of_values == 1) {
            $separator = '';
        }
        $output_string .= $separator;
        $num_of_values--;
    }

    return $output_string;
}