2011-09-20 16:41:17 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Felix Moeller <mail@felixmoeller.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Susinthiran Sithamparanathan <chesusin@gmail.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2011-09-20 16:41:17 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2011-09-20 16:41:17 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2011-09-20 16:41:17 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2011-09-20 16:41:17 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2011-09-20 16:41:17 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2011-09-20 16:41:17 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides a streamlined interface to the Sabre VObject classes
|
|
|
|
*/
|
2011-09-20 16:41:17 +04:00
|
|
|
class OC_VObject{
|
2012-10-17 14:25:34 +04:00
|
|
|
/** @var Sabre\VObject\Component */
|
2014-05-13 14:38:17 +04:00
|
|
|
protected $vObject;
|
2011-09-20 16:41:17 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-11 21:05:28 +04:00
|
|
|
* @return Sabre\VObject\Component
|
2011-09-20 16:41:17 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function getVObject() {
|
2014-05-13 14:38:17 +04:00
|
|
|
return $this->vObject;
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Parses the VObject
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param string $data VObject as string
|
2014-05-11 21:05:28 +04:00
|
|
|
* @return Sabre\VObject\Reader|null
|
2011-09-20 16:41:17 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function parse($data) {
|
2011-09-20 16:41:17 +04:00
|
|
|
try {
|
2012-10-17 14:25:34 +04:00
|
|
|
Sabre\VObject\Property::$classMap['LAST-MODIFIED'] = 'Sabre\VObject\Property\DateTime';
|
2014-05-13 14:38:17 +04:00
|
|
|
$vObject = Sabre\VObject\Reader::read($data);
|
|
|
|
if ($vObject instanceof Sabre\VObject\Component) {
|
|
|
|
$vObject = new OC_VObject($vObject);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
2014-05-13 14:38:17 +04:00
|
|
|
return $vObject;
|
2011-09-20 16:41:17 +04:00
|
|
|
} catch (Exception $e) {
|
2011-12-10 00:56:03 +04:00
|
|
|
OC_Log::write('vobject', $e->getMessage(), OC_Log::ERROR);
|
2011-09-20 16:41:17 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Escapes semicolons
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param array $value
|
2011-09-20 16:41:17 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function escapeSemicolons($value) {
|
|
|
|
foreach($value as &$i ) {
|
2011-09-20 16:41:17 +04:00
|
|
|
$i = implode("\\\\;", explode(';', $i));
|
|
|
|
}
|
2012-10-23 02:28:12 +04:00
|
|
|
return implode(';', $value);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Creates an array out of a multivalue property
|
2011-09-20 16:41:17 +04:00
|
|
|
* @param string $value
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public static function unescapeSemicolons($value) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$array = explode(';', $value);
|
2014-10-24 01:27:15 +04:00
|
|
|
$arrayCount = count($array);
|
|
|
|
for($i = 0; $i < $arrayCount; $i++) {
|
2012-10-23 02:28:12 +04:00
|
|
|
if(substr($array[$i], -2, 2)=="\\\\") {
|
2012-09-07 17:22:01 +04:00
|
|
|
if(isset($array[$i+1])) {
|
2012-10-23 02:28:12 +04:00
|
|
|
$array[$i] = substr($array[$i], 0, count($array[$i])-2).';'.$array[$i+1];
|
2011-09-20 16:41:17 +04:00
|
|
|
unset($array[$i+1]);
|
|
|
|
}
|
|
|
|
else{
|
2012-10-23 02:28:12 +04:00
|
|
|
$array[$i] = substr($array[$i], 0, count($array[$i])-2).';';
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
$i = $i - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-21 17:44:54 +04:00
|
|
|
* Constructor
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param Sabre\VObject\Component|string $vobject_or_name
|
2011-09-20 16:41:17 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function __construct($vobject_or_name) {
|
|
|
|
if (is_object($vobject_or_name)) {
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject = $vobject_or_name;
|
2011-09-20 16:41:17 +04:00
|
|
|
} else {
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject = new Sabre\VObject\Component($vobject_or_name);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param \OC_VObject|\Sabre\VObject\Component $item
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param null $itemValue
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function add($item, $itemValue = null) {
|
|
|
|
if ($item instanceof OC_VObject) {
|
2011-09-20 16:41:17 +04:00
|
|
|
$item = $item->getVObject();
|
|
|
|
}
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->add($item, $itemValue);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Add property to vobject
|
2011-09-20 16:41:17 +04:00
|
|
|
* @param object $name of property
|
|
|
|
* @param object $value of property
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param array|object $parameters of property
|
2014-05-11 21:05:28 +04:00
|
|
|
* @return Sabre\VObject\Property newly created
|
2011-09-20 16:41:17 +04:00
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function addProperty($name, $value, $parameters=array()) {
|
|
|
|
if(is_array($value)) {
|
2011-09-20 16:41:17 +04:00
|
|
|
$value = OC_VObject::escapeSemicolons($value);
|
|
|
|
}
|
2012-10-17 14:25:34 +04:00
|
|
|
$property = new Sabre\VObject\Property( $name, $value );
|
2012-09-07 17:22:01 +04:00
|
|
|
foreach($parameters as $name => $value) {
|
2012-10-17 14:25:34 +04:00
|
|
|
$property->parameters[] = new Sabre\VObject\Parameter($name, $value);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->add($property);
|
2011-09-20 16:41:17 +04:00
|
|
|
return $property;
|
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function setUID() {
|
2012-10-23 02:28:12 +04:00
|
|
|
$uid = substr(md5(rand().time()), 0, 10);
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->add('UID', $uid);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param mixed $name
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param string $string
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function setString($name, $string) {
|
|
|
|
if ($string != '') {
|
2011-12-10 00:56:03 +04:00
|
|
|
$string = strtr($string, array("\r\n"=>"\n"));
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->__set($name, $string);
|
2011-09-20 16:41:17 +04:00
|
|
|
}else{
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->__unset($name);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2014-04-21 17:44:54 +04:00
|
|
|
* @param string $name
|
2011-09-20 16:41:17 +04:00
|
|
|
* @param DateTime $datetime
|
|
|
|
* @param int $dateType
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-10-17 14:25:34 +04:00
|
|
|
public function setDateTime($name, $datetime, $dateType=Sabre\VObject\Property\DateTime::LOCALTZ) {
|
2012-09-07 17:22:01 +04:00
|
|
|
if ($datetime == 'now') {
|
2011-09-20 16:41:17 +04:00
|
|
|
$datetime = new DateTime();
|
|
|
|
}
|
2012-09-07 17:22:01 +04:00
|
|
|
if ($datetime instanceof DateTime) {
|
2012-10-17 14:25:34 +04:00
|
|
|
$datetime_element = new Sabre\VObject\Property\DateTime($name);
|
2011-09-20 16:41:17 +04:00
|
|
|
$datetime_element->setDateTime($datetime, $dateType);
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->__set($name, $datetime_element);
|
2011-09-20 16:41:17 +04:00
|
|
|
}else{
|
2014-05-13 14:38:17 +04:00
|
|
|
$this->vObject->__unset($name);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $name
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function getAsString($name) {
|
2014-05-13 14:38:17 +04:00
|
|
|
return $this->vObject->__isset($name) ?
|
|
|
|
$this->vObject->__get($name)->value :
|
2011-09-20 16:41:17 +04:00
|
|
|
'';
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $name
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function getAsArray($name) {
|
2011-09-20 16:41:17 +04:00
|
|
|
$values = array();
|
2014-05-13 14:38:17 +04:00
|
|
|
if ($this->vObject->__isset($name)) {
|
2011-09-20 16:41:17 +04:00
|
|
|
$values = explode(',', $this->getAsString($name));
|
|
|
|
$values = array_map('trim', $values);
|
|
|
|
}
|
|
|
|
return $values;
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $name
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array|OC_VObject|\Sabre\VObject\Property
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function &__get($name) {
|
|
|
|
if ($name == 'children') {
|
2014-05-13 14:38:17 +04:00
|
|
|
return $this->vObject->children;
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
2014-05-13 14:38:17 +04:00
|
|
|
$return = $this->vObject->__get($name);
|
2012-10-17 14:25:34 +04:00
|
|
|
if ($return instanceof Sabre\VObject\Component) {
|
2011-09-20 16:41:17 +04:00
|
|
|
$return = new OC_VObject($return);
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function __set($name, $value) {
|
2014-05-13 14:38:17 +04:00
|
|
|
return $this->vObject->__set($name, $value);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function __unset($name) {
|
2014-05-13 14:38:17 +04:00
|
|
|
return $this->vObject->__unset($name);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
|
|
|
* @param string $name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
public function __isset($name) {
|
2014-05-13 14:38:17 +04:00
|
|
|
return $this->vObject->__isset($name);
|
2011-12-10 00:56:03 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
|
|
|
* @todo Write documentation
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param callable $function
|
|
|
|
* @param array $arguments
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-11-02 22:53:02 +04:00
|
|
|
public function __call($function, $arguments) {
|
2014-05-13 14:38:17 +04:00
|
|
|
return call_user_func_array(array($this->vObject, $function), $arguments);
|
2011-09-20 16:41:17 +04:00
|
|
|
}
|
|
|
|
}
|