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