1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-29 11:24:11 +01:00

Don't import cards that can't be parsed by Sabre, but log it instead.

Fix missing 'N' or 'FN' fields on import.
This commit is contained in:
Thomas Tanghus 2012-02-08 07:56:43 +01:00
parent f1f05f9f2f
commit ab6a5fe76d
4 changed files with 55 additions and 32 deletions

View File

@ -36,8 +36,11 @@ OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$l=new OC_L10N('contacts'); $l=new OC_L10N('contacts');
$id = $_GET['id']; $id = isset($_GET['id'])?$_GET['id']:null;
$vcard = OC_Contacts_App::getContactVCard( $id ); $vcard = OC_Contacts_App::getContactVCard( $id );
if(is_null($id)) {
bailOut($l->t('Missing ID'));
}
if(is_null($vcard)) { if(is_null($vcard)) {
bailOut($l->t('Error parsing VCard for ID: "'.$id.'"')); bailOut($l->t('Error parsing VCard for ID: "'.$id.'"'));
} }

View File

@ -236,6 +236,7 @@ Contacts={
delete:function() { delete:function() {
$('#contacts_deletecard').tipsy('hide'); $('#contacts_deletecard').tipsy('hide');
$.getJSON('ajax/deletecard.php',{'id':this.id},function(jsondata){ $.getJSON('ajax/deletecard.php',{'id':this.id},function(jsondata){
console.log('Card.delete: ' + this.id);
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
$('#leftcontent [data-id="'+jsondata.data.id+'"]').remove(); $('#leftcontent [data-id="'+jsondata.data.id+'"]').remove();
$('#rightcontent').data('id',''); $('#rightcontent').data('id','');
@ -1055,6 +1056,7 @@ $(document).ready(function(){
*/ */
$('#leftcontent li').live('click',function(){ $('#leftcontent li').live('click',function(){
var id = $(this).data('id'); var id = $(this).data('id');
console.log('Contact ' + id + ' clicked.');
var oldid = $('#rightcontent').data('id'); var oldid = $('#rightcontent').data('id');
if(oldid != 0){ if(oldid != 0){
$('#leftcontent li[data-id="'+oldid+'"]').removeClass('active'); $('#leftcontent li[data-id="'+oldid+'"]').removeClass('active');
@ -1094,32 +1096,32 @@ $(document).ready(function(){
/** /**
* Add and insert a new contact into the list. NOTE: Deprecated * Add and insert a new contact into the list. NOTE: Deprecated
*/ */
$('#contacts_addcardform input[type="submit"]').live('click',function(){ // $('#contacts_addcardform input[type="submit"]').live('click',function(){
$.post('ajax/addcontact.php',$('#contact_identity').serialize(),function(jsondata){ // $.post('ajax/addcontact.php',$('#contact_identity').serialize(),function(jsondata){
if(jsondata.status == 'success'){ // if(jsondata.status == 'success'){
$('#rightcontent').data('id',jsondata.data.id); // $('#rightcontent').data('id',jsondata.data.id);
$('#rightcontent').html(jsondata.data.page); // $('#rightcontent').html(jsondata.data.page);
$('#leftcontent .active').removeClass('active'); // $('#leftcontent .active').removeClass('active');
var item = '<li data-id="'+jsondata.data.id+'" class="active"><a href="index.php?id='+jsondata.data.id+'" style="background: url(thumbnail.php?id='+jsondata.data.id+') no-repeat scroll 0% 0% transparent;">'+jsondata.data.name+'</a></li>'; // var item = '<li data-id="'+jsondata.data.id+'" class="active"><a href="index.php?id='+jsondata.data.id+'" style="background: url(thumbnail.php?id='+jsondata.data.id+') no-repeat scroll 0% 0% transparent;">'+jsondata.data.name+'</a></li>';
var added = false; // var added = false;
$('#leftcontent ul li').each(function(){ // $('#leftcontent ul li').each(function(){
if ($(this).text().toLowerCase() > jsondata.data.name.toLowerCase()) { // if ($(this).text().toLowerCase() > jsondata.data.name.toLowerCase()) {
$(this).before(item).fadeIn('fast'); // $(this).before(item).fadeIn('fast');
added = true; // added = true;
return false; // return false;
} // }
}); // });
if(!added) { // if(!added) {
$('#leftcontent ul').append(item); // $('#leftcontent ul').append(item);
} // }
} // }
else{ // else{
Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message); // Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
//alert(jsondata.data.message); // //alert(jsondata.data.message);
} // }
}, 'json'); // }, 'json');
return false; // return false;
}); // });
$('#contacts li').bind('inview', function(event, isInView, visiblePartX, visiblePartY) { $('#contacts li').bind('inview', function(event, isInView, visiblePartX, visiblePartY) {
if (isInView) { //NOTE: I've kept all conditions for future reference ;-) if (isInView) { //NOTE: I've kept all conditions for future reference ;-)

View File

@ -58,7 +58,7 @@ class OC_Contacts_App{
public static function getContactObject($id){ public static function getContactObject($id){
$card = OC_Contacts_VCard::find( $id ); $card = OC_Contacts_VCard::find( $id );
if( $card === false ){ if( $card === false ){
OC_JSON::error(array('data' => array( 'message' => self::$l10n->t('Contact could not be found.')))); OC_JSON::error(array('data' => array( 'message' => self::$l10n->t('Contact could not be found.').' '.$id)));
exit(); exit();
} }

View File

@ -99,7 +99,7 @@ class OC_Contacts_VCard{
* @brief Adds a card * @brief Adds a card
* @param integer $id Addressbook id * @param integer $id Addressbook id
* @param string $data vCard file * @param string $data vCard file
* @return insertid * @return insertid on success or null if card is not parseable.
*/ */
public static function add($id,$data){ public static function add($id,$data){
$fn = null; $fn = null;
@ -107,6 +107,22 @@ class OC_Contacts_VCard{
$card = OC_VObject::parse($data); $card = OC_VObject::parse($data);
if(!is_null($card)){ if(!is_null($card)){
$fn = $card->getAsString('FN'); $fn = $card->getAsString('FN');
if(!$fn){ // Fix missing 'FN' field.
$n = $card->getAsString('N');
if(!is_null($n)){
$fn = join(' ', array_reverse(array_slice(explode(';', $n), 0, 2)));
$card->setString('FN', $fn);
OC_Log::write('contacts','OC_Contacts_VCard::add. Added missing \'FN\' field: '.$fn,OC_Log::DEBUG);
} else {
$fn = 'Unknown Name';
}
}
$n = $card->getAsString('N');
if(!$n){ // Fix missing 'N' field.
$n = implode(';', array_reverse(array_slice(explode(' ', $fn), 0, 2))).';;;';
$card->setString('N', $n);
OC_Log::write('contacts','OC_Contacts_VCard::add. Added missing \'N\' field: '.$n,OC_Log::DEBUG);
}
$uid = $card->getAsString('UID'); $uid = $card->getAsString('UID');
if(is_null($uid)){ if(is_null($uid)){
$card->setUID(); $card->setUID();
@ -137,8 +153,10 @@ class OC_Contacts_VCard{
} }
else{ else{
// that's hard. Creating a UID and not saving it // that's hard. Creating a UID and not saving it
$uid = self::createUID(); OC_Log::write('contacts','OC_Contacts_VCard::add. Error parsing VCard: '.$data,OC_Log::ERROR);
$uri = $uid.'.vcf'; return null; // Ditch cards that can't be parsed by Sabre.
//$uid = self::createUID();
//$uri = $uid.'.vcf';
}; };
$stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' ); $stmt = OC_DB::prepare( 'INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)' );
@ -175,7 +193,7 @@ class OC_Contacts_VCard{
if($email) { if($email) {
$fn = $email; $fn = $email;
} else { } else {
$fn = 'Unknown'; $fn = 'Unknown Name';
} }
$card->addProperty('FN', $fn); $card->addProperty('FN', $fn);
$data = $card->serialize(); $data = $card->serialize();