| 1 |
<?php |
| 2 |
$GLOBALS['fwp_credentials'] = NULL; |
| 3 |
|
| 4 |
class FeedWordPress_File extends WP_SimplePie_File { |
| 5 |
function FeedWordPress_File ($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) { |
| 6 |
self::__construct($url, $timeout, $redirects, $headers, $useragent, $force_fsockopen); |
| 7 |
} |
| 8 |
|
| 9 |
function __construct ($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) { |
| 10 |
global $feedwordpress; |
| 11 |
global $wp_version; |
| 12 |
|
| 13 |
$source = NULL; |
| 14 |
if ($feedwordpress->subscribed($url)) : |
| 15 |
$source = $feedwordpress->subscription($url); |
| 16 |
endif; |
| 17 |
|
| 18 |
$this->url = $url; |
| 19 |
$this->timeout = $timeout; |
| 20 |
$this->redirects = $redirects; |
| 21 |
$this->headers = $headers; |
| 22 |
$this->useragent = $useragent; |
| 23 |
|
| 24 |
$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE; |
| 25 |
|
| 26 |
global $wpdb; |
| 27 |
global $fwp_credentials; |
| 28 |
|
| 29 |
if ( preg_match('/^http(s)?:\/\//i', $url) ) { |
| 30 |
$args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects); |
| 31 |
|
| 32 |
if ( !empty($this->headers) ) |
| 33 |
$args['headers'] = $this->headers; |
| 34 |
|
| 35 |
// Use default FWP user agent unless custom has been specified |
| 36 |
if ( SIMPLEPIE_USERAGENT != $this->useragent ) : |
| 37 |
$args['user-agent'] = $this->useragent; |
| 38 |
else : |
| 39 |
$args['user-agent'] = apply_filters('feedwordpress_user_agent', |
| 40 |
'FeedWordPress/'.FEEDWORDPRESS_VERSION |
| 41 |
.' (aggregator:feedwordpress; WordPress/'.$wp_version |
| 42 |
.' + '.SIMPLEPIE_NAME.'/'.SIMPLEPIE_VERSION |
| 43 |
.'; Allow like Gecko; +http://feedwordpress.radgeek.com/) at ' |
| 44 |
. feedwordpress_display_url(get_bloginfo('url')), |
| 45 |
$this |
| 46 |
); |
| 47 |
endif; |
| 48 |
|
| 49 |
// This is ugly as hell, but communicating up and down the chain |
| 50 |
// in any other way is difficult. |
| 51 |
|
| 52 |
if (!is_null($fwp_credentials)) : |
| 53 |
|
| 54 |
$args['authentication'] = $fwp_credentials['authentication']; |
| 55 |
$args['username'] = $fwp_credentials['username']; |
| 56 |
$args['password'] = $fwp_credentials['password']; |
| 57 |
|
| 58 |
elseif ($source InstanceOf SyndicatedLink) : |
| 59 |
|
| 60 |
$args['authentication'] = $source->authentication_method(); |
| 61 |
$args['username'] = $source->username(); |
| 62 |
$args['password'] = $source->password(); |
| 63 |
|
| 64 |
endif; |
| 65 |
|
| 66 |
FeedWordPress::diagnostic('updated_feeds:http', "HTTP [$url] ⇜ ".esc_html(FeedWordPress::val($args))); |
| 67 |
$res = wp_remote_request($url, $args); |
| 68 |
FeedWordPress::diagnostic('updated_feeds:http', "HTTP [$url] ⇝ ".esc_html(FeedWordPress::val($res))); |
| 69 |
|
| 70 |
if ( is_wp_error($res) ) { |
| 71 |
$this->error = 'WP HTTP Error: ' . $res->get_error_message(); |
| 72 |
$this->success = false; |
| 73 |
} else { |
| 74 |
$this->headers = wp_remote_retrieve_headers( $res ); |
| 75 |
$this->body = wp_remote_retrieve_body( $res ); |
| 76 |
$this->status_code = wp_remote_retrieve_response_code( $res ); |
| 77 |
} |
| 78 |
|
| 79 |
if ($source InstanceOf SyndicatedLink) : |
| 80 |
$source->update_setting('link/filesize', strlen($this->body)); |
| 81 |
$source->update_setting('link/http status', $this->status_code); |
| 82 |
$source->save_settings(/*reload=*/ true); |
| 83 |
endif; |
| 84 |
|
| 85 |
} else { |
| 86 |
if ( ! $this->body = file_get_contents($url) ) { |
| 87 |
$this->error = 'file_get_contents could not read the file'; |
| 88 |
$this->success = false; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// SimplePie makes a strongly typed check against integers with |
| 93 |
// this, but WordPress puts a string in. Which causes caching |
| 94 |
// to break and fall on its ass when SimplePie is getting a 304, |
| 95 |
// but doesn't realize it because this member is "304" instead. |
| 96 |
$this->status_code = (int) $this->status_code; |
| 97 |
} |
| 98 |
} /* class FeedWordPress_File () */ |
| 99 |
|
| 100 |
|