OC_JSON::error();
exit;
}
- $vcalendar = OC_Calendar_Object::parse($data['calendardata']);
+ $vcalendar = OC_VObject::parse($data['calendardata']);
$last_modified = $vcalendar->VEVENT->__get('LAST-MODIFIED');
if($last_modified && $_POST['lastmodified'] != $last_modified->getDateTime()->format('U')){
echo $l10n->t('Wrong calendar');
exit;
}
-$object = OC_Calendar_Object::parse($data['calendardata']);
+$object = OC_VObject::parse($data['calendardata']);
$vevent = $object->VEVENT;
$dtstart = $vevent->DTSTART;
$dtend = OC_Calendar_Object::getDTEndFromVEvent($vevent);
break;
}
-$summary = isset($vevent->SUMMARY) ? $vevent->SUMMARY->value : '';
-$location = isset($vevent->LOCATION) ? $vevent->LOCATION->value : '';
-$categories = array();
-if (isset($vevent->CATEGORIES)){
- $categories = explode(',', $vevent->CATEGORIES->value);
- $categories = array_map('trim', $categories);
-}
+$summary = $vevent->getAsString('SUMMARY');
+$location = $vevent->getAsString('LOCATION');
+$categories = $vevent->getAsArray('CATEGORIES');
+$repeat = $vevent->getAsString('CATEGORY');
+$description = $vevent->getAsString('DESCRIPTION');
foreach($categories as $category){
if (!in_array($category, $category_options)){
array_unshift($category_options, $category);
}
}
-$repeat = isset($vevent->CATEGORY) ? $vevent->CATEGORY->value : '';
-$description = isset($vevent->DESCRIPTION) ? $vevent->DESCRIPTION->value : '';
$last_modified = $vevent->__get('LAST-MODIFIED');
if ($last_modified){
$lastmodified = $last_modified->getDateTime()->format('U');
$user_timezone = OC_Preferences::getValue(OC_USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
$return = array();
foreach($events as $event){
- $object = OC_Calendar_Object::parse($event['calendardata']);
+ $object = OC_VObject::parse($event['calendardata']);
$vevent = $object->VEVENT;
$dtstart = $vevent->DTSTART;
$dtend = OC_Calendar_Object::getDTEndFromVEvent($vevent);
$delta->d = $_POST['dayDelta'];
$delta->i = $_POST['minuteDelta'];
-$vcalendar = OC_Calendar_Object::parse($data['calendardata']);
+$vcalendar = OC_VObject::parse($data['calendardata']);
$vevent = $vcalendar->VEVENT;
$last_modified = $vevent->__get('LAST-MODIFIED');
$dtend->setDateTime($dtend->getDateTime()->add($delta), $end_type);
unset($vevent->DURATION);
-$now = new DateTime();
-$last_modified = new Sabre_VObject_Element_DateTime('LAST-MODIFIED');
-$last_modified->setDateTime($now, Sabre_VObject_Element_DateTime::UTC);
-$vevent->__set('LAST-MODIFIED', $last_modified);
-
-$dtstamp = new Sabre_VObject_Element_DateTime('DTSTAMP');
-$dtstamp->setDateTime($now, Sabre_VObject_Element_DateTime::UTC);
-$vevent->DTSTAMP = $dtstamp;
+$vevent->setDateTime('LAST-MODIFIED', 'now', Sabre_VObject_Element_DateTime::UTC);
+$vevent->setDateTime('DTSTAMP', 'now', Sabre_VObject_Element_DateTime::UTC);
$result = OC_Calendar_Object::edit($id, $vcalendar->serialize());
OC_JSON::success(array('lastmodified'=>(int)$now->format('U')));
* @return insertid
*/
public static function add($id,$data){
- $object = self::parse($data);
+ $object = OC_VObject::parse($data);
list($type,$startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);
if(is_null($uid)){
* @return insertid
*/
public static function addFromDAVData($id,$uri,$data){
- $object = self::parse($data);
+ $object = OC_VObject::parse($data);
list($type,$startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*calendar_objects (calendarid,objecttype,startdate,enddate,repeating,summary,calendardata,uri,lastmodified) VALUES(?,?,?,?,?,?,?,?,?)' );
public static function edit($id, $data){
$oldobject = self::find($id);
- $object = self::parse($data);
+ $object = OC_VObject::parse($data);
list($type,$startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_objects SET objecttype=?,startdate=?,enddate=?,repeating=?,summary=?,calendardata=?, lastmodified = ? WHERE id = ?' );
public static function editFromDAVData($cid,$uri,$data){
$oldobject = self::findWhereDAVDataIs($cid,$uri);
- $object = self::parse($data);
+ $object = OC_VObject::parse($data);
list($type,$startdate,$enddate,$summary,$repeating,$uid) = self::extractData($object);
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*calendar_objects SET objecttype=?,startdate=?,enddate=?,repeating=?,summary=?,calendardata=?, lastmodified = ? WHERE id = ?' );
// Child to use
$children = 0;
$use = null;
- foreach($object->children as &$property){
+ foreach($object->children as $property){
if($property->name == 'VEVENT'){
$children++;
$thisone = true;
// one VTODO per object)
break;
}
- } unset($property);
+ }
// find the data
if(!is_null($use)){
$return[0] = $use->name;
- foreach($use->children as &$property){
+ foreach($use->children as $property){
if($property->name == 'DTSTART'){
$return[1] = self::getUTCforMDB($property->getDateTime());
}
elseif($property->name == 'UID'){
$return[5] = $property->value;
}
- } unset($property);
+ }
}
// More than one child means reoccuring!
return date('Y-m-d H:i', $datetime->format('U') - $datetime->getOffset());
}
- /**
- * @brief Parses the VObject
- * @param string VObject as string
- * @returns Sabre_VObject or null
- */
- public static function parse($data){
- try {
- Sabre_VObject_Reader::$elementMap['LAST-MODIFIED'] = 'Sabre_VObject_Element_DateTime';
- $calendar = Sabre_VObject_Reader::read($data);
- return $calendar;
- } catch (Exception $e) {
- return null;
- }
- }
-
public static function getDTEndFromVEvent($vevent)
{
if ($vevent->DTEND) {
public static function createVCalendarFromRequest($request)
{
- $vcalendar = new Sabre_VObject_Component('VCALENDAR');
+ $vcalendar = new OC_VObject('VCALENDAR');
$vcalendar->add('PRODID', 'ownCloud Calendar');
$vcalendar->add('VERSION', '2.0');
- $now = new DateTime();
-
- $vevent = new Sabre_VObject_Component('VEVENT');
+ $vevent = new OC_VObject('VEVENT');
$vcalendar->add($vevent);
- $created = new Sabre_VObject_Element_DateTime('CREATED');
- $created->setDateTime($now, Sabre_VObject_Element_DateTime::UTC);
- $vevent->add($created);
-
- $uid = self::createUID();
- $vevent->add('UID',$uid);
+ $vevent->setDateTime('CREATED', 'now', Sabre_VObject_Element_DateTime::UTC);
+ $vevent->setUID();
return self::updateVCalendarFromRequest($request, $vcalendar);
}
}*/
$repeat = "false";
- $now = new DateTime();
- $vevent = $vcalendar->VEVENT[0];
-
- $last_modified = new Sabre_VObject_Element_DateTime('LAST-MODIFIED');
- $last_modified->setDateTime($now, Sabre_VObject_Element_DateTime::UTC);
- $vevent->__set('LAST-MODIFIED', $last_modified);
-
- $dtstamp = new Sabre_VObject_Element_DateTime('DTSTAMP');
- $dtstamp->setDateTime($now, Sabre_VObject_Element_DateTime::UTC);
- $vevent->DTSTAMP = $dtstamp;
+ $vevent = $vcalendar->VEVENT;
- $vevent->SUMMARY = $title;
+ $vevent->setDateTime('LAST-MODIFIED', 'now', Sabre_VObject_Element_DateTime::UTC);
+ $vevent->setDateTime('DTSTAMP', 'now', Sabre_VObject_Element_DateTime::UTC);
+ $vevent->setString('SUMMARY', $title);
$dtstart = new Sabre_VObject_Element_DateTime('DTSTART');
$dtend = new Sabre_VObject_Element_DateTime('DTEND');
if($allday){
$start = new DateTime($from);
$end = new DateTime($to.' +1 day');
- $dtstart->setDateTime($start, Sabre_VObject_Element_DateTime::DATE);
- $dtend->setDateTime($end, Sabre_VObject_Element_DateTime::DATE);
+ $vevent->setDateTime('DTSTART', $start, Sabre_VObject_Element_DateTime::DATE);
+ $vevent->setDateTime('DTEND', $end, Sabre_VObject_Element_DateTime::DATE);
}else{
$timezone = OC_Preferences::getValue(OC_USER::getUser(), 'calendar', 'timezone', date_default_timezone_get());
$timezone = new DateTimeZone($timezone);
$start = new DateTime($from.' '.$fromtime, $timezone);
$end = new DateTime($to.' '.$totime, $timezone);
- $dtstart->setDateTime($start, Sabre_VObject_Element_DateTime::LOCALTZ);
- $dtend->setDateTime($end, Sabre_VObject_Element_DateTime::LOCALTZ);
+ $vevent->setDateTime('DTSTART', $start, Sabre_VObject_Element_DateTime::LOCALTZ);
+ $vevent->setDateTime('DTEND', $end, Sabre_VObject_Element_DateTime::LOCALTZ);
}
- $vevent->DTSTART = $dtstart;
- $vevent->DTEND = $dtend;
unset($vevent->DURATION);
- if($location != ""){
- $vevent->LOCATION = $location;
- }else{
- unset($vevent->LOCATION);
- }
+
+ $vevent->setString('LOCATION', $location);
if($description != ""){
$vevent->DESCRIPTION = $description;
$values = $_POST['value'];
$parameters = $_POST['parameters'];
-$vcard = new Sabre_VObject_Component('VCARD');
-$vcard->add(new Sabre_VObject_Property('FN',$fn));
-$vcard->add(new Sabre_VObject_Property('UID',OC_Contacts_VCard::createUID()));
+$vcard = new OC_VObject('VCARD');
+$vcard->setUID();
+$vcard->setString('FN',$fn);
// Data to add ...
$add = array('TEL', 'EMAIL', 'ORG');
else{
$prop_parameters = array();
}
- OC_Contacts_VCard::addVCardProperty($vcard, $propname, $value, $prop_parameters);
+ $vcard->addProperty($propname, $value, $prop_parameters);
}
$id = OC_Contacts_VCard::add($aid,$vcard->serialize());
exit();
}
-$vcard = OC_Contacts_VCard::parse($card['carddata']);
+$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
$value = $_POST['value'];
$parameters = isset($_POST['parameteres'])?$_POST['parameters']:array();
-$property = OC_Contacts_VCard::addVCardProperty($vcard, $name, $value, $parameters);
+$property = $vcard->addProperty($name, $value, $parameters);
$line = count($vcard->children) - 1;
$checksum = md5($property->serialize());
exit();
}
-$vcard = OC_Contacts_VCard::parse($card['carddata']);
+$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
-$vcard = OC_Contacts_VCard::parse($card['carddata']);
+$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
-$vcard = OC_Contacts_VCard::parse($card['carddata']);
+$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
// Set the value
$value = $_POST['value'];
if(is_array($value)){
- $value = OC_Contacts_VCard::escapeSemicolons($value);
+ $value = OC_VObject::escapeSemicolons($value);
}
$vcard->children[$line]->setValue($value);
exit();
}
-$vcard = OC_Contacts_VCard::parse($card['carddata']);
+$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
if( !is_null($id) || count($contacts)){
if(is_null($id)) $id = $contacts[0]['id'];
$contact = OC_Contacts_VCard::find($id);
- $vcard = OC_Contacts_VCard::parse($contact['carddata']);
+ $vcard = OC_VObject::parse($contact['carddata']);
$details = OC_Contacts_VCard::structureContact($vcard);
}
*/
public static function add($id,$data){
$fn = null;
- $uri = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
- // VCARD must have a version
- $hasversion = false;
- foreach($card->children as $property){
- if($property->name == 'FN'){
- $fn = $property->value;
- }
- elseif($property->name == 'VERSION'){
- $hasversion = true;
- }
- elseif(is_null($uri) && $property->name == 'UID' ){
- $uri = $property->value.'.vcf';
- }
- }
- if(is_null($uri)){
- $uid = self::createUID();
- $uri = $uid.'.vcf';
- $card->add(new Sabre_VObject_Property('UID',$uid));
+ $fn = $card->getAsString('FN');
+ $uid = $card->getAsString('UID');
+ if(is_null($uid)){
+ $card->setUID();
+ $uid = $card->getAsString('UID');
$data = $card->serialize();
};
+ $uri = $uid.'.vcf';
+ // VCARD must have a version
+ $version = $card->getAsString('VERSION');
// Add version if needed
- if(!$hasversion){
+ if(is_null($version)){
$card->add(new Sabre_VObject_Property('VERSION','3.0'));
$data = $card->serialize();
}
*/
public static function addFromDAVData($id,$uri,$data){
$fn = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$oldcard = self::find($id);
$fn = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$oldcard = self::findWhereDAVDataIs($aid,$uri);
$fn = null;
- $card = self::parse($data);
+ $card = OC_VObject::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
return true;
}
- /**
- * @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;
- }
-
- /**
- * @brief Add property to vcard object
- * @param object $vcard
- * @param object $name of property
- * @param object $value of property
- * @param object $paramerters of property
- */
- public static function addVCardProperty($vcard, $name, $value, $parameters=array()){
- if(is_array($value)){
- $value = OC_Contacts_VCard::escapeSemicolons($value);
- }
- $property = new Sabre_VObject_Property( $name, $value );
- $parameternames = array_keys($parameters);
- foreach($parameternames as $i){
- $values = $parameters[$i];
- if (!is_array($values)){
- $values = array($values);
- }
- foreach($values as $value){
- $property->add($i, $value);
- }
- }
-
- $vcard->add($property);
- return $property;
- }
-
/**
* @brief Data structure of vCard
* @param object $property
$value = $property->value;
$value = htmlspecialchars($value);
if($property->name == 'ADR' || $property->name == 'N'){
- $value = self::unescapeSemicolons($value);
+ $value = OC_VObject::unescapeSemicolons($value);
}
$temp = array(
'name' => $property->name,
return $temp;
}
- /**
- * @brief Parses a vcard file
- * @param string vCard
- * @return Sabre_VObject or null
- *
- * Will retun the vobject if sabre DAV is able to parse the file.
- */
- public static function parse($data){
- try {
- $card = Sabre_VObject_Reader::read($data);
- return $card;
- } catch (Exception $e) {
- return null;
- }
- }
public static function getTypesOfProperty($l, $prop){
switch($prop){
case 'ADR':
exit();
}
-$content = OC_Contacts_VCard::parse($card['carddata']);
+$content = OC_VObject::parse($card['carddata']);
// invalid vcard
if( is_null($content)){
--- /dev/null
+<?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_Reader::$elementMap['LAST-MODIFIED'] = 'Sabre_VObject_Element_DateTime';
+ $vobject = Sabre_VObject_Reader::read($data);
+ if ($vobject instanceof Sabre_VObject_Component){
+ $vobject = new OC_VObject($vobject);
+ }
+ return $vobject;
+ } catch (Exception $e) {
+ 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 != ''){
+ $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_Element_DateTime::LOCALTZ){
+ if ($datetime == 'now'){
+ $datetime = new DateTime();
+ }
+ if ($datetime instanceof DateTime){
+ $datetime_element = new Sabre_VObject_Element_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 __call($function,$arguments){
+ return call_user_func_array(array($this->vobject, $function), $arguments);
+ }
+}