root/afridex/vcard.php @ 8

Revision 1, 5.5 kB (checked in by admin, 18 years ago)
Line 
1<?
2function encode($string) {
3        return escape(quoted_printable_encode($string));
4}
5
6function escape($string) {
7        return str_replace(";","\;",$string);
8}
9
10// taken from PHP documentation comments
11function quoted_printable_encode($input, $line_max = 76) {
12        $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
13        $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
14        $eol = "\r\n";
15        $linebreak = "=0D=0A";
16        $escape = "=";
17        $output = "";
18
19        for ($j=0;$j<count($lines);$j++) {
20                $line = $lines[$j];
21                $linlen = strlen($line);
22                $newline = "";
23                for($i = 0; $i < $linlen; $i++) {
24                        $c = substr($line, $i, 1);
25                        $dec = ord($c);
26                        if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
27                                $c = "=20"; 
28                        } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
29                                $h2 = floor($dec/16); $h1 = floor($dec%16); 
30                                $c = $escape.$hex["$h2"].$hex["$h1"]; 
31                        }
32                        if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
33                                $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
34                                $newline = "    ";
35                        }
36                        $newline .= $c;
37                } // end of for
38                $output .= $newline;
39                if ($j<count($lines)-1) $output .= $linebreak;
40        }
41        return trim($output);
42}
43
44class vCard {
45        var $properties;
46        var $filename;
47       
48        function setPhoneNumber($number, $type="") {
49        // type may be PREF | WORK | HOME | VOICE | FAX | MSG | CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO or any senseful combination, e.g. "PREF;WORK;VOICE"
50                $key = "TEL";
51                if ($type!="") $key .= ";".$type;
52                $key.= ";ENCODING=QUOTED-PRINTABLE";
53                $this->properties[$key] = quoted_printable_encode($number);
54        }
55       
56        // UNTESTED !!!
57        function setPhoto($type, $photo) { // $type = "GIF" | "JPEG"
58                $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
59        }
60       
61        function setFormattedName($name) {
62                $this->properties["FN"] = quoted_printable_encode($name);
63        }
64
65        function setShow($show) {
66                $this->properties["X-ABShowAS"] = quoted_printable_encode($show);
67        }
68       
69        function setName($family="", $first="", $additional="", $prefix="", $suffix="") {
70                $this->properties["N"] = "$family;$first;$additional;$prefix;$suffix";
71        }
72
73        function setOrganization($org) { // $date format is YYYY-MM-DD
74                $this->properties["ORG"] = $org;
75                $this->filename = "$org.vcf";
76        }
77       
78        function setBirthday($date) { // $date format is YYYY-MM-DD
79                $this->properties["BDAY"] = $date;
80        }
81       
82        function setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL") {
83        // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
84                $key = "ADR";
85                if ($type!="") $key.= ";$type";
86                $key.= ";ENCODING=QUOTED-PRINTABLE";
87                $this->properties[$key] = encode($name).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
88               
89                if ($this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"] == "") {
90                        //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
91                }
92        }
93       
94        function setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL") {
95                $label = "";
96                if ($postoffice!="") $label.= "$postoffice\r\n";
97                if ($extended!="") $label.= "$extended\r\n";
98                if ($street!="") $label.= "$street\r\n";
99                if ($zip!="") $label.= "$zip ";
100                if ($city!="") $label.= "$city\r\n";
101                if ($region!="") $label.= "$region\r\n";
102                if ($country!="") $country.= "$country\r\n";
103               
104                $this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"] = quoted_printable_encode($label);
105        }
106       
107        function setEmail($address) {
108                $this->properties["EMAIL;INTERNET"] = $address;
109        }
110       
111        function setNote($note) {
112                $this->properties["NOTE;ENCODING=QUOTED-PRINTABLE"] = quoted_printable_encode($note);
113        }
114       
115        function setURL($url, $type="work") {
116        // $type may be WORK | HOME
117                $key = "URL";
118                if ($type!="") $key.= ";$type";
119                $this->properties[$key] = $url;
120        }
121       
122        function getVCard() {
123                $text = "BEGIN:VCARD\r\n";
124                $text.= "VERSION:2.1\r\n";
125                foreach($this->properties as $key => $value) {
126                        $text.= "$key:$value\r\n";
127                }
128                $text.= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
129                $text.= "";
130                $text.= "END:VCARD\r\n";
131                return $text;
132        }
133       
134        //writes filename displayed on download
135       
136        function getFileName() {
137                return $this->filename;
138        }
139}
140
141//  function runVcard() {
142//  USAGE EXAMPLE
143$org1 = get_post_meta($post->ID, "Company", true);
144$fn = get_post_meta($post->ID, "Firstname", true);
145$sn = get_post_meta($post->ID, "Surname", true);
146$off = get_post_meta($post->ID, "Office1", true);
147$adr = get_post_meta($post->ID, "Address1", true);
148$cou = get_post_meta($post->ID, "Country1", true);
149$cit = get_post_meta($post->ID, "City1", true);
150$eml = get_post_meta($post->ID, "Email1", true);
151$des = get_post_meta($post->ID, "Company", true);
152$htp = get_post_meta($post->ID, "Website1", true);
153$phn = get_post_meta($post->ID, "Office1", true);
154$v = new vCard();
155$v->setPhoneNumber("$phn", "PREF;WORK;VOICE");
156$v->setName("$sn", "$fn", "", "");
157$v->setOrganization ("$org1");
158$v->setBirthday("");
159$v->setAddress("", "", "$adr", "$cit", "", "", "$cou");
160$v->setEmail("$eml");
161$v->setNote("$org1 vCard provided by Afridex.net");
162$v->setURL("$htp", "WORK");
163$v->setShow("COMPANY");
164
165// Get vCard
166$output = $v->getVCard();
167$filename = $v->getFileName();
168
169// Trick Browser into Automatic Download
170Header("Content-Disposition: attachment; filename=$filename");
171Header("Content-Length: ".strlen($output));
172Header("Connection: close");
173Header("Content-Type: text/x-vCard; name=$filename");
174
175echo $output; 
176
177// }
178 
179?>
Note: See TracBrowser for help on using the browser.