8889841cFacebookUrlManipulator.php000064400000012135150513167620011675 0ustar00 0) { $query = '?' . http_build_query($params, null, '&'); } } $scheme = isset($parts['scheme']) ? $parts['scheme'] . '://' : ''; $host = isset($parts['host']) ? $parts['host'] : ''; $port = isset($parts['port']) ? ':' . $parts['port'] : ''; $path = isset($parts['path']) ? $parts['path'] : ''; $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : ''; return $scheme . $host . $port . $path . $query . $fragment; } /** * Gracefully appends params to the URL. * * @param string $url The URL that will receive the params. * @param array $newParams The params to append to the URL. * * @return string */ public static function appendParamsToUrl($url, array $newParams = []) { if (empty($newParams)) { return $url; } if (strpos($url, '?') === false) { return $url . '?' . http_build_query($newParams, null, '&'); } list($path, $query) = explode('?', $url, 2); $existingParams = []; parse_str($query, $existingParams); // Favor params from the original URL over $newParams $newParams = array_merge($newParams, $existingParams); // Sort for a predicable order ksort($newParams); return $path . '?' . http_build_query($newParams, null, '&'); } /** * Returns the params from a URL in the form of an array. * * @param string $url The URL to parse the params from. * * @return array */ public static function getParamsAsArray($url) { $query = parse_url($url, PHP_URL_QUERY); if (!$query) { return []; } $params = []; parse_str($query, $params); return $params; } /** * Adds the params of the first URL to the second URL. * * Any params that already exist in the second URL will go untouched. * * @param string $urlToStealFrom The URL harvest the params from. * @param string $urlToAddTo The URL that will receive the new params. * * @return string The $urlToAddTo with any new params from $urlToStealFrom. */ public static function mergeUrlParams($urlToStealFrom, $urlToAddTo) { $newParams = static::getParamsAsArray($urlToStealFrom); // Nothing new to add, return as-is if (!$newParams) { return $urlToAddTo; } return static::appendParamsToUrl($urlToAddTo, $newParams); } /** * Check for a "/" prefix and prepend it if not exists. * * @param string|null $string * * @return string|null */ public static function forceSlashPrefix($string) { if (!$string) { return $string; } return strpos($string, '/') === 0 ? $string : '/' . $string; } /** * Trims off the hostname and Graph version from a URL. * * @param string $urlToTrim The URL the needs the surgery. * * @return string The $urlToTrim with the hostname and Graph version removed. */ public static function baseGraphUrlEndpoint($urlToTrim) { return '/' . preg_replace('/^https:\/\/.+\.facebook\.com(\/v.+?)?\//', '', $urlToTrim); } } UrlDetectionInterface.php000064400000002461150513167620011510 0ustar00getHttpScheme() . '://' . $this->getHostName() . $this->getServerVar('REQUEST_URI'); } /** * Get the currently active URL scheme. * * @return string */ protected function getHttpScheme() { return $this->isBehindSsl() ? 'https' : 'http'; } /** * Tries to detect if the server is running behind an SSL. * * @return boolean */ protected function isBehindSsl() { // Check for proxy first $protocol = $this->getHeader('X_FORWARDED_PROTO'); if ($protocol) { return $this->protocolWithActiveSsl($protocol); } $protocol = $this->getServerVar('HTTPS'); if ($protocol) { return $this->protocolWithActiveSsl($protocol); } return (string)$this->getServerVar('SERVER_PORT') === '443'; } /** * Detects an active SSL protocol value. * * @param string $protocol * * @return boolean */ protected function protocolWithActiveSsl($protocol) { $protocol = strtolower((string)$protocol); return in_array($protocol, ['on', '1', 'https', 'ssl'], true); } /** * Tries to detect the host name of the server. * * Some elements adapted from * * @see https://github.com/symfony/HttpFoundation/blob/master/Request.php * * @return string */ protected function getHostName() { // Check for proxy first $header = $this->getHeader('X_FORWARDED_HOST'); if ($header && $this->isValidForwardedHost($header)) { $elements = explode(',', $header); $host = $elements[count($elements) - 1]; } elseif (!$host = $this->getHeader('HOST')) { if (!$host = $this->getServerVar('SERVER_NAME')) { $host = $this->getServerVar('SERVER_ADDR'); } } // trim and remove port number from host // host is lowercase as per RFC 952/2181 $host = strtolower(preg_replace('/:\d+$/', '', trim($host))); // Port number $scheme = $this->getHttpScheme(); $port = $this->getCurrentPort(); $appendPort = ':' . $port; // Don't append port number if a normal port. if (($scheme == 'http' && $port == '80') || ($scheme == 'https' && $port == '443')) { $appendPort = ''; } return $host . $appendPort; } protected function getCurrentPort() { // Check for proxy first $port = $this->getHeader('X_FORWARDED_PORT'); if ($port) { return (string)$port; } $protocol = (string)$this->getHeader('X_FORWARDED_PROTO'); if ($protocol === 'https') { return '443'; } return (string)$this->getServerVar('SERVER_PORT'); } /** * Returns the a value from the $_SERVER super global. * * @param string $key * * @return string */ protected function getServerVar($key) { return isset($_SERVER[$key]) ? $_SERVER[$key] : ''; } /** * Gets a value from the HTTP request headers. * * @param string $key * * @return string */ protected function getHeader($key) { return $this->getServerVar('HTTP_' . $key); } /** * Checks if the value in X_FORWARDED_HOST is a valid hostname * Could prevent unintended redirections * * @param string $header * * @return boolean */ protected function isValidForwardedHost($header) { $elements = explode(',', $header); $host = $elements[count($elements) - 1]; return preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $host) //valid chars check && 0 < strlen($host) && strlen($host) < 254 //overall length check && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $host); //length of each label } }