| 1 |
<?php |
| 2 |
|
| 3 |
abstract class DeepLData { |
| 4 |
public $allow_cache = true; |
| 5 |
|
| 6 |
protected function isCacheValid( $cache_file, $max_age = false ) { |
| 7 |
if ( !$this->allow_cache ) { |
| 8 |
$this->why_no_cache = "Cache not allowed"; |
| 9 |
return false; |
| 10 |
} |
| 11 |
elseif ( !file_exists( $cache_file ) ) { |
| 12 |
$this->why_no_cache = "No cache file $this->cache_file"; |
| 13 |
return false; |
| 14 |
} |
| 15 |
elseif ( filesize( $cache_file ) < 10 ) { |
| 16 |
$this->why_no_cache = "Cache file too small"; |
| 17 |
return false; |
| 18 |
} |
| 19 |
elseif ( !file_get_contents( $cache_file ) ) { |
| 20 |
$this->why_no_cache = "Cache file not readable"; |
| 21 |
return false; |
| 22 |
} |
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
protected function getCacheDirectory() { |
| 27 |
return WPDEEPL_FILES; |
| 28 |
} |
| 29 |
|
| 30 |
// http://nadeausoftware.com/articles/2008/05/php_tip_how_convert_relative_url_absolute_url |
| 31 |
protected function url_to_absolute( $baseUrl, $relativeUrl ) { |
| 32 |
// If relative URL has a scheme, clean path and return. |
| 33 |
$r = $this->split_url( $relativeUrl ); |
| 34 |
if ( $r === FALSE ) |
| 35 |
return FALSE; |
| 36 |
if ( !empty( $r['scheme'] ) ) { |
| 37 |
if ( !empty( $r['path'] ) && $r['path'][0] == '/' ) |
| 38 |
$r['path'] = $this->url_remove_dot_segments( $r['path'] ); |
| 39 |
return $this->join_url( $r ); |
| 40 |
} |
| 41 |
|
| 42 |
// Make sure the base URL is absolute. |
| 43 |
$b = $this->split_url( $baseUrl ); |
| 44 |
if ( $b === FALSE || empty( $b['scheme'] ) || empty( $b['host'] ) ) |
| 45 |
return FALSE; |
| 46 |
$r['scheme'] = $b['scheme']; |
| 47 |
|
| 48 |
// If relative URL has an authority, clean path and return. |
| 49 |
if ( isset( $r['host'] ) ) { |
| 50 |
if ( !empty( $r['path'] ) ) |
| 51 |
$r['path'] = $this->url_remove_dot_segments( $r['path'] ); |
| 52 |
return $this->join_url( $r ); |
| 53 |
} |
| 54 |
unset( $r['port'] ); |
| 55 |
unset( $r['user'] ); |
| 56 |
unset( $r['pass'] ); |
| 57 |
|
| 58 |
// Copy base authority. |
| 59 |
$r['host'] = $b['host']; |
| 60 |
if ( isset( $b['port'] ) ) $r['port'] = $b['port']; |
| 61 |
if ( isset( $b['user'] ) ) $r['user'] = $b['user']; |
| 62 |
if ( isset( $b['pass'] ) ) $r['pass'] = $b['pass']; |
| 63 |
|
| 64 |
// If relative URL has no path, use base path |
| 65 |
if ( empty( $r['path'] ) ) { |
| 66 |
if ( !empty( $b['path'] ) ) |
| 67 |
$r['path'] = $b['path']; |
| 68 |
if ( !isset( $r['query'] ) && isset( $b['query'] ) ) |
| 69 |
$r['query'] = $b['query']; |
| 70 |
return $this->join_url( $r ); |
| 71 |
} |
| 72 |
|
| 73 |
// If relative URL path doesn't start with /, merge with base path |
| 74 |
if ( $r['path'][0] != '/' ) { |
| 75 |
$base = mb_strrchr( $b['path'], '/', TRUE, 'UTF-8' ); |
| 76 |
if ( $base === FALSE ) $base = ''; |
| 77 |
$r['path'] = $base . '/' . $r['path']; |
| 78 |
} |
| 79 |
$r['path'] = $this->url_remove_dot_segments( $r['path'] ); |
| 80 |
return $this->join_url( $r ); |
| 81 |
} |
| 82 |
protected function url_remove_dot_segments( $path ) { |
| 83 |
// multi-byte character explode |
| 84 |
$inSegs = preg_split( '!/!u', $path ); |
| 85 |
$outSegs = array( ); |
| 86 |
foreach ( $inSegs as $seg ) { |
| 87 |
if ( $seg == '' || $seg == '.' ) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
if ( $seg == '..' ) { |
| 91 |
array_pop( $outSegs ); |
| 92 |
} |
| 93 |
else { |
| 94 |
array_push( $outSegs, $seg ); |
| 95 |
} |
| 96 |
} |
| 97 |
$outPath = implode( '/', $outSegs ); |
| 98 |
if ( $path[0] == '/' ) { |
| 99 |
$outPath = '/' . $outPath; |
| 100 |
} |
| 101 |
// compare last multi-byte character against '/' |
| 102 |
if ( |
| 103 |
$outPath != '/' |
| 104 |
&& ( mb_strlen( $path )-1 ) == mb_strrpos( $path, '/', 0, 'UTF-8' ) |
| 105 |
) { |
| 106 |
$outPath .= '/'; |
| 107 |
} |
| 108 |
return $outPath; |
| 109 |
} |
| 110 |
|
| 111 |
protected function split_url( $url, $decode=TRUE ) { |
| 112 |
$xunressub = 'a-zA-Z\d\-._~\!$&\'()*+, ;='; |
| 113 |
$xpchar = $xunressub . ':@%'; |
| 114 |
$xscheme = '( [a-zA-Z][a-zA-Z\d+-.]* )'; |
| 115 |
$xuserinfo = '( ( [' . $xunressub . '%]* )' . '( :( [' . $xunressub . ':%]* ) )? )'; |
| 116 |
$xipv4 = '( \d{1, 3}\.\d{1, 3}\.\d{1, 3}\.\d{1, 3} )'; |
| 117 |
$xipv6 = '( \[( [a-fA-F\d.:]+ )\] )'; |
| 118 |
$xhost_name = '( [a-zA-Z\d-.%]+ )'; |
| 119 |
$xhost = '( ' . $xhost_name . '|' . $xipv4 . '|' . $xipv6 . ' )'; |
| 120 |
$xport = '( \d* )'; |
| 121 |
$xauthority = '( ( ' . $xuserinfo . '@ )?' . $xhost . '?( :' . $xport . ' )? )'; |
| 122 |
$xslash_seg = '( /[' . $xpchar . ']* )'; |
| 123 |
$xpath_authabs = '( ( //' . $xauthority . ' )( ( /[' . $xpchar . ']* )* ) )'; |
| 124 |
$xpath_rel = '( [' . $xpchar . ']+' . $xslash_seg . '* )'; |
| 125 |
$xpath_abs = '( /( ' . $xpath_rel . ' )? )'; |
| 126 |
$xapath = '( ' . $xpath_authabs . '|' . $xpath_abs . '|' . $xpath_rel . ' )'; |
| 127 |
$xqueryfrag = '( [' . $xpchar . '/?' . ']* )'; |
| 128 |
$xurl = '^( ' . $xscheme . ': )?' . $xapath . '?' . '( \?' . $xqueryfrag . ' )?( #' . $xqueryfrag . ' )?$'; |
| 129 |
|
| 130 |
// Split the URL into components. |
| 131 |
if ( !preg_match( '!' . $xurl . '!', $url, $m ) ) |
| 132 |
return FALSE; |
| 133 |
|
| 134 |
if ( !empty( $m[2] ) ) $parts['scheme'] = strtolower( $m[2] ); |
| 135 |
|
| 136 |
if ( !empty( $m[7] ) ) { |
| 137 |
if ( isset( $m[9] ) ) $parts['user'] = $m[9]; |
| 138 |
else $parts['user'] = ''; |
| 139 |
} |
| 140 |
if ( !empty( $m[10] ) ) $parts['pass'] = $m[11]; |
| 141 |
|
| 142 |
if ( !empty( $m[13] ) ) $h=$parts['host'] = $m[13]; |
| 143 |
else if ( !empty( $m[14] ) ) $parts['host'] = $m[14]; |
| 144 |
else if ( !empty( $m[16] ) ) $parts['host'] = $m[16]; |
| 145 |
else if ( !empty( $m[5] ) ) $parts['host'] = ''; |
| 146 |
if ( !empty( $m[17] ) ) $parts['port'] = $m[18]; |
| 147 |
|
| 148 |
if ( !empty( $m[19] ) ) $parts['path'] = $m[19]; |
| 149 |
else if ( !empty( $m[21] ) ) $parts['path'] = $m[21]; |
| 150 |
else if ( !empty( $m[25] ) ) $parts['path'] = $m[25]; |
| 151 |
|
| 152 |
if ( !empty( $m[27] ) ) $parts['query'] = $m[28]; |
| 153 |
if ( !empty( $m[29] ) ) $parts['fragment']= $m[30]; |
| 154 |
|
| 155 |
if ( !$decode ) |
| 156 |
return $parts; |
| 157 |
if ( !empty( $parts['user'] ) ) |
| 158 |
$parts['user'] = rawurldecode( $parts['user'] ); |
| 159 |
if ( !empty( $parts['pass'] ) ) |
| 160 |
$parts['pass'] = rawurldecode( $parts['pass'] ); |
| 161 |
if ( !empty( $parts['path'] ) ) |
| 162 |
$parts['path'] = rawurldecode( $parts['path'] ); |
| 163 |
if ( isset( $h ) ) |
| 164 |
$parts['host'] = rawurldecode( $parts['host'] ); |
| 165 |
if ( !empty( $parts['query'] ) ) |
| 166 |
$parts['query'] = rawurldecode( $parts['query'] ); |
| 167 |
if ( !empty( $parts['fragment'] ) ) |
| 168 |
$parts['fragment'] = rawurldecode( $parts['fragment'] ); |
| 169 |
return $parts; |
| 170 |
} |
| 171 |
protected function join_url( $parts, $encode=TRUE ) { |
| 172 |
if ( $encode ) { |
| 173 |
if ( isset( $parts['user'] ) ) |
| 174 |
$parts['user'] = rawurlencode( $parts['user'] ); |
| 175 |
if ( isset( $parts['pass'] ) ) |
| 176 |
$parts['pass'] = rawurlencode( $parts['pass'] ); |
| 177 |
if ( isset( $parts['host'] ) && !preg_match( '!^( \[[\da-f.:]+\]] )|( [\da-f.:]+ )$!ui', $parts['host'] ) ) |
| 178 |
$parts['host'] = rawurlencode( $parts['host'] ); |
| 179 |
if ( !empty( $parts['path'] ) ) |
| 180 |
$parts['path'] = preg_replace( '!%2F!ui', '/', rawurlencode( $parts['path'] ) ); |
| 181 |
if ( isset( $parts['query'] ) ) |
| 182 |
$parts['query'] = rawurlencode( $parts['query'] ); |
| 183 |
if ( isset( $parts['fragment'] ) ) |
| 184 |
$parts['fragment'] = rawurlencode( $parts['fragment'] ); |
| 185 |
} |
| 186 |
|
| 187 |
$url = ''; |
| 188 |
if ( !empty( $parts['scheme'] ) ) |
| 189 |
$url .= $parts['scheme'] . ':'; |
| 190 |
if ( isset( $parts['host'] ) ) { |
| 191 |
$url .= '//'; |
| 192 |
if ( isset( $parts['user'] ) ) { |
| 193 |
$url .= $parts['user']; |
| 194 |
if ( isset( $parts['pass'] ) ) |
| 195 |
$url .= ':' . $parts['pass']; |
| 196 |
$url .= '@'; |
| 197 |
} |
| 198 |
if ( preg_match( '!^[\da-f]*:[\da-f.:]+$!ui', $parts['host'] ) ) |
| 199 |
$url .= '[' . $parts['host'] . ']'; // IPv6 |
| 200 |
else |
| 201 |
$url .= $parts['host']; // IPv4 or name |
| 202 |
if ( isset( $parts['port'] ) ) |
| 203 |
$url .= ':' . $parts['port']; |
| 204 |
if ( !empty( $parts['path'] ) && $parts['path'][0] != '/' ) |
| 205 |
$url .= '/'; |
| 206 |
} |
| 207 |
if ( !empty( $parts['path'] ) ) |
| 208 |
$url .= $parts['path']; |
| 209 |
if ( isset( $parts['query'] ) ) |
| 210 |
$url .= '?' . $parts['query']; |
| 211 |
if ( isset( $parts['fragment'] ) ) |
| 212 |
$url .= '#' . $parts['fragment']; |
| 213 |
return $url; |
| 214 |
} |
| 215 |
} |