]> source.dussan.org Git - nextcloud-server.git/commitdiff
Improve reading of vCard files
authorJakob Sack <kde@jakobsack.de>
Thu, 15 Sep 2011 09:06:21 +0000 (11:06 +0200)
committerJakob Sack <kde@jakobsack.de>
Thu, 15 Sep 2011 09:06:21 +0000 (11:06 +0200)
apps/contacts/ajax/addproperty.php
apps/contacts/ajax/deleteproperty.php
apps/contacts/ajax/getdetails.php
apps/contacts/ajax/setproperty.php
apps/contacts/ajax/showsetproperty.php
apps/contacts/lib/addressbook.php

index 68075efc6aa307d3f06069726f9e09a6865cec2a..70b2c8dcf82a6024b2ce096e2278f115133fdfbc 100644 (file)
@@ -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();
index f73758a3676b7b6687901dc9d51b20555b1af32c..52adca877f5ac71bf99475a52d27e4d9562fb717 100644 (file)
@@ -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;$i<count($vcard->children);$i++){
        if(md5($vcard->children[$i]->serialize()) == $checksum ){
index ddd29be95c932b970537d396d78ad5ce731a4e86..e13cb9dfe93abf2ce2d7b7fd45f4c0be23b0ac59 100644 (file)
@@ -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);
index f3b8e0c56b7d59074c96872f183b9a16dacb739b..8b9bc4b3cfe209800ccd917936b5978d4dd192ee 100644 (file)
@@ -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;$i<count($vcard->children);$i++){
        if(md5($vcard->children[$i]->serialize()) == $checksum ){
index adae17553cf5722c0993911c20b07ba61da15152..722ee8d9e3510c713b1952d48599203da10cbe70 100644 (file)
@@ -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;$i<count($vcard->children);$i++){
        if(md5($vcard->children[$i]->serialize()) == $checksum ){
index 89894d33b5b0fdc0a8e0fe383b5bd16d0cfe4a9a..574484b932e798818ca9d0e80c0016ea5dfb7344 100644 (file)
@@ -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;
                }
        }
 }