1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Reusable API for l10n remote updates.
|
6
|
*/
|
7
|
|
8
|
include_once DRUPAL_ROOT . '/includes/locale.inc';
|
9
|
module_load_include('locale.inc', 'l10n_update');
|
10
|
|
11
|
/**
|
12
|
* Default update server, filename and URL.
|
13
|
*/
|
14
|
define('L10N_UPDATE_DEFAULT_SERVER', 'localize.drupal.org');
|
15
|
define('L10N_UPDATE_DEFAULT_SERVER_URL', 'http://localize.drupal.org/l10n_server.xml');
|
16
|
define('L10N_UPDATE_DEFAULT_UPDATE_URL', 'http://ftp.drupal.org/files/translations/%core/%project/%project-%release.%language.po');
|
17
|
|
18
|
// Translation filename, will be used just for local imports
|
19
|
define('L10N_UPDATE_DEFAULT_FILENAME', '%project-%release.%language.po');
|
20
|
|
21
|
// Translation status: String imported from po
|
22
|
define('L10N_UPDATE_STRING_DEFAULT', 0);
|
23
|
|
24
|
// Translation status: Custom string, overridden original import
|
25
|
define('L10N_UPDATE_STRING_CUSTOM', 1);
|
26
|
|
27
|
/**
|
28
|
* Retrieve data for default server.
|
29
|
*
|
30
|
* @return array
|
31
|
* Server parameters:
|
32
|
* name : Localization server name
|
33
|
* server_url : Localization server URL where language list can be retrieved.
|
34
|
* update_url : URL containing po file pattern.
|
35
|
*/
|
36
|
function l10n_update_default_server() {
|
37
|
return array(
|
38
|
'name' => variable_get('l10n_update_default_server', L10N_UPDATE_DEFAULT_SERVER),
|
39
|
'server_url' => variable_get('l10n_update_default_server_url', L10N_UPDATE_DEFAULT_SERVER_URL),
|
40
|
'update_url' => variable_get('l10n_update_default_update_url', L10N_UPDATE_DEFAULT_UPDATE_URL),
|
41
|
);
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Download and import remote translation file.
|
46
|
*
|
47
|
* @param $download_url
|
48
|
* Download URL.
|
49
|
* @param $locale
|
50
|
* Language code.
|
51
|
* @param $mode
|
52
|
* Download mode. How to treat exising and modified translations.
|
53
|
*
|
54
|
* @return boolean
|
55
|
* TRUE on success.
|
56
|
*/
|
57
|
function l10n_update_download_import($download_url, $locale, $mode = LOCALE_IMPORT_OVERWRITE) {
|
58
|
if ($file = l10n_update_download_file($download_url)) {
|
59
|
$result = l10n_update_import_file($file, $locale, $mode);
|
60
|
return $result;
|
61
|
}
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Import local file into the database.
|
66
|
*
|
67
|
* @param $file
|
68
|
* File object of localy stored file
|
69
|
* or path to localy stored file.
|
70
|
* @param $locale
|
71
|
* Language code.
|
72
|
* @param $mode
|
73
|
* Download mode. How to treat exising and modified translations.
|
74
|
*
|
75
|
* @return boolean
|
76
|
* Result array on success. FALSE on failure
|
77
|
*/
|
78
|
function l10n_update_import_file($file, $locale, $mode = LOCALE_IMPORT_OVERWRITE) {
|
79
|
// If the file is a uri, create a $file object
|
80
|
if (is_string($file)) {
|
81
|
$uri = $file;
|
82
|
$file = new stdClass();
|
83
|
$file->uri = $uri;
|
84
|
$file->filename = $uri;
|
85
|
}
|
86
|
return _l10n_update_locale_import_po($file, $locale, $mode, 'default');
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Get remote file and download it to a temporary path.
|
91
|
*
|
92
|
* @param $download_url
|
93
|
* URL of remote file.
|
94
|
* @param $destination
|
95
|
* URL of local destination file. By default the download will be stored
|
96
|
* in a temporary file.
|
97
|
*/
|
98
|
function l10n_update_download_file($download_url, $destination = NULL) {
|
99
|
$t = get_t();
|
100
|
$variables['%download_link'] = $download_url;
|
101
|
|
102
|
// Create temporary file or use specified file destination.
|
103
|
// Temporary files get a 'translation-' file name prefix.
|
104
|
$file = $destination ? $destination : drupal_tempnam(file_directory_temp(), 'translation-');
|
105
|
|
106
|
if ($file) {
|
107
|
$variables['%tmpfile'] = $file;
|
108
|
// We download and store the file (in one if statement! Isnt't that neat ;) ).
|
109
|
// @todo remove the timeout once we use the batch API to download the files.
|
110
|
if (($contents = drupal_http_request($download_url, array('timeout' => 90))) && $contents->code == 200 && $file_result = file_put_contents($file, $contents->data)) {
|
111
|
watchdog('l10n_update', 'Successfully downloaded %download_link to %tmpfile', $variables);
|
112
|
return $file;
|
113
|
}
|
114
|
else {
|
115
|
if (isset($contents->error)) {
|
116
|
watchdog('l10n_update', 'An error occured during the download operation: %error.', array('%error' => $contents->error), WATCHDOG_ERROR);
|
117
|
}
|
118
|
elseif (isset($contents->code) && $contents->code != 200) {
|
119
|
watchdog('l10n_update', 'An error occured during the download operation: HTTP status code %code.', array('%code' => $contents->code), WATCHDOG_ERROR);
|
120
|
}
|
121
|
if (isset($file_result)) {
|
122
|
// file_put_contents() was called but returned FALSE.
|
123
|
watchdog('l10n_update', 'Unable to save %download_link file to %tmpfile.', $variables, WATCHDOG_ERROR);
|
124
|
}
|
125
|
}
|
126
|
|
127
|
}
|
128
|
else {
|
129
|
$variables['%tmpdir'] = file_directory_temp();
|
130
|
watchdog('l10n_update', 'Error creating temporary file for download in %tmpdir. Remote file is %download_link.', $variables, WATCHDOG_ERROR);
|
131
|
}
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* Get names for the language list from locale system.
|
136
|
*
|
137
|
* @param $string_list
|
138
|
* Comma separated list of language codes.
|
139
|
* Language codes must exist in languages from _locale_get_predefined_list().
|
140
|
* @return array
|
141
|
* Array of language names keyed by language code.
|
142
|
*/
|
143
|
function l10n_update_get_language_names($string_list) {
|
144
|
$t = get_t();
|
145
|
$language_codes = array_map('trim', explode(',', $string_list));
|
146
|
$languages = _locale_get_predefined_list();
|
147
|
$result = array();
|
148
|
foreach ($language_codes as $lang) {
|
149
|
if (array_key_exists($lang, $languages)) {
|
150
|
// Try to use verbose locale name
|
151
|
$name = $lang;
|
152
|
$name = $languages[$name][0] . (isset($languages[$name][1]) ? ' ' . $t('(@language)', array('@language' => $languages[$name][1])) : '');
|
153
|
$result[$lang] = $name;
|
154
|
}
|
155
|
}
|
156
|
return $result;
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Build project data as an object.
|
161
|
*
|
162
|
* @param $name
|
163
|
* Project name.
|
164
|
* @param $version
|
165
|
* Project version.
|
166
|
* @param $server
|
167
|
* Localisation server name.
|
168
|
* @param $path
|
169
|
* Localisation server URL.
|
170
|
* @return object
|
171
|
* Project object containing the supplied data.
|
172
|
*/
|
173
|
function _l10n_update_build_project($name, $version = NULL, $server = L10N_UPDATE_DEFAULT_SERVER, $path = L10N_UPDATE_DEFAULT_SERVER_URL) {
|
174
|
$project = new stdClass();
|
175
|
$project->name = $name;
|
176
|
$project->version = $version;
|
177
|
$project->l10n_server = $server;
|
178
|
$project->l10n_path = $path;
|
179
|
return $project;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Update the file history table.
|
184
|
*
|
185
|
* @param $file
|
186
|
* Object representing the file just imported or downloaded.
|
187
|
* @return integer
|
188
|
* FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED.
|
189
|
* @see drupal_write_record()
|
190
|
*/
|
191
|
function l10n_update_file_history($file) {
|
192
|
// Update or write new record
|
193
|
if (db_query("SELECT project FROM {l10n_update_file} WHERE project = :project AND language = :language", array(':project' => $file->project, ':language' => $file->language))->fetchField()) {
|
194
|
$update = array('project', 'language');
|
195
|
}
|
196
|
else {
|
197
|
$update = array();
|
198
|
}
|
199
|
return drupal_write_record('l10n_update_file', $file, $update);
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* Delete the history of downloaded translations.
|
204
|
*
|
205
|
* @param string $langcode
|
206
|
* Language code of the file history to be deleted.
|
207
|
*/
|
208
|
function l10n_update_delete_file_history($langcode) {
|
209
|
db_delete('l10n_update_file')
|
210
|
->condition('language', $langcode)
|
211
|
->execute();
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Flag the file history as up to date.
|
216
|
*
|
217
|
* Compare history data in the {l10n_update_file} table with translations
|
218
|
* available at translations server(s). Update the 'last_checked' timestamp of
|
219
|
* the files which are up to date.
|
220
|
*
|
221
|
* @param $available
|
222
|
* Available translations as retreived from remote server.
|
223
|
*/
|
224
|
function l10n_update_flag_history($available) {
|
225
|
if ($history = l10n_update_get_history()) {
|
226
|
foreach($history as $name => $project) {
|
227
|
foreach ($project as $langcode => $current) {
|
228
|
if (isset($available[$name][$langcode])) {
|
229
|
$update = $available[$name][$langcode];
|
230
|
// When the available update is equal to the current translation the current
|
231
|
// is marked checked in the {l10n_update_file} table.
|
232
|
if (_l10n_update_source_compare($current, $update) == 0 && $current->version == $update->version) {
|
233
|
db_update('l10n_update_file')
|
234
|
->fields(array(
|
235
|
'last_checked' => REQUEST_TIME,
|
236
|
))
|
237
|
->condition('project', $current->project)
|
238
|
->condition('language', $current->language)
|
239
|
->execute();
|
240
|
}
|
241
|
}
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* Check if remote file exists and when it was last updated.
|
249
|
*
|
250
|
* @param $url
|
251
|
* URL of remote file.
|
252
|
* @param $headers
|
253
|
* HTTP request headers.
|
254
|
* @return object
|
255
|
* Result object containing the HTTP request headers, response code, headers,
|
256
|
* data, redirect status and updated timestamp.
|
257
|
* @see l10n_update_http_request()
|
258
|
*/
|
259
|
function l10n_update_http_check($url, $headers = array()) {
|
260
|
$result = l10n_update_http_request($url, array('headers' => $headers, 'method' => 'HEAD'));
|
261
|
if (!isset($result->error)) {
|
262
|
if ($result && $result->code == 200) {
|
263
|
$result->updated = isset($result->headers['last-modified']) ? strtotime($result->headers['last-modified']) : 0;
|
264
|
}
|
265
|
return $result;
|
266
|
}
|
267
|
else {
|
268
|
switch ($result->code) {
|
269
|
case 404:
|
270
|
// File not found occurs when a translation file is not yet available
|
271
|
// at the translation server. But also if a custom module or custom
|
272
|
// theme does not define the location of a translation file. By default
|
273
|
// the file is checked at the translation server, but it will not be
|
274
|
// found there.
|
275
|
watchdog('l10n_update', 'File not found: @uri.', array('@uri' => $url));
|
276
|
return TRUE;
|
277
|
case 0:
|
278
|
watchdog('l10n_update', 'Error occurred when trying to check @remote: @errormessage.', array('@errormessage' => $result->error, '@remote' => $url), WATCHDOG_ERROR);
|
279
|
break;
|
280
|
default:
|
281
|
watchdog('l10n_update', 'HTTP error @errorcode occurred when trying to check @remote.', array('@errorcode' => $result->code, '@remote' => $url), WATCHDOG_ERROR);
|
282
|
break;
|
283
|
}
|
284
|
}
|
285
|
return $result;
|
286
|
}
|
287
|
|
288
|
/**
|
289
|
* Perform an HTTP request.
|
290
|
*
|
291
|
* We cannot use drupal_http_request() at install, see http://drupal.org/node/527484
|
292
|
*
|
293
|
* This is a flexible and powerful HTTP client implementation. Correctly
|
294
|
* handles GET, POST, PUT or any other HTTP requests. Handles redirects.
|
295
|
*
|
296
|
* @param $url
|
297
|
* A string containing a fully qualified URI.
|
298
|
* @param array $options
|
299
|
* (optional) An array that can have one or more of the following elements:
|
300
|
* - headers: An array containing request headers to send as name/value pairs.
|
301
|
* - method: A string containing the request method. Defaults to 'GET'.
|
302
|
* - data: A string containing the request body, formatted as
|
303
|
* 'param=value¶m=value&...'. Defaults to NULL.
|
304
|
* - max_redirects: An integer representing how many times a redirect
|
305
|
* may be followed. Defaults to 3.
|
306
|
* - timeout: A float representing the maximum number of seconds the function
|
307
|
* call may take. The default is 30 seconds. If a timeout occurs, the error
|
308
|
* code is set to the HTTP_REQUEST_TIMEOUT constant.
|
309
|
* - context: A context resource created with stream_context_create().
|
310
|
*
|
311
|
* @return object
|
312
|
* An object that can have one or more of the following components:
|
313
|
* - request: A string containing the request body that was sent.
|
314
|
* - code: An integer containing the response status code, or the error code
|
315
|
* if an error occurred.
|
316
|
* - protocol: The response protocol (e.g. HTTP/1.1 or HTTP/1.0).
|
317
|
* - status_message: The status message from the response, if a response was
|
318
|
* received.
|
319
|
* - redirect_code: If redirected, an integer containing the initial response
|
320
|
* status code.
|
321
|
* - redirect_url: If redirected, a string containing the URL of the redirect
|
322
|
* target.
|
323
|
* - error: If an error occurred, the error message. Otherwise not set.
|
324
|
* - headers: An array containing the response headers as name/value pairs.
|
325
|
* HTTP header names are case-insensitive (RFC 2616, section 4.2), so for
|
326
|
* easy access the array keys are returned in lower case.
|
327
|
* - data: A string containing the response body that was received.
|
328
|
*/
|
329
|
function l10n_update_http_request($url, array $options = array()) {
|
330
|
$result = new stdClass();
|
331
|
|
332
|
// Parse the URL and make sure we can handle the schema.
|
333
|
$uri = @parse_url($url);
|
334
|
|
335
|
if ($uri == FALSE) {
|
336
|
$result->error = 'unable to parse URL';
|
337
|
$result->code = -1001;
|
338
|
return $result;
|
339
|
}
|
340
|
|
341
|
if (!isset($uri['scheme'])) {
|
342
|
$result->error = 'missing schema';
|
343
|
$result->code = -1002;
|
344
|
return $result;
|
345
|
}
|
346
|
|
347
|
timer_start(__FUNCTION__);
|
348
|
|
349
|
// Merge the default options.
|
350
|
$options += array(
|
351
|
'headers' => array(),
|
352
|
'method' => 'GET',
|
353
|
'data' => NULL,
|
354
|
'max_redirects' => 3,
|
355
|
'timeout' => 30.0,
|
356
|
'context' => NULL,
|
357
|
);
|
358
|
|
359
|
// Merge the default headers.
|
360
|
$options['headers'] += array(
|
361
|
'User-Agent' => 'Drupal (+http://drupal.org/)',
|
362
|
);
|
363
|
|
364
|
// stream_socket_client() requires timeout to be a float.
|
365
|
$options['timeout'] = (float) $options['timeout'];
|
366
|
|
367
|
// Use a proxy if one is defined and the host is not on the excluded list.
|
368
|
$proxy_server = variable_get('proxy_server', '');
|
369
|
if ($proxy_server && _drupal_http_use_proxy($uri['host'])) {
|
370
|
// Set the scheme so we open a socket to the proxy server.
|
371
|
$uri['scheme'] = 'proxy';
|
372
|
// Set the path to be the full URL.
|
373
|
$uri['path'] = $url;
|
374
|
// Since the URL is passed as the path, we won't use the parsed query.
|
375
|
unset($uri['query']);
|
376
|
|
377
|
// Add in username and password to Proxy-Authorization header if needed.
|
378
|
if ($proxy_username = variable_get('proxy_username', '')) {
|
379
|
$proxy_password = variable_get('proxy_password', '');
|
380
|
$options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : ''));
|
381
|
}
|
382
|
// Some proxies reject requests with any User-Agent headers, while others
|
383
|
// require a specific one.
|
384
|
$proxy_user_agent = variable_get('proxy_user_agent', '');
|
385
|
// The default value matches neither condition.
|
386
|
if ($proxy_user_agent === NULL) {
|
387
|
unset($options['headers']['User-Agent']);
|
388
|
}
|
389
|
elseif ($proxy_user_agent) {
|
390
|
$options['headers']['User-Agent'] = $proxy_user_agent;
|
391
|
}
|
392
|
}
|
393
|
|
394
|
switch ($uri['scheme']) {
|
395
|
case 'proxy':
|
396
|
// Make the socket connection to a proxy server.
|
397
|
$socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080);
|
398
|
// The Host header still needs to match the real request.
|
399
|
$options['headers']['Host'] = $uri['host'];
|
400
|
$options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : '';
|
401
|
break;
|
402
|
|
403
|
case 'http':
|
404
|
case 'feed':
|
405
|
$port = isset($uri['port']) ? $uri['port'] : 80;
|
406
|
$socket = 'tcp://' . $uri['host'] . ':' . $port;
|
407
|
// RFC 2616: "non-standard ports MUST, default ports MAY be included".
|
408
|
// We don't add the standard port to prevent from breaking rewrite rules
|
409
|
// checking the host that do not take into account the port number.
|
410
|
$options['headers']['Host'] = $uri['host'] . ($port != 80 ? ':' . $port : '');
|
411
|
break;
|
412
|
|
413
|
case 'https':
|
414
|
// Note: Only works when PHP is compiled with OpenSSL support.
|
415
|
$port = isset($uri['port']) ? $uri['port'] : 443;
|
416
|
$socket = 'ssl://' . $uri['host'] . ':' . $port;
|
417
|
$options['headers']['Host'] = $uri['host'] . ($port != 443 ? ':' . $port : '');
|
418
|
break;
|
419
|
|
420
|
default:
|
421
|
$result->error = 'invalid schema ' . $uri['scheme'];
|
422
|
$result->code = -1003;
|
423
|
return $result;
|
424
|
}
|
425
|
|
426
|
if (empty($options['context'])) {
|
427
|
$fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
|
428
|
}
|
429
|
else {
|
430
|
// Create a stream with context. Allows verification of a SSL certificate.
|
431
|
$fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
|
432
|
}
|
433
|
|
434
|
// Make sure the socket opened properly.
|
435
|
if (!$fp) {
|
436
|
// When a network error occurs, we use a negative number so it does not
|
437
|
// clash with the HTTP status codes.
|
438
|
$result->code = -$errno;
|
439
|
$result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket));
|
440
|
|
441
|
// Mark that this request failed. This will trigger a check of the web
|
442
|
// server's ability to make outgoing HTTP requests the next time that
|
443
|
// requirements checking is performed.
|
444
|
// See system_requirements().
|
445
|
// variable_set('drupal_http_request_fails', TRUE);
|
446
|
|
447
|
return $result;
|
448
|
}
|
449
|
|
450
|
// Construct the path to act on.
|
451
|
$path = isset($uri['path']) ? $uri['path'] : '/';
|
452
|
if (isset($uri['query'])) {
|
453
|
$path .= '?' . $uri['query'];
|
454
|
}
|
455
|
|
456
|
// Only add Content-Length if we actually have any content or if it is a POST
|
457
|
// or PUT request. Some non-standard servers get confused by Content-Length in
|
458
|
// at least HEAD/GET requests, and Squid always requires Content-Length in
|
459
|
// POST/PUT requests.
|
460
|
$content_length = strlen($options['data']);
|
461
|
if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
|
462
|
$options['headers']['Content-Length'] = $content_length;
|
463
|
}
|
464
|
|
465
|
// If the server URL has a user then attempt to use basic authentication.
|
466
|
if (isset($uri['user'])) {
|
467
|
$options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
|
468
|
}
|
469
|
|
470
|
// If the database prefix is being used by SimpleTest to run the tests in a copied
|
471
|
// database then set the user-agent header to the database prefix so that any
|
472
|
// calls to other Drupal pages will run the SimpleTest prefixed database. The
|
473
|
// user-agent is used to ensure that multiple testing sessions running at the
|
474
|
// same time won't interfere with each other as they would if the database
|
475
|
// prefix were stored statically in a file or database variable.
|
476
|
$test_info = &$GLOBALS['drupal_test_info'];
|
477
|
if (!empty($test_info['test_run_id'])) {
|
478
|
$options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
|
479
|
}
|
480
|
|
481
|
$request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
|
482
|
foreach ($options['headers'] as $name => $value) {
|
483
|
$request .= $name . ': ' . trim($value) . "\r\n";
|
484
|
}
|
485
|
$request .= "\r\n" . $options['data'];
|
486
|
$result->request = $request;
|
487
|
// Calculate how much time is left of the original timeout value.
|
488
|
$timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
|
489
|
if ($timeout > 0) {
|
490
|
stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
|
491
|
fwrite($fp, $request);
|
492
|
}
|
493
|
|
494
|
// Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
|
495
|
// and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
|
496
|
// instead must invoke stream_get_meta_data() each iteration.
|
497
|
$info = stream_get_meta_data($fp);
|
498
|
$alive = !$info['eof'] && !$info['timed_out'];
|
499
|
$response = '';
|
500
|
|
501
|
while ($alive) {
|
502
|
// Calculate how much time is left of the original timeout value.
|
503
|
$timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
|
504
|
if ($timeout <= 0) {
|
505
|
$info['timed_out'] = TRUE;
|
506
|
break;
|
507
|
}
|
508
|
stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
|
509
|
$chunk = fread($fp, 1024);
|
510
|
$response .= $chunk;
|
511
|
$info = stream_get_meta_data($fp);
|
512
|
$alive = !$info['eof'] && !$info['timed_out'] && $chunk;
|
513
|
}
|
514
|
fclose($fp);
|
515
|
|
516
|
if ($info['timed_out']) {
|
517
|
$result->code = HTTP_REQUEST_TIMEOUT;
|
518
|
$result->error = 'request timed out';
|
519
|
return $result;
|
520
|
}
|
521
|
// Parse response headers from the response body.
|
522
|
// Be tolerant of malformed HTTP responses that separate header and body with
|
523
|
// \n\n or \r\r instead of \r\n\r\n.
|
524
|
list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
|
525
|
$response = preg_split("/\r\n|\n|\r/", $response);
|
526
|
|
527
|
// Parse the response status line.
|
528
|
list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
|
529
|
$result->protocol = $protocol;
|
530
|
$result->status_message = $status_message;
|
531
|
|
532
|
$result->headers = array();
|
533
|
|
534
|
// Parse the response headers.
|
535
|
while ($line = trim(array_shift($response))) {
|
536
|
list($name, $value) = explode(':', $line, 2);
|
537
|
$name = strtolower($name);
|
538
|
if (isset($result->headers[$name]) && $name == 'set-cookie') {
|
539
|
// RFC 2109: the Set-Cookie response header comprises the token Set-
|
540
|
// Cookie:, followed by a comma-separated list of one or more cookies.
|
541
|
$result->headers[$name] .= ',' . trim($value);
|
542
|
}
|
543
|
else {
|
544
|
$result->headers[$name] = trim($value);
|
545
|
}
|
546
|
}
|
547
|
|
548
|
$responses = array(
|
549
|
100 => 'Continue',
|
550
|
101 => 'Switching Protocols',
|
551
|
200 => 'OK',
|
552
|
201 => 'Created',
|
553
|
202 => 'Accepted',
|
554
|
203 => 'Non-Authoritative Information',
|
555
|
204 => 'No Content',
|
556
|
205 => 'Reset Content',
|
557
|
206 => 'Partial Content',
|
558
|
300 => 'Multiple Choices',
|
559
|
301 => 'Moved Permanently',
|
560
|
302 => 'Found',
|
561
|
303 => 'See Other',
|
562
|
304 => 'Not Modified',
|
563
|
305 => 'Use Proxy',
|
564
|
307 => 'Temporary Redirect',
|
565
|
400 => 'Bad Request',
|
566
|
401 => 'Unauthorized',
|
567
|
402 => 'Payment Required',
|
568
|
403 => 'Forbidden',
|
569
|
404 => 'Not Found',
|
570
|
405 => 'Method Not Allowed',
|
571
|
406 => 'Not Acceptable',
|
572
|
407 => 'Proxy Authentication Required',
|
573
|
408 => 'Request Time-out',
|
574
|
409 => 'Conflict',
|
575
|
410 => 'Gone',
|
576
|
411 => 'Length Required',
|
577
|
412 => 'Precondition Failed',
|
578
|
413 => 'Request Entity Too Large',
|
579
|
414 => 'Request-URI Too Large',
|
580
|
415 => 'Unsupported Media Type',
|
581
|
416 => 'Requested range not satisfiable',
|
582
|
417 => 'Expectation Failed',
|
583
|
500 => 'Internal Server Error',
|
584
|
501 => 'Not Implemented',
|
585
|
502 => 'Bad Gateway',
|
586
|
503 => 'Service Unavailable',
|
587
|
504 => 'Gateway Time-out',
|
588
|
505 => 'HTTP Version not supported',
|
589
|
);
|
590
|
// RFC 2616 states that all unknown HTTP codes must be treated the same as the
|
591
|
// base code in their class.
|
592
|
if (!isset($responses[$code])) {
|
593
|
$code = floor($code / 100) * 100;
|
594
|
}
|
595
|
$result->code = $code;
|
596
|
|
597
|
switch ($code) {
|
598
|
case 200: // OK
|
599
|
case 304: // Not modified
|
600
|
break;
|
601
|
case 301: // Moved permanently
|
602
|
case 302: // Moved temporarily
|
603
|
case 307: // Moved temporarily
|
604
|
$location = $result->headers['location'];
|
605
|
$options['timeout'] -= timer_read(__FUNCTION__) / 1000;
|
606
|
if ($options['timeout'] <= 0) {
|
607
|
$result->code = HTTP_REQUEST_TIMEOUT;
|
608
|
$result->error = 'request timed out';
|
609
|
}
|
610
|
elseif ($options['max_redirects']) {
|
611
|
// Redirect to the new location.
|
612
|
$options['max_redirects']--;
|
613
|
$result = l10n_update_http_request($location, $options);
|
614
|
$result->redirect_code = $code;
|
615
|
}
|
616
|
if (!isset($result->redirect_url)) {
|
617
|
$result->redirect_url = $location;
|
618
|
}
|
619
|
break;
|
620
|
default:
|
621
|
$result->error = $status_message;
|
622
|
}
|
623
|
|
624
|
return $result;
|
625
|
}
|
626
|
|
627
|
/**
|
628
|
* Build abstract translation source, to be mapped to a file or a download.
|
629
|
*
|
630
|
* @param $project
|
631
|
* Project object containing data to be inserted in the template.
|
632
|
* @param $template
|
633
|
* String containing place holders. Available place holders:
|
634
|
* - '%project': Project name.
|
635
|
* - '%release': Poject version.
|
636
|
* - '%core': Project core version.
|
637
|
* - '%language': Language code.
|
638
|
* - '%filename': Project file name.
|
639
|
* @return string
|
640
|
* String with replaced place holders.
|
641
|
*/
|
642
|
function l10n_update_build_string($project, $template) {
|
643
|
$variables = array(
|
644
|
'%project' => $project->name,
|
645
|
'%release' => $project->version,
|
646
|
'%core' => $project->core,
|
647
|
'%language' => isset($project->language) ? $project->language : '%language',
|
648
|
'%filename' => isset($project->filename) ? $project->filename : '%filename',
|
649
|
);
|
650
|
return strtr($template, $variables);
|
651
|
}
|