1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-30 19:52:17 +01:00

Split OC_Contacts_Addressbook in _Addressbook and _VCard

This commit is contained in:
Jakob Sack 2011-09-17 00:26:57 +02:00
parent 22163ead6d
commit e916f7eeeb
17 changed files with 428 additions and 390 deletions

View File

@ -32,7 +32,7 @@ if( !OC_User::isLoggedIn()){
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $aid );
$addressbook = OC_Contacts_Addressbook::find( $aid );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your addressbook!'))));
exit();
@ -42,10 +42,10 @@ $fn = $_POST['fn'];
$vcard = new Sabre_VObject_Component('VCARD');
$vcard->add(new Sabre_VObject_Property('FN',$fn));
$vcard->add(new Sabre_VObject_Property('UID',OC_Contacts_Addressbook::createUID()));
$id = OC_Contacts_Addressbook::addCard($aid,$vcard->serialize());
$vcard->add(new Sabre_VObject_Property('UID',OC_Contacts_VCard::createUID()));
$id = OC_Contacts_VCard::add($aid,$vcard->serialize());
$details = OC_Contacts_Addressbook::structureContact($vcard);
$details = OC_Contacts_VCard::structureContact($vcard);
$tmpl = new OC_Template('contacts','part.details');
$tmpl->assign('details',$details);
$tmpl->assign('id',$id);

View File

@ -32,19 +32,19 @@ if( !OC_User::isLoggedIn()){
exit();
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
$vcard = OC_Contacts_Addressbook::parse($card['carddata']);
$vcard = OC_Contacts_VCard::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!'))));
@ -56,7 +56,7 @@ $value = $_POST['value'];
$parameters = isset($_POST['parameteres'])?$_POST['parameters']:array();
if(is_array($value)){
$value = OC_Contacts_Addressbook::escapeSemicolons($value);
$value = OC_Contacts_VCard::escapeSemicolons($value);
}
$property = new Sabre_VObject_Property( $name, $value );
$parameternames = array_keys($parameters);
@ -69,10 +69,10 @@ $vcard->add($property);
$line = count($vcard->children) - 1;
$checksum = md5($property->serialize());
OC_Contacts_Addressbook::editCard($id,$vcard->serialize());
OC_Contacts_VCard::edit($id,$vcard->serialize());
$tmpl = new OC_Template('contacts','part.property');
$tmpl->assign('property',OC_Contacts_Addressbook::structureProperty($property,$line));
$tmpl->assign('property',OC_Contacts_VCard::structureProperty($property,$line));
$page = $tmpl->fetchPage();
echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page )));

View File

@ -33,11 +33,11 @@ if( !OC_User::isLoggedIn()){
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $id );
$addressbook = OC_Contacts_Addressbook::find( $id );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
OC_Contacts_Addressbook::deleteAddressbook($id);
OC_Contacts_Addressbook::delete($id);
echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id )));

View File

@ -34,17 +34,17 @@ if( !OC_User::isLoggedIn()){
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
OC_Contacts_Addressbook::deleteCard($id);
OC_Contacts_VCard::delete($id);
echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id )));

View File

