aboutsummaryrefslogtreecommitdiffstats
path: root/lib/vobject.php
blob: ec80e1d605ae2d163c06b2a78fb1068351378db8 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?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 */
	protected $vobject;

	/**
	 * @returns Sabre_VObject_Component
	 */
	public function getVObject(){
		return $this->vobject;
	}

	/**
	 * @brief Parses the VObject
	 * @param string VObject as string
	 * @returns Sabre_VObject or null
	 */
	public static function parse($data){
		try {
			Sabre_VObject_Property::$classMap['LAST-MODIFIED'] = 'Sabre_VObject_Property_DateTime';
			$vobject = Sabre_VObject_Reader::read($data);
			if ($vobject instanceof Sabre_VObject_Component){
				$vobject = new OC_VObject($vobject);
			}
			return $vobject;
		} catch (Exception $e) {
			OC_Log::write('vobject', $e->getMessage(), OC_Log::ERROR);
			return null;
		}
	}

	/**
	 * @brief Escapes semicolons
	 * @param string $value
	 * @return string
	 */
	public static function escapeSemicolons($value){
		foreach($value as &$i ){
			$i = implode("\\\\;", explode(';', $i));
		}
		return implode(';',$value);
	}

	/**
	 * @brief Creates an array out of a multivalue property
	 * @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;
	}

	/**
	 * Constuctor
	 * @param Sabre_VObject_Component or string
	 */
	public function __construct($vobject_or_name){
		if (is_object($vobject_or_name)){
			$this->vobject = $vobject_or_name;
		} else {
			$this->vobject = new Sabre_VObject_Component($vobject_or_name);
		}
	}

	public function add($item, $itemValue = null){
		if ($item instanceof OC_VObject){
			$item = $item->getVObject();
		}
		$this->vobject->add($item, $itemValue);
	}

	/**
	 * @brief Add property to vobject
	 * @param object $name of property
	 * @param object $value of property
	 * @param object $parameters of property
	 * @returns Sabre_VObject_Property newly created
	 */
	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);
		}

		$this->vobject->add($property);
		return $property;
	}

	public function setUID(){
		$uid = substr(md5(rand().time()),0,10);
		$this->vobject->add('UID',$uid);
	}

	public function setString($name, $string){
		if ($string != ''){
			$string = strtr($string, array("\r\n"=>"\n"));
			$this->vobject->__set($name, $string);
		}else{
			$this->vobject->__unset($name);
		}
	}

	/**
	 * 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
	 *
	 * @param string property name
	 * @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);
			$this->vobject->__set($name, $datetime_element);
		}else{
			$this->vobject->__unset($name);
		}
	}

	public function getAsString($name){
		return $this->vobject->__isset($name) ?
			$this->vobject->__get($name)->value :
			'';
	}

	public function getAsArray($name){
		$values = array();
		if ($this->vobject->__isset($name)){
			$values = explode(',', $this->getAsString($name));
			$values = array_map('trim', $values);
		}
		return $values;
	}

	public function &__get($name){
		if ($name == 'children'){
			return $this->vobject->children;
		}
		$return = $this->vobject->__get($name);
		if ($return instanceof Sabre_VObject_Component){
			$return = new OC_VObject($return);
		}
		return $return;
	}

	public function __set($name, $value){
		return $this->vobject->__set($name, $value);
	}

	public function __unset($name){
		return $this->vobject->__unset($name);
	}

	public function __isset($name){
		return $this->vobject->__isset($name);
	}

	public function __call($function,$arguments){
		return call_user_func_array(array($this->vobject, $function), $arguments);
	}
}