class-my-yoast-proxy.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Loads the MyYoast proxy.
  9. *
  10. * This class registers a proxy page on `admin.php`. Which is reached with the `page=PAGE_IDENTIFIER` parameter.
  11. * It will read external files and serves them like they are located locally.
  12. */
  13. class WPSEO_MyYoast_Proxy implements WPSEO_WordPress_Integration {
  14. /**
  15. * The page identifier used in WordPress to register the MyYoast proxy page.
  16. *
  17. * @var string
  18. */
  19. const PAGE_IDENTIFIER = 'wpseo_myyoast_proxy';
  20. /**
  21. * The cache control's max age. Used in the header of a successful proxy response.
  22. *
  23. * @var int
  24. */
  25. const CACHE_CONTROL_MAX_AGE = DAY_IN_SECONDS;
  26. /**
  27. * Registers the hooks when the user is on the right page.
  28. *
  29. * @codeCoverageIgnore
  30. *
  31. * @return void
  32. */
  33. public function register_hooks() {
  34. if ( ! $this->is_proxy_page() ) {
  35. return;
  36. }
  37. // Register the page for the proxy.
  38. add_action( 'admin_menu', [ $this, 'add_proxy_page' ] );
  39. add_action( 'admin_init', [ $this, 'handle_proxy_page' ] );
  40. }
  41. /**
  42. * Registers the proxy page. It does not actually add a link to the dashboard.
  43. *
  44. * @codeCoverageIgnore
  45. *
  46. * @return void
  47. */
  48. public function add_proxy_page() {
  49. add_dashboard_page( '', '', 'read', self::PAGE_IDENTIFIER, '' );
  50. }
  51. /**
  52. * Renders the requested proxy page and exits to prevent the WordPress UI from loading.
  53. *
  54. * @codeCoverageIgnore
  55. *
  56. * @return void
  57. */
  58. public function handle_proxy_page() {
  59. $this->render_proxy_page();
  60. // Prevent the WordPress UI from loading.
  61. exit;
  62. }
  63. /**
  64. * Renders the requested proxy page.
  65. *
  66. * This is separated from the exits to be able to test it.
  67. *
  68. * @return void
  69. */
  70. public function render_proxy_page() {
  71. $proxy_options = $this->determine_proxy_options();
  72. if ( $proxy_options === [] ) {
  73. // Do not accept any other file than implemented.
  74. $this->set_header( 'HTTP/1.0 501 Requested file not implemented' );
  75. return;
  76. }
  77. // Set the headers before serving the remote file.
  78. $this->set_header( 'Content-Type: ' . $proxy_options['content_type'] );
  79. $this->set_header( 'Cache-Control: max-age=' . self::CACHE_CONTROL_MAX_AGE );
  80. if ( $this->should_load_url_directly() ) {
  81. /*
  82. * If an error occurred, fallback to the next proxy method (`wp_remote_get`).
  83. * Otherwise, we are done here.
  84. */
  85. if ( $this->load_url( $proxy_options['url'] ) ) {
  86. return;
  87. }
  88. }
  89. try {
  90. echo $this->get_remote_url_body( $proxy_options['url'] );
  91. }
  92. catch ( Exception $e ) {
  93. /*
  94. * Reset the file headers because the loading failed.
  95. *
  96. * Note: Due to supporting PHP 5.2 `header_remove` can not be used here.
  97. * Overwrite the headers instead.
  98. */
  99. $this->set_header( 'Content-Type: text/plain' );
  100. $this->set_header( 'Cache-Control: max-age=0' );
  101. $this->set_header( 'HTTP/1.0 500 ' . $e->getMessage() );
  102. }
  103. }
  104. /**
  105. * Tries to load the given url via `wp_remote_get`.
  106. *
  107. * @codeCoverageIgnore
  108. *
  109. * @param string $url The url to load.
  110. *
  111. * @throws Exception When `wp_remote_get` returned an error.
  112. * @throws Exception When the response code is not 200.
  113. *
  114. * @return string The body of the response.
  115. */
  116. protected function get_remote_url_body( $url ) {
  117. $response = wp_remote_get( $url );
  118. if ( $response instanceof WP_Error ) {
  119. throw new Exception( 'Unable to retrieve file from MyYoast' );
  120. }
  121. if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
  122. throw new Exception( 'Received unexpected response from MyYoast' );
  123. }
  124. return wp_remote_retrieve_body( $response );
  125. }
  126. /**
  127. * Tries to load the given url.
  128. *
  129. * @link https://php.net/manual/en/function.readfile.php
  130. *
  131. * @codeCoverageIgnore
  132. *
  133. * @param string $url The url to load.
  134. *
  135. * @return bool False if an error occurred.
  136. */
  137. protected function load_url( $url ) {
  138. return readfile( $url ) !== false;
  139. }
  140. /**
  141. * Determines the proxy options based on the file and plugin version arguments.
  142. *
  143. * When the file is known it returns an array like this:
  144. * <code>
  145. * $array = array(
  146. * 'content_type' => 'the content type'
  147. * 'url' => 'the url, possibly with the plugin version'
  148. * )
  149. * </code>
  150. *
  151. * @return array Empty for an unknown file. See format above for known files.
  152. */
  153. protected function determine_proxy_options() {
  154. if ( $this->get_proxy_file() === 'research-webworker' ) {
  155. return [
  156. 'content_type' => 'text/javascript; charset=UTF-8',
  157. 'url' => 'https://my.yoast.com/api/downloads/file/analysis-worker?plugin_version=' . $this->get_plugin_version(),
  158. ];
  159. }
  160. return [];
  161. }
  162. /**
  163. * Checks the PHP configuration of allow_url_fopen.
  164. *
  165. * @codeCoverageIgnore
  166. *
  167. * @link https://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
  168. *
  169. * @return bool True when the PHP configuration allows for url loading via readfile.
  170. */
  171. protected function should_load_url_directly() {
  172. return ! ! ini_get( 'allow_url_fopen' );
  173. }
  174. /**
  175. * Checks if the current page is the MyYoast proxy page.
  176. *
  177. * @codeCoverageIgnore
  178. *
  179. * @return bool True when the page request parameter equals the proxy page.
  180. */
  181. protected function is_proxy_page() {
  182. return filter_input( INPUT_GET, 'page' ) === self::PAGE_IDENTIFIER;
  183. }
  184. /**
  185. * Returns the proxy file from the HTTP request parameters.
  186. *
  187. * @codeCoverageIgnore
  188. *
  189. * @return string The sanitized file request parameter.
  190. */
  191. protected function get_proxy_file() {
  192. return filter_input( INPUT_GET, 'file', FILTER_SANITIZE_STRING );
  193. }
  194. /**
  195. * Returns the plugin version from the HTTP request parameters.
  196. *
  197. * @codeCoverageIgnore
  198. *
  199. * @return string The sanitized plugin_version request parameter.
  200. */
  201. protected function get_plugin_version() {
  202. $plugin_version = filter_input( INPUT_GET, 'plugin_version', FILTER_SANITIZE_STRING );
  203. // Replace slashes to secure against requiring a file from another path.
  204. $plugin_version = str_replace( [ '/', '\\' ], '_', $plugin_version );
  205. return $plugin_version;
  206. }
  207. /**
  208. * Sets the HTTP header.
  209. *
  210. * This is a tiny helper function to enable better testing.
  211. *
  212. * @codeCoverageIgnore
  213. *
  214. * @param string $header The header to set.
  215. *
  216. * @return void
  217. */
  218. protected function set_header( $header ) {
  219. header( $header );
  220. }
  221. }