string) */ protected $baseUriParts; /** * Caches pathes that are a collection. * * Those will get a '/' appended on reserialization. Works only if they had * been unserialized before. * * @todo This is a temporary hack to satisfy memory backend and RFC. * * @var array(string=>bool) */ protected $collectionPathes = array(); /** * Creates a new path factory. * Creates a new object to parse URIs to local pathes. The URL given as a * parameter is used to strip URL/path parts from incoming URIs and add the * specific parts to outgoin ones. * * @param string $baseUri * @return void */ public function __construct( $baseUri = '' ) { $this->baseUriParts = parse_url( $baseUri ); } /** * Parses the given URI to a locally understandable path. * * This method retrieves a URI (either full qualified or relative) and * translates it into a local path, which can be understood by the WebDAV * elements. * * A locally understandable path MUST NOT contain a trailing slash, but * MUST always contain a starting slash. For the root URI the path "/" MUST * be used. * * @param string $uri * @return string */ public function parseUriToPath( $uri ) { $requestPath = parse_url( trim( $uri ), PHP_URL_PATH ); if ( substr( $requestPath, -1, 1 ) === '/' ) { // @todo This is a cleanup for the memory backend $requestPath = substr( $requestPath, 0, -1 ); $this->collectionPathes[substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) )] = true; } else { // @todo Some clients first send with / and then discover it is not a resource // therefore the upper todo might be refined. if ( isset( $this->collectionPathes[substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) )] ) ) { unset( $this->collectionPathes[substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) )] ); } } $requestPath = substr( $requestPath, ( isset( $this->baseUriParts['path'] ) ? strlen( $this->baseUriParts['path'] ) : 0 ) ); // Ensure starting slash for root node if ( !$requestPath ) { $requestPath = '/'; } return $requestPath; } /** * Generates a URI from a local path. * * This method receives a local $path string, representing a node in the * local WebDAV store and translates it into a full qualified URI to be * used as external reference. * * @param string $path * @return string */ public function generateUriFromPath( $path ) { return $this->baseUriParts['scheme'] . '://' . ( isset( $this->baseUriParts['user'] ) ? $this->baseUriParts['user'] : '' ) . ( isset( $this->baseUriParts['pass'] ) ? ':' . $this->baseUriParts['pass'] : '' ) . ( isset( $this->baseUriParts['user'] ) || isset( $this->baseUriParts['pass'] ) ? '@' : '' ) . $this->baseUriParts['host'] . ( isset( $this->baseUriParts['path'] ) ? $this->baseUriParts['path'] : '' ) . trim( $path ) . ( isset( $this->collectionPathes[$path] ) ? '/' : '' ) . ( isset( $this->baseUriParts['query'] ) ? '?' . $this->baseUriParts['query'] : '' ) . ( isset( $this->baseUriParts['fragment'] ) ? '#' . $this->baseUriParts['fragment'] : '' ); } } ?>