|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CardDAV to FritzBox! XML (automatic upload) |
|
4
|
|
|
* inspired by http://www.wehavemorefun.de/fritzbox/Hochladen_eines_MySQL-Telefonbuchs |
|
5
|
|
|
* |
|
6
|
|
|
* Requirements: |
|
7
|
|
|
* php5, php5-curl, php5-ftp |
|
8
|
|
|
* |
|
9
|
|
|
* used libraries: |
|
10
|
|
|
* * vCard-parser <https://github.com/nuovo/vCard-parser> (LICNECE: unknown) |
|
11
|
|
|
* * CardDAV-PHP <https://github.com/graviox/CardDAV-PHP>(LICENCE: AGPLv3) |
|
12
|
|
|
* * fritzbox_api_php <https://github.com/carlos22/fritzbox_api_php> (LICENCE: CC-by-SA 3.0) |
|
13
|
|
|
* |
|
14
|
|
|
* LICENCE (of this file): MIT |
|
15
|
|
|
* |
|
16
|
|
|
* Autors: Karl Glatz (original author) |
|
17
|
|
|
* Martin Rost |
|
18
|
|
|
* Jens Maus <[email protected]> |
|
19
|
|
|
* Johannes Freiburger |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
error_reporting(E_ALL); |
|
23
|
|
|
setlocale(LC_ALL, 'de_DE.UTF8'); |
|
24
|
|
|
|
|
25
|
|
|
// Version identifier for CardDAV2FB |
|
26
|
|
|
$carddav2fb_version = '1.11 (2016-05-12)'; |
|
27
|
|
|
|
|
28
|
|
|
// check for the minimum php version |
|
29
|
|
|
$php_min_version = '5.3.6'; |
|
30
|
|
|
if(version_compare(PHP_VERSION, $php_min_version) < 0) |
|
31
|
|
|
{ |
|
32
|
|
|
print 'ERROR: PHP version ' . $php_min_version . ' is required. Found version: ' . PHP_VERSION . PHP_EOL; |
|
33
|
|
|
exit(1); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
require_once('lib/CardDAV-PHP/carddav.php'); |
|
37
|
|
|
require_once('lib/vCard-parser/vCard.php'); |
|
38
|
|
|
require_once('lib/fritzbox_api_php/fritzbox_api.class.php'); |
|
39
|
|
|
|
|
40
|
|
|
if($argc == 2) |
|
41
|
|
|
$config_file_name = $argv[1]; |
|
42
|
|
|
else |
|
43
|
|
|
$config_file_name = __DIR__ . '/config.php'; |
|
44
|
|
|
|
|
45
|
|
|
// default/fallback config options |
|
46
|
|
|
$config['tmp_dir'] = sys_get_temp_dir(); |
|
47
|
|
|
$config['fritzbox_ip'] = 'fritz.box'; |
|
48
|
|
|
$config['fritzbox_ip_ftp'] = $config['fritzbox_ip']; |
|
49
|
|
|
$config['fritzbox_force_local_login'] = false; |
|
50
|
|
|
$config['phonebook_number'] = '0'; |
|
51
|
|
|
$config['phonebook_name'] = 'Telefonbuch'; |
|
52
|
|
|
$config['usb_disk'] = ''; |
|
53
|
|
|
$config['fritzbox_path'] = 'file:///var/media/ftp/'; |
|
54
|
|
|
$config['fullname_format'] = 0; // see config.example.php for options |
|
55
|
|
|
$config['prefix'] = false; |
|
56
|
|
|
$config['suffix'] = false; |
|
57
|
|
|
$config['addnames'] = false; |
|
58
|
|
|
$config['orgname'] = false; |
|
59
|
|
|
$config['build_photos'] = true; |
|
60
|
|
|
$config['quickdial_keyword'] = 'Quickdial:'; |
|
61
|
|
|
|
|
62
|
|
|
if(is_file($config_file_name)) |
|
63
|
|
|
require($config_file_name); |
|
64
|
|
|
else |
|
65
|
|
|
{ |
|
66
|
|
|
print 'ERROR: No ' . $config_file_name . ' found, please take a look at config.example.php and create a ' . $config_file_name . ' file!' . PHP_EOL; |
|
67
|
|
|
exit(1); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// --------------------------------------------- |
|
71
|
|
|
// MAIN |
|
72
|
|
|
print "carddav2fb.php " . $carddav2fb_version . " - CardDAV to FRITZ!Box phonebook conversion tool" . PHP_EOL; |
|
73
|
|
|
print "Copyright (c) 2012-2016 Karl Glatz, Martin Rost, Jens Maus, Johannes Freiburger" . PHP_EOL . PHP_EOL; |
|
74
|
|
|
|
|
75
|
|
|
$client = new CardDAV2FB($config); |
|
76
|
|
|
|
|
77
|
|
|
// read vcards from webdav |
|
78
|
|
|
print 'Retrieving VCards from all CardDAV server(s):' . PHP_EOL; |
|
79
|
|
|
$client->get_carddav_entries(); |
|
80
|
|
|
print 'Done.' . PHP_EOL; |
|
81
|
|
|
|
|
82
|
|
|
flush(); // in case this script runs by php-cgi |
|
83
|
|
|
|
|
84
|
|
|
// transform them to a fritzbox compatible xml file |
|
85
|
|
|
print 'Converting VCards to FritzBox XML format:' . PHP_EOL; |
|
86
|
|
|
$client->build_fb_xml(); |
|
87
|
|
|
print 'Done.' . PHP_EOL; |
|
88
|
|
|
|
|
89
|
|
|
flush(); // in case this script runs by php-cgi |
|
90
|
|
|
|
|
91
|
|
|
// upload the XML-file to the FRITZ!Box (CAUTION: this will overwrite all current entries in the phone book!!) |
|
92
|
|
|
print 'Upload data to FRITZ!Box @ ' . $config['fritzbox_ip'] . PHP_EOL; |
|
93
|
|
|
if($client->upload_to_fb()) |
|
94
|
|
|
print 'Done.' . PHP_EOL; |
|
95
|
|
|
else |
|
96
|
|
|
exit(1); |
|
97
|
|
|
|
|
98
|
|
|
flush(); // in case this script runs by php-cgi |
|
99
|
|
|
|
|
100
|
|
|
// --------------------------------------------- |
|
101
|
|
|
// Class definition |
|
102
|
|
|
class CardDAV2FB |
|
103
|
|
|
{ |
|
104
|
|
|
protected $entries = array(); |
|
105
|
|
|
protected $fbxml = ""; |
|
106
|
|
|
protected $config = null; |
|
107
|
|
|
protected $tmpdir = null; |
|
108
|
|
|
|
|
109
|
|
|
public function __construct($config) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->config = $config; |
|
112
|
|
|
|
|
113
|
|
|
// create a temp directory where we store photos |
|
114
|
|
|
$this->tmpdir = $this->mktemp($this->config['tmp_dir']); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function __destruct() |
|
118
|
|
|
{ |
|
119
|
|
|
// remote temp directory |
|
120
|
|
|
$this->rmtemp($this->tmpdir); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// Source: https://php.net/manual/de/function.tempnam.php#61436 |
|
124
|
|
|
public function mktemp($dir, $prefix = '', $mode = 0700) |
|
125
|
|
|
{ |
|
126
|
|
|
if(substr($dir, -1) != '/') |
|
127
|
|
|
$dir .= '/'; |
|
128
|
|
|
|
|
129
|
|
|
do |
|
130
|
|
|
{ |
|
131
|
|
|
$path = $dir . $prefix . mt_rand(0, 9999999); |
|
132
|
|
|
} |
|
133
|
|
|
while(!mkdir($path, $mode)); |
|
134
|
|
|
|
|
135
|
|
|
return $path; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function rmtemp($dir) |
|
139
|
|
|
{ |
|
140
|
|
|
if(is_dir($dir)) |
|
141
|
|
|
{ |
|
142
|
|
|
$objects = scandir($dir); |
|
143
|
|
|
foreach($objects as $object) |
|
144
|
|
|
{ |
|
145
|
|
|
if($object != "." && $object != "..") |
|
146
|
|
|
{ |
|
147
|
|
|
if(filetype($dir . "/" . $object) == "dir") |
|
148
|
|
|
rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
reset($objects); |
|
152
|
|
|
rmdir($dir); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function is_base64($str) |
|
157
|
|
|
{ |
|
158
|
|
|
try |
|
159
|
|
|
{ |
|
160
|
|
|
// Check if there are valid base64 characters |
|
161
|
|
|
if(!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $str)) |
|
162
|
|
|
return false; |
|
163
|
|
|
|
|
164
|
|
|
// Decode the string in strict mode and check the results |
|
165
|
|
|
$decoded = base64_decode($str, true); |
|
166
|
|
|
if($decoded === false) |
|
167
|
|
|
return false; |
|
168
|
|
|
|
|
169
|
|
|
// Encode the string again |
|
170
|
|
|
if(base64_encode($decoded) === $str) |
|
171
|
|
|
return true; |
|
172
|
|
|
else |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
catch(Exception $e) |
|
176
|
|
|
{ |
|
177
|
|
|
// If exception is caught, then it is not a base64 encoded string |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function base64_to_jpeg($inputfile, $outputfile) |
|
183
|
|
|
{ |
|
184
|
|
|
// read data (binary) |
|
185
|
|
|
$ifp = fopen($inputfile, "rb"); |
|
186
|
|
|
$imageData = fread($ifp, filesize($inputfile)); |
|
187
|
|
|
fclose($ifp); |
|
188
|
|
|
|
|
189
|
|
|
// encode & write data (binary) |
|
190
|
|
|
$ifp = fopen($outputfile, "wb"); |
|
191
|
|
|
fwrite($ifp, base64_decode($imageData)); |
|
192
|
|
|
fclose($ifp); |
|
193
|
|
|
|
|
194
|
|
|
// return output filename |
|
195
|
|
|
return($outputfile); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function get_carddav_entries() |
|
199
|
|
|
{ |
|
200
|
|
|
$entries = array(); |
|
201
|
|
|
$snum = 0; |
|
202
|
|
|
|
|
203
|
|
|
if(is_array($this->config['carddav'])) |
|
204
|
|
|
{ |
|
205
|
|
|
foreach($this->config['carddav'] as $conf) |
|
206
|
|
|
{ |
|
207
|
|
|
print " [" . $snum . "]: " . $conf['url'] . " "; |
|
208
|
|
|
$carddav = new CardDavPHP\CardDavBackend($conf['url']); |
|
209
|
|
|
$carddav->setAuth($conf['user'], $conf['pw']); |
|
210
|
|
|
|
|
211
|
|
|
// set the vcard extension in case the user |
|
212
|
|
|
// defined it in the config |
|
213
|
|
|
if(isset($conf['extension'])) |
|
214
|
|
|
$carddav->setVcardExtension($conf['extension']); |
|
215
|
|
|
|
|
216
|
|
|
// retrieve data from the CardDAV server now |
|
217
|
|
|
$xmldata = $carddav->get(); |
|
218
|
|
|
|
|
219
|
|
|
// identify if we received UTF-8 encoded data from the |
|
220
|
|
|
// CardDAV server and if not reencode it since the FRITZ!Box |
|
221
|
|
|
// requires UTF-8 encoded data |
|
222
|
|
|
if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata) |
|
223
|
|
|
$xmldata = utf8_encode($xmldata); |
|
224
|
|
|
|
|
225
|
|
|
// read raw_vcard data from xml response |
|
226
|
|
|
$raw_vcards = array(); |
|
227
|
|
|
$xmlvcard = new SimpleXMLElement($xmldata); |
|
228
|
|
|
|
|
229
|
|
|
foreach($xmlvcard->element as $vcard_element) |
|
230
|
|
|
{ |
|
231
|
|
|
$id = $vcard_element->id->__toString(); |
|
232
|
|
|
$value = (string)$vcard_element->vcard->__toString(); |
|
233
|
|
|
$raw_vcards[$id] = $value; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
print " " . count($raw_vcards) . " VCards retrieved." . PHP_EOL; |
|
237
|
|
|
|
|
238
|
|
|
// parse raw_vcards |
|
239
|
|
|
$quick_dial_arr = array(); |
|
240
|
|
|
foreach($raw_vcards as $v) |
|
241
|
|
|
{ |
|
242
|
|
|
$vcard_obj = new vCard(false, $v); |
|
243
|
|
|
$name_arr = null; |
|
244
|
|
|
if(isset($vcard_obj->n[0])) |
|
|
|
|
|
|
245
|
|
|
$name_arr = $vcard_obj->n[0]; |
|
|
|
|
|
|
246
|
|
|
$org_arr = null; |
|
247
|
|
|
if(isset($vcard_obj->org[0])) |
|
|
|
|
|
|
248
|
|
|
$org_arr = $vcard_obj->org[0]; |
|
|
|
|
|
|
249
|
|
|
$addnames = ''; |
|
250
|
|
|
$prefix = ''; |
|
251
|
|
|
$suffix = ''; |
|
252
|
|
|
$orgname = ''; |
|
253
|
|
|
$formattedname = ''; |
|
254
|
|
|
|
|
255
|
|
|
// Build name Parts if existing ans switch to true in config |
|
256
|
|
|
if(isset($name_arr['prefixes']) and $this->config['prefix']) |
|
257
|
|
|
$prefix = trim($name_arr['prefixes']); |
|
258
|
|
|
|
|
259
|
|
|
if(isset($name_arr['suffixes']) and $this->config['suffix']) |
|
260
|
|
|
$suffix = trim($name_arr['suffixes']); |
|
261
|
|
|
|
|
262
|
|
|
if(isset($name_arr['additionalnames']) and $this->config['addnames']) |
|
263
|
|
|
$addnames = trim($name_arr['additionalnames']); |
|
264
|
|
|
|
|
265
|
|
|
if(isset($org_arr['name']) and $this->config['orgname']) |
|
266
|
|
|
$orgname = trim($org_arr['name']); |
|
267
|
|
|
|
|
268
|
|
|
if (isset($vcard_obj->fn[0])) |
|
|
|
|
|
|
269
|
|
|
$formattedname = $vcard_obj->fn[0]; |
|
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
$firstname = trim($name_arr['firstname']); |
|
272
|
|
|
$lastname = trim($name_arr['lastname']); |
|
273
|
|
|
|
|
274
|
|
|
// the following section implemented different ways of constructing the |
|
275
|
|
|
// final phonebook name entry depending on user preferred settings |
|
276
|
|
|
// selectable in the config file. Possible options are: |
|
277
|
|
|
// |
|
278
|
|
|
// $this->config['fullname_format']: |
|
|
|
|
|
|
279
|
|
|
// |
|
280
|
|
|
// 0: "Prefix Lastname, Firstname AdditionalNames Suffix (orgname)" |
|
281
|
|
|
// 1: "Prefix Firstname Lastname AdditionalNames Suffix (orgname)" |
|
282
|
|
|
// 2: "Prefix Firstname AdditionalNames Lastname Suffix (orgname)" |
|
283
|
|
|
// |
|
284
|
|
|
$name = ''; |
|
285
|
|
|
$format = $this->config['fullname_format']; |
|
286
|
|
|
|
|
287
|
|
|
// Prefix |
|
288
|
|
|
if(!empty($prefix)) |
|
289
|
|
|
$name .= $prefix; |
|
290
|
|
|
|
|
291
|
|
View Code Duplication |
if($format == 0) |
|
|
|
|
|
|
292
|
|
|
{ |
|
293
|
|
|
// Lastname |
|
294
|
|
|
if(!empty($name) and !empty($lastname)) |
|
295
|
|
|
$name .= ' ' . $lastname; |
|
296
|
|
|
else |
|
297
|
|
|
$name .= $lastname; |
|
298
|
|
|
} |
|
299
|
|
|
else |
|
300
|
|
|
{ |
|
301
|
|
|
// Firstname |
|
302
|
|
|
if(!empty($name) and !empty($firstname)) |
|
303
|
|
|
$name .= ' ' . $firstname; |
|
304
|
|
|
else |
|
305
|
|
|
$name .= $firstname; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
View Code Duplication |
if($format == 2) |
|
|
|
|
|
|
309
|
|
|
{ |
|
310
|
|
|
// AdditionalNames |
|
311
|
|
|
if(!empty($name) and !empty($addnames)) |
|
312
|
|
|
$name .= ' ' . $addnames; |
|
313
|
|
|
else |
|
314
|
|
|
$name .= $addnames; |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
View Code Duplication |
if($format == 0) |
|
|
|
|
|
|
318
|
|
|
{ |
|
319
|
|
|
// Firstname |
|
320
|
|
|
if(!empty($name) and !empty($firstname)) |
|
321
|
|
|
$name .= ', ' . $firstname; |
|
322
|
|
|
else |
|
323
|
|
|
$name .= $firstname; |
|
324
|
|
|
} |
|
325
|
|
|
else |
|
326
|
|
|
{ |
|
327
|
|
|
// Lastname |
|
328
|
|
|
if(!empty($name) and !empty($lastname)) |
|
329
|
|
|
$name .= ' ' . $lastname; |
|
330
|
|
|
else |
|
331
|
|
|
$name .= $lastname; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
View Code Duplication |
if($format != 2) |
|
|
|
|
|
|
335
|
|
|
{ |
|
336
|
|
|
// AdditionalNames |
|
337
|
|
|
if(!empty($name) and !empty($addnames)) |
|
338
|
|
|
$name .= ' ' . $addnames; |
|
339
|
|
|
else |
|
340
|
|
|
$name .= $addnames; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
// Suffix |
|
344
|
|
|
if(!empty($name) and !empty($suffix)) |
|
345
|
|
|
$name .= ' ' . $suffix; |
|
346
|
|
|
else |
|
347
|
|
|
$name .= $suffix; |
|
348
|
|
|
|
|
349
|
|
|
// OrgName |
|
350
|
|
|
if(!empty($name) and !empty($orgname)) |
|
351
|
|
|
$name .= ' (' . $orgname . ')'; |
|
352
|
|
|
else |
|
353
|
|
|
$name .= $orgname; |
|
354
|
|
|
|
|
355
|
|
|
// make sure to trim whitespaces and double spaces |
|
356
|
|
|
$name = trim(str_replace(' ', ' ', $name)); |
|
357
|
|
|
|
|
358
|
|
|
// perform a fallback to formatted name, if we don't have any name and formatted name is available |
|
359
|
|
|
if(empty($name) and !empty($formattedname)) |
|
360
|
|
|
$name = $formattedname; |
|
361
|
|
|
|
|
362
|
|
|
if(empty($name)) |
|
363
|
|
|
{ |
|
364
|
|
|
print ' WARNING: No fullname, lastname, orgname or formatted name found!' . PHP_EOL; |
|
365
|
|
|
$name = 'UNKNOWN'; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
// format filename of contact photo; remove special letters |
|
369
|
|
|
if($vcard_obj->photo) |
|
|
|
|
|
|
370
|
|
|
{ |
|
371
|
|
|
$photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'), |
|
372
|
|
|
array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name); |
|
373
|
|
|
} |
|
374
|
|
|
else |
|
375
|
|
|
$photo = ''; |
|
376
|
|
|
|
|
377
|
|
|
// phone |
|
378
|
|
|
$phone_no = array(); |
|
379
|
|
|
if($vcard_obj->categories) |
|
|
|
|
|
|
380
|
|
|
$categories = $vcard_obj->categories[0]; |
|
|
|
|
|
|
381
|
|
|
else |
|
382
|
|
|
$categories = array(); |
|
383
|
|
|
|
|
384
|
|
|
// check for quickdial entry |
|
385
|
|
|
if(isset($vcard_obj->note[0])) |
|
|
|
|
|
|
386
|
|
|
{ |
|
387
|
|
|
$note = $vcard_obj->note[0]; |
|
|
|
|
|
|
388
|
|
|
$notes = explode($this->config['quickdial_keyword'], $note); |
|
389
|
|
|
foreach($notes as $linenr => $linecontent) |
|
390
|
|
|
{ |
|
391
|
|
|
$found = strrpos($linecontent, ":**7"); |
|
392
|
|
|
if($found > 0) |
|
393
|
|
|
{ |
|
394
|
|
|
$pos_qd_start = strrpos($linecontent, ":**7"); |
|
395
|
|
|
$quick_dial_for_nr = preg_replace("/[^0-9+]/", "", substr($linecontent, 0, $pos_qd_start)); |
|
396
|
|
|
$quick_dial_nr = intval(substr($linecontent, $pos_qd_start + 4, 3)); |
|
397
|
|
|
$quick_dial_arr[$quick_dial_for_nr] = $quick_dial_nr; |
|
398
|
|
|
} |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
// e-mail addresses |
|
403
|
|
|
$email_add = array(); |
|
404
|
|
|
$vip = isset($this->config['group_vip']) && in_array((string)$this->config['group_vip'], $categories); |
|
405
|
|
|
|
|
406
|
|
|
if(array_key_exists('group_filter', $this->config) && is_array($this->config['group_filter'])) |
|
407
|
|
|
{ |
|
408
|
|
|
$add_entry = 0; |
|
409
|
|
|
foreach($this->config['group_filter'] as $group_filter) |
|
410
|
|
|
{ |
|
411
|
|
|
if(in_array($group_filter, $categories)) |
|
412
|
|
|
{ |
|
413
|
|
|
$add_entry = 1; |
|
414
|
|
|
break; |
|
415
|
|
|
} |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
else |
|
419
|
|
|
$add_entry = 1; |
|
420
|
|
|
|
|
421
|
|
|
if($add_entry == 1) |
|
422
|
|
|
{ |
|
423
|
|
|
foreach($vcard_obj->tel as $t) |
|
|
|
|
|
|
424
|
|
|
{ |
|
425
|
|
|
$prio = 0; |
|
426
|
|
|
$quickdial = null; |
|
427
|
|
|
|
|
428
|
|
|
if(!is_array($t) || empty($t['type'])) |
|
429
|
|
|
{ |
|
430
|
|
|
$type = "mobile"; |
|
431
|
|
|
$phone_number = $t; |
|
432
|
|
|
} |
|
433
|
|
|
else |
|
434
|
|
|
{ |
|
435
|
|
|
$phone_number = $t['value']; |
|
436
|
|
|
|
|
437
|
|
|
$phone_number_clean = preg_replace("/[^0-9+]/", "", $phone_number); |
|
438
|
|
|
foreach($quick_dial_arr as $qd_phone_nr => $value) |
|
439
|
|
|
{ |
|
440
|
|
|
if($qd_phone_nr == $phone_number_clean) |
|
441
|
|
|
{ |
|
442
|
|
|
//Set quickdial |
|
443
|
|
|
if($value == 1) |
|
444
|
|
|
print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n"; |
|
445
|
|
|
elseif($value >= 100) |
|
446
|
|
|
print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n"; |
|
447
|
|
|
|
|
448
|
|
|
$quickdial = $value; |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
$typearr_lower = unserialize(strtolower(serialize($t['type']))); |
|
453
|
|
|
|
|
454
|
|
|
// find out priority |
|
455
|
|
|
if(in_array("pref", $typearr_lower)) |
|
456
|
|
|
$prio = 1; |
|
457
|
|
|
|
|
458
|
|
|
// set the proper type |
|
459
|
|
|
if(in_array("cell", $typearr_lower)) |
|
460
|
|
|
$type = "mobile"; |
|
461
|
|
|
elseif(in_array("home", $typearr_lower)) |
|
462
|
|
|
$type = "home"; |
|
463
|
|
|
elseif(in_array("fax", $typearr_lower)) |
|
464
|
|
|
$type = "fax_work"; |
|
465
|
|
|
elseif(in_array("work", $typearr_lower)) |
|
466
|
|
|
$type = "work"; |
|
467
|
|
|
elseif(in_array("other", $typearr_lower)) |
|
468
|
|
|
$type = "other"; |
|
469
|
|
|
elseif(in_array("dom", $typearr_lower)) |
|
470
|
|
|
$type = "other"; |
|
471
|
|
|
else |
|
472
|
|
|
continue; |
|
473
|
|
|
} |
|
474
|
|
|
$phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number)); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
// request email address and type |
|
478
|
|
|
if($vcard_obj->email) |
|
|
|
|
|
|
479
|
|
|
{ |
|
480
|
|
|
foreach($vcard_obj->email as $e) |
|
|
|
|
|
|
481
|
|
|
{ |
|
482
|
|
|
if(empty($e['type'])) |
|
483
|
|
|
{ |
|
484
|
|
|
$type_email = "work"; |
|
485
|
|
|
$email = $e; |
|
486
|
|
|
} |
|
487
|
|
|
else |
|
488
|
|
|
{ |
|
489
|
|
|
$email = $e['value']; |
|
490
|
|
|
$typearr_lower = unserialize(strtolower(serialize($e['type']))); |
|
491
|
|
|
if(in_array("work", $typearr_lower)) |
|
492
|
|
|
$type_email = "work"; |
|
493
|
|
|
elseif(in_array("home", $typearr_lower)) |
|
494
|
|
|
$type_email = "home"; |
|
495
|
|
|
elseif(in_array("other", $typearr_lower)) |
|
496
|
|
|
$type_email = "other"; |
|
497
|
|
|
else |
|
498
|
|
|
continue; |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
// DEBUG: print out the email address on the console |
|
502
|
|
|
//print $type_email.": ".$email."\n"; |
|
|
|
|
|
|
503
|
|
|
|
|
504
|
|
|
$email_add[] = array("type"=>$type_email, "value" => $email); |
|
505
|
|
|
} |
|
506
|
|
|
} |
|
507
|
|
|
$entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo); |
|
|
|
|
|
|
508
|
|
|
} |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
$snum++; |
|
512
|
|
|
} |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
$this->entries = $entries; |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
private function _clear_phone_number($number) |
|
519
|
|
|
{ |
|
520
|
|
|
return preg_replace("/[^0-9+]/", "", $number); |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
public function build_fb_xml() |
|
524
|
|
|
{ |
|
525
|
|
|
if(empty($this->entries)) |
|
526
|
|
|
throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!'); |
|
527
|
|
|
|
|
528
|
|
|
// create FB XML in utf-8 format |
|
529
|
|
|
$root = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><phonebooks><phonebook></phonebook></phonebooks>'); |
|
530
|
|
|
$pb = $root->phonebook; |
|
531
|
|
|
$pb->addAttribute("name", $this->config['phonebook_name']); |
|
532
|
|
|
|
|
533
|
|
|
foreach($this->entries as $entry) |
|
534
|
|
|
{ |
|
535
|
|
|
$contact = $pb->addChild("contact"); |
|
536
|
|
|
$contact->addChild("category", $entry['vip']); |
|
537
|
|
|
$person = $contact->addChild("person"); |
|
538
|
|
|
$person->addChild("realName", $this->_convert_text($entry['realName'])); |
|
539
|
|
|
|
|
540
|
|
|
echo " VCard: '" . utf8_decode($entry['realName']) . "'" . PHP_EOL; |
|
541
|
|
|
|
|
542
|
|
|
// telephone: put the phonenumbers into the fritzbox xml file |
|
543
|
|
|
$telephony = $contact->addChild("telephony"); |
|
544
|
|
|
$id = 0; |
|
545
|
|
|
foreach($entry['telephony'] as $tel) |
|
546
|
|
|
{ |
|
547
|
|
|
$num = $telephony->addChild("number", $tel['value']); |
|
548
|
|
|
$num->addAttribute("type", $tel['type']); |
|
549
|
|
|
$num->addAttribute("vanity", ""); |
|
550
|
|
|
$num->addAttribute("prio", $tel['prio']); |
|
551
|
|
|
$num->addAttribute("id", $id); |
|
552
|
|
|
|
|
553
|
|
|
if(isset($tel['quickdial'])) |
|
554
|
|
|
{ |
|
555
|
|
|
$num->addAttribute("quickdial", $tel['quickdial']); |
|
556
|
|
|
print " Added quickdial: " . $tel['quickdial'] . " for: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL; |
|
557
|
|
|
} |
|
558
|
|
|
|
|
559
|
|
|
$id++; |
|
560
|
|
|
print " Added phone: " . $tel['value'] . " (" . $tel['type'] . ")" . PHP_EOL; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
// output a warning if no telephone number was found |
|
564
|
|
|
if($id == 0) |
|
565
|
|
|
print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL; |
|
566
|
|
|
|
|
567
|
|
|
// email: put the email addresses into the fritzbox xml file |
|
568
|
|
|
$email = $contact->addChild("services"); |
|
569
|
|
|
$id = 0; |
|
570
|
|
|
foreach($entry['email'] as $mail) |
|
571
|
|
|
{ |
|
572
|
|
|
$mail_adr = $email->addChild("email", $mail['value']); |
|
573
|
|
|
$mail_adr->addAttribute("classifier", $mail['type']); |
|
574
|
|
|
$mail_adr->addAttribute("id", $id); |
|
575
|
|
|
$id++; |
|
576
|
|
|
|
|
577
|
|
|
print " Added email: " . $mail['value'] . " (" . $mail['type'] . ")" . PHP_EOL; |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
// check for a photo being part of the VCard |
|
581
|
|
|
if(($entry['photo']) and ($entry['photo_data']) and (is_array($entry['photo_data'])) and ($entry['photo_data'][0])) |
|
582
|
|
|
{ |
|
583
|
|
|
// check if 'photo_data'[0] is an array as well because then |
|
584
|
|
|
// we have to extract ['value'] and friends. |
|
585
|
|
|
if(is_array($entry['photo_data'][0]) and (array_key_exists('value', $entry['photo_data'][0]))) |
|
586
|
|
|
{ |
|
587
|
|
|
// check if photo_data really contains JPEG data |
|
588
|
|
|
if((array_key_exists('type', $entry['photo_data'][0])) and (is_array($entry['photo_data'][0]['type'])) and |
|
589
|
|
|
($entry['photo_data'][0]['type'][0] == 'jpeg' or $entry['photo_data'][0]['type'][0] == 'jpg' or $entry['photo_data'][0]['type'][0] == 'image/jpeg')) |
|
590
|
|
|
{ |
|
591
|
|
|
// get photo, rename, base64 convert and save as jpg |
|
592
|
|
|
$photo_data = $entry['photo_data'][0]['value']; |
|
593
|
|
|
$photo_version = substr(sha1($photo_data), 0, 5); |
|
594
|
|
|
$photo_file = $this->tmpdir . '/' . "{$entry['photo']}_{$photo_version}.jpg"; |
|
595
|
|
|
|
|
596
|
|
|
// check for base64 encoding of the photo data and convert it |
|
597
|
|
|
// accordingly. |
|
598
|
|
|
if(((array_key_exists('encoding', $entry['photo_data'][0])) and ($entry['photo_data'][0]['encoding'] == 'b')) or $this->is_base64($photo_data)) |
|
599
|
|
|
{ |
|
600
|
|
|
file_put_contents($photo_file . ".b64", $photo_data); |
|
601
|
|
|
$this->base64_to_jpeg($photo_file . ".b64", $photo_file); |
|
602
|
|
|
unlink($photo_file . ".b64"); |
|
603
|
|
|
} |
|
604
|
|
|
else |
|
605
|
|
|
{ |
|
606
|
|
|
print " WARNING: non-base64 encoded photo data found and used." . PHP_EOL; |
|
607
|
|
|
file_put_contents($photo_file, $photo_data); |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
// add contact photo to xml |
|
611
|
|
|
$person->addChild("imageURL", $this->config['fritzbox_path'] . $this->config['usb_disk'] . "FRITZ/fonpix/" . basename($photo_file)); |
|
612
|
|
|
|
|
613
|
|
|
print " Added photo: " . basename($photo_file) . PHP_EOL; |
|
614
|
|
|
} |
|
615
|
|
|
else |
|
616
|
|
|
print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
617
|
|
|
} |
|
618
|
|
|
elseif(substr($entry['photo_data'][0], 0, 4) == 'http') |
|
619
|
|
|
{ |
|
620
|
|
|
// add contact photo to xml |
|
621
|
|
|
$person->addChild("imageURL", $entry['photo_data'][0]); |
|
622
|
|
|
|
|
623
|
|
|
print " Added photo: " . $entry['photo_data'][0] . PHP_EOL; |
|
624
|
|
|
} |
|
625
|
|
|
else |
|
626
|
|
|
print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL; |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
$contact->addChild("services"); |
|
630
|
|
|
$contact->addChild("setup"); |
|
631
|
|
|
$contact->addChild("mod_time", (string)time()); |
|
632
|
|
|
} |
|
633
|
|
|
|
|
634
|
|
|
if($root->asXML() !== false) |
|
635
|
|
|
$this->fbxml = $root->asXML(); |
|
636
|
|
|
else |
|
637
|
|
|
{ |
|
638
|
|
|
print " ERROR: created XML data isn't well-formed." . PHP_EOL; |
|
639
|
|
|
exit(1); |
|
640
|
|
|
} |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
public function _convert_text($text) |
|
644
|
|
|
{ |
|
645
|
|
|
$text = htmlspecialchars($text); |
|
646
|
|
|
return $text; |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
public function _concat($text1, $text2) |
|
650
|
|
|
{ |
|
651
|
|
|
if($text1 == '') |
|
652
|
|
|
return $text2; |
|
653
|
|
|
elseif($text2 == '') |
|
654
|
|
|
return $text1; |
|
655
|
|
|
else |
|
656
|
|
|
return $text1 . ", " . $text2; |
|
657
|
|
|
} |
|
658
|
|
|
|
|
659
|
|
|
public function _parse_fb_result($text) |
|
660
|
|
|
{ |
|
661
|
|
|
if(preg_match("/\<h2\>([^\<]+)\<\/h2\>/", $text, $matches) == 1 && !empty($matches)) |
|
662
|
|
|
return $matches[1]; |
|
663
|
|
|
else |
|
664
|
|
|
return "Error while uploading xml to fritzbox"; |
|
665
|
|
|
} |
|
666
|
|
|
|
|
667
|
|
|
public function upload_to_fb() |
|
668
|
|
|
{ |
|
669
|
|
|
// if the user wants to save the xml to a separate file, we do so now |
|
670
|
|
|
if(array_key_exists('output_file', $this->config)) |
|
671
|
|
|
{ |
|
672
|
|
|
// build md5 hash of previous stored xml without <mod_time> Elements |
|
673
|
|
|
$oldphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugoldtsreplace)); |
|
674
|
|
|
$output = fopen($this->config['output_file'], 'w'); |
|
675
|
|
|
if($output) |
|
676
|
|
|
{ |
|
677
|
|
|
fwrite($output, $this->fbxml); |
|
678
|
|
|
fclose($output); |
|
679
|
|
|
print " Saved to file " . $this->config['output_file'] . PHP_EOL; |
|
680
|
|
|
} |
|
681
|
|
|
if (array_key_exists('output_and_upload', $this->config) and $this->config['output_and_upload']) |
|
682
|
|
|
{ |
|
683
|
|
|
$newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugnewtsreplace)); |
|
684
|
|
|
print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL; |
|
685
|
|
|
if($oldphonebhash === $newphonebhash) |
|
686
|
|
|
{ |
|
687
|
|
|
print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box.". PHP_EOL; |
|
688
|
|
|
return 0; |
|
689
|
|
|
} |
|
690
|
|
|
else |
|
691
|
|
|
print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
692
|
|
|
} |
|
693
|
|
|
else |
|
694
|
|
|
return 0; |
|
695
|
|
|
} |
|
696
|
|
|
// now we upload the photo jpgs first being stored in the |
|
697
|
|
|
// temp directory. |
|
698
|
|
|
|
|
699
|
|
|
// perform an ftps-connection to copy over the photos to a specified directory |
|
700
|
|
|
$ftp_server = $this->config['fritzbox_ip_ftp']; |
|
701
|
|
|
$conn_id = ftp_ssl_connect($ftp_server); |
|
702
|
|
View Code Duplication |
if($conn_id == false) |
|
|
|
|
|
|
703
|
|
|
{ |
|
704
|
|
|
print " WARNING: Secure connection to FTP-server '" . $ftp_server . "' failed, retrying without SSL." . PHP_EOL; |
|
705
|
|
|
$conn_id = ftp_connect($ftp_server); |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
if($conn_id != false) |
|
709
|
|
|
{ |
|
710
|
|
|
ftp_set_option($conn_id, FTP_TIMEOUT_SEC, 60); |
|
711
|
|
|
$login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']); |
|
712
|
|
|
if($login_result === true) |
|
713
|
|
|
{ |
|
714
|
|
|
ftp_pasv($conn_id, true); |
|
715
|
|
|
|
|
716
|
|
|
// create remote photo path on FRITZ!Box if it doesn't exist |
|
717
|
|
|
$remote_path = $this->config['usb_disk'] . "/FRITZ/fonpix"; |
|
718
|
|
|
$all_existing_files = ftp_nlist($conn_id, $remote_path); |
|
719
|
|
|
if($all_existing_files == false) |
|
720
|
|
|
{ |
|
721
|
|
|
ftp_mkdir($conn_id, $remote_path); |
|
722
|
|
|
$all_existing_files = array(); |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
// now iterate through all jpg files in tempdir and upload them if necessary |
|
726
|
|
|
$dir = new DirectoryIterator($this->tmpdir); |
|
727
|
|
|
foreach($dir as $fileinfo) |
|
728
|
|
|
{ |
|
729
|
|
|
if(!$fileinfo->isDot()) |
|
730
|
|
|
{ |
|
731
|
|
|
if($fileinfo->getExtension() == "jpg") |
|
732
|
|
|
{ |
|
733
|
|
|
$file = $fileinfo->getFilename(); |
|
734
|
|
|
|
|
735
|
|
|
print " FTP-Upload '" . $file . "'..."; |
|
736
|
|
|
if(!in_array($remote_path . "/" . $file, $all_existing_files)) |
|
737
|
|
|
{ |
|
738
|
|
|
if(!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) |
|
739
|
|
|
{ |
|
740
|
|
|
// retry when a fault occurs. |
|
741
|
|
|
print " retrying... "; |
|
742
|
|
|
$conn_id = ftp_ssl_connect($ftp_server); |
|
743
|
|
View Code Duplication |
if($conn_id == false) |
|
|
|
|
|
|
744
|
|
|
{ |
|
745
|
|
|
print " WARNING: Secure re-connection to FTP-server '" . $ftp_server . "' failed, retrying without SSL." . PHP_EOL; |
|
746
|
|
|
$conn_id = ftp_connect($ftp_server); |
|
747
|
|
|
} |
|
748
|
|
|
|
|
749
|
|
|
if($conn_id == false) |
|
750
|
|
|
{ |
|
751
|
|
|
print " ERROR: couldn't re-connect to FTP server '" . $ftp_server . "', abortіng." . PHP_EOL; |
|
752
|
|
|
break; |
|
753
|
|
|
} |
|
754
|
|
|
|
|
755
|
|
|
$login_result = ftp_login($conn_id, $this->config['fritzbox_user'], $this->config['fritzbox_pw']); |
|
756
|
|
|
if($login_result === false) |
|
757
|
|
|
{ |
|
758
|
|
|
print " ERROR: couldn't re-login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL; |
|
759
|
|
|
break; |
|
760
|
|
|
} |
|
761
|
|
|
|
|
762
|
|
|
ftp_pasv($conn_id, true); |
|
763
|
|
|
if(!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) |
|
764
|
|
|
print " ERROR: while uploading file " . $fileinfo->getFilename() . PHP_EOL; |
|
765
|
|
|
else |
|
766
|
|
|
print " ok." . PHP_EOL; |
|
767
|
|
|
} |
|
768
|
|
|
else |
|
769
|
|
|
print " ok." . PHP_EOL; |
|
770
|
|
|
|
|
771
|
|
|
// cleanup old files |
|
772
|
|
|
foreach($all_existing_files as $existing_file) |
|
773
|
|
|
{ |
|
774
|
|
|
if(strpos($existing_file, $remote_path . "/" . substr($file, 0, -10)) !== false) |
|
775
|
|
|
{ |
|
776
|
|
|
print " FTP-Delete: " . $existing_file . PHP_EOL; |
|
777
|
|
|
ftp_delete($conn_id, $remote_path . "/" . basename($existing_file)); |
|
778
|
|
|
} |
|
779
|
|
|
} |
|
780
|
|
|
} |
|
781
|
|
|
else |
|
782
|
|
|
print " already exists." . PHP_EOL; |
|
783
|
|
|
} |
|
784
|
|
|
} |
|
785
|
|
|
} |
|
786
|
|
|
} |
|
787
|
|
|
else |
|
788
|
|
|
print " ERROR: couldn't login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL; |
|
789
|
|
|
|
|
790
|
|
|
// close ftp connection |
|
791
|
|
|
ftp_close($conn_id); |
|
792
|
|
|
} |
|
793
|
|
|
else |
|
794
|
|
|
print " ERROR: couldn't connect to FTP server '" . $ftp_server . "'." . PHP_EOL; |
|
795
|
|
|
|
|
796
|
|
|
// lets post the phonebook xml to the FRITZ!Box |
|
797
|
|
|
print " Uploading Phonebook XML to " . $this->config['fritzbox_ip'] . PHP_EOL; |
|
798
|
|
|
try |
|
799
|
|
|
{ |
|
800
|
|
|
$fritz = new fritzbox_api($this->config['fritzbox_pw'], |
|
801
|
|
|
$this->config['fritzbox_user'], |
|
802
|
|
|
$this->config['fritzbox_ip'], |
|
803
|
|
|
$this->config['fritzbox_force_local_login']); |
|
804
|
|
|
|
|
805
|
|
|
$formfields = array( |
|
806
|
|
|
'PhonebookId' => $this->config['phonebook_number'] |
|
807
|
|
|
); |
|
808
|
|
|
|
|
809
|
|
|
$filefileds = array('PhonebookImportFile' => array( |
|
810
|
|
|
'type' => 'text/xml', |
|
811
|
|
|
'filename' => 'updatepb.xml', |
|
812
|
|
|
'content' => $this->fbxml, |
|
813
|
|
|
) |
|
814
|
|
|
); |
|
815
|
|
|
|
|
816
|
|
|
$raw_result = $fritz->doPostFile($formfields, $filefileds); // send the command |
|
817
|
|
|
$msg = $this->_parse_fb_result($raw_result); |
|
818
|
|
|
unset($fritz); // destroy the object to log out |
|
819
|
|
|
|
|
820
|
|
|
print " FRITZ!Box returned message: '" . $msg . "'" . PHP_EOL; |
|
821
|
|
|
} |
|
822
|
|
|
catch(Exception $e) |
|
823
|
|
|
{ |
|
824
|
|
|
print " ERROR: " . $e->getMessage() . PHP_EOL; // show the error message in anything failed |
|
825
|
|
|
return false; |
|
826
|
|
|
} |
|
827
|
|
|
return true; |
|
828
|
|
|
} |
|
829
|
|
|
} |
|
830
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.