Uri.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Sabre\VObject\Property;
  3. use Sabre\VObject\Parameter;
  4. use Sabre\VObject\Property;
  5. /**
  6. * URI property.
  7. *
  8. * This object encodes URI values. vCard 2.1 calls these URL.
  9. *
  10. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://sabre.io/license/ Modified BSD License
  13. */
  14. class Uri extends Text
  15. {
  16. /**
  17. * In case this is a multi-value property. This string will be used as a
  18. * delimiter.
  19. *
  20. * @var string
  21. */
  22. public $delimiter = '';
  23. /**
  24. * Returns the type of value.
  25. *
  26. * This corresponds to the VALUE= parameter. Every property also has a
  27. * 'default' valueType.
  28. *
  29. * @return string
  30. */
  31. public function getValueType()
  32. {
  33. return 'URI';
  34. }
  35. /**
  36. * Returns an iterable list of children.
  37. *
  38. * @return array
  39. */
  40. public function parameters()
  41. {
  42. $parameters = parent::parameters();
  43. if (!isset($parameters['VALUE']) && in_array($this->name, ['URL', 'PHOTO'])) {
  44. // If we are encoding a URI value, and this URI value has no
  45. // VALUE=URI parameter, we add it anyway.
  46. //
  47. // This is not required by any spec, but both Apple iCal and Apple
  48. // AddressBook (at least in version 10.8) will trip over this if
  49. // this is not set, and so it improves compatibility.
  50. //
  51. // See Issue #227 and #235
  52. $parameters['VALUE'] = new Parameter($this->root, 'VALUE', 'URI');
  53. }
  54. return $parameters;
  55. }
  56. /**
  57. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  58. *
  59. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  60. * not yet done, but parameters are not included.
  61. *
  62. * @param string $val
  63. */
  64. public function setRawMimeDirValue($val)
  65. {
  66. // Normally we don't need to do any type of unescaping for these
  67. // properties, however.. we've noticed that Google Contacts
  68. // specifically escapes the colon (:) with a backslash. While I have
  69. // no clue why they thought that was a good idea, I'm unescaping it
  70. // anyway.
  71. //
  72. // Good thing backslashes are not allowed in urls. Makes it easy to
  73. // assume that a backslash is always intended as an escape character.
  74. if ('URL' === $this->name) {
  75. $regex = '# (?: (\\\\ (?: \\\\ | : ) ) ) #x';
  76. $matches = preg_split($regex, $val, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  77. $newVal = '';
  78. foreach ($matches as $match) {
  79. switch ($match) {
  80. case '\:':
  81. $newVal .= ':';
  82. break;
  83. default:
  84. $newVal .= $match;
  85. break;
  86. }
  87. }
  88. $this->value = $newVal;
  89. } else {
  90. $this->value = strtr($val, ['\,' => ',']);
  91. }
  92. }
  93. /**
  94. * Returns a raw mime-dir representation of the value.
  95. *
  96. * @return string
  97. */
  98. public function getRawMimeDirValue()
  99. {
  100. if (is_array($this->value)) {
  101. $value = $this->value[0];
  102. } else {
  103. $value = $this->value;
  104. }
  105. return strtr($value, [',' => '\,']);
  106. }
  107. }