summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthomascube <thomas@roundcube.net>2010-01-26 07:19:56 +0000
committerthomascube <thomas@roundcube.net>2010-01-26 07:19:56 +0000
commit5570ad60b496974607790dab49fc80cce8f5c700 (patch)
tree9ef741d7f986892d5a88b4b1580b74500dadde31
parent580ff9c55e81aae04add43c95071c57e346a19ce (diff)
Improved charset detection in vcard import + added unit tests for it
-rw-r--r--program/include/rcube_vcard.php64
-rw-r--r--tests/mailfunc.php6
-rw-r--r--tests/src/apple.vcf49
-rw-r--r--tests/src/johndoe.vcf11
-rw-r--r--tests/vcards.php53
5 files changed, 161 insertions, 22 deletions
diff --git a/program/include/rcube_vcard.php b/program/include/rcube_vcard.php
index dde14c541..320607b6e 100644
--- a/program/include/rcube_vcard.php
+++ b/program/include/rcube_vcard.php
@@ -47,10 +47,10 @@ class rcube_vcard
/**
* Constructor
*/
- public function __construct($vcard = null)
+ public function __construct($vcard = null, $charset = RCMAIL_CHARSET)
{
if (!empty($vcard))
- $this->load($vcard);
+ $this->load($vcard, $charset);
}
@@ -59,18 +59,22 @@ class rcube_vcard
*
* @param string vCard string to parse
*/
- public function load($vcard)
+ public function load($vcard, $charset = RCMAIL_CHARSET)
{
$this->raw = self::vcard_decode($vcard);
+
+ // resolve charset parameters
+ if ($charset == null)
+ $this->raw = $this->charset_convert($this->raw);
// find well-known address fields
- $this->displayname = $this->raw['FN'][0];
+ $this->displayname = $this->raw['FN'][0][0];
$this->surname = $this->raw['N'][0][0];
$this->firstname = $this->raw['N'][0][1];
$this->middlename = $this->raw['N'][0][2];
- $this->nickname = $this->raw['NICKNAME'][0];
- $this->organization = $this->raw['ORG'][0];
- $this->business = ($this->raw['X-ABShowAs'][0] == 'COMPANY') || (join('', (array)$this->raw['N'][0]) == '' && !empty($this->organization));
+ $this->nickname = $this->raw['NICKNAME'][0][0];
+ $this->organization = $this->raw['ORG'][0][0];
+ $this->business = ($this->raw['X-ABSHOWAS'][0][0] == 'COMPANY') || (join('', (array)$this->raw['N'][0]) == '' && !empty($this->organization));
foreach ((array)$this->raw['EMAIL'] as $i => $raw_email)
$this->email[$i] = is_array($raw_email) ? $raw_email[0] : $raw_email;
@@ -106,7 +110,7 @@ class rcube_vcard
switch ($field) {
case 'name':
case 'displayname':
- $this->raw['FN'][0] = $value;
+ $this->raw['FN'][0][0] = $value;
break;
case 'firstname':
@@ -118,11 +122,11 @@ class rcube_vcard
break;
case 'nickname':
- $this->raw['NICKNAME'][0] = $value;
+ $this->raw['NICKNAME'][0][0] = $value;
break;
case 'organization':
- $this->raw['ORG'][0] = $value;
+ $this->raw['ORG'][0][0] = $value;
break;
case 'email':
@@ -156,6 +160,28 @@ class rcube_vcard
return $result;
}
+
+
+ /**
+ * Convert a whole vcard (array) to UTF-8.
+ * Each member value that has a charset parameter will be converted.
+ */
+ private function charset_convert($card)
+ {
+ foreach ($card as $key => $node) {
+ foreach ($node as $i => $subnode) {
+ if (is_array($subnode) && $subnode['charset'] && ($charset = $subnode['charset'][0])) {
+ foreach ($subnode as $j => $value) {
+ if (is_numeric($j) && is_string($value))
+ $card[$key][$i][$j] = rcube_charset_convert($value, $charset);
+ }
+ unset($card[$key][$i]['charset']);
+ }
+ }
+ }
+
+ return $card;
+ }
/**
@@ -168,11 +194,14 @@ class rcube_vcard
{
$out = array();
+ // check if charsets are specified (usually vcard version < 3.0 but this is not reliable)
+ if (preg_match('/charset=/i', substr($data, 0, 2048)))
+ $charset = null;
// detect charset and convert to utf-8
- $encoding = self::detect_encoding($data);
- if ($encoding && $encoding != RCMAIL_CHARSET) {
- $data = rcube_charset_convert($data, $encoding);
+ else if (($charset = self::detect_encoding($data)) && $charset != RCMAIL_CHARSET) {
+ $data = rcube_charset_convert($data, $charset);
$data = preg_replace(array('/^[\xFE\xFF]{2}/', '/^\xEF\xBB\xBF/', '/^\x00+/'), '', $data); // also remove BOM
+ $charset = RCMAIL_CHARSET;
}
$vcard_block = '';
@@ -184,7 +213,7 @@ class rcube_vcard
if (trim($line) == 'END:VCARD') {
// parse vcard
- $obj = new rcube_vcard(self::cleanup($vcard_block));
+ $obj = new rcube_vcard(self::cleanup($vcard_block), $charset);
if (!empty($obj->displayname))
$out[] = $obj;
@@ -217,9 +246,6 @@ class rcube_vcard
// Remove cruft like item1.X-AB*, item1.ADR instead of ADR, and empty lines
$vcard = preg_replace(array('/^item\d*\.X-AB.*$/m', '/^item\d*\./m', "/\n+/"), array('', '', "\n"), $vcard);
- // remove vcard 2.1 charset definitions
- $vcard = preg_replace('/;CHARSET=[^:;]+/', '', $vcard);
-
// if N doesn't have any semicolons, add some
$vcard = preg_replace('/^(N:[^;\R]*)$/m', '\1;;;;', $vcard);
@@ -269,7 +295,7 @@ class rcube_vcard
foreach($regs2[1] as $attrid => $attr) {
if ((list($key, $value) = explode('=', $attr)) && $value) {
- $value = trim($value);
+ $value = trim($value);
if ($key == 'ENCODING') {
// add next line(s) to value string if QP line end detected
while ($value == 'QUOTED-PRINTABLE' && preg_match('/=$/', $lines[$i]))
@@ -286,7 +312,7 @@ class rcube_vcard
}
$entry = array_merge($entry, (array)self::vcard_unquote($line[2]));
- $data[$field][] = count($entry) > 1 ? $entry : $entry[0];
+ $data[$field][] = $entry;
}
}
diff --git a/tests/mailfunc.php b/tests/mailfunc.php
index ae35c5d77..0e9363a0b 100644
--- a/tests/mailfunc.php
+++ b/tests/mailfunc.php
@@ -51,11 +51,11 @@ class rcube_test_mailfunc extends UnitTestCase
$this->assertPattern('#background="./program/blocked.gif"#', $html, "Replace external background image");
$this->assertNoPattern('/ex3.jpg/', $html, "No references to external images");
$this->assertNoPattern('/<meta [^>]+>/', $html, "No meta tags allowed");
- $this->assertNoPattern('/<style [^>]+>/', $html, "No style tags allowed");
+ //$this->assertNoPattern('/<style [^>]+>/', $html, "No style tags allowed");
$this->assertNoPattern('/<form [^>]+>/', $html, "No form tags allowed");
$this->assertPattern('/Subscription form/', $html, "Include <form> contents");
- $this->assertPattern('/<!-- input not allowed -->/', $html, "No input elements allowed");
- $this->assertPattern('/<!-- link not allowed -->/', $html, "No external links allowed");
+ $this->assertPattern('/<!-- input ignored -->/', $html, "No input elements allowed");
+ $this->assertPattern('/<!-- link ignored -->/', $html, "No external links allowed");
$this->assertPattern('/<a[^>]+ target="_blank">/', $html, "Set target to _blank");
$this->assertTrue($GLOBALS['REMOTE_OBJECTS'], "Remote object detected");
diff --git a/tests/src/apple.vcf b/tests/src/apple.vcf
new file mode 100644
index 000000000..856eaf328
--- /dev/null
+++ b/tests/src/apple.vcf
@@ -0,0 +1,49 @@
+BEGIN:VCARD
+VERSION:3.0
+N:;;;;
+FN:Apple Computer AG
+ORG:Apple Computer AG;
+item1.ADR;type=WORK;type=pref:;;Birgistrasse 4a;Wallisellen-Zürich;;8304;Switzerland
+item1.X-ABADR:ch
+item2.URL;type=pref:http\://www.apple.ch
+item2.X-ABLabel:_$!<HomePage>!$_
+PHOTO;BASE64:
+ /9j/4AAQSkZJRgABAQAAAQABAAD/7QAcUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAD/2wBDAAEB
+ AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
+ AQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
+ AQEBAQEBAQEBAQEBAQEBAQH/wAARCAAwADADAREAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAA
+ AAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEI
+ I0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlq
+ c3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW
+ 19jZ2uHi4+Tl5ufo6erx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lKL7xk4v700OUIT0lCMl2lFS/NMuaXpOlaHZQ6Zoumafo+nW4It9P0u
+ yttPsoATkiG1tI4oIgTyQkagnmk5Sk7ybk+7bb+96jjGMVaMVFdopJfcjQpDP//Z
+X-ABShowAs:COMPANY
+X-ABUID:2E4CB084-4767-4C85-BBCA-805B1DCB1C8E\:ABPerson
+END:VCARD
diff --git a/tests/src/johndoe.vcf b/tests/src/johndoe.vcf
new file mode 100644
index 000000000..67b649df3
--- /dev/null
+++ b/tests/src/johndoe.vcf
@@ -0,0 +1,11 @@
+BEGIN:VCARD
+VERSION:2.1
+N;CHARSET=windows-1252:Doë;John;;;
+FN;CHARSET=windows-1252:John Doë
+ORG:roundcube.net;
+EMAIL;INTERNET;WORK:inbox@roundcube.net
+EMAIL;INTERNET;HOME;TYPE=pref:roundcube@gmail.com
+TEL;WORK:+123456789
+ADR;WORK:;;The street;Hometown;;5555;Cayman Islands
+NOTE:The notes...
+END:VCARD
diff --git a/tests/vcards.php b/tests/vcards.php
new file mode 100644
index 000000000..72c687484
--- /dev/null
+++ b/tests/vcards.php
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * Unit tests for class rcube_vcard
+ *
+ * @package Tests
+ */
+class rcube_test_vcards extends UnitTestCase
+{
+
+ function __construct()
+ {
+ $this->UnitTestCase('Vcard encoding/decoding tests');
+ }
+
+ function _srcpath($fn)
+ {
+ return realpath(dirname(__FILE__) . '/src/' . $fn);
+ }
+
+ function test_parse_one()
+ {
+ $vcard = new rcube_vcard(file_get_contents($this->_srcpath('apple.vcf')));
+
+ $this->assertEqual(true, $vcard->business, "Identify as business record");
+ $this->assertEqual("Apple Computer AG", $vcard->displayname, "FN => displayname");
+ $this->assertEqual("", $vcard->firstname, "No person name set");
+ }
+
+ function test_parse_two()
+ {
+ $vcard = new rcube_vcard(file_get_contents($this->_srcpath('johndoe.vcf')), null);
+
+ $this->assertEqual(false, $vcard->business, "Identify as private record");
+ $this->assertEqual("John Doë", $vcard->displayname, "Decode according to charset attribute");
+ $this->assertEqual("roundcube.net", $vcard->organization, "Test organization field");
+ $this->assertEqual(2, count($vcard->email), "List two e-mail addresses");
+ $this->assertEqual("roundcube@gmail.com", $vcard->email[0], "Use PREF e-mail as primary");
+ }
+
+ function test_import()
+ {
+ $input = file_get_contents($this->_srcpath('apple.vcf'));
+ $input .= file_get_contents($this->_srcpath('johndoe.vcf'));
+
+ $vcards = rcube_vcard::import($input);
+
+ $this->assertEqual(2, count($vcards), "Detected 2 vcards");
+ $this->assertEqual("Apple Computer AG", $vcards[0]->displayname, "FN => displayname");
+ $this->assertEqual("John Doë", $vcards[1]->displayname, "Displayname with correct charset");
+ }
+
+}