Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.UsercardVcard.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Class use to import and export VCARD format with FREEDOM USER Family
9  *
10  * @author Anakeen 2001
11  * @version \$Id: Class.UsercardVcard.php,v 1.16 2005/11/23 14:04:20 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage USERCARD
15  */
16 /**
17  * Class use to read, import and export VCARD format with FREEDOM USER Family
18  *
19  */
21 {
22  var $import = array(
23  "FN" => "",
24 
25  "N" => "US_LNAME;US_FNAME",
26  "N;GIVEN" => "US_FNAME",
27  "N;FAMILY" => "US_LNAME",
28  "N;MIDDLE" => "",
29  "N;PREFIX" => "",
30  "N;SUFFIX" => "",
31  "SOUND" => "",
32  "BDAY" => "",
33  "NOTE" => "",
34  "TZ" => "",
35  "GEO" => "",
36  "URL" => "US_WORKWEB",
37  "URL;WORK" => "US_WORKWEB",
38  "PUBKEY" => "",
39  "ORG" => "US_SOCIETY;US_UNIT",
40  "ORG;NAME" => "US_SOCIETY",
41  "ORG;UNIT" => "",
42  "TITLE" => "US_TYPE",
43 
44  "ADR;TYPE;WORK" => "",
45  "ADR;TYPE;HOME" => "",
46  "TEL;PREFER" => "",
47  "EMAIL;INTERNET" => "US_MAIL",
48  "EMAIL;INTERNET;WORK" => "US_MAIL",
49  "EMAIL;PREF;INTERNET" => "US_MAIL",
50  "EMAIL;INTERNET;HOME" => "",
51 
52  "ADR;WORK" => "0;0;US_WORKADDR;US_WORKTOWN;0;US_WORKPOSTALCODE;US_COUNTRY",
53  "ADR;WORK;STREET" => "US_WORKADDR",
54  "ADR;WORK;LOCALITY" => "US_WORKTOWN",
55  "ADR;WORK;REGION" => "",
56  "ADR;WORK;POSTALCODE" => "US_WORKPOSTALCODE",
57  "ADR;WORK;COUNTRYNAME" => "US_COUNTRY",
58  "EXT" => "",
59  "LABEL" => "",
60 
61  "ADR;HOME" => "0;0;320;325;0;322",
62  "ADR;HOME;STREET" => "320",
63  "ADR;HOME;LOCALITY" => "325",
64  "ADR;HOME;REGION" => "",
65  "ADR;HOME;POSTALCODE" => "322",
66  "ADR;HOME;COUNTRYNAME" => "",
67 
68  "TEL;WORK" => "US_PPHONE",
69  "TEL;WORK;VOICE" => "US_PPHONE",
70  "TEL;HOME" => "",
71  "TEL;VOICE" => "",
72  "TEL;FAX" => "US_PFAX",
73  "TEL;WORK;FAX" => "US_PFAX",
74  "TEL;MSG" => "",
75  "TEL;CELL" => "US_MOBILE",
76  "TEL;CELL;VOICE" => "US_MOBILE",
77  "TEL;PAGER" => "",
78  "TEL;BBS" => "",
79  "TEL;MODEM" => "",
80  "TEL;CAR" => "",
81  "TEL;ISDN" => "",
82  "TEL;VIDEO" => "",
83  "EMAIL;WORK" => "",
84  "EMAIL;HOME" => ""
85  );
86 
87  var $mime_type = "text/x-vcard";
88  var $ext = "vcf";
89  // --------------------------------------------------------------------
90  function Open($filename, $mode = "r")
91  {
92  // Open import/export file : return file descriptor
93  $this->fd = fopen($filename, $mode);
94  return ($this->fd);
95  }
96 
97  function Close()
98  {
99  // Close import file
100  if ($this->fd) fclose($this->fd);
101  }
102  function ReadCard(&$tattr)
103  {
104  // Read a structure of import file : return array ('name', 'value')
105  $tattr = array();
106  $endCardFound = false;
107  $beginCardFound = false;
108  $line = "";
109  // search begin of a card : BEGIN:VCARD
110  while ((!feof($this->fd)) && (!$beginCardFound)) {
111  $line = fgets($this->fd, 4096);
112  $beginCardFound = preg_match("/BEGIN:VCARD(.*)/", $line);
113  }
114  // search element of a card until : END:VCARD
115  while ((!feof($this->fd)) && (!$endCardFound)) {
116  $line = fgets($this->fd, 4096);
117  $endCardFound = preg_match("/END:VCARD(.*)/", $line);
118  if (!$endCardFound) {
119  if (preg_match("/([A-Z;]*);ENCODING=QUOTED-PRINTABLE:(.*)/", $line, $reg)) {
120  $tattr[$reg[1]] = utf8_encode(quoted_printable_decode(rtrim($reg[2])));
121  } elseif (preg_match("/([A-Z;]*);CHARSET=UTF-8:(.*)/", $line, $reg)) {
122  $tattr[$reg[1]] = rtrim($reg[2]);
123  } elseif (preg_match("/([A-Z;]*):(.*)/", $line, $reg)) {
124  //line like TEL;WORK:05.61.15.54.54
125  $tattr[$reg[1]] = str_replace("\\n", "\n", rtrim($reg[2]));
126  }
127  }
128  }
129 
130  return (!feof($this->fd));
131  }
132  // --------------------------------------------------------------------
133  function Read(&$tattr)
134  {
135  // Read import file : return attribute object
136  if ($cr = $this->ReadCard($tbrut)) {
137 
138  $tattr = array();
139  while (list($k, $v) = each($tbrut)) {
140 
141  if (isset($this->import[$k]) && ($this->import[$k] != "")) {
142  // if is single value (no regexp)
143  if (preg_match("/^[0-9A-Z_]*$/", $this->import[$k])) {
144  // suppress http
145  if ($this->import[$k] == "US_WORKWEB") $tattr[$this->import[$k]] = str_replace("http://", "", $v);
146  else $tattr[$this->import[$k]] = $v;
147  } else {
148  // regexp case
149  // example A;B;C;D;E;F
150  $complxreg = "/([^;]*)[;]{0,1}([^;]*)[;]{0,1}([^;]*)[;]{0,1}([^;]*)[;]{0,1}([^;]*)[;]{0,1}([^;]*)[;]{0,1}/";
151 
152  if (preg_match($complxreg, $this->import[$k], $reg)) {
153  if (preg_match($complxreg, $v, $regv)) {
154  for ($ir = 1; $ir < 7; $ir++) {
155  if ($reg[$ir] == "US_WORKWEB") $tattr[$reg[$ir]] = str_replace("http://", "", $regv[$ir]);
156  else $tattr[$reg[$ir]] = $regv[$ir];
157  }
158  }
159  }
160  }
161  }
162  }
163  }
164  return ($cr);
165  }
166  function WriteCard($title, $tattr)
167  {
168  // Write a structure in export file
169  fputs($this->fd, "BEGIN:VCARD\n");
170  fputs($this->fd, "FN:" . chop($title) . "\n");
171  reset($this->import);
172 
173  while (list($k, $v) = each($this->import)) {
174  if ($v != "") {
175  $v = strtolower($v);
176  if (isset($tattr[$v])) fputs($this->fd, $k . ":" . str_replace("\n", "\\n", $tattr[$v]) . "\n");
177  else { // multi fields
178  $lidattr = explode(";", $v);
179  if ((is_array($lidattr)) && (count($lidattr) > 1)) {
180  fputs($this->fd, "$k:");
181  while (list($k2, $idattr) = each($lidattr)) {
182 
183  if (isset($tattr[$idattr])) fputs($this->fd, str_replace("\n", "\\n", $tattr[$idattr]));
184  if ($k2 < count($lidattr) - 1) fputs($this->fd, ";");
185  }
186  fputs($this->fd, "\n");
187  }
188  }
189  }
190  }
191  fputs($this->fd, "END:VCARD\n\n");
192  }
193 }
194 ?>
← centre documentaire © anakeen - published under CC License - Dynacase