PublicEmailController.php
1 month ago
PublicEmailRoute.php
1 month ago
ShareMetadataBuilder.php
2 months ago
ShareVisibility.php
2 months ago
index.php
2 months ago
PublicEmailRoute.php
137 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Sharing; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | use MailPoet\Newsletter\Url as NewsletterUrl; |
| 10 | use MailPoet\Util\ThirdPartyOutput; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | class PublicEmailRoute { |
| 14 | public const QUERY_VAR = 'mailpoet_public_email'; |
| 15 | private const IDENTIFIER_CAPTURE = '([0-9a-f]{12}(?:-[^/]+)?)'; |
| 16 | |
| 17 | /** @var PublicEmailController */ |
| 18 | private $controller; |
| 19 | |
| 20 | /** @var NewsletterUrl */ |
| 21 | private $newsletterUrl; |
| 22 | |
| 23 | /** @var WPFunctions */ |
| 24 | private $wp; |
| 25 | |
| 26 | public function __construct( |
| 27 | PublicEmailController $controller, |
| 28 | NewsletterUrl $newsletterUrl, |
| 29 | WPFunctions $wp |
| 30 | ) { |
| 31 | $this->controller = $controller; |
| 32 | $this->newsletterUrl = $newsletterUrl; |
| 33 | $this->wp = $wp; |
| 34 | } |
| 35 | |
| 36 | public function init(): void { |
| 37 | $this->wp->addAction('init', [$this, 'registerRewriteRule']); |
| 38 | $this->wp->addFilter('query_vars', [$this, 'addQueryVars']); |
| 39 | $this->wp->addAction('template_redirect', [$this, 'render']); |
| 40 | } |
| 41 | |
| 42 | public function registerRewriteRule(): void { |
| 43 | $prefix = ltrim($this->newsletterUrl->getPublicSharePathPrefix(), '/'); |
| 44 | $this->wp->addRewriteRule( |
| 45 | '^' . $prefix . self::IDENTIFIER_CAPTURE . '/?$', |
| 46 | 'index.php?' . self::QUERY_VAR . '=$matches[1]', |
| 47 | 'top' |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | public function addQueryVars(array $queryVars): array { |
| 52 | $queryVars[] = self::QUERY_VAR; |
| 53 | return $queryVars; |
| 54 | } |
| 55 | |
| 56 | public function render(): void { |
| 57 | $identifier = $this->getIdentifier(); |
| 58 | if ($identifier === '') { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | $newsletter = $this->controller->getNewsletter($identifier); |
| 64 | } catch (InvalidArgumentException $e) { |
| 65 | // The path matched our prefix but the identifier didn't resolve to a |
| 66 | // shareable newsletter. Let WP continue: it will serve a real page at |
| 67 | // that URL if one exists, or fall through to its own 404. |
| 68 | return; |
| 69 | } |
| 70 | if (!$this->controller->isCanonicalIdentifier($identifier, $newsletter)) { |
| 71 | $this->wp->wpSafeRedirect($this->controller->getCanonicalUrl($newsletter), 301); |
| 72 | exit; |
| 73 | } |
| 74 | $this->display($this->controller->render($newsletter)); |
| 75 | } |
| 76 | |
| 77 | private function getIdentifier(): string { |
| 78 | $identifier = $this->wp->getQueryVar(self::QUERY_VAR, ''); |
| 79 | if (is_string($identifier) && trim($identifier) !== '') { |
| 80 | return trim($identifier); |
| 81 | } |
| 82 | |
| 83 | return $this->getIdentifierFromRequestPath(); |
| 84 | } |
| 85 | |
| 86 | private function getIdentifierFromRequestPath(): string { |
| 87 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized through WPFunctions below. |
| 88 | $rawUri = $_SERVER['REQUEST_URI'] ?? ''; |
| 89 | if (!is_string($rawUri) || $rawUri === '') { |
| 90 | return ''; |
| 91 | } |
| 92 | $unslashed = $this->wp->wpUnslash($rawUri); |
| 93 | if (!is_string($unslashed)) { |
| 94 | return ''; |
| 95 | } |
| 96 | $requestUri = $this->wp->sanitizeTextField($unslashed); |
| 97 | if (!is_string($requestUri) || $requestUri === '') { |
| 98 | return ''; |
| 99 | } |
| 100 | |
| 101 | $requestPath = $this->wp->wpParseUrl($requestUri, PHP_URL_PATH); |
| 102 | if (!is_string($requestPath)) { |
| 103 | return ''; |
| 104 | } |
| 105 | |
| 106 | $homePath = $this->wp->wpParseUrl($this->wp->homeUrl('/'), PHP_URL_PATH); |
| 107 | if (is_string($homePath) && $homePath !== '/') { |
| 108 | $homePath = rtrim($homePath, '/'); |
| 109 | if (strpos($requestPath, $homePath . '/') === 0) { |
| 110 | $requestPath = substr($requestPath, strlen($homePath)); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $pathPrefix = $this->newsletterUrl->getPublicSharePathPrefix(); |
| 115 | if (strpos($requestPath, $pathPrefix) !== 0) { |
| 116 | return ''; |
| 117 | } |
| 118 | |
| 119 | $identifier = trim(substr($requestPath, strlen($pathPrefix)), '/'); |
| 120 | if ($identifier === '' || strpos($identifier, '/') !== false) { |
| 121 | return ''; |
| 122 | } |
| 123 | |
| 124 | return $this->newsletterUrl->parsePublicShareIdentifier($identifier) ? $identifier : ''; |
| 125 | } |
| 126 | |
| 127 | private function display(string $html): void { |
| 128 | ThirdPartyOutput::preventHtmlRewriting(); |
| 129 | header('Content-Type: text/html; charset=utf-8'); |
| 130 | header('Cache-Control: private, no-store, max-age=0'); |
| 131 | header('X-Robots-Tag: noindex, nofollow'); |
| 132 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 133 | echo $html; |
| 134 | exit; |
| 135 | } |
| 136 | } |
| 137 |