From c3ff534b19ec50dac75c6d1e54dff1d349f85000 Mon Sep 17 00:00:00 2001 From: Jakob Sack Date: Thu, 15 Sep 2011 11:06:21 +0200 Subject: [PATCH] Improve reading of vCard files --- apps/contacts/ajax/addproperty.php | 5 +-- apps/contacts/ajax/deleteproperty.php | 4 +- apps/contacts/ajax/getdetails.php | 4 +- apps/contacts/ajax/setproperty.php | 4 +- apps/contacts/ajax/showsetproperty.php | 4 +- apps/contacts/lib/addressbook.php | 55 ++++++++++++++------------ 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/apps/contacts/ajax/addproperty.php b/apps/contacts/ajax/addproperty.php index 68075efc6aa..70b2c8dcf82 100644 --- a/apps/contacts/ajax/addproperty.php +++ b/apps/contacts/ajax/addproperty.php @@ -44,14 +44,13 @@ if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ exit(); } +$vcard = OC_Contacts_Addressbook::parse($card['carddata']); // Check if the card is valid -if( !OC_Contacts_Addressbook::isValidVObject($card['carddata'])){ +if(is_null($vcard)){ echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!')))); exit(); } -$vcard = Sabre_VObject_Reader::read($card['carddata']); - $name = $_POST['name']; $value = $_POST['value']; $parameters = isset($_POST['parameteres'])?$_POST['parameters']:array(); diff --git a/apps/contacts/ajax/deleteproperty.php b/apps/contacts/ajax/deleteproperty.php index f73758a3676..52adca877f5 100644 --- a/apps/contacts/ajax/deleteproperty.php +++ b/apps/contacts/ajax/deleteproperty.php @@ -48,13 +48,13 @@ if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ exit(); } +$vcard = OC_Contacts_Addressbook::parse($card['carddata']); // Check if the card is valid -if( !OC_Contacts_Addressbook::isValidVObject($card['carddata'])){ +if(is_null($vcard)){ echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!')))); exit(); } -$vcard = Sabre_VObject_Reader::read($card['carddata']); $line = null; for($i=0;$ichildren);$i++){ if(md5($vcard->children[$i]->serialize()) == $checksum ){ diff --git a/apps/contacts/ajax/getdetails.php b/apps/contacts/ajax/getdetails.php index ddd29be95c9..e13cb9dfe93 100644 --- a/apps/contacts/ajax/getdetails.php +++ b/apps/contacts/ajax/getdetails.php @@ -46,13 +46,13 @@ if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ exit(); } +$vcard = OC_Contacts_Addressbook::parse($card['carddata']); // Check if the card is valid -if( !OC_Contacts_Addressbook::isValidVObject($card['carddata'])){ +if(is_null($vcard)){ echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!')))); exit(); } -$vcard = Sabre_VObject_Reader::read($card['carddata']); $details = OC_Contacts_Addressbook::structureContact($vcard); $tmpl = new OC_Template('contacts','part.details'); $tmpl->assign('details',$details); diff --git a/apps/contacts/ajax/setproperty.php b/apps/contacts/ajax/setproperty.php index f3b8e0c56b7..8b9bc4b3cfe 100644 --- a/apps/contacts/ajax/setproperty.php +++ b/apps/contacts/ajax/setproperty.php @@ -45,13 +45,13 @@ if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ exit(); } +$vcard = OC_Contacts_Addressbook::parse($card['carddata']); // Check if the card is valid -if( !OC_Contacts_Addressbook::isValidVObject($card['carddata'])){ +if(is_null($vcard)){ echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!')))); exit(); } -$vcard = Sabre_VObject_Reader::read($card['carddata']); $line = null; for($i=0;$ichildren);$i++){ if(md5($vcard->children[$i]->serialize()) == $checksum ){ diff --git a/apps/contacts/ajax/showsetproperty.php b/apps/contacts/ajax/showsetproperty.php index adae17553cf..722ee8d9e35 100644 --- a/apps/contacts/ajax/showsetproperty.php +++ b/apps/contacts/ajax/showsetproperty.php @@ -45,13 +45,13 @@ if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ exit(); } +$vcard = OC_Contacts_Addressbook::parse($card['carddata']); // Check if the card is valid -if( !OC_Contacts_Addressbook::isValidVObject($card['carddata'])){ +if(is_null($vcard)){ echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!')))); exit(); } -$vcard = Sabre_VObject_Reader::read($card['carddata']); $line = null; for($i=0;$ichildren);$i++){ if(md5($vcard->children[$i]->serialize()) == $checksum ){ diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php index 89894d33b5b..574484b932e 100644 --- a/apps/contacts/lib/addressbook.php +++ b/apps/contacts/lib/addressbook.php @@ -32,7 +32,7 @@ * description TEXT, * ctag INT(11) UNSIGNED NOT NULL DEFAULT '1' * ); - * + * * CREATE TABLE contacts_cards ( * id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, * addressbookid INT(11) UNSIGNED NOT NULL, @@ -55,7 +55,7 @@ class OC_Contacts_Addressbook{ public static function allAddressbooks($uid){ $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ?' ); $result = $stmt->execute(array($uid)); - + $addressbooks = array(); while( $row = $result->fetchRow()){ $addressbooks[] = $row; @@ -63,7 +63,7 @@ class OC_Contacts_Addressbook{ return $addressbooks; } - + /** * @brief Returns the list of addressbooks for a principal (DAV term of user) * @param string $principaluri @@ -118,7 +118,7 @@ class OC_Contacts_Addressbook{ */ public static function addAddressbookFromDAVData($principaluri,$uri,$name,$description){ $userid = self::extractUserID($principaluri); - + $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)' ); $result = $stmt->execute(array($userid,$name,$uri,$description,1)); @@ -142,7 +142,7 @@ class OC_Contacts_Addressbook{ if(is_null($description)){ $description = $addressbook['description']; } - + $stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET displayname=?,description=?, ctag=ctag+1 WHERE id=?' ); $result = $stmt->execute(array($name,$description,$id)); @@ -169,7 +169,7 @@ class OC_Contacts_Addressbook{ public static function deleteAddressbook($id){ $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' ); $stmt->execute(array($id)); - + $stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE addressbookid = ?' ); $stmt->execute(array($id)); @@ -195,7 +195,7 @@ class OC_Contacts_Addressbook{ return $addressbooks; } - + /** * @brief Returns a card * @param integer $id @@ -230,8 +230,9 @@ class OC_Contacts_Addressbook{ public static function addCard($id,$data){ $fn = null; $uri = null; - if(self::isValidVObject($data)){ - $card = Sabre_VObject_Reader::read($data); + + $card = self::parse($data); + if(!is_null($card)){ foreach($card->children as $property){ if($property->name == 'FN'){ $fn = $property->value; @@ -270,8 +271,8 @@ class OC_Contacts_Addressbook{ */ public static function addCardFromDAVData($id,$uri,$data){ $fn = null; - if(self::isValidVObject($data)){ - $card = Sabre_VObject_Reader::read($data); + $card = self::parse($data); + if(!is_null($card)){ foreach($card->children as $property){ if($property->name == 'FN'){ $fn = $property->value; @@ -296,8 +297,9 @@ class OC_Contacts_Addressbook{ public static function editCard($id, $data){ $oldcard = self::findCard($id); $fn = null; - if(self::isValidVObject($data)){ - $card = Sabre_VObject_Reader::read($data); + + $card = self::parse($data); + if(!is_null($card)){ foreach($card->children as $property){ if($property->name == 'FN'){ $fn = $property->value; @@ -324,7 +326,8 @@ class OC_Contacts_Addressbook{ $oldcard = self::findCardWhereDAVDataIs($aid,$uri); $fn = null; - if(self::isValidVObject($data)){ + $card = self::parse($data); + if(!is_null($card)){ $card = Sabre_VObject_Reader::read($data); foreach($card->children as $property){ if($property->name == 'FN'){ @@ -340,7 +343,7 @@ class OC_Contacts_Addressbook{ return true; } - + /** * @brief deletes a card * @param integer $id id of card @@ -365,7 +368,7 @@ class OC_Contacts_Addressbook{ return true; } - + /** * @brief Creates a URI for Addressbook * @param string $name name of the addressbook @@ -390,7 +393,7 @@ class OC_Contacts_Addressbook{ public static function createUID(){ return substr(md5(rand().time()),0,10); } - + /** * @brief gets the userid from a principal path * @return string @@ -454,13 +457,13 @@ class OC_Contacts_Addressbook{ } return $details; } - + /** * @brief Data structure of properties * @param object $property * @return associative array * - * returns an associative array with + * returns an associative array with * ['name'] name of property * ['value'] htmlspecialchars escaped value of property * ['parameters'] associative array name=>value @@ -489,18 +492,18 @@ class OC_Contacts_Addressbook{ } /** - * @brief Checks if SabreDAV can parse the file + * @brief Parses a vcard file * @param string vCard - * @return boolean + * @return Sabre_VObject or null * - * The code is largely copypasted from Sabre_VObject_Reader + * Will retun the vobject if sabre DAV is able to parse the file. */ - public static function isValidVObject($data){ + public static function parse($data){ try { - Sabre_VObject_Reader::read($data); - return true; + $card = Sabre_VObject_Reader::read($data); + return $card; } catch (Exception $e) { - return false; + return null; } } } -- 2.39.5