@ -36,19 +36,19 @@ if( !OC_User::isLoggedIn()){
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
$vcard = OC_Contacts_Addressbook::parse($card['carddata']);
$vcard = OC_Contacts_VCard::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!'))));
@ -68,5 +68,5 @@ if(is_null($line)){
unset($vcard->children[$line]);
OC_Contacts_Addressbook::editCard($id,$vcard->serialize());
OC_Contacts_VCard::edit($id,$vcard->serialize());
echo json_encode( array( 'status' => 'success', 'data' => array( 'id' => $id )));

View File

@ -34,26 +34,26 @@ if( !OC_User::isLoggedIn()){
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::findCard( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
$vcard = OC_Contacts_Addressbook::parse($card['carddata']);
$vcard = OC_Contacts_VCard::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!'))));
exit();
}
$details = OC_Contacts_Addressbook::structureContact($vcard);
$details = OC_Contacts_VCard::structureContact($vcard);
$tmpl = new OC_Template('contacts','part.details');
$tmpl->assign('details',$details);
$tmpl->assign('id',$id);

View File

@ -33,19 +33,19 @@ if( !OC_User::isLoggedIn()){
exit();
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
$vcard = OC_Contacts_Addressbook::parse($card['carddata']);
$vcard = OC_Contacts_VCard::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!'))));
@ -66,7 +66,7 @@ if(is_null($line)){
// Set the value
$value = $_POST['value'];
if(is_array($value)){
$value = OC_Contacts_Addressbook::escapeSemicolons($value);
$value = OC_Contacts_VCard::escapeSemicolons($value);
}
$vcard->children[$line]->setValue($value);
@ -94,10 +94,10 @@ foreach($missingparameters as $i){
// Do checksum and be happy
$checksum = md5($vcard->children[$line]->serialize());
OC_Contacts_Addressbook::editCard($id,$vcard->serialize());
OC_Contacts_VCard::edit($id,$vcard->serialize());
$tmpl = new OC_Template('contacts','part.property');
$tmpl->assign('property',OC_Contacts_Addressbook::structureProperty($vcard->children[$line],$line));
$tmpl->assign('property',OC_Contacts_VCard::structureProperty($vcard->children[$line],$line));
$page = $tmpl->fetchPage();
echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page, 'line' => $line, 'oldchecksum' => $_POST['checksum'] )));

View File

@ -31,7 +31,7 @@ if( !OC_User::isLoggedIn()){
exit();
}
$addressbooks = OC_Contacts_Addressbook::allAddressbooks(OC_USER::getUser());
$addressbooks = OC_Contacts_Addressbook::all(OC_USER::getUser());
$tmpl = new OC_Template('contacts','part.addcardform');
$tmpl->assign('addressbooks',$addressbooks);
$page = $tmpl->fetchPage();

View File

@ -32,13 +32,13 @@ if( !OC_User::isLoggedIn()){
exit();
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();

View File

@ -33,19 +33,19 @@ if( !OC_User::isLoggedIn()){
exit();
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Can not find Contact!'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('This is not your contact!'))));
exit();
}
$vcard = OC_Contacts_Addressbook::parse($card['carddata']);
$vcard = OC_Contacts_VCard::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
echo json_encode( array( 'status' => 'error', 'data' => array( 'message' => $l10n->t('Unable to parse vCard!'))));
@ -67,7 +67,7 @@ if(is_null($line)){
$tmpl = new OC_Template('contacts','part.setpropertyform');
$tmpl->assign('id',$id);
$tmpl->assign('checksum',$checksum);
$tmpl->assign('property',OC_Contacts_Addressbook::structureProperty($vcard->children[$line]));
$tmpl->assign('property',OC_Contacts_VCard::structureProperty($vcard->children[$line]));
$page = $tmpl->fetchPage();
echo json_encode( array( 'status' => 'success', 'data' => array( 'page' => $page )));

View File

@ -1,16 +1,17 @@
<?php
OC::$CLASSPATH['OC_Contacts_Addressbook'] = 'apps/contacts/lib/addressbook.php';
OC::$CLASSPATH['OC_Contacts_VCard'] = 'apps/contacts/lib/vcard.php';
OC::$CLASSPATH['OC_Contacts_Hooks'] = 'apps/contacts/lib/hooks.php';
OC::$CLASSPATH['OC_Connector_Sabre_CardDAV'] = 'apps/contacts/lib/connector_sabre.php';
OC_HOOK::connect('OC_User', 'post_createUser', 'OC_Contacts_Hooks', 'deleteUser');
OC_App::register( array(
OC_App::register( array(
'order' => 10,
'id' => 'contacts',
'name' => 'Contacts' ));
OC_App::addNavigationEntry( array(
OC_App::addNavigationEntry( array(
'id' => 'contacts_index',
'order' => 10,
'href' => OC_Helper::linkTo( 'contacts', 'index.php' ),

View File

@ -34,10 +34,10 @@ if( !OC_User::isLoggedIn()){
}
// Check if the user has an addressbook
$addressbooks = OC_Contacts_Addressbook::allAddressbooks(OC_User::getUser());
$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
if( count($addressbooks) == 0){
OC_Contacts_Addressbook::addAddressbook(OC_User::getUser(),'default','Default Address Book');
$addressbooks = OC_Contacts_Addressbook::allAddressbooks(OC_User::getUser());
OC_Contacts_Addressbook::add(OC_User::getUser(),'default','Default Address Book');
$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
}
$prefbooks = OC_Preferences::getValue(OC_User::getUser(),'contacts','openaddressbooks',null);
if(is_null($prefbooks)){
@ -58,7 +58,7 @@ $openaddressbooks = explode(';',$prefbooks);
$contacts = array();
foreach( $openaddressbooks as $addressbook ){
$addressbookcontacts = OC_Contacts_Addressbook::allCards($addressbook);
$addressbookcontacts = OC_Contacts_VCard::all($addressbook);
foreach( $addressbookcontacts as $contact ){
if(is_null($contact['fullname'])){
continue;
@ -73,9 +73,9 @@ $details = array();
if( !is_null($id) || count($contacts)){
if(is_null($id)) $id = $contacts[0]['id'];
$contact = OC_Contacts_Addressbook::findCard($id);
$vcard = Sabre_VObject_Reader::read($contact['carddata']);
$details = OC_Contacts_Addressbook::structureContact($vcard);
$contact = OC_Contacts_VCard::find($id);
$vcard = OC_Contacts_VCard::parse($contact['carddata']);
$details = OC_Contacts_VCard::structureContact($vcard);
}
// Process the template

View File

@ -33,16 +33,7 @@
* 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,
* fullname VARCHAR(255),
* carddata TEXT,
* uri VARCHAR(100),
* lastmodified INT(11) UNSIGNED
* );
*/
/**
* This class manages our addressbooks.
*/
@ -52,7 +43,7 @@ class OC_Contacts_Addressbook{
* @param string $uid
* @return array
*/
public static function allAddressbooks($uid){
public static function all($uid){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ?' );
$result = $stmt->execute(array($uid));
@ -69,9 +60,9 @@ class OC_Contacts_Addressbook{
* @param string $principaluri
* @return array
*/
public static function allAddressbooksWherePrincipalURIIs($principaluri){
public static function allWherePrincipalURIIs($principaluri){
$uid = self::extractUserID($principaluri);
return self::allAddressbooks($uid);
return self::all($uid);
}
/**
@ -79,7 +70,7 @@ class OC_Contacts_Addressbook{
* @param integer $id
* @return associative array
*/
public static function findAddressbook($id){
public static function find($id){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
$result = $stmt->execute(array($id));
@ -93,8 +84,8 @@ class OC_Contacts_Addressbook{
* @param string $description
* @return insertid
*/
public static function addAddressbook($userid,$name,$description){
$all = self::allAddressbooks($userid);
public static function add($userid,$name,$description){
$all = self::all($userid);
$uris = array();
foreach($all as $i){
$uris[] = $i['uri'];
@ -116,7 +107,7 @@ class OC_Contacts_Addressbook{
* @param string $description
* @return insertid
*/
public static function addAddressbookFromDAVData($principaluri,$uri,$name,$description){
public static function addFromDAVData($principaluri,$uri,$name,$description){
$userid = self::extractUserID($principaluri);
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_addressbooks (userid,displayname,uri,description,ctag) VALUES(?,?,?,?,?)' );
@ -132,7 +123,7 @@ class OC_Contacts_Addressbook{
* @param string $description
* @return boolean
*/
public static function editAddressbook($id,$name,$description){
public static function edit($id,$name,$description){
// Need these ones for checking uri
$addressbook = self::find($id);
@ -149,226 +140,35 @@ class OC_Contacts_Addressbook{
return true;
}
/**
* @brief Updates ctag for addressbook
* @param integer $id
* @return boolean
*/
public static function touchAddressbook($id){
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET ctag = ctag + 1 WHERE id = ?' );
$stmt->execute(array($id));
return true;
}
/**
* @brief removes an address book
* @param integer $id
* @return boolean
*/
public static function deleteAddressbook($id){
public static function delete($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));
$cards = OC_Contacts_VCard::all($id);
foreach($cards as $card){
OC_Contacts_VCard::delete($card['id']);
}
return true;
}
/**
* @brief Returns all cards of an address book
* @brief Updates ctag for addressbook
* @param integer $id
* @return array
*
* The cards are associative arrays. You'll find the original vCard in
* ['carddata']
*/
public static function allCards($id){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ?' );
$result = $stmt->execute(array($id));
$addressbooks = array();
while( $row = $result->fetchRow()){
$addressbooks[] = $row;
}
return $addressbooks;
}
/**
* @brief Returns a card
* @param integer $id
* @return associative array
*/
public static function findCard($id){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE id = ?' );
$result = $stmt->execute(array($id));
return $result->fetchRow();
}
/**
* @brief finds a card by its DAV Data
* @param integer $aid Addressbook id
* @param string $uri the uri ('filename')
* @return associative array
*/
public static function findCardWhereDAVDataIs($aid,$uri){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri = ?' );
$result = $stmt->execute(array($aid,$uri));
return $result->fetchRow();
}
/**
* @brief Adds a card
* @param integer $id Addressbook id
* @param string $data vCard file
* @return insertid
*/
public static function addCard($id,$data){
$fn = null;
$uri = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
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));
$data = $card->serialize();
};
}
else{
// that's hard. Creating a UID and not saving it
$uid = self::createUID();
$uri = $uid.'.vcf';
};
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
$result = $stmt->execute(array($id,$fn,$data,$uri,time()));
self::touchAddressbook($id);
return OC_DB::insertid();
}
/**
* @brief Adds a card with the data provided by sabredav
* @param integer $id Addressbook id
* @param string $uri the uri the card will have
* @param string $data vCard file
* @return insertid
*/
public static function addCardFromDAVData($id,$uri,$data){
$fn = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
}
}
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
$result = $stmt->execute(array($id,$fn,$data,$uri,time()));
self::touchAddressbook($id);
return OC_DB::insertid();
}
/**
* @brief edits a card
* @param integer $id id of card
* @param string $data vCard file
* @return boolean
*/
public static function editCard($id, $data){
$oldcard = self::findCard($id);
$fn = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
}
}
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
$result = $stmt->execute(array($fn,$data,time(),$id));
self::touchAddressbook($oldcard['addressbookid']);
return true;
}
/**
* @brief edits a card with the data provided by sabredav
* @param integer $id Addressbook id
* @param string $uri the uri of the card
* @param string $data vCard file
* @return boolean
*/
public static function editCardFromDAVData($aid,$uri,$data){
$oldcard = self::findCardWhereDAVDataIs($aid,$uri);
$fn = null;
$card = self::parse($data);
if(!is_null($card)){
$card = Sabre_VObject_Reader::read($data);
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
}
}
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
$result = $stmt->execute(array($fn,$data,time(),$oldcard['id']));
self::touchAddressbook($oldcard['addressbookid']);
return true;
}
/**
* @brief deletes a card
* @param integer $id id of card
* @return boolean
*/
public static function deleteCard($id){
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE id = ?' );
public static function touch($id){
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET ctag = ctag + 1 WHERE id = ?' );
$stmt->execute(array($id));
return true;
}
/**
* @brief deletes a card with the data provided by sabredav
* @param integer $aid Addressbook id
* @param string $uri the uri of the card
* @return boolean
*/
public static function deleteCardFromDAVData($aid,$uri){
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri=?' );
$stmt->execute(array($aid,$uri));
return true;
}
/**
* @brief Creates a URI for Addressbook
* @param string $name name of the addressbook
@ -386,14 +186,6 @@ class OC_Contacts_Addressbook{
return $newname;
}
/**
* @brief Creates a UID
* @return string
*/
public static function createUID(){
return substr(md5(rand().time()),0,10);
}
/**
* @brief gets the userid from a principal path
* @return string
@ -402,108 +194,4 @@ class OC_Contacts_Addressbook{
list($prefix,$userid) = Sabre_DAV_URLUtil::splitPath($principaluri);
return $userid;
}
/**
* @brief Escapes semicolons
* @param string $value
* @return string
*/
public static function escapeSemicolons($value){
foreach($value as &$i ){
$i = implode("\\\\;", explode(';', $i));
} unset($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 Data structure of vCard
* @param object $property
* @return associative array
*
* look at code ...
*/
public static function structureContact($object){
$details = array();
foreach($object->children as $property){
$temp = self::structureProperty($property);
if(array_key_exists($property->name,$details)){
$details[$property->name][] = $temp;
}
else{
$details[$property->name] = array($temp);
}
}
return $details;
}
/**
* @brief Data structure of properties
* @param object $property
* @return associative array
*
* returns an associative array with
* ['name'] name of property
* ['value'] htmlspecialchars escaped value of property
* ['parameters'] associative array name=>value
* ['checksum'] checksum of whole property
*/
public static function structureProperty($property){
$value = $property->value;
$value = htmlspecialchars($value);
if($property->name == 'ADR' || $property->name == 'N'){
$value = self::unescapeSemicolons($value);
}
$temp = array(
'name' => $property->name,
'value' => $value,
'parameters' => array(),
'checksum' => md5($property->serialize()));
foreach($property->parameters as $parameter){
// Faulty entries by kaddressbook
if($parameter->name == 'TYPE' && $parameter->value == 'PREF'){
$parameter->name = 'PREF';
$parameter->value = '1';
}
$temp['parameters'][$parameter->name] = $parameter->value;
}
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;
}
}
}

View File

@ -31,9 +31,9 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return array
*/
public function getAddressBooksForUser($principaluri) {
$data = OC_Contacts_Addressbook::allAddressbooksWherePrincipalURIIs($principaluri);
$data = OC_Contacts_Addressbook::allWherePrincipalURIIs($principaluri);
$addressbooks = array();
foreach($data as $i) {
$addressbooks[] = array(
'id' => $i['id'],
@ -79,7 +79,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
}
}
OC_Contacts_Addressbook::editAddressbook($addressbookid,$name,$description);
OC_Contacts_Addressbook::edit($addressbookid,$name,$description);
return true;
@ -113,7 +113,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
}
OC_Contacts_Addressbook::addAddressbookFromDAVData($principaluri,$url,$name,$description);
OC_Contacts_Addressbook::addFromDAVData($principaluri,$url,$name,$description);
}
/**
@ -123,7 +123,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return void
*/
public function deleteAddressBook($addressbookid) {
OC_Contacts_Addressbook::deleteAddressbook($addressbookid);
OC_Contacts_Addressbook::delete($addressbookid);
}
/**
@ -133,7 +133,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return array
*/
public function getCards($addressbookid) {
$data = OC_Contacts_Addressbook::allCards($addressbookid);
$data = OC_Contacts_VCard::all($addressbookid);
$cards = array();
foreach($data as $i){
$cards[] = array(
@ -145,7 +145,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
return $cards;
}
/**
* Returns a specfic card
*
@ -154,7 +154,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return array
*/
public function getCard($addressbookid, $carduri) {
return OC_Contacts_Addressbook::findCardWhereDAVDataIs($addressbookid,$carduri);
return OC_Contacts_VCard::findWhereDAVDataIs($addressbookid,$carduri);
}
@ -167,7 +167,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return bool
*/
public function createCard($addressbookid, $carduri, $carddata) {
OC_Contacts_Addressbook::addCardFromDAVData($addressbookid, $carduri, $carddata);
OC_Contacts_VCard::addFromDAVData($addressbookid, $carduri, $carddata);
return true;
}
@ -180,7 +180,7 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return bool
*/
public function updateCard($addressbookid, $carduri, $carddata) {
return OC_Contacts_Addressbook::editCardFromDAVData($addressbookid, $carduri, $carddata);
return OC_Contacts_VCard::editFromDAVData($addressbookid, $carduri, $carddata);
}
/**
@ -191,6 +191,6 @@ class OC_Connector_Sabre_CardDAV extends Sabre_CardDAV_Backend_Abstract {
* @return bool
*/
public function deleteCard($addressbookid, $carduri) {
return OC_Contacts_Addressbook::deleteCardFromDAVData($addressbookid, $carduri);
return OC_Contacts_VCard::deleteFromDAVData($addressbookid, $carduri);
}
}

View File

@ -30,10 +30,10 @@ class OC_Contacts_Hooks{
* @return array
*/
public function deleteUser($parameters) {
$addressbooks = OC_Contacts_Addressbook::allAddressbooks($parameters['uid']);
$addressbooks = OC_Contacts_Addressbook::all($parameters['uid']);
foreach($addressbooks as $addressbook) {
OC_Contacts_Addressbook::deleteAddressbook($addressbook['id']);
OC_Contacts_Addressbook::delete($addressbook['id']);
}
return true;

344
lib/vcard.php Normal file
View File

@ -0,0 +1,344 @@
<?php
/**
* ownCloud - Addressbook
*
* @author Jakob Sack
* @copyright 2011 Jakob Sack mail@jakobsack.de
*
* 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/>.
*
*/
/*
*
* The following SQL statement is just a help for developers and will not be
* executed!
*
* CREATE TABLE contacts_cards (
* id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
* addressbookid INT(11) UNSIGNED NOT NULL,
* fullname VARCHAR(255),
* carddata TEXT,
* uri VARCHAR(100),
* lastmodified INT(11) UNSIGNED
* );
*/
/**
* This class manages our vCards
*/
class OC_Contacts_VCard{
/**
* @brief Returns all cards of an address book
* @param integer $id
* @return array
*
* The cards are associative arrays. You'll find the original vCard in
* ['carddata']
*/
public static function all($id){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ?' );
$result = $stmt->execute(array($id));
$addressbooks = array();
while( $row = $result->fetchRow()){
$addressbooks[] = $row;
}
return $addressbooks;
}
/**
* @brief Returns a card
* @param integer $id
* @return associative array
*/
public static function find($id){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE id = ?' );
$result = $stmt->execute(array($id));
return $result->fetchRow();
}
/**
* @brief finds a card by its DAV Data
* @param integer $aid Addressbook id
* @param string $uri the uri ('filename')
* @return associative array
*/
public static function findWhereDAVDataIs($aid,$uri){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri = ?' );
$result = $stmt->execute(array($aid,$uri));
return $result->fetchRow();
}
/**
* @brief Adds a card
* @param integer $id Addressbook id
* @param string $data vCard file
* @return insertid
*/
public static function add($id,$data){
$fn = null;
$uri = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
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));
$data = $card->serialize();
};
}
else{
// that's hard. Creating a UID and not saving it
$uid = self::createUID();
$uri = $uid.'.vcf';
};
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
$result = $stmt->execute(array($id,$fn,$data,$uri,time()));
OC_Contacts_Addressbook::touch($id);
return OC_DB::insertid();
}
/**
* @brief Adds a card with the data provided by sabredav
* @param integer $id Addressbook id
* @param string $uri the uri the card will have
* @param string $data vCard file
* @return insertid
*/
public static function addFromDAVData($id,$uri,$data){
$fn = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
}
}
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
$result = $stmt->execute(array($id,$fn,$data,$uri,time()));
OC_Contacts_Addressbook::touch($id);
return OC_DB::insertid();
}
/**
* @brief edits a card
* @param integer $id id of card
* @param string $data vCard file
* @return boolean
*/
public static function edit($id, $data){
$oldcard = self::find($id);
$fn = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
}
}
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
$result = $stmt->execute(array($fn,$data,time(),$id));
OC_Contacts_Addressbook::touch($oldcard['addressbookid']);
return true;
}
/**
* @brief edits a card with the data provided by sabredav
* @param integer $id Addressbook id
* @param string $uri the uri of the card
* @param string $data vCard file
* @return boolean
*/
public static function editFromDAVData($aid,$uri,$data){
$oldcard = self::findWhereDAVDataIs($aid,$uri);
$fn = null;
$card = self::parse($data);
if(!is_null($card)){
foreach($card->children as $property){
if($property->name == 'FN'){
$fn = $property->value;
}
}
}
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET fullname = ?,carddata = ?, lastmodified = ? WHERE id = ?' );
$result = $stmt->execute(array($fn,$data,time(),$oldcard['id']));
OC_Contacts_Addressbook::touch($oldcard['addressbookid']);
return true;
}
/**
* @brief deletes a card
* @param integer $id id of card
* @return boolean
*/
public static function delete($id){
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE id = ?' );
$stmt->execute(array($id));
return true;
}
/**
* @brief Creates a UID
* @return string
*/
public static function createUID(){
return substr(md5(rand().time()),0,10);
}
/**
* @brief deletes a card with the data provided by sabredav
* @param integer $aid Addressbook id
* @param string $uri the uri of the card
* @return boolean
*/
public static function deleteCardFromDAVData($aid,$uri){
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_cards WHERE addressbookid = ? AND uri=?' );
$stmt->execute(array($aid,$uri));
return true;
}
/**
* @brief Escapes semicolons
* @param string $value
* @return string
*/
public static function escapeSemicolons($value){
foreach($value as &$i ){
$i = implode("\\\\;", explode(';', $i));
} unset($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 Data structure of vCard
* @param object $property
* @return associative array
*
* look at code ...
*/
public static function structureContact($object){
$details = array();
foreach($object->children as $property){
$temp = self::structureProperty($property);
if(array_key_exists($property->name,$details)){
$details[$property->name][] = $temp;
}
else{
$details[$property->name] = array($temp);
}
}
return $details;
}
/**
* @brief Data structure of properties
* @param object $property
* @return associative array
*
* returns an associative array with
* ['name'] name of property
* ['value'] htmlspecialchars escaped value of property
* ['parameters'] associative array name=>value
* ['checksum'] checksum of whole property
*/
public static function structureProperty($property){
$value = $property->value;
$value = htmlspecialchars($value);
if($property->name == 'ADR' || $property->name == 'N'){
$value = self::unescapeSemicolons($value);
}
$temp = array(
'name' => $property->name,
'value' => $value,
'parameters' => array(),
'checksum' => md5($property->serialize()));
foreach($property->parameters as $parameter){
// Faulty entries by kaddressbook
if($parameter->name == 'TYPE' && $parameter->value == 'PREF'){
$parameter->name = 'PREF';
$parameter->value = '1';
}
$temp['parameters'][$parameter->name] = $parameter->value;
}
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;
}
}
}

View File

@ -34,20 +34,25 @@ if( !OC_User::isLoggedIn()){
}
$card = OC_Contacts_Addressbook::findCard( $id );
$card = OC_Contacts_VCard::find( $id );
if( $card === false ){
echo $l10n->t('Can not find Contact!');
exit();
}
$addressbook = OC_Contacts_Addressbook::findAddressbook( $card['addressbookid'] );
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
echo $l10n->t('This is not your contact!');
exit();
}
$content = Sabre_VObject_Reader::read($card['carddata']);
$content = OC_Contacts_Card::parse($card['carddata']);
// invalid vcard
if( is_null($content)){
echo $l10n->t('This card is not RFC compatible!');
exit();
}
// Photo :-)
foreach($content->children as $child){
if($child->name == 'PHOTO'){