| 1 |
<?php |
| 2 |
global $fwp_credentials; |
| 3 |
|
| 4 |
/** @var array|null If required, login and password to connect to RSS feed; NULL otherwise. */ |
| 5 |
$fwp_credentials = NULL; |
| 6 |
/** |
| 7 |
* Our own variant of WP's SimplePie |
| 8 |
* |
| 9 |
* @extends WP_SimplePie_File |
| 10 |
*/ |
| 11 |
class FeedWordPie_File extends WP_SimplePie_File { |
| 12 |
|
| 13 |
/** |
| 14 |
* Timeout. |
| 15 |
* |
| 16 |
* @var int How long the connection should stay open in seconds. |
| 17 |
* |
| 18 |
* @see WP_SimplePie_File |
| 19 |
*/ |
| 20 |
public $timeout = 10; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class constructor. |
| 24 |
* |
| 25 |
* @param string $url URL for the RSS feed. |
| 26 |
* @param int $timeout Timeout to wait for this feed. |
| 27 |
* @param int $redirects How many redirects we follow. |
| 28 |
* @param array|null $headers List of HTTP headers to send (i,portant for authentication, etc.) |
| 29 |
* @param string|null $useragent Identification of the User-Agent making the RSS request. |
| 30 |
* @param bool $force_fsockopen @deprecated and always ignored |
| 31 |
* |
| 32 |
* @uses FeedWordPress::diagnostic() |
| 33 |
* @uses MyPHP::val() |
| 34 |
* |
| 35 |
* @global $feedwordpress |
| 36 |
* @global $wp_version |
| 37 |
*/ |
| 38 |
public function __construct( $url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) { |
| 39 |
global $feedwordpress; |
| 40 |
global $wp_version; |
| 41 |
|
| 42 |
if ( $force_fsockopen ) { |
| 43 |
error_log( "deprecated usage of \$force_fsockopen, ignored" ); |
| 44 |
} |
| 45 |
|
| 46 |
$source = NULL; |
| 47 |
if ( !empty( $feedwordpress ) and $feedwordpress->subscribed( $url ) ) : |
| 48 |
$source = $feedwordpress->subscription( $url ); |
| 49 |
endif; |
| 50 |
|
| 51 |
$this->url = $url; |
| 52 |
$this->timeout = $timeout; |
| 53 |
$this->redirects = $redirects; |
| 54 |
$this->headers = $headers; |
| 55 |
$this->useragent = $useragent; |
| 56 |
|
| 57 |
$this->method = SIMPLEPIE_FILE_SOURCE_REMOTE; |
| 58 |
|
| 59 |
global $fwp_credentials; |
| 60 |
|
| 61 |
if ( preg_match( '/^http(s)?:\/\//i', $url ) ) : |
| 62 |
$args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects ); |
| 63 |
|
| 64 |
if ( ! empty( $this->headers ) ) : |
| 65 |
$args['headers'] = $this->headers; |
| 66 |
endif; |
| 67 |
|
| 68 |
// Use default FWP user agent unless custom has been specified |
| 69 |
if ( SIMPLEPIE_USERAGENT != $this->useragent ) : |
| 70 |
$args['user-agent'] = $this->useragent; |
| 71 |
else : |
| 72 |
$args['user-agent'] = apply_filters( |
| 73 |
'feedwordpress_user_agent', |
| 74 |
'FeedWordPress/' . FEEDWORDPRESS_VERSION |
| 75 |
. ' (aggregator:feedwordpress; WordPress/' . $wp_version |
| 76 |
. ' + ' . SIMPLEPIE_NAME . '/' . SIMPLEPIE_VERSION |
| 77 |
. '; Allow like Gecko; +http://feedwordpress.radgeek.com/) at ' |
| 78 |
. feedwordpress_display_url( get_bloginfo( 'url' ) ), |
| 79 |
$this |
| 80 |
); |
| 81 |
endif; |
| 82 |
|
| 83 |
// This is ugly as hell, but communicating up and down the chain |
| 84 |
// in any other way is difficult. |
| 85 |
if ( ! is_null( $fwp_credentials ) ) : |
| 86 |
$args['authentication'] = $fwp_credentials['authentication']; |
| 87 |
$args['username'] = $fwp_credentials['username']; |
| 88 |
$args['password'] = $fwp_credentials['password']; |
| 89 |
elseif ( $source InstanceOf SyndicatedLink ) : |
| 90 |
$args['authentication'] = $source->authentication_method(); |
| 91 |
$args['username'] = $source->username(); |
| 92 |
$args['password'] = $source->password(); |
| 93 |
endif; |
| 94 |
|
| 95 |
FeedWordPress::diagnostic( 'updated_feeds:http', "HTTP [$url] ⌈ " . esc_html( MyPHP::val( $args ) ) ); |
| 96 |
$res = wp_remote_request( $url, $args ); |
| 97 |
FeedWordPress::diagnostic( 'updated_feeds:http', "HTTP [$url] ⌉ " . esc_html( MyPHP::val( $res ) ) ); |
| 98 |
|
| 99 |
if ( is_wp_error( $res ) ) : |
| 100 |
$this->error = 'WP HTTP Error: ' . $res->get_error_message(); |
| 101 |
$this->success = false; |
| 102 |
else : |
| 103 |
$this->headers = wp_remote_retrieve_headers( $res ); |
| 104 |
$this->body = wp_remote_retrieve_body( $res ); |
| 105 |
$this->status_code = wp_remote_retrieve_response_code( $res ); |
| 106 |
endif; |
| 107 |
|
| 108 |
if ( $source InstanceOf SyndicatedLink ) : |
| 109 |
$source->update_setting( 'link/filesize', strlen( $this->body ) ); |
| 110 |
$source->update_setting( 'link/http status', $this->status_code ); |
| 111 |
$source->save_settings( /*reload=*/ true ); |
| 112 |
endif; |
| 113 |
|
| 114 |
/* Do not allow schemes other than http(s)? for the time being. |
| 115 |
* They are unlikely to be used; and unrestricted use of schemes |
| 116 |
* allows for user to use an unrestricted file:/// scheme, which |
| 117 |
* may result in exploits by WordPress users against the web |
| 118 |
* hosting environment. |
| 119 |
*/ |
| 120 |
else : |
| 121 |
$this->error = __( 'FeedWordPress only allows HTTP or HTTPS URL schemes' ); |
| 122 |
$this->success = false; |
| 123 |
endif; |
| 124 |
|
| 125 |
/* SimplePie makes a strongly typed check against integers with |
| 126 |
* this, but WordPress puts a string in. Which causes caching |
| 127 |
* to break and fall on its ass when SimplePie is getting a 304, |
| 128 |
* but doesn't realize it because this member is "304" instead. |
| 129 |
*/ |
| 130 |
$this->status_code = (int) $this->status_code; |
| 131 |
} /* FeedWordPie_File::__construct () */ |
| 132 |
} /* class FeedWordPie_File () */ |
| 133 |
|
| 134 |
|