Skip to content

Commit

Permalink
Add 'lib/vCard-parser/' from commit 'cada6640638bb13c8c070de8c834325e…
Browse files Browse the repository at this point in the history
…dc5e4aab'

git-subtree-dir: lib/vCard-parser
git-subtree-mainline: a1d667e
git-subtree-split: cada664
  • Loading branch information
jens-maus committed Sep 6, 2015
2 parents a1d667e + cada664 commit 27b0c59
Show file tree
Hide file tree
Showing 9 changed files with 1,074 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/vCard-parser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
Downloads/*
*.vcf
24 changes: 24 additions & 0 deletions lib/vCard-parser/Example.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BEGIN:VCARD
VERSION:3.0
N:Doe;John;Q.,Public
FN;CHARSET=UTF-8:John Doe
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
URL:https://www.google.com/
PHOTO;VALUE=URL;TYPE=PNG:http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Example_svg.svg/200px-Example_svg.svg.png
AGENT:BEGIN:VCARD
VERSION:3.0
N:Doe;John;Q.,Public
FN:John Doe
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
EMAIL;TYPE=INTERNET:example@example.com
PHOTO;VALUE=URL;TYPE=PNG:http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Example_svg.svg/200px-Example_svg.svg.png
END:VCARD
END:VCARD
15 changes: 15 additions & 0 deletions lib/vCard-parser/Example2.1.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:100 Waters Edge=0D=0ABaytown, LA 30314=0D=0AUnited States of America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:42 Plantation St.=0D=0ABaytown, LA 30314=0D=0AUnited States of America
EMAIL;PREF;INTERNET;HOME:forrestgump@example.com
REV:20080424T195243Z
END:VCARD
36 changes: 36 additions & 0 deletions lib/vCard-parser/Example3.0.vcf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;VALUE=URL;TYPE=GIF:http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Example_svg.svg/200px-Example_svg.svg.png
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
AGENT;VALUE=uri:
CID:JQPUBLIC.part3.960129T083020.xyzMail@host3.com
AGENT:BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;VALUE=URL;TYPE=GIF:http://upload.wikimedia.org/wikipedia/commons/thumb/a/a5/Example_svg.svg/200px-Example_svg.svg.png
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
TEL;TYPE=HOME,TYPE=VOICE:(404) 555-1213
ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
REV:20080424T195243Z
END:VCARD
REV:20080424T195243Z
END:VCARD
20 changes: 20 additions & 0 deletions lib/vCard-parser/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License

Copyright (C) 2012 Nuovo

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48 changes: 48 additions & 0 deletions lib/vCard-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Nuovo/Nouveau vCard-parser is a simple vCard file parser with the focus on ease of use.

The parser was written mostly because I couldn't find one that I was satisfied with - all those that I tried either failed with real world data or were too unwieldy or inconvenient, hence this parser.

The parser can read both single and multiple vCards from a single file and with the help of PHP's magic methods and interfaces it can be written concisely. For example:

include('vCard.php');
$vCard = new vCard('Example3.0.vcf');

Get the number of vCards in the file

echo count($vCard);

In the single-vCard mode every element is accessible directly.

if (count($vCard) == 1)
{
print_r($vCard -> n);
print_r($vCard -> tel);
}

In the multiple-vCard mode the object can be used as an array to retrieve separate vCard objects for each vCard in the file.

else
{
foreach ($vCard as $vCardPart)
{
print_r($vCardPart -> n);
print_r($vCardPart -> tel);
}
}

Every vCard element is accessible as an object member by the vCard element name. Every element is an array with the data parsed out of the file.
It is possible to specify an option to the vCard constructor that will let you access every element as a single value in cases where there is just one value, e.g.:

$vCard = new vCard('Example3.0.vcf', false, array('Collapse' => true));

More on usage in [the wiki](https://github.com/nuovo/vCard-parser/wiki)

See also:
* http://tools.ietf.org/html/rfc2425 - A MIME Content-Type for Directory Information
* http://tools.ietf.org/html/rfc2426 - vCard MIME directory profile
* http://tools.ietf.org/html/rfc4770 - vCard Extensions for Instant Messaging (IM)

TODOs planned:
* Add support for non-standard ("X-...") elements;

http://www.nuovo.lv
216 changes: 216 additions & 0 deletions lib/vCard-parser/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
body
{
font-family: Corbel, Arial, sans-serif;
padding: 20px 50px;
}
div.Agent
{
padding: 20px;
border: 1px solid #ddd;
background-color: #fafafa;
}
img
{
float: right;
margin: 10px;
padding: 10px;
border: 1px solid #ddd;
}
</style>
</head>
<body>

<?php
require_once('vCard.php');

/**
* Test function for vCard content output
* @param vCard vCard object
*/
function OutputvCard(vCard $vCard)
{
echo '<h2>'.$vCard -> FN[0].'</h2>';

if ($vCard -> PHOTO)
{
foreach ($vCard -> PHOTO as $Photo)
{
if ($Photo['encoding'] == 'b')
{
echo '<img src="data:image/'.$Photo['type'][0].';base64,'.$Photo['value'].'" /><br />';
}
else
{
echo '<img src="'.$Photo['value'].'" /><br />';
}

/*
// It can also be saved to a file
try
{
$vCard -> SaveFile('photo', 0, 'test_image.jpg');
// The parameters are:
// - name of the file we want to save (photo, logo or sound)
// - index of the file in case of multiple files (defaults to 0)
// - target path to save to, including the filenam
}
catch (Exception $E)
{
// Target path not writable
}
*/
}
}

foreach ($vCard -> N as $Name)
{
echo '<h3>Name: '.$Name['firstname'].' '.$Name['lastname'].'</h3>';
}

foreach ($vCard -> ORG as $Organization)
{
echo '<h3>Organization: '.$Organization['name'].
($Organization['unit1'] || $Organization['unit2'] ?
' ('.implode(', ', array($Organization['unit1'], $Organization['unit2'])).')' :
''
).'</h3>';
}

if ($vCard -> TEL)
{
echo '<p><h4>Phone</h4>';
foreach ($vCard -> TEL as $Tel)
{
if (is_scalar($Tel))
{
echo $Tel.'<br />';
}
else
{
echo $Tel['value'].' ('.implode(', ', $Tel['type']).')<br />';
}
}
echo '</p>';
}

if ($vCard -> EMAIL)
{
echo '<p><h4>Email</h4>';
foreach ($vCard -> EMAIL as $Email)
{
if (is_scalar($Email))
{
echo $Email;
}
else
{
echo $Email['value'].' ('.implode(', ', $Email['type']).')<br />';
}
}
echo '</p>';
}

if ($vCard -> URL)
{
echo '<p><h4>URL</h4>';
foreach ($vCard -> URL as $URL)
{
if (is_scalar($URL))
{
echo $URL.'<br />';
}
else
{
echo $URL['value'].'<br />';
}
}
echo '</p>';
}

if ($vCard -> IMPP)
{
echo '<p><h4>Instant messaging</h4>';
foreach ($vCard -> IMPP as $IMPP)
{
if (is_scalar($IMPP))
{
echo $IMPP.'<br />';
}
else
{
echo $IMPP['value'].'<br/ >';
}
}
echo '</p>';
}

if ($vCard -> ADR)
{
foreach ($vCard -> ADR as $Address)
{
echo '<p><h4>Address ('.implode(', ', $Address['type']).')</h4>';
echo 'Street address: <strong>'.($Address['streetaddress'] ? $Address['streetaddress'] : '-').'</strong><br />'.
'PO Box: <strong>'.($Address['pobox'] ? $Address['pobox'] : '-').'</strong><br />'.
'Extended address: <strong>'.($Address['extendedaddress'] ? $Address['extendedaddress'] : '-').'</strong><br />'.
'Locality: <strong>'.($Address['locality'] ? $Address['locality'] : '-').'</strong><br />'.
'Region: <strong>'.($Address['region'] ? $Address['region'] : '-').'</strong><br />'.
'ZIP/Post code: <strong>'.($Address['postalcode'] ? $Address['postalcode'] : '-').'</strong><br />'.
'Country: <strong>'.($Address['country'] ? $Address['country'] : '-').'</strong>';
}
echo '</p>';
}

if ($vCard -> AGENT)
{
echo '<h4>Agents</h4>';
foreach ($vCard -> AGENT as $Agent)
{
if (is_scalar($Agent))
{
echo '<div class="Agent">'.$Agent.'</div>';
}
elseif (is_a($Agent, 'vCard'))
{
echo '<div class="Agent">';
OutputvCard($Agent);
echo '</div>';
}
}
}
}

$vCard = new vCard(
'Example3.0.vcf', // Path to vCard file
false, // Raw vCard text, can be used instead of a file
array( // Option array
// This lets you get single values for elements that could contain multiple values but have only one value.
// This defaults to false so every value that could have multiple values is returned as array.
'Collapse' => false
)
);

if (count($vCard) == 0)
{
throw new Exception('vCard test: empty vCard!');
}
// if the file contains a single vCard, it is accessible directly.
elseif (count($vCard) == 1)
{
OutputvCard($vCard);
}
// if the file contains multiple vCards, they are accessible as elements of an array
else
{
foreach ($vCard as $Index => $vCardPart)
{
OutputvCard($vCardPart);
}
}
?>
</body>
</html>
Loading

0 comments on commit 27b0c59

Please sign in to comment.