|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\VCard; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Helper class that bulds the string buffer for one contact in the VCard format. |
|
8
|
|
|
* |
|
9
|
|
|
* @package SKien-VCard |
|
10
|
|
|
* @since 1.0.4 |
|
11
|
|
|
* @version 1.0.4 |
|
12
|
|
|
* @author Stefanius <[email protected]> |
|
13
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
14
|
|
|
*/ |
|
15
|
|
|
class VCardContactWriter |
|
16
|
|
|
{ |
|
17
|
|
|
use VCardHelper; |
|
18
|
|
|
|
|
19
|
|
|
/** @var VCardContact the contact to write */ |
|
20
|
|
|
protected VCardContact $oContact; |
|
21
|
|
|
/** @var string internal buffer */ |
|
22
|
|
|
protected string $buffer = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a contactbuilder object. |
|
26
|
|
|
* @param VCardContact $oContact the contact to build |
|
27
|
|
|
*/ |
|
28
|
|
|
function __construct(VCardContact $oContact) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->oContact = $oContact; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Return internal string buffer. |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __toString() : string |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->buildData(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Build the contact as VCard compatible string. |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function buildData() : string |
|
47
|
|
|
{ |
|
48
|
|
|
$this->startBuffer(); |
|
49
|
|
|
|
|
50
|
|
|
$this->buffer .= $this->buildName(); |
|
51
|
|
|
$this->buffer .= $this->buildOrganization(); |
|
52
|
|
|
$this->buffer .= $this->buildAddresses(); |
|
53
|
|
|
$this->buffer .= $this->buildPhoneNumbers(); |
|
54
|
|
|
$this->buffer .= $this->buildMailaddresses(); |
|
55
|
|
|
$this->buffer .= $this->buildAdditionalData(); |
|
56
|
|
|
$this->buffer .= $this->buildCategories(); |
|
57
|
|
|
$this->buffer .= $this->buildPhoto(); |
|
58
|
|
|
|
|
59
|
|
|
$this->endBuffer(); |
|
60
|
|
|
|
|
61
|
|
|
return $this->buffer; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Create the start sequence. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function startBuffer() : void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->buffer = 'BEGIN:VCARD' . PHP_EOL; |
|
70
|
|
|
$this->buffer .= 'VERSION:3.0' . PHP_EOL; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Create the end sequence. |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function endBuffer() : void |
|
77
|
|
|
{ |
|
78
|
|
|
// END |
|
79
|
|
|
$this->buffer .= 'END:VCARD' . PHP_EOL; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Build all name relevant contact properties. |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function buildName() : string |
|
87
|
|
|
{ |
|
88
|
|
|
// name properties |
|
89
|
|
|
// family name; given name; additional name(s); honorific prefixes; honorific sufffixes |
|
90
|
|
|
$strName = $this->maskString($this->oContact->getLastName()) . ';'; |
|
91
|
|
|
$strName .= $this->maskString($this->oContact->getFirstName()) . ';'; |
|
92
|
|
|
$strName .= ';'; |
|
93
|
|
|
$strName .= $this->maskString($this->oContact->getPrefix()) . ';'; |
|
94
|
|
|
$strName .= $this->maskString($this->oContact->getSuffix()); |
|
95
|
|
|
|
|
96
|
|
|
$buffer = $this->buildProperty('N', $strName, false); |
|
97
|
|
|
$buffer .= $this->buildProperty('FN', $this->oContact->getFirstName() . ' ' . $this->oContact->getLastName()); |
|
98
|
|
|
$buffer .= $this->buildProperty('NICKNAME', $this->oContact->getNickName()); |
|
99
|
|
|
|
|
100
|
|
|
return $buffer; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Build all organization relevant contact properties. |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function buildOrganization() : string |
|
108
|
|
|
{ |
|
109
|
|
|
// organisation |
|
110
|
|
|
$strOrg = $this->maskString($this->oContact->getOrganisation()) . ';'; |
|
111
|
|
|
$strOrg .= $this->maskString($this->oContact->getSection()); |
|
112
|
|
|
|
|
113
|
|
|
$buffer = $this->buildProperty('ORG', $strOrg, false); |
|
114
|
|
|
$buffer .= $this->buildProperty('TITLE', $this->oContact->getPosition()); |
|
115
|
|
|
$buffer .= $this->buildProperty('ROLE', $this->oContact->getRole()); |
|
116
|
|
|
|
|
117
|
|
|
return $buffer; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Build all addresses. |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function buildAddresses() : string |
|
125
|
|
|
{ |
|
126
|
|
|
// addresses |
|
127
|
|
|
$buffer = ''; |
|
128
|
|
|
$iCnt = $this->oContact->getAddressCount(); |
|
129
|
|
|
$iPref = 0; |
|
130
|
|
|
for ($i = 0; $i < $iCnt; $i++) { |
|
131
|
|
|
$oAddress = $this->oContact->getAddress($i); |
|
132
|
|
|
if ($oAddress) { |
|
133
|
|
|
$buffer .= $oAddress->buildFullAddress(); |
|
134
|
|
|
$buffer .= $oAddress->buildLabel(); |
|
135
|
|
|
if ($oAddress->getPreferred()) { |
|
136
|
|
|
$iPref = $i; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
// set preferred address also as default postal address for MS |
|
141
|
|
|
$buffer .= $this->buildProperty('X-MS-OL-DEFAULT-POSTAL-ADDRESS', (string) ($iPref + 1)); |
|
142
|
|
|
|
|
143
|
|
|
return $buffer; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Build all phone numbers. |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function buildPhoneNumbers() : string |
|
151
|
|
|
{ |
|
152
|
|
|
// phone numbers |
|
153
|
|
|
$buffer = ''; |
|
154
|
|
|
$iCnt = $this->oContact->getPhoneCount(); |
|
155
|
|
|
for ($i = 0; $i < $iCnt; $i++) { |
|
156
|
|
|
$aPhone = $this->oContact->getPhone($i); |
|
157
|
|
|
if ($aPhone) { |
|
158
|
|
|
$strName = 'TEL;TYPE=' . $aPhone['strType']; |
|
159
|
|
|
$buffer .= $this->buildProperty($strName, $aPhone['strPhone']); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
return $buffer; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Build all e-mail addresses. |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function buildMailaddresses() : string |
|
170
|
|
|
{ |
|
171
|
|
|
// mailaddresses |
|
172
|
|
|
$buffer = ''; |
|
173
|
|
|
$iCnt = $this->oContact->getEMailCount(); |
|
174
|
|
|
for ($i = 0; $i < $iCnt; $i++) { |
|
175
|
|
|
$buffer .= $this->buildProperty('EMAIL;TYPE=INTERNET', $this->oContact->getEMail($i)); |
|
176
|
|
|
} |
|
177
|
|
|
return $buffer; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Build all additional properties. |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function buildAdditionalData() : string |
|
185
|
|
|
{ |
|
186
|
|
|
// homepage |
|
187
|
|
|
$buffer = $this->buildProperty('URL;TYPE=WORK', $this->oContact->getHomepage()); |
|
188
|
|
|
|
|
189
|
|
|
// personal data |
|
190
|
|
|
$buffer .= $this->buildProperty('BDAY', $this->oContact->getDateOfBirth()); |
|
191
|
|
|
if ($this->oContact->getGender() > 0) { |
|
192
|
|
|
$buffer .= $this->buildProperty('X-WAB-GENDER', (string) $this->oContact->getGender()); |
|
193
|
|
|
} |
|
194
|
|
|
// annotation |
|
195
|
|
|
$buffer .= $this->buildProperty('NOTE', $this->oContact->getNote()); |
|
196
|
|
|
|
|
197
|
|
|
return $buffer; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Build all categories. |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function buildCategories() : string |
|
205
|
|
|
{ |
|
206
|
|
|
// categories |
|
207
|
|
|
$iCnt = $this->oContact->getCategoriesCount(); |
|
208
|
|
|
$strSep = ''; |
|
209
|
|
|
$strValue = ''; |
|
210
|
|
|
for ($i = 0; $i < $iCnt; $i++) { |
|
211
|
|
|
$strValue .= $strSep . $this->maskString($this->oContact->getCategory($i)); |
|
212
|
|
|
$strSep = ','; |
|
213
|
|
|
} |
|
214
|
|
|
return $this->buildProperty('CATEGORIES', $strValue, false); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Build the photo. |
|
219
|
|
|
* @return string |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function buildPhoto() : string |
|
222
|
|
|
{ |
|
223
|
|
|
// photo |
|
224
|
|
|
$buffer = ''; |
|
225
|
|
|
$blobPortrait = $this->oContact->getPortraitBlob(); |
|
226
|
|
|
if (strlen($blobPortrait) > 0) { |
|
227
|
|
|
// extract image type from binary data |
|
228
|
|
|
$strType = ''; |
|
229
|
|
|
$strImage = ''; |
|
230
|
|
|
$this->parseImageData($blobPortrait, $strType, $strImage); |
|
231
|
|
|
if (strlen($strType) > 0 && strlen($strImage) > 0) { |
|
232
|
|
|
$strName = 'PHOTO;TYPE=' . $strType . ';ENCODING=B'; |
|
233
|
|
|
$buffer .= $this->buildProperty($strName, $strImage, false); |
|
234
|
|
|
$buffer .= PHP_EOL; // even though in vcard 3.0 spec blank line after binary value no longer is requires, MS Outlook need it... |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
return $buffer; |
|
238
|
|
|
} |
|
239
|
|
|
} |