ServerUrl.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * Helper for returning the current server URL (optionally with request URI)
  23. *
  24. * @category Zend
  25. * @package Zend_View
  26. * @subpackage Helper
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_View_Helper_ServerUrl
  31. {
  32. /**
  33. * Scheme
  34. *
  35. * @var string
  36. */
  37. protected $_scheme;
  38. /**
  39. * Host (including port)
  40. *
  41. * @var string
  42. */
  43. protected $_host;
  44. /**
  45. * Constructor
  46. *
  47. * @return void
  48. */
  49. public function __construct()
  50. {
  51. if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)) {
  52. $scheme = 'https';
  53. } else {
  54. $scheme = 'http';
  55. }
  56. $this->setScheme($scheme);
  57. if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
  58. $this->setHost($_SERVER['HTTP_HOST']);
  59. } else if (isset($_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'])) {
  60. $name = $_SERVER['SERVER_NAME'];
  61. $port = $_SERVER['SERVER_PORT'];
  62. if (($scheme == 'http' && $port == 80) ||
  63. ($scheme == 'https' && $port == 443)) {
  64. $this->setHost($name);
  65. } else {
  66. $this->setHost($name . ':' . $port);
  67. }
  68. }
  69. }
  70. /**
  71. * View helper entry point:
  72. * Returns the current host's URL like http://site.com
  73. *
  74. * @param string|boolean $requestUri [optional] if true, the request URI
  75. * found in $_SERVER will be appended
  76. * as a path. If a string is given, it
  77. * will be appended as a path. Default
  78. * is to not append any path.
  79. * @return string server url
  80. */
  81. public function serverUrl($requestUri = null)
  82. {
  83. if ($requestUri === true) {
  84. $path = $_SERVER['REQUEST_URI'];
  85. } else if (is_string($requestUri)) {
  86. $path = $requestUri;
  87. } else {
  88. $path = '';
  89. }
  90. return $this->getScheme() . '://' . $this->getHost() . $path;
  91. }
  92. /**
  93. * Returns host
  94. *
  95. * @return string host
  96. */
  97. public function getHost()
  98. {
  99. return $this->_host;
  100. }
  101. /**
  102. * Sets host
  103. *
  104. * @param string $host new host
  105. * @return Zend_View_Helper_ServerUrl fluent interface, returns self
  106. */
  107. public function setHost($host)
  108. {
  109. $this->_host = $host;
  110. return $this;
  111. }
  112. /**
  113. * Returns scheme (typically http or https)
  114. *
  115. * @return string scheme (typically http or https)
  116. */
  117. public function getScheme()
  118. {
  119. return $this->_scheme;
  120. }
  121. /**
  122. * Sets scheme (typically http or https)
  123. *
  124. * @param string $scheme new scheme (typically http or https)
  125. * @return Zend_View_Helper_ServerUrl fluent interface, returns self
  126. */
  127. public function setScheme($scheme)
  128. {
  129. $this->_scheme = $scheme;
  130. return $this;
  131. }
  132. }