translationproxy-api.class.php
3.71 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
<?php
/**
* @package wpml-core
* @subpackage wpml-core
*/
if( class_exists( 'TranslationProxy_Api' ) ) {
// Workaround for UnitTests.
return;
}
class TranslationProxy_Api {
const API_VERSION = 1.1;
public static function proxy_request(
$path,
$params = array(),
$method = 'GET',
$multi_part = false,
$has_return_value = true
) {
return wpml_tm_load_tp_networking()->send_request(
OTG_TRANSLATION_PROXY_URL . $path,
$params,
$method,
$has_return_value
);
}
public static function proxy_download( $path, $params ) {
return wpml_tm_load_tp_networking()->send_request(
OTG_TRANSLATION_PROXY_URL . $path,
$params,
'GET',
true,
false
);
}
public static function service_request(
$url,
$params = array(),
$method = 'GET',
$has_return_value = true,
$json_response = false,
$has_api_response = false
) {
return wpml_tm_load_tp_networking()->send_request(
$url,
$params,
$method,
$has_return_value,
$json_response,
$has_api_response
);
}
public static function add_parameters_to_url( $url, $params ) {
if ( preg_match_all( '/\{.+?\}/', $url, $symbs ) ) {
foreach ( $symbs[0] as $symb ) {
$without_braces = preg_replace( '/\{|\}/', '', $symb );
if ( preg_match_all( '/\w+/', $without_braces, $indexes ) ) {
foreach ( $indexes[0] as $index ) {
if ( isset( $params[ $index ] ) ) {
$value = $params[ $index ];
$url = preg_replace( preg_quote( "/$symb/" ), $value, $url );
}
}
}
}
}
return $url;
}
}
if ( ! function_exists( 'gzdecode' ) ) {
/**
* Inflates a string enriched with gzip headers. Counterpart to gzencode().
* Extracted from upgradephp
* http://include-once.org/p/upgradephp/
*
* officially available by default in php @since 5.4.
*/
function gzdecode( $gzdata, $maxlen = null ) {
// -- decode header
$len = strlen( $gzdata );
if ( $len < 20 ) {
return;
}
$head = substr( $gzdata, 0, 10 );
$head = unpack( 'n1id/C1cm/C1flg/V1mtime/C1xfl/C1os', $head );
if( ! $head ) {
return;
}
list( $ID, $CM, $FLG, $MTIME, $XFL, $OS ) = array_values( $head );
$FTEXT = 1 << 0;
$FHCRC = 1 << 1;
$FEXTRA = 1 << 2;
$FNAME = 1 << 3;
$FCOMMENT = 1 << 4;
$head = unpack( 'V1crc/V1isize', substr( $gzdata, $len - 8, 8 ) );
if ( ! $head ) {
return;
}
list( $CRC32, $ISIZE ) = array_values( $head );
// -- check gzip stream identifier
if ( $ID != 0x1f8b ) {
trigger_error( 'gzdecode: not in gzip format', E_USER_WARNING );
return;
}
// -- check for deflate algorithm
if ( $CM != 8 ) {
trigger_error( 'gzdecode: cannot decode anything but deflated streams', E_USER_WARNING );
return;
}
// -- start of data, skip bonus fields
$s = 10;
if ( $FLG & $FEXTRA ) {
$s += $XFL;
}
if ( $FLG & $FNAME ) {
$s = strpos( $gzdata, "\000", $s ) + 1;
}
if ( $FLG & $FCOMMENT ) {
$s = strpos( $gzdata, "\000", $s ) + 1;
}
if ( $FLG & $FHCRC ) {
$s += 2; // cannot check
}
// -- get data, uncompress
$gzdata = substr( $gzdata, $s, $len - $s );
if ( $maxlen ) {
$gzdata = gzinflate( $gzdata, $maxlen );
return ( $gzdata ); // no checks(?!)
} else {
$gzdata = gzinflate( $gzdata );
}
// -- check+fin
$chk = crc32( (string) $gzdata );
if ( $CRC32 != $chk ) {
trigger_error( "gzdecode: checksum failed (real$chk != comp$CRC32)", E_USER_WARNING );
} elseif ( $ISIZE != strlen( (string) $gzdata ) ) {
trigger_error( 'gzdecode: stream size mismatch', E_USER_WARNING );
} else {
return ( $gzdata );
}
}
}