Cheetah
FacebookUrlDetectionHandler.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\Url;
25 
32 {
36  public function getCurrentUrl()
37  {
38  return $this->getHttpScheme() . '://' . $this->getHostName() . $this->getServerVar('REQUEST_URI');
39  }
40 
46  protected function getHttpScheme()
47  {
48  return $this->isBehindSsl() ? 'https' : 'http';
49  }
50 
56  protected function isBehindSsl()
57  {
58  // Check for proxy first
59  $protocol = $this->getHeader('X_FORWARDED_PROTO');
60  if ($protocol) {
61  return $this->protocolWithActiveSsl($protocol);
62  }
63 
64  $protocol = $this->getServerVar('HTTPS');
65  if ($protocol) {
66  return $this->protocolWithActiveSsl($protocol);
67  }
68 
69  return (string)$this->getServerVar('SERVER_PORT') === '443';
70  }
71 
79  protected function protocolWithActiveSsl($protocol)
80  {
81  $protocol = strtolower((string)$protocol);
82 
83  return in_array($protocol, ['on', '1', 'https', 'ssl'], true);
84  }
85 
95  protected function getHostName()
96  {
97  // Check for proxy first
98  $header = $this->getHeader('X_FORWARDED_HOST');
99  if ($header && $this->isValidForwardedHost($header)) {
100  $elements = explode(',', $header);
101  $host = $elements[count($elements) - 1];
102  } elseif (!$host = $this->getHeader('HOST')) {
103  if (!$host = $this->getServerVar('SERVER_NAME')) {
104  $host = $this->getServerVar('SERVER_ADDR');
105  }
106  }
107 
108  // trim and remove port number from host
109  // host is lowercase as per RFC 952/2181
110  $host = strtolower(preg_replace('/:\d+$/', '', trim($host)));
111 
112  // Port number
113  $scheme = $this->getHttpScheme();
114  $port = $this->getCurrentPort();
115  $appendPort = ':' . $port;
116 
117  // Don't append port number if a normal port.
118  if (($scheme == 'http' && $port == '80') || ($scheme == 'https' && $port == '443')) {
119  $appendPort = '';
120  }
121 
122  return $host . $appendPort;
123  }
124 
125  protected function getCurrentPort()
126  {
127  // Check for proxy first
128  $port = $this->getHeader('X_FORWARDED_PORT');
129  if ($port) {
130  return (string)$port;
131  }
132 
133  $protocol = (string)$this->getHeader('X_FORWARDED_PROTO');
134  if ($protocol === 'https') {
135  return '443';
136  }
137 
138  return (string)$this->getServerVar('SERVER_PORT');
139  }
140 
148  protected function getServerVar($key)
149  {
150  return isset($_SERVER[$key]) ? $_SERVER[$key] : '';
151  }
152 
160  protected function getHeader($key)
161  {
162  return $this->getServerVar('HTTP_' . $key);
163  }
164 
173  protected function isValidForwardedHost($header)
174  {
175  $elements = explode(',', $header);
176  $host = $elements[count($elements) - 1];
177 
178  return preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $host) //valid chars check
179  && 0 < strlen($host) && strlen($host) < 254 //overall length check
180  && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $host); //length of each label
181  }
182 }
Facebook\Url\UrlDetectionInterface
Definition: UrlDetectionInterface.php:32
php
Facebook\Url\FacebookUrlDetectionHandler\isValidForwardedHost
isValidForwardedHost($header)
Definition: FacebookUrlDetectionHandler.php:173
Facebook\Url\FacebookUrlDetectionHandler
Definition: FacebookUrlDetectionHandler.php:32
Facebook\Url\FacebookUrlDetectionHandler\getServerVar
getServerVar($key)
Definition: FacebookUrlDetectionHandler.php:148
Facebook\Url
Definition: FacebookUrlDetectionHandler.php:24
Facebook\Url\FacebookUrlDetectionHandler\getCurrentPort
getCurrentPort()
Definition: FacebookUrlDetectionHandler.php:125
Facebook\Url\FacebookUrlDetectionHandler\getCurrentUrl
getCurrentUrl()
Definition: FacebookUrlDetectionHandler.php:36
Facebook\Url\FacebookUrlDetectionHandler\getHeader
getHeader($key)
Definition: FacebookUrlDetectionHandler.php:160
Facebook\Url\FacebookUrlDetectionHandler\protocolWithActiveSsl
protocolWithActiveSsl($protocol)
Definition: FacebookUrlDetectionHandler.php:79
Facebook\Url\FacebookUrlDetectionHandler\getHttpScheme
getHttpScheme()
Definition: FacebookUrlDetectionHandler.php:46
Facebook\Url\FacebookUrlDetectionHandler\getHostName
getHostName()
Definition: FacebookUrlDetectionHandler.php:95
Facebook\Url\FacebookUrlDetectionHandler\isBehindSsl
isBehindSsl()
Definition: FacebookUrlDetectionHandler.php:56