/** * Call the inliner CSS processor * * @param string $content * * @return string */ public function process($content) { GeneralUtility::requireOnce(ExtensionManagementUtility::extPath('ink', 'Resources/Private/Php/vendor/autoload.php')); $pattern = '%<(link|style)(?=[^<>]*?(?:type="(text/css)"|>))(?=[^<>]*?(?:media="([^<>"]*)"|>))(?=[^<>]*?(?:href="(.*?)"|>))(?=[^<>]*(?:rel="([^<>"]*)"|>))(?:.*?</\\1>|[^<>]*>)%si'; $matches = array(); $css = ''; preg_match_all($pattern, $content, $matches); if (isset($matches[0])) { foreach ($matches[0] as $key => $match) { if ($matches[1][$key] === 'style') { $css .= strip_tags($match); } elseif (strpos($match, 'type="text/css"') !== FALSE) { $file = preg_replace('/^(.+)\\.(\\d+)\\.css$/', '$1.css', $matches[4][$key]); $parts = parse_url($file); if (isset($parts['query'])) { unset($parts['query']); } if (!isset($parts['host'])) { $parts['path'] = ltrim($parts['path'], '/'); } if ($parts['host'] === GeneralUtility::getIndpEnv('TYPO3_HOST_ONLY')) { unset($parts['scheme']); unset($parts['host']); $parts['path'] = ltrim($parts['path'], '/'); } $file = HttpUtility::buildUrl($parts); $css .= GeneralUtility::getUrl($file); } else { continue; } $content = str_replace($match, '', $content); } } $format = new CssToInlineStyles($content, $css); return $format->convert(); }
/** * Checks if a given string is a Uniform Resource Locator (URL). * * On seriously malformed URLs, parse_url may return FALSE and emit an * E_WARNING. * * filter_var() requires a scheme to be present. * * http://www.faqs.org/rfcs/rfc2396.html * Scheme names consist of a sequence of characters beginning with a * lower case letter and followed by any combination of lower case letters, * digits, plus ("+"), period ("."), or hyphen ("-"). For resiliency, * programs interpreting URI should treat upper case letters as equivalent to * lower case in scheme names (e.g., allow "HTTP" as well as "http"). * scheme = alpha *( alpha | digit | "+" | "-" | "." ) * * Convert the domain part to punicode if it does not look like a regular * domain name. Only the domain part because RFC3986 specifies the the rest of * the url may not contain special characters: * http://tools.ietf.org/html/rfc3986#appendix-A * * @param string $url The URL to be validated * @return bool Whether the given URL is valid */ public static function isValidUrl($url) { $parsedUrl = parse_url($url); if (!$parsedUrl || !isset($parsedUrl['scheme'])) { return false; } // HttpUtility::buildUrl() will always build urls with <scheme>:// // our original $url might only contain <scheme>: (e.g. mail:) // so we convert that to the double-slashed version to ensure // our check against the $recomposedUrl is proper if (!self::isFirstPartOfStr($url, $parsedUrl['scheme'] . '://')) { $url = str_replace($parsedUrl['scheme'] . ':', $parsedUrl['scheme'] . '://', $url); } $recomposedUrl = HttpUtility::buildUrl($parsedUrl); if ($recomposedUrl !== $url) { // The parse_url() had to modify characters, so the URL is invalid return false; } if (isset($parsedUrl['host']) && !preg_match('/^[a-z0-9.\\-]*$/i', $parsedUrl['host'])) { $parsedUrl['host'] = self::idnaEncode($parsedUrl['host']); } return filter_var(HttpUtility::buildUrl($parsedUrl), FILTER_VALIDATE_URL) !== false; }
/** * @param array $urlParts * @param string $expected * @dataProvider isUrlBuiltCorrectlyDataProvider * @test */ public function isUrlBuiltCorrectly(array $urlParts, $expected) { $url = \TYPO3\CMS\Core\Utility\HttpUtility::buildUrl($urlParts); $this->assertEquals($expected, $url); }
public function getUser() { $user = false; if ($this->isServiceResponsible()) { $loginPid = $this->extConf['basic.']['login_pid']; $urlParts = ['scheme' => GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https' : 'http', 'host' => GeneralUtility::getIndpEnv('HTTP_HOST')]; $additionalUrlParts = ['query' => 'id=' . $loginPid . '&no_cache=1&logintype=login&tx_pxhybridauth_login[provider]=' . $this->provider . '&pid=' . $this->userRecordStoragePage . '&tx_pxhybridauth_login[redirect_url]=' . $this->redirectUrl . '&tx_pxhybridauth_login[redirect_pid]=' . $this->redirectPid]; ArrayUtility::mergeRecursiveWithOverrule($urlParts, $additionalUrlParts); $returnUrl = HttpUtility::buildUrl($urlParts); $this->signalSlotDispatcher->dispatch(__CLASS__, 'returnUrl', [&$returnUrl, $this]); $additionalUrlParts = ['query' => 'id=' . $loginPid . '&no_cache=1&tx_pxhybridauth_login[login_error]=1&tx_pxhybridauth_login[provider]=' . $this->provider]; ArrayUtility::mergeRecursiveWithOverrule($urlParts, $additionalUrlParts); $returnUrlNoUser = HttpUtility::buildUrl($urlParts); $this->signalSlotDispatcher->dispatch(__CLASS__, 'returnUrlNoUser', [&$returnUrlNoUser, $this]); $socialUser = $this->singleSignOnUtility->authenticate($this->provider, $returnUrl); $user = $this->fetchUserRecordByIdentifier($socialUser->identifier); if (isset($user['username'])) { $this->login['uname'] = $user['username']; } $this->signalSlotDispatcher->dispatch(__CLASS__, 'getUser', [&$user, $socialUser, $this]); // redirect to px_hybrid_auth login box, when no user found if (!$user) { HttpUtility::redirect($returnUrlNoUser); } } return $user; }
/** * Get the Service URI * * @return string */ protected function getServiceUri() { $uri = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL'); $parts = (array) parse_url($uri); $parts['query'] = 'eID=SoapServer&server=' . $this->serverKey; return HttpUtility::buildUrl($parts); }
/** * Correct url based on RealURL configuration * if defaultToHTMLsuffixOnPrev is not set, force the trailing / in url * * @param string $url * @return string */ protected function correctUrl($url) { $urlParameters = parse_url($url); if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('realurl')) { if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']) && (int) $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']['_DEFAULT']['fileName']['defaultToHTMLsuffixOnPrev'] === 0) { if (substr($url, -1) !== '/') { if (isset($urlParameters['path'])) { $pathInfo = pathinfo($urlParameters['path']); if (empty($pathInfo['extension'])) { $urlParameters['path'] = rtrim($urlParameters['path'], '/') . '/'; $url = \TYPO3\CMS\Core\Utility\HttpUtility::buildUrl($urlParameters); } } } } } // Only look if query is configured and link is relative to the root if (isset($urlParameters['query']) && !isset($urlParameters['host'])) { $idOnlyRegEx = '/^id=[1-9][0-9]{0,15}$/i'; if (preg_match($idOnlyRegEx, $urlParameters['query'])) { $pageId = (int) str_replace('id=', '', $urlParameters['query']); if ($pageId > 0) { $url = $pageId; } } elseif (class_exists('TYPO3\\CMS\\Core\\Utility\\MathUtility') && method_exists('TYPO3\\CMS\\Core\\Utility\\MathUtility', 'canBeInterpretedAsInteger')) { if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($urlParameters['query'])) { $url = $urlParameters['query']; } } } return $url; }
/** * Generate the Url Hash * * @param string $url * @return string */ public function generateUrlHash($url) { $urlParts = parse_url($url); if (!empty($urlParts['path'])) { // Remove trailing slash from url generation $urlParts['path'] = rtrim($urlParts['path'], '/'); } if (!empty($urlParts['query'])) { $excludedQueryParameters = $this->getCHashExcludedParameters(); if (!empty($excludedQueryParameters)) { parse_str($urlParts['query'], $queryParameters); if (!empty($queryParameters)) { foreach ($queryParameters as $key => $value) { if (in_array($key, $excludedQueryParameters)) { unset($queryParameters[$key]); $this->keptQueryParameters[$key] = $value; } } $urlParts['query'] = !empty($queryParameters) ? http_build_query($queryParameters) : null; } } } $url = HttpUtility::buildUrl($urlParts); // Make sure the hash is case-insensitive $url = strtolower($url); return sha1($url); }
protected function redirectToAction($action = 'main') { $urlParts = parse_url(GeneralUtility::getIndpEnv('REQUEST_URI')); parse_str($urlParts['query'], $queryParts); $queryParts['action'] = $action; $urlParts['query'] = http_build_query($queryParts); $redirectUrl = \TYPO3\CMS\Core\Utility\HttpUtility::buildUrl($urlParts); header('Location: ' . $redirectUrl); exit; }