/lib/Sabre/DAV/URLUtil.php

https://github.com/lkneschke/SabreDAV · PHP · 125 lines · 41 code · 20 blank · 64 comment · 8 complexity · 5eff0a2e279d267eb7bbea5bb34757c9 MD5 · raw file

  1. <?php
  2. /**
  3. * URL utility class
  4. *
  5. * This class provides methods to deal with encoding and decoding url (percent encoded) strings.
  6. *
  7. * It was not possible to use PHP's built-in methods for this, because some clients don't like
  8. * encoding of certain characters.
  9. *
  10. * Specifically, it was found that GVFS (gnome's webdav client) does not like encoding of ( and
  11. * ). Since these are reserved, but don't have a reserved meaning in url, these characters are
  12. * kept as-is.
  13. *
  14. * @package Sabre
  15. * @subpackage DAV
  16. * @copyright Copyright (C) 2007-2011 Rooftop Solutions. All rights reserved.
  17. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  18. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  19. */
  20. class Sabre_DAV_URLUtil {
  21. /**
  22. * Encodes the path of a url.
  23. *
  24. * slashes (/) are treated as path-separators.
  25. *
  26. * @param string $path
  27. * @return string
  28. */
  29. static function encodePath($path) {
  30. $valid_chars = '/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.~()';
  31. $newStr = '';
  32. for( $i=0; isset($path[$i]); ++$i ) {
  33. if( strpos($valid_chars,($c=$path[$i]))===false ) $newStr .= '%'.sprintf('%02x',ord($c));
  34. else $newStr .= $c;
  35. }
  36. return $newStr;
  37. }
  38. /**
  39. * Encodes a 1 segment of a path
  40. *
  41. * Slashes are considered part of the name, and are encoded as %2f
  42. *
  43. * @param string $pathSegment
  44. * @return string
  45. */
  46. static function encodePathSegment($pathSegment) {
  47. $valid_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.~()';
  48. $newStr = '';
  49. for( $i=0; isset($pathSegment[$i]); ++$i ) {
  50. if( strpos($valid_chars,($c=$pathSegment[$i]))===false ) $newStr .= '%'.sprintf('%02x',ord($c));
  51. else $newStr .= $c;
  52. }
  53. return $newStr;
  54. }
  55. /**
  56. * Decodes a url-encoded path
  57. *
  58. * @param string $path
  59. * @return string
  60. */
  61. static function decodePath($path) {
  62. return self::decodePathSegment($path);
  63. }
  64. /**
  65. * Decodes a url-encoded path segment
  66. *
  67. * @param string $path
  68. * @return string
  69. */
  70. static function decodePathSegment($path) {
  71. $path = urldecode($path);
  72. $encoding = mb_detect_encoding($path, array('UTF-8','ISO-8859-1'));
  73. switch($encoding) {
  74. case 'ISO-8859-1' :
  75. $path = utf8_encode($path);
  76. }
  77. return $path;
  78. }
  79. /**
  80. * Returns the 'dirname' and 'basename' for a path.
  81. *
  82. * The reason there is a custom function for this purpose, is because
  83. * basename() is locale aware (behaviour changes if C locale or a UTF-8 locale is used)
  84. * and we need a method that just operates on UTF-8 characters.
  85. *
  86. * In addition basename and dirname are platform aware, and will treat backslash (\) as a
  87. * directory separator on windows.
  88. *
  89. * This method returns the 2 components as an array.
  90. *
  91. * If there is no dirname, it will return an empty string. Any / appearing at the end of the
  92. * string is stripped off.
  93. *
  94. * @param string $path
  95. * @return array
  96. */
  97. static function splitPath($path) {
  98. $matches = array();
  99. if(preg_match('/^(?:(?:(.*)(?:\/+))?([^\/]+))(?:\/?)$/u',$path,$matches)) {
  100. return array($matches[1],$matches[2]);
  101. } else {
  102. return array(null,null);
  103. }
  104. }
  105. }