Changeset 4575 in subversion


Ignore:
Timestamp:
Feb 27, 2011 8:30:34 AM (2 years ago)
Author:
thomasb
Message:

Improve vcard import: map more fields, support photo urls, better UTF-16 charset detection

Location:
trunk/roundcubemail
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/rcube_vcard.php

    r4541 r4575  
    4141    'email'    => 'EMAIL', 
    4242    'address'  => 'ADR', 
     43    'jobtitle' => 'TITLE', 
    4344    'gender'      => 'X-GENDER', 
    4445    'maidenname'  => 'X-MAIDENNAME', 
     
    166167            } 
    167168          } 
     169 
     170          // force subtype if none set 
     171          if (preg_match('/^(email|phone|address|website)/', $key) && !$subtype) 
     172            $subtype = 'other'; 
    168173           
    169174          if ($subtype) 
     
    278283         
    279284      case 'photo': 
    280         $encoded = !preg_match('![^a-z0-9/=+-]!i', $value); 
    281         $this->raw['PHOTO'][0] = array(0 => $encoded ? $value : base64_encode($value), 'BASE64' => true); 
     285        if (strpos($value, 'http:') === 0) { 
     286            // TODO: fetch file from URL and save it locally? 
     287            $this->raw['PHOTO'][0] = array(0 => $value, 'URL' => true); 
     288        } 
     289        else { 
     290            $encoded = !preg_match('![^a-z0-9/=+-]!i', $value); 
     291            $this->raw['PHOTO'][0] = array(0 => $encoded ? $value : base64_encode($value), 'BASE64' => true); 
     292        } 
    282293        break; 
    283294         
     
    423434    // Convert special types (like Skype) to normal type='skype' classes with this simple regex ;) 
    424435    $vcard = preg_replace( 
    425       '/item(\d+)\.(TEL|URL)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./s', 
     436      '/item(\d+)\.(TEL|EMAIL|URL)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./s', 
    426437      '\2;type=\5\3:\4', 
     438      $vcard); 
     439 
     440    // convert Apple X-ABRELATEDNAMES into X-* fields for better compatibility 
     441    $vcard = preg_replace_callback( 
     442      '/item(\d+)\.(X-ABRELATEDNAMES)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./s', 
     443      array('self', 'x_abrelatednames_callback'), 
    427444      $vcard); 
    428445 
     
    440457 
    441458    return $vcard; 
     459  } 
     460   
     461  private static function x_abrelatednames_callback($matches) 
     462  { 
     463    return 'X-' . strtoupper($matches[5]) . $matches[3] . ':'. $matches[4]; 
    442464  } 
    443465 
     
    632654    if (substr($string, 0, 3) == "\xEF\xBB\xBF") return 'UTF-8'; 
    633655 
     656    // heuristics 
     657    if ($string[0] == "\0" && $string[1] == "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-32BE'; 
     658    if ($string[0] != "\0" && $string[1] == "\0" && $string[2] == "\0" && $string[3] == "\0") return 'UTF-32LE'; 
     659    if ($string[0] == "\0" && $string[1] != "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-16BE'; 
     660    if ($string[0] != "\0" && $string[1] == "\0" && $string[2] != "\0" && $string[3] == "\0") return 'UTF-16LE'; 
     661 
    634662    // use mb_detect_encoding() 
    635663    $encodings = array('UTF-8', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 
  • trunk/roundcubemail/program/steps/addressbook/func.inc

    r4527 r4575  
    366366                    if (!$coltypes[$col]) 
    367367                        continue; 
     368                     
     369                    // only string values are expected here 
     370                    if (is_array($record[$col])) 
     371                        $record[$col] = join(' ', $record[$col]); 
    368372 
    369373                    if ($RCMAIL->action == 'show') { 
     
    564568        $RCMAIL->output->set_env('photo_placeholder', $photo_img); 
    565569         
    566         if ($record['photo']) 
     570        if (strpos($record['photo'], 'http:') === 0) 
     571            $photo_img = $record['photo']; 
     572        else if ($record['photo']) 
    567573            $photo_img = $RCMAIL->url(array('_action' => 'photo', '_cid' => $record['ID'], '_source' => $_REQUEST['_source'])); 
    568574        $img = html::img(array('src' => $photo_img, 'border' => 1, 'alt' => '')); 
  • trunk/roundcubemail/program/steps/addressbook/import.inc

    r4484 r4575  
    139139      $email = rcube_idn_to_utf8($email); 
    140140       
    141       if (!$replace) { 
     141      if (!$replace && $email) { 
    142142        // compare e-mail address 
    143143        $existing = $CONTACTS->search('email', $email, false, false); 
  • trunk/roundcubemail/tests/vcards.php

    r4393 r4575  
    5555  } 
    5656   
     57  function test_encodings() 
     58  { 
     59      $input = file_get_contents($this->_srcpath('utf-16_sample.vcf')); 
     60       
     61      $vcards = rcube_vcard::import($input); 
     62      $this->assertEqual("ÇŒgean ÄœdaMonté", $vcards[0]->displayname, "Decoded from UTF-16"); 
     63  } 
     64   
    5765} 
Note: See TracChangeset for help on using the changeset viewer.