Blame view

sources/lib/private/vobject.php 6.11 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  <?php
  /**
   * ownCloud
   *
   * @author Bart Visscher
   * @copyright 2011 Bart Visscher bartv@thisnet.nl
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  /**
   * This class provides a streamlined interface to the Sabre VObject classes
   */
  class OC_VObject{
  	/** @var Sabre\VObject\Component */
6d9380f96   Cédric Dupont   Update sources OC...
28
  	protected $vObject;
03e52840d   Kload   Init
29
30
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
31
  	 * @return Sabre\VObject\Component
03e52840d   Kload   Init
32
33
  	 */
  	public function getVObject() {
6d9380f96   Cédric Dupont   Update sources OC...
34
  		return $this->vObject;
03e52840d   Kload   Init
35
36
37
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
38
39
40
  	 * Parses the VObject
  	 * @param string $data VObject as string
  	 * @return Sabre\VObject\Reader|null
03e52840d   Kload   Init
41
42
43
44
  	 */
  	public static function parse($data) {
  		try {
  			Sabre\VObject\Property::$classMap['LAST-MODIFIED'] = 'Sabre\VObject\Property\DateTime';
6d9380f96   Cédric Dupont   Update sources OC...
45
46
47
  			$vObject = Sabre\VObject\Reader::read($data);
  			if ($vObject instanceof Sabre\VObject\Component) {
  				$vObject = new OC_VObject($vObject);
03e52840d   Kload   Init
48
  			}
6d9380f96   Cédric Dupont   Update sources OC...
49
  			return $vObject;
03e52840d   Kload   Init
50
51
52
53
54
55
56
  		} catch (Exception $e) {
  			OC_Log::write('vobject', $e->getMessage(), OC_Log::ERROR);
  			return null;
  		}
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
57
58
  	 * Escapes semicolons
  	 * @param array $value
03e52840d   Kload   Init
59
60
61
62
63
64
65
66
67
68
  	 * @return string
  	 */
  	public static function escapeSemicolons($value) {
  		foreach($value as &$i ) {
  			$i = implode("\\\\;", explode(';', $i));
  		}
  		return implode(';', $value);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
69
  	 * Creates an array out of a multivalue property
03e52840d   Kload   Init
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  	 * @param string $value
  	 * @return array
  	 */
  	public static function unescapeSemicolons($value) {
  		$array = explode(';', $value);
  		for($i=0;$i<count($array);$i++) {
  			if(substr($array[$i], -2, 2)=="\\\\") {
  				if(isset($array[$i+1])) {
  					$array[$i] = substr($array[$i], 0, count($array[$i])-2).';'.$array[$i+1];
  					unset($array[$i+1]);
  				}
  				else{
  					$array[$i] = substr($array[$i], 0, count($array[$i])-2).';';
  				}
  				$i = $i - 1;
  			}
  		}
  		return $array;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
91
92
  	 * Constructor
  	 * @param Sabre\VObject\Component|string $vobject_or_name
03e52840d   Kload   Init
93
94
95
  	 */
  	public function __construct($vobject_or_name) {
  		if (is_object($vobject_or_name)) {
6d9380f96   Cédric Dupont   Update sources OC...
96
  			$this->vObject = $vobject_or_name;
03e52840d   Kload   Init
97
  		} else {
6d9380f96   Cédric Dupont   Update sources OC...
98
  			$this->vObject = new Sabre\VObject\Component($vobject_or_name);
03e52840d   Kload   Init
99
100
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
101
102
103
104
105
  	/**
  	 * @todo Write documentation
  	 * @param \OC_VObject|\Sabre\VObject\Component $item
  	 * @param null $itemValue
  	 */
03e52840d   Kload   Init
106
107
108
109
  	public function add($item, $itemValue = null) {
  		if ($item instanceof OC_VObject) {
  			$item = $item->getVObject();
  		}
6d9380f96   Cédric Dupont   Update sources OC...
110
  		$this->vObject->add($item, $itemValue);
03e52840d   Kload   Init
111
112
113
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
114
  	 * Add property to vobject
03e52840d   Kload   Init
115
116
  	 * @param object $name of property
  	 * @param object $value of property
6d9380f96   Cédric Dupont   Update sources OC...
117
118
  	 * @param array|object $parameters of property
  	 * @return Sabre\VObject\Property newly created
03e52840d   Kload   Init
119
120
121
122
123
124
125
126
127
  	 */
  	public function addProperty($name, $value, $parameters=array()) {
  		if(is_array($value)) {
  			$value = OC_VObject::escapeSemicolons($value);
  		}
  		$property = new Sabre\VObject\Property( $name, $value );
  		foreach($parameters as $name => $value) {
  			$property->parameters[] = new Sabre\VObject\Parameter($name, $value);
  		}
6d9380f96   Cédric Dupont   Update sources OC...
128
  		$this->vObject->add($property);
03e52840d   Kload   Init
129
130
131
132
133
  		return $property;
  	}
  
  	public function setUID() {
  		$uid = substr(md5(rand().time()), 0, 10);
6d9380f96   Cédric Dupont   Update sources OC...
134
  		$this->vObject->add('UID', $uid);
03e52840d   Kload   Init
135
  	}
6d9380f96   Cédric Dupont   Update sources OC...
136
137
138
139
140
  	/**
  	 * @todo Write documentation
  	 * @param mixed $name
  	 * @param string $string
  	 */
03e52840d   Kload   Init
141
142
143
144
145
  	public function setString($name, $string) {
  		if ($string != '') {
  			$string = strtr($string, array("\r
  "=>"
  "));
6d9380f96   Cédric Dupont   Update sources OC...
146
  			$this->vObject->__set($name, $string);
03e52840d   Kload   Init
147
  		}else{
6d9380f96   Cédric Dupont   Update sources OC...
148
  			$this->vObject->__unset($name);
03e52840d   Kload   Init
149
150
151
152
153
154
155
156
  		}
  	}
  
  	/**
  	 * Sets or unsets the Date and Time for a property.
  	 * When $datetime is set to 'now', use the current time
  	 * When $datetime is null, unset the property
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
157
  	 * @param string $name
03e52840d   Kload   Init
158
159
160
161
162
163
164
165
166
167
168
  	 * @param DateTime $datetime
  	 * @param int $dateType
  	 * @return void
  	 */
  	public function setDateTime($name, $datetime, $dateType=Sabre\VObject\Property\DateTime::LOCALTZ) {
  		if ($datetime == 'now') {
  			$datetime = new DateTime();
  		}
  		if ($datetime instanceof DateTime) {
  			$datetime_element = new Sabre\VObject\Property\DateTime($name);
  			$datetime_element->setDateTime($datetime, $dateType);
6d9380f96   Cédric Dupont   Update sources OC...
169
  			$this->vObject->__set($name, $datetime_element);
03e52840d   Kload   Init
170
  		}else{
6d9380f96   Cédric Dupont   Update sources OC...
171
  			$this->vObject->__unset($name);
03e52840d   Kload   Init
172
173
  		}
  	}
6d9380f96   Cédric Dupont   Update sources OC...
174
175
176
177
178
  	/**
  	 * @todo Write documentation
  	 * @param string $name
  	 * @return string
  	 */
03e52840d   Kload   Init
179
  	public function getAsString($name) {
6d9380f96   Cédric Dupont   Update sources OC...
180
181
  		return $this->vObject->__isset($name) ?
  			$this->vObject->__get($name)->value :
03e52840d   Kload   Init
182
183
  			'';
  	}
6d9380f96   Cédric Dupont   Update sources OC...
184
185
186
187
188
  	/**
  	 * @todo Write documentation
  	 * @param string $name
  	 * @return array
  	 */
03e52840d   Kload   Init
189
190
  	public function getAsArray($name) {
  		$values = array();
6d9380f96   Cédric Dupont   Update sources OC...
191
  		if ($this->vObject->__isset($name)) {
03e52840d   Kload   Init
192
193
194
195
196
  			$values = explode(',', $this->getAsString($name));
  			$values = array_map('trim', $values);
  		}
  		return $values;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
197
198
199
200
201
  	/**
  	 * @todo Write documentation
  	 * @param string $name
  	 * @return array|OC_VObject|\Sabre\VObject\Property
  	 */
03e52840d   Kload   Init
202
203
  	public function &__get($name) {
  		if ($name == 'children') {
6d9380f96   Cédric Dupont   Update sources OC...
204
  			return $this->vObject->children;
03e52840d   Kload   Init
205
  		}
6d9380f96   Cédric Dupont   Update sources OC...
206
  		$return = $this->vObject->__get($name);
03e52840d   Kload   Init
207
208
209
210
211
  		if ($return instanceof Sabre\VObject\Component) {
  			$return = new OC_VObject($return);
  		}
  		return $return;
  	}
6d9380f96   Cédric Dupont   Update sources OC...
212
213
214
215
216
  	/**
  	 * @todo Write documentation
  	 * @param string $name
  	 * @param string $value
  	 */
03e52840d   Kload   Init
217
  	public function __set($name, $value) {
6d9380f96   Cédric Dupont   Update sources OC...
218
  		return $this->vObject->__set($name, $value);
03e52840d   Kload   Init
219
  	}
6d9380f96   Cédric Dupont   Update sources OC...
220
221
222
223
  	/**
  	 * @todo Write documentation
  	 * @param string $name
  	 */
03e52840d   Kload   Init
224
  	public function __unset($name) {
6d9380f96   Cédric Dupont   Update sources OC...
225
  		return $this->vObject->__unset($name);
03e52840d   Kload   Init
226
  	}
6d9380f96   Cédric Dupont   Update sources OC...
227
228
229
230
231
  	/**
  	 * @todo Write documentation
  	 * @param string $name
  	 * @return bool
  	 */
03e52840d   Kload   Init
232
  	public function __isset($name) {
6d9380f96   Cédric Dupont   Update sources OC...
233
  		return $this->vObject->__isset($name);
03e52840d   Kload   Init
234
  	}
6d9380f96   Cédric Dupont   Update sources OC...
235
236
237
238
239
240
  	/**
  	 * @todo Write documentation
  	 * @param callable $function
  	 * @param array $arguments
  	 * @return mixed
  	 */
03e52840d   Kload   Init
241
  	public function __call($function, $arguments) {
6d9380f96   Cédric Dupont   Update sources OC...
242
  		return call_user_func_array(array($this->vObject, $function), $arguments);
03e52840d   Kload   Init
243
244
  	}
  }