2
0

ServerUrl.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * @version $Id$
  21. */
  22. /**
  23. * Helper for returning the current server URL (optionally with request URI)
  24. *
  25. * @category Zend
  26. * @package Zend_View
  27. * @subpackage Helper
  28. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_View_Helper_ServerUrl
  32. {
  33. /**
  34. * Scheme
  35. *
  36. * @var string
  37. */
  38. protected $_scheme;
  39. /**
  40. * Host (including port)
  41. *
  42. * @var string
  43. */
  44. protected $_host;
  45. /**
  46. * Constructor
  47. *
  48. * @return void
  49. */
  50. public function __construct()
  51. {
  52. if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] === true)) {
  53. $scheme = 'https';
  54. } else {
  55. $scheme = 'http';
  56. }
  57. $this->setScheme($scheme);
  58. if (isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])) {
  59. $this->setHost($_SERVER['HTTP_HOST']);
  60. } else if (isset($_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'])) {
  61. $name = $_SERVER['SERVER_NAME'];
  62. $port = $_SERVER['SERVER_PORT'];
  63. if (($scheme == 'http' && $port == 80) ||
  64. ($scheme == 'https' && $port == 443)) {
  65. $this->setHost($name);
  66. } else {
  67. $this->setHost($name . ':' . $port);
  68. }
  69. }
  70. }
  71. /**
  72. * View helper entry point:
  73. * Returns the current host's URL like http://site.com
  74. *
  75. * @param string|boolean $requestUri [optional] if true, the request URI
  76. * found in $_SERVER will be appended
  77. * as a path. If a string is given, it
  78. * will be appended as a path. Default
  79. * is to not append any path.
  80. * @return string server url
  81. */
  82. public function serverUrl($requestUri = null)
  83. {
  84. if ($requestUri === true) {
  85. $path = $_SERVER['REQUEST_URI'];
  86. } else if (is_string($requestUri)) {
  87. $path = $requestUri;
  88. } else {
  89. $path = '';
  90. }
  91. return $this->getScheme() . '://' . $this->getHost() . $path;
  92. }
  93. /**
  94. * Returns host
  95. *
  96. * @return string host
  97. */
  98. public function getHost()
  99. {
  100. return $this->_host;
  101. }
  102. /**
  103. * Sets host
  104. *
  105. * @param string $host new host
  106. * @return Zend_View_Helper_ServerUrl fluent interface, returns self
  107. */
  108. public function setHost($host)
  109. {
  110. $this->_host = $host;
  111. return $this;
  112. }
  113. /**
  114. * Returns scheme (typically http or https)
  115. *
  116. * @return string scheme (typically http or https)
  117. */
  118. public function getScheme()
  119. {
  120. return $this->_scheme;
  121. }
  122. /**
  123. * Sets scheme (typically http or https)
  124. *
  125. * @param string $scheme new scheme (typically http or https)
  126. * @return Zend_View_Helper_ServerUrl fluent interface, returns self
  127. */
  128. public function setScheme($scheme)
  129. {
  130. $this->_scheme = $scheme;
  131. return $this;
  132. }
  133. }