| 1 |
<?php |
| 2 |
// mp3info class for use in the Blubrry PowerPress |
| 3 |
// Main purpose of this file is to obtain the duration string for the itunes:duration field. |
| 4 |
// Library is packaged thin with only basic mp3 support. |
| 5 |
// Concept with this library is to get the information without downlaoding the entire file. |
| 6 |
// for efficienccy |
| 7 |
|
| 8 |
class Mp3Info { |
| 9 |
//var $m_DownloadBytesLimit = 1638400; // 1600K (1600*1024) bytes file |
| 10 |
//var $m_DownloadBytesLimit = 204800; // 200K (200*1024) bytes file |
| 11 |
//var $m_DownloadBytesLimit = 327680; // 320K (320*1024) bytes file |
| 12 |
//var $m_DownloadBytesLimit = 409600; // 400K (400*1024) bytes file |
| 13 |
// var $m_DownloadBytesLimit = 614400; // 600K (600*1024) bytes file |
| 14 |
var $m_DownloadBytesLimit = 1048576; // 1MB (1024*1024) bytes file |
| 15 |
var $m_RedirectLimit = 12; // Number of times to do the 302 redirect |
| 16 |
var $m_UserAgent = 'Blubrry PowerPress'; |
| 17 |
var $m_error = ''; |
| 18 |
var $m_warnings = array(); |
| 19 |
var $m_ContentLength = false; |
| 20 |
var $m_ContentType = ''; |
| 21 |
var $m_RedirectCount = 0; |
| 22 |
Var $m_file_size_only = false; |
| 23 |
|
| 24 |
var $m_data = ''; |
| 25 |
|
| 26 |
// Constructor |
| 27 |
function __construct() |
| 28 |
{ |
| 29 |
// Empty for now |
| 30 |
if( defined('POWERPRESS_VERSION') ) |
| 31 |
$this->m_UserAgent = 'Blubrry PowerPress/'.POWERPRESS_VERSION; |
| 32 |
else |
| 33 |
$this->m_UserAgent = 'Blubrry PowerPress'; |
| 34 |
} |
| 35 |
|
| 36 |
/* |
| 37 |
Set how much of the media file to download. Default: 25K |
| 38 |
*/ |
| 39 |
function SetDownloadBytesLimit($limit=204800) |
| 40 |
{ |
| 41 |
$this->m_DownloadBytesLimit = $limit; |
| 42 |
} |
| 43 |
|
| 44 |
/* |
| 45 |
Set how many times we can follow a HTTP 30x header redirect before we fail. |
| 46 |
*/ |
| 47 |
function SetRedirectLimit($limit=5) |
| 48 |
{ |
| 49 |
$this->m_RedirectLimit = $limit; |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
Set the user agent to be sent by this plugin |
| 54 |
*/ |
| 55 |
function SetUserAgent($user_agent) |
| 56 |
{ |
| 57 |
$this->m_UserAgent = $user_agent; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
Return the last set error message |
| 62 |
*/ |
| 63 |
function GetError() |
| 64 |
{ |
| 65 |
return $this->m_error; |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
Set the last error message |
| 70 |
*/ |
| 71 |
function SetError($msg) |
| 72 |
{ |
| 73 |
$this->m_error = $msg; |
| 74 |
} |
| 75 |
|
| 76 |
function GetWarnings() |
| 77 |
{ |
| 78 |
return $this->m_warnings; |
| 79 |
} |
| 80 |
|
| 81 |
function AddWarning($msg) |
| 82 |
{ |
| 83 |
$this->m_warnings[] = $msg; |
| 84 |
} |
| 85 |
|
| 86 |
/* |
| 87 |
Get the length in bytes of the file to download. |
| 88 |
*/ |
| 89 |
function GetContentLength() |
| 90 |
{ |
| 91 |
return $this->m_ContentLength; |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
Get the content type of the file to download. |
| 96 |
*/ |
| 97 |
function GetContentType() |
| 98 |
{ |
| 99 |
return $this->m_ContentType; |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
Get the number of times we followed 30x header redirects |
| 104 |
*/ |
| 105 |
function GetRedirectCount() |
| 106 |
{ |
| 107 |
return $this->m_RedirectCount; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/* |
| 112 |
Get the ID3 headers by first downloading the first 10 bytes, then download the rest based on what's left |
| 113 |
*/ |
| 114 |
function DownloadID3Headers($url) // Mp3 only |
| 115 |
{ |
| 116 |
$CurrentLimit = $this->m_DownloadBytesLimit; |
| 117 |
$this->m_DownloadBytesLimit = 10; // Get the first 10 bytes |
| 118 |
// Do download |
| 119 |
$success = $this->Download($url); |
| 120 |
$this->m_DownloadBytesLimit = $CurrentLimit; |
| 121 |
|
| 122 |
if( empty($success) ) |
| 123 |
return false; |
| 124 |
|
| 125 |
if( file_exists($success) ) |
| 126 |
{ |
| 127 |
$id3header = file_get_contents($success); |
| 128 |
unlink($success); // Clean up after ourselves |
| 129 |
|
| 130 |
if( substr($id3header, 0, 3) == 'ID3' && strlen($id3header) == 10) |
| 131 |
{ |
| 132 |
$this->_load_id3lib(); |
| 133 |
$getid3 = new getID3; // So we can use the getid3_lib static function |
| 134 |
|
| 135 |
$headerlength = getid3_lib::BigEndian2Int(substr($id3header, 6, 4), 1)+10; |
| 136 |
|
| 137 |
// Awesome, now we need to download the file based on this size... |
| 138 |
if( $headerlength > 50 ) { // Do we have enough bytes? |
| 139 |
$this->m_DownloadBytesLimit = $headerlength +100000; // Add 100k to find valid MPEG synch |
| 140 |
} else { |
| 141 |
$this->m_DownloadBytesLimit = $CurrentLimit; // Best we can do is use the default limit |
| 142 |
} |
| 143 |
|
| 144 |
$success = $this->Download($url); |
| 145 |
$this->m_DownloadBytesLimit = $CurrentLimit; |
| 146 |
|
| 147 |
if( empty($success) ) |
| 148 |
return false; |
| 149 |
|
| 150 |
return $success; |
| 151 |
} |
| 152 |
else // no ID3 v2 headers, lets fallback to the previous logic... |
| 153 |
{ |
| 154 |
//$this->SetError('No ID3v2 headers found'); |
| 155 |
//return false; |
| 156 |
return $this->Download($url); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
$this->SetError('Temporary file was empty.'); |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
Start the download and get the headers, handles the redirect if there are any |
| 166 |
*/ |
| 167 |
function Download($url, $RedirectCount = 0) |
| 168 |
{ |
| 169 |
if( !$this->ini_get( 'allow_url_fopen' ) && !function_exists( 'curl_init' ) ) |
| 170 |
{ |
| 171 |
$this->SetError( __('Your server must either have the php.ini setting \'allow_url_fopen\' enabled or have the PHP cURL library installed in order to continue.', 'powerpress') ); |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
if( function_exists( 'curl_init' ) ) |
| 176 |
return $this->DownloadCurl($url); |
| 177 |
|
| 178 |
// The following code relies on fopen_url capability. |
| 179 |
if( $RedirectCount > $this->m_RedirectLimit ) |
| 180 |
{ |
| 181 |
$this->SetError( sprintf( __('Media URL exceeded redirect limit of %d (fopen).', 'powerpress'), $this->m_RedirectLimit) ); |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
$this->m_ContentLength = false; |
| 186 |
$this->m_ContentType = ''; |
| 187 |
$this->m_RedirectCount = $RedirectCount; |
| 188 |
|
| 189 |
$urlParts = parse_url($url); |
| 190 |
if( !isset( $urlParts['host']) ) |
| 191 |
{ |
| 192 |
if( empty($url) ) |
| 193 |
$this->SetError( __('Unable to obtain host name from URL.', 'powerpress') ); |
| 194 |
else |
| 195 |
$this->SetError( __('Unable to obtain host name from the URL:', 'powerpress') .' '.$url ); |
| 196 |
return false; |
| 197 |
} |
| 198 |
if( !isset( $urlParts['path']) ) |
| 199 |
$urlParts['path'] = '/'; |
| 200 |
if( !isset( $urlParts['port']) ) |
| 201 |
$urlParts['port'] = 80; |
| 202 |
if( !isset( $urlParts['scheme']) ) |
| 203 |
$urlParts['scheme'] = 'http'; |
| 204 |
|
| 205 |
$fp = fsockopen($urlParts['host'], $urlParts['port'], $errno, $errstr, 30); |
| 206 |
if ($fp) |
| 207 |
{ |
| 208 |
// Create and send the request headers |
| 209 |
$RequestHeaders = ($this->m_file_size_only?'HEAD ':'GET ').$urlParts['path'].(isset($urlParts['query']) ? '?'.$urlParts['query'] : '')." HTTP/1.0\r\n"; |
| 210 |
$RequestHeaders .= 'Host: '.$urlParts['host'].($urlParts['port'] != 80 ? ':'.$urlParts['port'] : '')."\r\n"; |
| 211 |
$RequestHeaders .= "Connection: Close\r\n"; |
| 212 |
$RequestHeaders .= "User-Agent: {$this->m_UserAgent}\r\n"; |
| 213 |
fwrite($fp, $RequestHeaders."\r\n"); |
| 214 |
|
| 215 |
$Redirect = false; |
| 216 |
$RedirectURL = false; |
| 217 |
$ContentLength = false; |
| 218 |
$ContentType = false; |
| 219 |
$ReturnCode = 0; |
| 220 |
$headers = ''; |
| 221 |
// Loop through the headers |
| 222 |
while( !feof($fp) ) |
| 223 |
{ |
| 224 |
$line = fgets($fp, 1280); // Get the next header line... |
| 225 |
if( $line === false ) |
| 226 |
break; // Something happened |
| 227 |
if ($line == "\r\n") |
| 228 |
break; // Okay we're ending the headers, now onto the content |
| 229 |
|
| 230 |
$headers .= $line; |
| 231 |
$line = rtrim($line); // Clean out the new line characters |
| 232 |
$key = ''; |
| 233 |
$value = ''; |
| 234 |
if( strstr($line, ':') ) |
| 235 |
list($key, $value) = explode(':', $line, 2); |
| 236 |
$key = trim($key); |
| 237 |
$value = trim($value); |
| 238 |
|
| 239 |
if( stristr($line, '301 Moved Permanently') || stristr($line, '302 Found') || stristr($line, '307 Temporary Redirect') ) |
| 240 |
{ |
| 241 |
$Redirect = true; // We are dealing with a redirect, lets handle it |
| 242 |
} |
| 243 |
else if( preg_match('/^HTTPS?\/\d\.\d (\d{3})(.*)/i', $line, $matches) ) |
| 244 |
{ |
| 245 |
$ReturnCode = $matches[1]; |
| 246 |
if( $ReturnCode < 200 || $ReturnCode > 206 ) |
| 247 |
{ |
| 248 |
fclose($fp); |
| 249 |
if( $ReturnCode == 404 ) |
| 250 |
$this->SetError( __('The requested URL returned error code 404, file not found.', 'powerpress') ); |
| 251 |
else |
| 252 |
$this->SetError( sprintf(__('The requested URL returned error code %d','powerpress'), $ReturnCode.$matches[2]) ); |
| 253 |
return false; |
| 254 |
} |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
switch( strtolower($key) ) |
| 259 |
{ |
| 260 |
case 'location': { |
| 261 |
$RedirectURL = $value; |
| 262 |
}; break; |
| 263 |
case 'content-length': { |
| 264 |
$ContentLength = $value; |
| 265 |
}; break; |
| 266 |
case 'content-type' : { |
| 267 |
$this->m_ContentType = $value; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// Loop through the content till we reach our limit... |
| 274 |
$Content = ''; |
| 275 |
if( $this->m_DownloadBytesLimit ) |
| 276 |
{ |
| 277 |
while( !feof($fp) ) |
| 278 |
{ |
| 279 |
$Content .= fread($fp, 8096); |
| 280 |
if( strlen($Content) > $this->m_DownloadBytesLimit ) |
| 281 |
break; // We got enough of the file we should be able to determine the duration |
| 282 |
} |
| 283 |
} |
| 284 |
fclose($fp); |
| 285 |
|
| 286 |
// If we're dealing with a redirect, lets call our nested function call now |
| 287 |
if( $Redirect ) |
| 288 |
{ |
| 289 |
unset($Content); // clear what may be using a lot of memory |
| 290 |
return $this->Download($RedirectURL, $RedirectCount + 1 ); // Follow this redirect |
| 291 |
} |
| 292 |
else // Otherwise, lets set the data and return true for part two |
| 293 |
{ |
| 294 |
if( $this->m_file_size_only ) |
| 295 |
{ |
| 296 |
if( $ContentLength ) |
| 297 |
{ |
| 298 |
$this->m_ContentLength = $ContentLength; |
| 299 |
return true; |
| 300 |
} |
| 301 |
else |
| 302 |
{ |
| 303 |
$this->SetError( __('Unable to obtain media size from web server.', 'powerpress') ); |
| 304 |
return false; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
//global $TempFile; |
| 309 |
if( function_exists('get_temp_dir') ) // If wordpress function is available, lets use it |
| 310 |
$TempFile = tempnam(get_temp_dir(), 'wp_powerpress'); |
| 311 |
else // otherwise use the default path |
| 312 |
$TempFile = tempnam('/tmp', 'wp_powerpress'); |
| 313 |
|
| 314 |
if( $TempFile === false ) |
| 315 |
{ |
| 316 |
$this->SetError( __('Unable to save media information to temporary directory.', 'powerpress') ); |
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
$fp = fopen( $TempFile, 'w' ); |
| 321 |
fwrite($fp, $Content); |
| 322 |
fclose($fp); |
| 323 |
|
| 324 |
if( $ContentLength ) |
| 325 |
$this->m_ContentLength = $ContentLength; |
| 326 |
return $TempFile; |
| 327 |
} |
| 328 |
} |
| 329 |
$this->SetError( __('Unable to connect to host:','powerpress') .' '.$urlParts['host']); |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
/* |
| 334 |
Alternative method (curl) for downloading portion of a media file |
| 335 |
*/ |
| 336 |
function DownloadCurl($url, $RedirectCount = 0) |
| 337 |
{ |
| 338 |
// In case we are dealing with a restriction with a server that does not allow cURL to do redirects itself... |
| 339 |
if ( $this->ini_get('safe_mode') || $this->ini_get('open_basedir') ) |
| 340 |
{ |
| 341 |
if( $RedirectCount > $this->m_RedirectLimit ) |
| 342 |
{ |
| 343 |
$this->SetError( sprintf( __('Media URL exceeded redirect limit of %d (cURL in safe mode).', 'powerpress'), $this->m_RedirectLimit) ); |
| 344 |
return false; |
| 345 |
} |
| 346 |
$this->m_RedirectCount = $RedirectCount; |
| 347 |
} |
| 348 |
|
| 349 |
$curl = curl_init(); |
| 350 |
// First, get the content-length... |
| 351 |
curl_setopt($curl, CURLOPT_USERAGENT, $this->m_UserAgent ); |
| 352 |
curl_setopt($curl, CURLOPT_URL, $url); |
| 353 |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
| 354 |
if( defined('MEPR_PLUGIN_NAME') ) { |
| 355 |
curl_setopt($curl, CURLOPT_COOKIEFILE, ""); // For MemberPress |
| 356 |
} |
| 357 |
curl_setopt($curl, CURLOPT_HEADER, true); // header will be at output |
| 358 |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request |
| 359 |
curl_setopt($curl, CURLOPT_NOBODY, true ); |
| 360 |
curl_setopt($curl, CURLOPT_FAILONERROR, true); |
| 361 |
if( preg_match('/^https:\/\//', $url) !== false ) |
| 362 |
{ |
| 363 |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2 ); |
| 364 |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true ); |
| 365 |
if( defined('ABSPATH') && defined('WPINC') ) |
| 366 |
curl_setopt($curl, CURLOPT_CAINFO, ABSPATH . WPINC . '/certificates/ca-bundle.crt'); |
| 367 |
} |
| 368 |
|
| 369 |
if ( !$this->ini_get('safe_mode') && !$this->ini_get('open_basedir') ) |
| 370 |
{ |
| 371 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
| 372 |
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->m_RedirectLimit); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
| 377 |
curl_setopt($curl, CURLOPT_MAXREDIRS, 0 ); // We will attempt to handle redirects ourself |
| 378 |
} |
| 379 |
$Headers = curl_exec($curl); |
| 380 |
$ContentLength = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
| 381 |
$this->m_ContentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); |
| 382 |
$HttpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 383 |
$ContentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); |
| 384 |
$ErrorMsg = curl_error($curl); |
| 385 |
if ( !$this->ini_get('safe_mode') && !$this->ini_get('open_basedir') ) |
| 386 |
$this->m_RedirectCount = curl_getinfo($curl, CURLINFO_REDIRECT_COUNT); |
| 387 |
|
| 388 |
if( $HttpCode < 200 || $HttpCode > 250 ) |
| 389 |
{ |
| 390 |
switch( $HttpCode ) |
| 391 |
{ |
| 392 |
case 301: |
| 393 |
case 302: |
| 394 |
case 307: { |
| 395 |
if ( !$this->ini_get('safe_mode') && !$this->ini_get('open_basedir') ) |
| 396 |
{ |
| 397 |
$this->SetError( sprintf( __('Media URL exceeded redirect limit of %d (cURL).', 'powerpress'), $this->m_RedirectLimit) ); |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
$redirect_url = false; |
| 402 |
if( preg_match('/^location:(.*)$/im', $Headers, $matches) ) |
| 403 |
$redirect_url = trim($matches[1]); |
| 404 |
|
| 405 |
if( $redirect_url ) |
| 406 |
{ |
| 407 |
if (version_compare(PHP_VERSION, '8.0', '<')) { |
| 408 |
curl_close($curl); |
| 409 |
} else { |
| 410 |
unset($curl); |
| 411 |
} |
| 412 |
return $this->DownloadCurl($redirect_url, $RedirectCount +1); |
| 413 |
} |
| 414 |
else |
| 415 |
{ |
| 416 |
$this->SetError( sprintf(__('Unable to obtain HTTP %d redirect URL.', 'powerpress'), $HttpCode) ); |
| 417 |
} |
| 418 |
} |
| 419 |
}; break; |
| 420 |
case '404': { |
| 421 |
$this->SetError( __('The requested URL returned error code 404, file not found.', 'powerpress') ); |
| 422 |
}; break; |
| 423 |
default: { |
| 424 |
$this->SetError( curl_error($curl) ); |
| 425 |
}; break; |
| 426 |
} |
| 427 |
if (version_compare(PHP_VERSION, '8.0', '<')) { |
| 428 |
curl_close($curl); |
| 429 |
} else { |
| 430 |
unset($curl); |
| 431 |
} |
| 432 |
return false; |
| 433 |
} |
| 434 |
|
| 435 |
/* |
| 436 |
if( stristr($ContentType, 'text') ) |
| 437 |
{ |
| 438 |
$this->SetError( 'Invalid content type returned.' ); |
| 439 |
return false; |
| 440 |
} |
| 441 |
*/ |
| 442 |
|
| 443 |
$FinalURL = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); |
| 444 |
if (version_compare(PHP_VERSION, '8.0', '<')) { |
| 445 |
curl_close($curl); |
| 446 |
} else { |
| 447 |
unset($curl); |
| 448 |
} |
| 449 |
|
| 450 |
if( $this->m_file_size_only ) |
| 451 |
{ |
| 452 |
if( $ContentLength ) |
| 453 |
{ |
| 454 |
$this->m_ContentLength = $ContentLength; |
| 455 |
return true; |
| 456 |
} |
| 457 |
$this->SetError( __('Unable to obtain media size from server.', 'powerpress') ); |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
global $TempFile; |
| 462 |
if( function_exists('get_temp_dir') ) // If wordpress function is available, lets use it |
| 463 |
$TempFile = tempnam(get_temp_dir(), 'wp_powerpress'); |
| 464 |
else // otherwise use the default path |
| 465 |
$TempFile = tempnam('/tmp', 'wp_powerpress'); |
| 466 |
if( $TempFile === false ) |
| 467 |
{ |
| 468 |
$this->SetError( __('Unable to create temporary file for checking media information.', 'powerpress') ); |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
$fp = fopen($TempFile, 'w+b'); |
| 473 |
// Next get the first chunk of the file... |
| 474 |
|
| 475 |
$curl = curl_init(); |
| 476 |
curl_setopt($curl, CURLOPT_URL, $FinalURL); |
| 477 |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); // Don't set this as it is knwon to cause errors with the function callback. |
| 478 |
|
| 479 |
// only use this constant in versions lower than 8.4 |
| 480 |
if (version_compare(PHP_VERSION, '8.4', '<')) { |
| 481 |
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true); |
| 482 |
} |
| 483 |
curl_setopt($curl, CURLOPT_USERAGENT, $this->m_UserAgent); |
| 484 |
curl_setopt($curl, CURLOPT_FILE, $fp); |
| 485 |
curl_setopt($curl, CURLOPT_HEADER, false); // header will be at output |
| 486 |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // HTTP request |
| 487 |
curl_setopt($curl, CURLOPT_NOBODY, false ); |
| 488 |
if( preg_match('/^https:\/\//', $url) !== false ) |
| 489 |
{ |
| 490 |
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2 ); |
| 491 |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true ); |
| 492 |
if( defined('ABSPATH') && defined('WPINC') ) |
| 493 |
curl_setopt($curl, CURLOPT_CAINFO, ABSPATH . WPINC . '/certificates/ca-bundle.crt'); |
| 494 |
} |
| 495 |
|
| 496 |
if ( !$this->ini_get('safe_mode') && !$this->ini_get('open_basedir') ) |
| 497 |
{ |
| 498 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
| 499 |
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->m_RedirectLimit); |
| 500 |
} |
| 501 |
else |
| 502 |
{ |
| 503 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
| 504 |
curl_setopt($curl, CURLOPT_MAXREDIRS, 0 ); // We will attempt to handle redirects ourself |
| 505 |
} |
| 506 |
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
| 507 |
|
| 508 |
// First lets try a range request |
| 509 |
curl_setopt($curl, CURLOPT_RANGE, '0-'.($this->m_DownloadBytesLimit - 1) ); |
| 510 |
// curl_setopt($curl, CURLOPT_HTTPHEADER, array('Range: bytes=0-'.($this->m_DownloadBytesLimit - 1) )); |
| 511 |
$success = curl_exec($curl); |
| 512 |
|
| 513 |
if( !$success && curl_getinfo($curl, CURLINFO_HTTP_CODE) == 406 ) |
| 514 |
{ |
| 515 |
if (version_compare(PHP_VERSION, '8.0', '<')) { |
| 516 |
curl_close($curl); |
| 517 |
} else { |
| 518 |
unset($curl); |
| 519 |
} |
| 520 |
$curl = curl_init(); |
| 521 |
//curl_setopt($curl, CURLOPT_URL, $url); |
| 522 |
curl_setopt($curl, CURLOPT_URL, $FinalURL); |
| 523 |
curl_setopt($curl, CURLOPT_USERAGENT, $this->m_UserAgent); |
| 524 |
curl_setopt($curl, CURLOPT_HEADER, false); // header will be at output |
| 525 |
curl_setopt($curl, CURLOPT_NOBODY, false ); |
| 526 |
curl_setopt($curl, CURLOPT_WRITEFUNCTION, array($this, 'remoteread_curl_writefunc') ); |
| 527 |
if ( !$this->ini_get('safe_mode') && !$this->ini_get('open_basedir') ) |
| 528 |
{ |
| 529 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
| 530 |
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->m_RedirectLimit); |
| 531 |
} |
| 532 |
else |
| 533 |
{ |
| 534 |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); |
| 535 |
curl_setopt($curl, CURLOPT_MAXREDIRS, 0 ); // We will attempt to handle redirects ourself |
| 536 |
} |
| 537 |
|
| 538 |
$success = curl_exec($curl); |
| 539 |
if( ($success || curl_errno($curl) == 23) && $this->m_data != '' ) |
| 540 |
{ |
| 541 |
fwrite($fp , $this->m_data); |
| 542 |
$this->m_data = ''; // Free up this memory by setting the value to a blank string |
| 543 |
$success = true; // Actually this was a success |
| 544 |
} |
| 545 |
else if( $success && $this->m_data == '' ) |
| 546 |
{ |
| 547 |
$this->SetError( __('Unable to download media.', 'powerpress') ); |
| 548 |
$success = false; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
if( !$success ) |
| 553 |
{ |
| 554 |
if( curl_errno($curl) ) |
| 555 |
$this->SetError( __('Retrieving file info:', 'powerpress') .' '. curl_error($curl) ); |
| 556 |
else if( $this->GetError() == '' ) |
| 557 |
$this->SetError( __('Unable to download media.', 'powerpress') ); |
| 558 |
} |
| 559 |
if (version_compare(PHP_VERSION, '8.0', '<')) { |
| 560 |
curl_close($curl); |
| 561 |
} else { |
| 562 |
unset($curl); |
| 563 |
} |
| 564 |
fclose($fp); |
| 565 |
|
| 566 |
if( $success ) |
| 567 |
{ |
| 568 |
if( $ContentLength ) |
| 569 |
$this->m_ContentLength = $ContentLength; |
| 570 |
return $TempFile; |
| 571 |
} |
| 572 |
|
| 573 |
@unlink($TempFile); |
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
/* |
| 578 |
Get the MP3 information |
| 579 |
*/ |
| 580 |
function GetMp3Info($File, $file_size_only = false) |
| 581 |
{ |
| 582 |
$this->m_file_size_only = $file_size_only; |
| 583 |
$DeleteFile = false; |
| 584 |
$LocalFile = false; |
| 585 |
|
| 586 |
// If the URL starts with a http:// or https:// and ends with an mp3, then lets try the smart id3 method... |
| 587 |
if( preg_match('/^https?:\/\/.*\.mp3$/i', $File) != false ) |
| 588 |
{ |
| 589 |
$LocalFile = $this->DownloadID3Headers($File); |
| 590 |
if( $LocalFile === false ) |
| 591 |
return false; |
| 592 |
|
| 593 |
if( $this->m_file_size_only ) |
| 594 |
return true; |
| 595 |
|
| 596 |
$DeleteFile = true; |
| 597 |
} |
| 598 |
|
| 599 |
// Try old method |
| 600 |
if( false == $LocalFile ) { |
| 601 |
if( false !== preg_match('/^https?:\/\//i', $File) ) |
| 602 |
{ |
| 603 |
$LocalFile = $this->Download($File); |
| 604 |
if( $LocalFile === false ) |
| 605 |
return false; |
| 606 |
|
| 607 |
if( $this->m_file_size_only ) |
| 608 |
return true; |
| 609 |
|
| 610 |
$DeleteFile = true; |
| 611 |
} |
| 612 |
else |
| 613 |
{ |
| 614 |
if( $this->m_file_size_only ) |
| 615 |
{ |
| 616 |
if( file_exists($File) ) |
| 617 |
{ |
| 618 |
$this->m_ContentLength = filesize($File); |
| 619 |
return true; |
| 620 |
} |
| 621 |
return false; |
| 622 |
} |
| 623 |
$LocalFile = $File; |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
if( !is_file($LocalFile) ) |
| 628 |
{ |
| 629 |
$this->SetError( __('Error occurred downloading media file.', 'powerpress') ); |
| 630 |
return false; |
| 631 |
} |
| 632 |
|
| 633 |
if( $this->m_ContentLength == -1 ) |
| 634 |
{ |
| 635 |
$this->SetError( __('Error occurred downloading media file.', 'powerpress') ); |
| 636 |
return false; |
| 637 |
} |
| 638 |
|
| 639 |
if( $this->m_ContentLength < 1 ) |
| 640 |
$this->m_ContentLength = filesize($LocalFile); |
| 641 |
|
| 642 |
if( $this->m_ContentLength == 0 ) |
| 643 |
{ |
| 644 |
$this->SetError( __('Downloaded media file is empty.', 'powerpress') ); |
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
//if( !preg_match('/(audio|video)/i', $this->GetContentType() ) ) |
| 649 |
//{ |
| 650 |
// $this->SetError( sprintf(__('URL is reporting incorrect content type: %s', 'powerpress'), $this->GetContentType()) ); |
| 651 |
// return false; |
| 652 |
//} |
| 653 |
$this->_load_id3lib(); |
| 654 |
$getID3 = new getID3; |
| 655 |
$FileInfo = $getID3->analyze( $LocalFile, $this->m_ContentLength, $File ); |
| 656 |
if( $DeleteFile ) |
| 657 |
@unlink($LocalFile); |
| 658 |
|
| 659 |
if( $FileInfo ) |
| 660 |
{ |
| 661 |
{ |
| 662 |
$temp = $FileInfo; |
| 663 |
unset($temp['audio']); |
| 664 |
if( isset($temp['id3v2']) ) |
| 665 |
unset($temp['id3v2']); |
| 666 |
if( isset($temp['id3v1']) ) |
| 667 |
unset($temp['id3v1']); |
| 668 |
|
| 669 |
if( isset($temp['comments']['picture'][0]['data']) ) |
| 670 |
unset($temp['comments']['picture'][0]['data']); |
| 671 |
} |
| 672 |
|
| 673 |
if( isset($FileInfo['error']) ) |
| 674 |
{ |
| 675 |
// Speical case, if the content type does not include audio or video, report that as possible error... |
| 676 |
|
| 677 |
if( !preg_match('/(audio|video)/i', $this->GetContentType() ) ) |
| 678 |
{ |
| 679 |
$this->SetError( sprintf(__('Media URL reporting incorrect content type: %s', 'powerpress'), $this->GetContentType()) ); |
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
$errors = ''; |
| 684 |
foreach( $FileInfo['error'] as $null => $error ) |
| 685 |
{ |
| 686 |
if( strstr($error, 'error parsing') ) |
| 687 |
continue; |
| 688 |
$errors .= " $error."; |
| 689 |
} |
| 690 |
if( !empty($errors) ) |
| 691 |
{ |
| 692 |
$this->SetError( trim($errors) ); |
| 693 |
return false; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
if( false && isset($FileInfo['warning']) ) |
| 698 |
{ |
| 699 |
$errors = ''; |
| 700 |
foreach( $FileInfo['warning'] as $null => $warning ) |
| 701 |
$this->AddWarning($warning ); |
| 702 |
} |
| 703 |
|
| 704 |
// Remove extra data that is not necessary for us to return... |
| 705 |
//unset($FileInfo['mpeg']); |
| 706 |
unset($FileInfo['audio']); |
| 707 |
if( isset($FileInfo['id3v2']) ) |
| 708 |
unset($FileInfo['id3v2']); |
| 709 |
if( isset($FileInfo['id3v1']) ) |
| 710 |
unset($FileInfo['id3v1']); |
| 711 |
|
| 712 |
if( !isset($FileInfo['playtime_seconds']) ) |
| 713 |
$FileInfo['playtime_seconds'] = ''; |
| 714 |
if( !isset($FileInfo['playtime_string']) ) |
| 715 |
$FileInfo['playtime_string'] = ''; |
| 716 |
|
| 717 |
if( !empty($FileInfo['playtime_seconds']) ) |
| 718 |
$FileInfo['playtime_seconds'] = round($FileInfo['playtime_seconds']); |
| 719 |
else |
| 720 |
$FileInfo['playtime_seconds'] = 0; |
| 721 |
|
| 722 |
// No longer checking for the right sample rates and channel mode for flash, flash is now OBSOLETE |
| 723 |
/* |
| 724 |
if( isset($FileInfo['mpeg']['audio']) && $FileInfo['mpeg']['audio'] ) |
| 725 |
{ |
| 726 |
$Audio = $FileInfo['mpeg']['audio']; |
| 727 |
if( $Audio['sample_rate'] != 22050 && $Audio['sample_rate'] != 44100 ) |
| 728 |
{ |
| 729 |
// Add warning here |
| 730 |
$this->AddWarning( sprintf(__('Sample Rate %dKhz may cause playback issues, we recommend 22Khz or 44Khz for maximum player compatibility.', 'powerpress'), $Audio['sample_rate']/1000 ) ); |
| 731 |
} |
| 732 |
|
| 733 |
//if( stristr($Audio['channelmode'], 'stereo' ) === false ) |
| 734 |
//{ |
| 735 |
// // Add warning here |
| 736 |
// $this->AddWarning( sprintf(__('Channel Mode \'%s\' may cause playback issues, we recommend \'joint stereo\' for maximum player compatibility.', 'powerpress'), trim($Audio['channelmode']) ) ); |
| 737 |
//} |
| 738 |
} |
| 739 |
*/ |
| 740 |
|
| 741 |
return $FileInfo; |
| 742 |
} |
| 743 |
|
| 744 |
return false; |
| 745 |
} |
| 746 |
|
| 747 |
private function ini_get($field) |
| 748 |
{ |
| 749 |
switch( $field ) { |
| 750 |
case 'safe_mode': { |
| 751 |
if( version_compare( PHP_VERSION, '5.3.0') >= 0 ) { // If php 5.3+ |
| 752 |
return false; |
| 753 |
} |
| 754 |
}; break; |
| 755 |
} |
| 756 |
return ini_get($field); |
| 757 |
} |
| 758 |
|
| 759 |
private function _load_id3lib() |
| 760 |
{ |
| 761 |
if( defined('POWERPRESS_GETID3_LOADED') ) |
| 762 |
return true; // Rock and roll |
| 763 |
|
| 764 |
if( class_exists('getID3') && !defined('POWERPRESS_GETID3_LOADED') ) |
| 765 |
{ |
| 766 |
$pre_msg = __('PowerPress is unable to detect media information.', 'powerpress') .'<br />'; |
| 767 |
$getID3 = new getID3; |
| 768 |
if( defined('GETID3_INCLUDEPATH') ) { |
| 769 |
$plugin_title = ''; |
| 770 |
$plugins_path = dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR; |
| 771 |
$path = substr(GETID3_INCLUDEPATH, strlen($plugins_path)); |
| 772 |
if( preg_match('/^([^\\/\\\\]*)/i', $path, $matches) ) |
| 773 |
{ |
| 774 |
$plugin_folder = $matches[1]; |
| 775 |
$current_plugins = get_option('active_plugins'); |
| 776 |
foreach( $current_plugins as $null => $plugin_local_path ) |
| 777 |
{ |
| 778 |
if( substr($plugin_local_path, 0, strpos($plugin_local_path, '/') ) != $plugin_folder ) |
| 779 |
continue; |
| 780 |
$plugin_data = get_plugin_data( rtrim(WP_PLUGIN_DIR, '/\\'). '/'. rtrim($plugin_local_path, '\\/'), false, false ); //Do not apply markup/translate as it'll be cached. |
| 781 |
$plugin_title = $plugin_data['Title']; |
| 782 |
} |
| 783 |
} |
| 784 |
if( $plugin_title ) |
| 785 |
$this->SetError( $pre_msg. sprintf(__('Plugin \'%s\' has included a different version of the GetID3 library located in %s', 'powerpress'), $plugin_title, $path) ); |
| 786 |
else |
| 787 |
$this->SetError( $pre_msg. sprintf(__('Another plugin has included a different version of the GetID3 library located in %s', 'powerpress'), $path) ); |
| 788 |
} else { |
| 789 |
$this->SetError( $pre_msg. __('Another plugin has included a different version of the GetID3 library.', 'powerpress') ); |
| 790 |
} |
| 791 |
return false; |
| 792 |
} |
| 793 |
|
| 794 |
// Hack so this works in Windows, helper apps are not necessary for what we're doing anyway |
| 795 |
if( !defined('GETID3_HELPERAPPSDIR') ) { |
| 796 |
define('GETID3_HELPERAPPSDIR', false ); |
| 797 |
} |
| 798 |
|
| 799 |
if( !defined('GETID3_TEMP_DIR') && function_exists('get_temp_dir') ) // If wordpress function is available, lets use it |
| 800 |
{ |
| 801 |
$temp_dir = get_temp_dir(); // WordPress temp folder |
| 802 |
if( is_dir($temp_dir) ) |
| 803 |
define('GETID3_TEMP_DIR', $temp_dir); |
| 804 |
} |
| 805 |
|
| 806 |
if( !empty($GLOBALS['wp_version']) && version_compare($GLOBALS['wp_version'], '5.2', '>' ) && file_exists(ABSPATH . WPINC . '/ID3/getid3.php') ) |
| 807 |
require_once( ABSPATH . WPINC . '/ID3/getid3.php' ); |
| 808 |
else if( defined('POWERPRESS_GETID3_LIBRARY') && is_file(POWERPRESS_GETID3_LIBRARY) ) |
| 809 |
require_once(POWERPRESS_GETID3_LIBRARY); |
| 810 |
else if( defined('POWERPRESS_ABSPATH') ) |
| 811 |
require_once(POWERPRESS_ABSPATH.'/getid3/getid3.php'); |
| 812 |
else if( file_exists( dirname(__FILE__) .'/getid3/getid3.php' ) ) |
| 813 |
require_once( dirname(__FILE__) .'/getid3/getid3.php'); |
| 814 |
|
| 815 |
define('POWERPRESS_GETID3_LOADED', true); |
| 816 |
return true; |
| 817 |
} |
| 818 |
|
| 819 |
function remoteread_curl_writefunc($curl, $data) |
| 820 |
{ |
| 821 |
$this->m_data .= $data; |
| 822 |
if( strlen($this->m_data) > $this->m_DownloadBytesLimit ) |
| 823 |
{ |
| 824 |
return 0; // stop the download here... |
| 825 |
} |
| 826 |
return strlen($data); |
| 827 |
} |
| 828 |
}; |
| 829 |
|
| 830 |
/* |
| 831 |
// Example usage: |
| 832 |
$Mp3Info = new Mp3Info(); |
| 833 |
$file = 'http://content.blubrry.com/geeknewscentral/GNC-2017-07-06.mp3'; |
| 834 |
if( $Data = $Mp3Info->GetMp3Info($file) ) |
| 835 |
{ |
| 836 |
echo 'Success: '; |
| 837 |
echo print_r( $Data ); |
| 838 |
echo PHP_EOL; |
| 839 |
exit; |
| 840 |
} |
| 841 |
else |
| 842 |
{ |
| 843 |
echo 'Error: '; |
| 844 |
echo $Mp3Info->GetError(); |
| 845 |
echo PHP_EOL; |
| 846 |
exit; |
| 847 |
} |
| 848 |
*/ |
| 849 |
|
| 850 |
// eof |