/protected/components/ezcomponents/Webdav/src/path_factories/automatic.php

https://github.com/kamarulismail/kamarul-playground · PHP · 168 lines · 68 code · 13 blank · 87 comment · 15 complexity · b4ba199ba6ddb438f58403287abca098 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcWebdavAutomaticPathFactory class.
  4. *
  5. * @package Webdav
  6. * @version 1.1.4
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Path factory that automatically determines configuration.
  12. *
  13. * An object of this class is meant to be used in {@link
  14. * ezcWebdavTransportOptions} as the $pathFactory property. The instance of
  15. * {@link ezcWebdavTransport} utilizes the path factory to translate between
  16. * external paths/URIs and internal path representations.
  17. *
  18. * An instance of this class examines several server variables like
  19. * <ul>
  20. * <li>$_SERVER['DOCUMENT_ROOT']</li>
  21. * <li>$_SERVER['SCRIPT_FILENAME']</li>
  22. * </ul>
  23. * to determine the server configuration. It then examines incoming URIs to
  24. * determine which parts must be stripped an reconstructs the information when
  25. * serializing a path back to a URI.
  26. *
  27. * @version 1.1.4
  28. * @package Webdav
  29. */
  30. class ezcWebdavAutomaticPathFactory implements ezcWebdavPathFactory
  31. {
  32. /**
  33. * Caches paths that are a collection.
  34. *
  35. * Those will get a '/' appended on re-serialization. Works only if they
  36. * had been unserialized before.
  37. *
  38. * @var array(string=>bool)
  39. *
  40. * @apichange This property will be renamed to $collectionPaths in the next
  41. * major release.
  42. */
  43. protected $collectionPathes = array();
  44. /**
  45. * Base path on the server.
  46. *
  47. * Auto-detected during __construct().
  48. *
  49. * @var string
  50. */
  51. protected $serverFile;
  52. /**
  53. * Creates a new path factory.
  54. *
  55. * Creates a new path factory to be used in {@link
  56. * ezcWebdavServerConfiguration}. This path factory automatically detects
  57. * information from the running web server and automatically determines the
  58. * suitable values for parsing paths and generating URIs.
  59. *
  60. * @return void
  61. */
  62. public function __construct()
  63. {
  64. // Get Docroot and ensure proper definition
  65. if ( !isset( $_SERVER['DOCUMENT_ROOT'] ) )
  66. {
  67. throw new ezcWebdavMissingServerVariableException( 'DOCUMENT_ROOT' );
  68. }
  69. // Ensure trailing slash in doc root.
  70. $docRoot = $_SERVER['DOCUMENT_ROOT'];
  71. if ( substr( $docRoot, -1, 1 ) !== '/' )
  72. {
  73. $docRoot .= '/';
  74. }
  75. // Get script filename
  76. if ( !isset( $_SERVER['SCRIPT_FILENAME'] ) )
  77. {
  78. throw new ezcWebdavMissingServerVariableException( 'SCRIPT_FILENAME' );
  79. }
  80. $scriptFileName = $_SERVER['SCRIPT_FILENAME'];
  81. // Get script path absolute to doc root
  82. $this->serverFile = '/' . str_replace(
  83. $docRoot, '', $scriptFileName
  84. );
  85. }
  86. /**
  87. * Parses the given URI to a path suitable to be used in the backend.
  88. *
  89. * This method retrieves a URI (either full qualified or relative) and
  90. * translates it into a local path, which can be understood by the {@link
  91. * ezcWebdavBackend} instance used in the {@link ezcWebdavServer}.
  92. *
  93. * A locally understandable path MUST NOT contain a trailing slash, but
  94. * MUST always contain a starting slash. For the root URI the path "/" MUST
  95. * be used.
  96. *
  97. * @param string $uri
  98. * @return string
  99. */
  100. public function parseUriToPath( $uri )
  101. {
  102. $requestPath = parse_url( $uri, PHP_URL_PATH );
  103. $serverBase = dirname( $this->serverFile );
  104. // Check for request path including index.php
  105. if ( strpos( $requestPath, $this->serverFile ) === 0 )
  106. {
  107. $path = substr( $requestPath, strlen( $this->serverFile ) );
  108. }
  109. // Check for request path without index.php, but with some root to cut
  110. else if ( $serverBase !== '/' && strpos( $requestPath, $serverBase ) === 0 )
  111. {
  112. $path = substr( $requestPath, strlen( $serverBase ) );
  113. $this->serverFile = $serverBase;
  114. }
  115. // Already a good path, just use it
  116. else
  117. {
  118. $path = $requestPath;
  119. $this->serverFile = '';
  120. }
  121. if ( substr( $path, -1, 1 ) === '/' )
  122. {
  123. $path = substr( $path, 0, -1 );
  124. $this->collectionPathes[$path] = true;
  125. }
  126. elseif ( isset( $this->collectionPathes[$path] ) )
  127. {
  128. unset( $this->collectionPathes[$path] );
  129. }
  130. return ( is_string( $path ) && $path !== '' ? $path : '/' );
  131. }
  132. /**
  133. * Generates a URI from a local path.
  134. *
  135. * This method receives a local $path string, representing a resource in
  136. * the {@link ezcWebdavBackend} and translates it into a full qualified URI
  137. * to be used as external reference.
  138. *
  139. * @param string $path
  140. * @return string
  141. */
  142. public function generateUriFromPath( $path )
  143. {
  144. $proto = ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http' );
  145. $port = ( $proto === 'http' && $_SERVER['SERVER_PORT'] == 80 || $proto === 'https' && $_SERVER['SERVER_PORT'] == 443 )
  146. ? null
  147. : $_SERVER['SERVER_PORT'];
  148. return $proto . '://' . $_SERVER['SERVER_NAME']
  149. . ( $port !== null ? ':' . $port : '' )
  150. . $this->serverFile
  151. . $path
  152. . ( isset( $this->collectionPathes[$path] ) ? '/' : '' );
  153. }
  154. }
  155. ?>