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

https://github.com/kamarulismail/kamarul-playground · PHP · 168 lines · 70 code · 12 blank · 86 comment · 9 complexity · 5bd5ce1e0fc00e93ba76ca37c8846d90 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcWebdavBasicPathFactory 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. * Basic path factory.
  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. * This simple implementation of a path factory expects the base URI it should
  19. * be working on as the constructor parameter. It will translate all incoming
  20. * URIs to internal paths and the other way round based on this URI.
  21. *
  22. * You may want to provide custome implementations for different mappings. The
  23. * {@link ezcWebdavAutomaticPathFactory} is a more advanced implementation,
  24. * which automatically "guesses" the server configuration and should be
  25. * suitable for almost every case.
  26. *
  27. * @version 1.1.4
  28. * @package Webdav
  29. */
  30. class ezcWebdavBasicPathFactory implements ezcWebdavPathFactory
  31. {
  32. /**
  33. * Result of parse_url() for the {@link $baseUri} submitted to the ctor.
  34. *
  35. * @var array(string=>string)
  36. */
  37. protected $baseUriParts;
  38. /**
  39. * Caches paths that are a collection.
  40. *
  41. * Those will get a '/' appended on re-serialization. Works only if they
  42. * had been unserialized before.
  43. *
  44. * @var array(string=>bool)
  45. *
  46. * @apichange This property will be renamed to $collectionPaths in the next
  47. * major release.
  48. */
  49. protected $collectionPathes = array();
  50. /**
  51. * Creates a new path factory.
  52. *
  53. * Creates a new object to parse URIs to local paths and vice versa. The
  54. * URL given as a parameter is used to strip URL and path parts from
  55. * incoming URIs {@link parseUriToPath()} and to add the specific parts to
  56. * outgoing ones {@link generateUriFromPath()}.
  57. *
  58. * @param string $baseUri
  59. * @return void
  60. */
  61. public function __construct( $baseUri = '' )
  62. {
  63. // Fix potentially incorrect $baseUri, since we need the first '/' or
  64. // the path.
  65. if ( substr( $baseUri, -1, 1 ) === '/' )
  66. {
  67. $baseUri = substr( $baseUri, 0, -1 );
  68. }
  69. if ( ( $this->baseUriParts = @parse_url( $baseUri ) ) === false )
  70. {
  71. throw new ezcWebdavBrokenBaseUriException( $baseUri );
  72. }
  73. // Sanity checks for essential elements
  74. if ( !isset( $this->baseUriParts['scheme'] ) )
  75. {
  76. throw new ezcWebdavBrokenBaseUriException(
  77. $baseUri, 'Missing URI scheme.'
  78. );
  79. }
  80. if ( !isset( $this->baseUriParts['host'] ) )
  81. {
  82. throw new ezcWebdavBrokenBaseUriException(
  83. $baseUri, 'Missing URI host.'
  84. );
  85. }
  86. }
  87. /**
  88. * Parses the given URI to a path suitable to be used in the backend.
  89. *
  90. * This method retrieves a URI (either full qualified or relative) and
  91. * translates it into a local path, which can be understood by the {@link
  92. * ezcWebdavBackend} instance used in the {@link ezcWebdavServer}.
  93. *
  94. * A locally understandable path MUST NOT contain a trailing slash, but
  95. * MUST always contain a starting slash. For the root URI the path "/" MUST
  96. * be used.
  97. *
  98. * @param string $uri
  99. * @return string
  100. */
  101. public function parseUriToPath( $uri )
  102. {
  103. $requestPath = parse_url( trim( $uri ), PHP_URL_PATH );
  104. $requestPath = str_replace( '//', '/', $requestPath );
  105. if ( substr( $requestPath, -1, 1 ) === '/' )
  106. {
  107. $requestPath = substr( $requestPath, 0, -1 );
  108. $this->collectionPathes[substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) )] = true;
  109. }
  110. else
  111. {
  112. // @todo Some clients first send with / and then discover it is not a resource
  113. // therefore the upper todo might be refined.
  114. if ( isset( $this->collectionPathes[substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) )] ) )
  115. {
  116. unset( $this->collectionPathes[substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) )] );
  117. }
  118. }
  119. $requestPath = substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) );
  120. // Ensure starting slash for root node
  121. if ( !$requestPath )
  122. {
  123. $requestPath = '/';
  124. }
  125. // Attach fragment to avoid performing operations occasionally
  126. if ( ( $frag = parse_url( $uri, PHP_URL_FRAGMENT ) ) !== null )
  127. {
  128. $requestPath .= "#$frag";
  129. }
  130. return $requestPath;
  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. return $this->baseUriParts['scheme']
  145. . '://'
  146. . ( isset( $this->baseUriParts['user'] ) ? $this->baseUriParts['user'] : '' )
  147. . ( isset( $this->baseUriParts['pass'] ) ? ':' . $this->baseUriParts['pass'] : '' )
  148. . ( isset( $this->baseUriParts['user'] ) || isset( $this->baseUriParts['pass'] ) ? '@' : '' )
  149. . $this->baseUriParts['host']
  150. . ( isset( $this->baseUriParts['path'] ) ? $this->baseUriParts['path'] : '' )
  151. . trim( $path ) . ( isset( $this->collectionPathes[$path] ) ? '/' : '' )
  152. . ( isset( $this->baseUriParts['query'] ) ? '?' . $this->baseUriParts['query'] : '' )
  153. . ( isset( $this->baseUriParts['fragment'] ) ? '#' . $this->baseUriParts['fragment'] : '' );
  154. }
  155. }
  156. ?>