nextcloud/lib/private/vobject.php

253 lines
6.1 KiB
PHP
Raw Normal View History

<?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{
2012-10-17 14:25:34 +04:00
/** @var Sabre\VObject\Component */
protected $vobject;
/**
2012-10-17 14:25:34 +04:00
* @returns Sabre\VObject\Component
*/
2012-09-07 17:22:01 +04:00
public function getVObject() {
return $this->vobject;
}
/**
* @brief Parses the VObject
2014-04-21 17:44:54 +04:00
* @param string $data VObject as string
* @returns Sabre\VObject\Reader|null
*/
2012-09-07 17:22:01 +04:00
public static function parse($data) {
try {
2012-10-17 14:25:34 +04:00
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) {
2011-12-10 00:56:03 +04:00
OC_Log::write('vobject', $e->getMessage(), OC_Log::ERROR);
return null;
}
}
/**
* @brief Escapes semicolons
2014-04-21 17:44:54 +04:00
* @param array $value
* @return string
*/
2012-09-07 17:22:01 +04:00
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
*/
2012-09-07 17:22:01 +04:00
public static function unescapeSemicolons($value) {
$array = explode(';', $value);
2012-09-07 17:22:01 +04:00
for($i=0;$i<count($array);$i++) {
if(substr($array[$i], -2, 2)=="\\\\") {
2012-09-07 17:22:01 +04:00
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;
}
/**
2014-04-21 17:44:54 +04:00
* Constructor
2012-10-17 14:25:34 +04:00
* @param Sabre\VObject\Component or string
*/
2012-09-07 17:22:01 +04:00
public function __construct($vobject_or_name) {
if (is_object($vobject_or_name)) {
$this->vobject = $vobject_or_name;
} else {
2012-10-17 14:25:34 +04:00
$this->vobject = new Sabre\VObject\Component($vobject_or_name);
}
}
2014-04-21 17:44:54 +04:00
/**
* @todo Write documentation
* @param $item
* @param null $itemValue
*/
2012-09-07 17:22:01 +04:00
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
2014-04-21 17:44:54 +04:00
* @param array|object $parameters of property
* @returns Sabre\VObject\Property newly created
*/
2012-09-07 17:22:01 +04:00
public function addProperty($name, $value, $parameters=array()) {
if(is_array($value)) {
$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);
}
$this->vobject->add($property);
return $property;
}
2012-09-07 17:22:01 +04:00
public function setUID() {
$uid = substr(md5(rand().time()), 0, 10);
$this->vobject->add('UID', $uid);
}
2014-04-21 17:44:54 +04:00
/**
* @todo Write documentation
* @param mixed $name
* @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"));
$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
*
2014-04-21 17:44:54 +04:00
* @param string $name
* @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') {
$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);
$datetime_element->setDateTime($datetime, $dateType);
$this->vobject->__set($name, $datetime_element);
}else{
$this->vobject->__unset($name);
}
}
2014-04-21 17:44:54 +04:00
/**
* @todo Write documentation
* @param $name
* @return string
*/
2012-09-07 17:22:01 +04:00
public function getAsString($name) {
return $this->vobject->__isset($name) ?
$this->vobject->__get($name)->value :
'';
}
2014-04-21 17:44:54 +04:00
/**
* @todo Write documentation
* @param $name
* @return array
*/
2012-09-07 17:22:01 +04:00
public function getAsArray($name) {
$values = array();
2012-09-07 17:22:01 +04:00
if ($this->vobject->__isset($name)) {
$values = explode(',', $this->getAsString($name));
$values = array_map('trim', $values);
}
return $values;
}
2014-04-21 17:44:54 +04:00
/**
* @todo Write documentation
* @param $name
* @return array|OC_VObject|\Sabre\VObject\Property
*/
2012-09-07 17:22:01 +04:00
public function &__get($name) {
if ($name == 'children') {
return $this->vobject->children;
}
$return = $this->vobject->__get($name);
2012-10-17 14:25:34 +04:00
if ($return instanceof Sabre\VObject\Component) {
$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) {
return $this->vobject->__set($name, $value);
}
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) {
return $this->vobject->__unset($name);
}
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) {
2011-12-10 00:56:03 +04:00
return $this->vobject->__isset($name);
}
2014-04-21 17:44:54 +04:00
/**
* @todo Write documentation
* @param $function
* @param $arguments
* @return mixed
*/
2012-11-02 22:53:02 +04:00
public function __call($function, $arguments) {
return call_user_func_array(array($this->vobject, $function), $arguments);
}
}