forked from christian-putzke/Roundcube-CardDAV
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcarddav.php
615 lines (531 loc) · 17.1 KB
/
carddav.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* Include required CardDAV classes
*/
require_once dirname(__FILE__) . '/carddav_backend.php';
require_once dirname(__FILE__) . '/carddav_addressbook.php';
/**
* Roundcube CardDAV implementation
*
* This is a CardDAV implementation for roundcube 0.6 or higher. It allows every user to add
* multiple CardDAV server in their settings. The CardDAV contacts (vCards) will be synchronized
* automaticly with their addressbook.
*
* @author Christian Putzke <[email protected]>
* @copyright Christian Putzke @ Graviox Studios
* @since 06.09.2011
* @link http://www.graviox.de/
* @link https://twitter.com/graviox/
* @version 0.5.1
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*
*/
class carddav extends rcube_plugin
{
/**
* Roundcube CardDAV version
*
* @constant string
*/
const VERSION = '0.5.1';
/**
* Tasks where the CardDAV plugin is loaded
*
* @var string
*/
public $task = 'settings|addressbook|mail';
/**
* CardDAV addressbook
*
* @var string
*/
protected $carddav_addressbook = 'carddav_addressbook';
/**
* Init CardDAV plugin - register actions, include scripts, load texts, add hooks
*
* @return void
*/
public function init()
{
$rcmail = rcmail::get_instance();
$skin_path = $this->local_skin_path();
if (!is_dir($skin_path))
{
$skin_path = 'skins/default';
}
$this->add_texts('localization/', true);
$this->include_stylesheet($skin_path . '/carddav.css');
switch ($rcmail->task)
{
case 'settings':
$this->register_action('plugin.carddav-server', array($this, 'carddav_server'));
$this->register_action('plugin.carddav-server-save', array($this, 'carddav_server_save'));
$this->register_action('plugin.carddav-server-delete', array($this, 'carddav_server_delete'));
$this->include_script('carddav_settings.js');
$this->include_script('jquery.base64.js');
$this->add_hook('addressbooks_list', array($this, 'get_carddav_addressbook_sources'));
break;
case 'addressbook':
if ($this->carddav_server_available())
{
$this->register_action('plugin.carddav-addressbook-sync', array($this, 'carddav_addressbook_sync'));
$this->include_script('carddav_addressbook.js');
$this->add_hook('addressbooks_list', array($this, 'get_carddav_addressbook_sources'));
$this->add_hook('addressbook_get', array($this, 'get_carddav_addressbook'));
$this->add_button(array(
'command' => 'plugin.carddav-addressbook-sync',
'imagepas' => $skin_path . '/sync_pas.png',
'imageact' => $skin_path . '/sync_act.png',
'width' => 32,
'height' => 32,
'title' => 'carddav.addressbook_sync'
), 'toolbar');
}
break;
case 'mail':
if ($this->carddav_server_available())
{
$this->add_hook('addressbooks_list', array($this, 'get_carddav_addressbook_sources'));
$this->add_hook('addressbook_get', array($this, 'get_carddav_addressbook'));
$sources = (array) $rcmail->config->get('autocomplete_addressbooks', array('sql'));
$servers = $this->get_carddav_server();
foreach ($servers as $server)
{
if (!in_array($this->carddav_addressbook . $server['carddav_server_id'], $sources))
{
$sources[] = $this->carddav_addressbook . $server['carddav_server_id'];
$rcmail->config->set('autocomplete_addressbooks', $sources);
}
}
}
break;
}
}
/**
* Extend the original local_skin_path method with the default skin path as fallback
*
* @param boolean $include_plugins_directory Include plugins directory
* @return string $skin_path Roundcubes skin path
*/
public function local_skin_path($include_plugins_directory = false)
{
$skin_path = parent::local_skin_path();
if (!is_dir($skin_path))
{
$skin_path = 'skins/default';
}
if ($include_plugins_directory === true)
{
$skin_path = 'plugins/carddav/' . $skin_path;
}
return $skin_path;
}
/**
* Get all CardDAV servers from the current user
*
* @param boolean $carddav_server_id CardDAV server id to load a single CardDAV server
* @return array CardDAV server array with label, url, username, password (encrypted)
*/
public function get_carddav_server($carddav_server_id = false)
{
$servers = array();
$rcmail = rcmail::get_instance();
$user_id = $rcmail->user->data['user_id'];
$query = "
SELECT
*
FROM
".$rcmail->db->table_name('carddav_server')."
WHERE
user_id = ?
".($carddav_server_id !== false ? " AND carddav_server_id = ?" : null)."
";
$result = $rcmail->db->query($query, $user_id, $carddav_server_id);
while($server = $rcmail->db->fetch_assoc($result))
{
$servers[] = $server;
}
return $servers;
}
/**
* Render available CardDAV server list in HTML
*
* @return string HTML rendered CardDAV server list
*/
protected function get_carddav_server_list()
{
$servers = $this->get_carddav_server();
if (!empty($servers))
{
$skin_path = $this->local_skin_path(true);
$content = html::div(array('class' => 'carddav_headline'), $this->gettext('settings_server'));
$table = new html_table(array(
'cols' => 6,
'class' => 'carddav_server_list'
));
$table->add_header(array('width' => '13%'), $this->gettext('settings_label'));
$table->add_header(array('width' => '36%'), $this->gettext('server'));
$table->add_header(array('width' => '13%'), $this->gettext('username'));
$table->add_header(array('width' => '13%'), $this->gettext('password'));
$table->add_header(array('width' => '13%'), $this->gettext('settings_read_only'));
$table->add_header(array('width' => '13%'), null);
foreach ($servers as $server)
{
// $rcmail->output->button() seems not to work within ajax requests so we build the button manually
$delete_submit = '<input
type="button"
value="'.$this->gettext('delete').'"
onclick="return rcmail.command(\'plugin.carddav-server-delete\', \''.$server['carddav_server_id'].'\', this)"
class="button mainaction"
/>';
$table->add(array(), $server['label']);
$table->add(array(), $server['url']);
$table->add(array(), $server['username']);
$table->add(array(), '**********');
$table->add(array(), ($server['read_only'] ? html::img($skin_path . '/checked.png') : null));
$table->add(array(), $delete_submit);
}
$content .= html::div(array('class' => 'carddav_container'), $table->show());
return $content;
}
else
{
return null;
}
}
/**
* Get CardDAV addressbook instance
*
* @param array $addressbook Array with all available addressbooks
* @return array Array with all available addressbooks
*/
public function get_carddav_addressbook($addressbook)
{
$servers = $this->get_carddav_server();
foreach ($servers as $server)
{
if ($addressbook['id'] === $this->carddav_addressbook . $server['carddav_server_id'])
{
$addressbook['instance'] = new carddav_addressbook($server['carddav_server_id'], $server['label'], ($server['read_only'] == 1 ? true : false));
}
}
return $addressbook;
}
/**
* Get CardDAV addressbook source
*
* @param array $addressbook Array with all available addressbooks sources
* @return array Array with all available addressbooks sources
*/
public function get_carddav_addressbook_sources($addressbook)
{
$servers = $this->get_carddav_server();
foreach ($servers as $server)
{
$carddav_addressbook = new carddav_addressbook($server['carddav_server_id'], $server['label'], ($server['read_only'] == 1 ? true : false));
$addressbook['sources'][$this->carddav_addressbook . $server['carddav_server_id']] = array(
'id' => $this->carddav_addressbook . $server['carddav_server_id'],
'name' => $server['label'],
'readonly' => $carddav_addressbook->readonly,
'groups' => $carddav_addressbook->groups
);
}
return $addressbook;
}
/**
* Check if CURL is installed or not
*
* @return boolean
*/
private function check_curl_installed()
{
if (function_exists('curl_init'))
{
return true;
}
else
{
return false;
}
}
/**
* Synchronize CardDAV addressbook
*
* @param boolean $carddav_server_id CardDAV server id to synchronize a single CardDAV server
* @param boolean $ajax Within a ajax request or not
* @return void
*/
public function carddav_addressbook_sync($carddav_server_id = false, $ajax = true)
{
$servers = $this->get_carddav_server();
$result = false;
foreach ($servers as $server)
{
if ($carddav_server_id === false || $carddav_server_id == $server['carddav_server_id'])
{
$carddav_addressbook = new carddav_addressbook($server['carddav_server_id'], $server['label'], ($server['read_only'] == 1 ? true : false));
$result = $carddav_addressbook->carddav_addressbook_sync($server);
}
}
if ($ajax === true)
{
$rcmail = rcmail::get_instance();
if ($result === true)
{
$rcmail->output->command('plugin.carddav_addressbook_message', array(
'message' => $this->gettext('addressbook_synced'),
'check' => true
));
}
else
{
$rcmail->output->command('plugin.carddav_addressbook_message', array(
'message' => $this->gettext('addressbook_sync_failed'),
'check' => false
));
}
}
}
/**
* Render CardDAV server settings
*
* @return void
*/
public function carddav_server()
{
$rcmail = rcmail::get_instance();
$this->register_handler('plugin.body', array($this, 'carddav_server_form'));
$rcmail->output->set_pagetitle($this->gettext('settings'));
$rcmail->output->send('plugin');
}
/**
* Check if CardDAV server are available in the local database
*
* @return boolean If CardDAV-Server are available in the local database return true else false
*/
protected function carddav_server_available()
{
$rcmail = rcmail::get_instance();
$user_id = $rcmail->user->data['user_id'];
$query = "
SELECT
count(*)
FROM
".$rcmail->db->table_name('carddav_server')."
WHERE
user_id = ?
";
$result = $rcmail->db->query($query, $user_id);
$count = $rcmail->db->fetch_array($result);
return ($count[0] > 0);
}
/**
* Check if it's possible to connect to the CardDAV server
*
* @return boolean
*/
public function carddav_server_check_connection()
{
$url = parse_input_value(base64_decode($_POST['_server_url']));
$username = parse_input_value(base64_decode($_POST['_username']));
$password = parse_input_value(base64_decode($_POST['_password']));
$carddav_backend = new carddav_backend($url);
$carddav_backend->set_auth($username, $password);
return $carddav_backend->check_connection();
}
/**
* Render CardDAV server settings formular and register JavaScript actions
*
* @return string HTML CardDAV server formular
*/
public function carddav_server_form()
{
$rcmail = rcmail::get_instance();
$boxcontent = null;
if ($this->check_curl_installed())
{
$input_label = new html_inputfield(array(
'name' => '_label',
'id' => '_label',
'size' => '17'
));
$input_server_url = new html_inputfield(array(
'name' => '_server_url',
'id' => '_server_url',
'size' => '44'
));
$input_username = new html_inputfield(array(
'name' => '_username',
'id' => '_username',
'size' => '17'
));
$input_password = new html_passwordfield(array(
'name' => '_password',
'id' => '_password',
'size' => '17'
));
$input_read_only = new html_checkbox(array(
'name' => '_read_only',
'id' => '_read_only',
'value' => 1
));
$input_submit = $rcmail->output->button(array(
'command' => 'plugin.carddav-server-save',
'type' => 'input',
'class' => 'button mainaction',
'label' => 'save'
));
$table = new html_table(array(
'cols' => 6,
'class' => 'carddav_server_list'
));
$table->add_header(array('width' => '13%'), $this->gettext('settings_label'));
$table->add_header(array('width' => '35%'), $this->gettext('server'));
$table->add_header(array('width' => '13%'), $this->gettext('username'));
$table->add_header(array('width' => '13%'), $this->gettext('password'));
$table->add_header(array('width' => '13%'), $this->gettext('settings_read_only'));
$table->add_header(array('width' => '13%'), null);
$table->add(array(), $input_label->show());
$table->add(array(), $input_server_url->show());
$table->add(array(), $input_username->show());
$table->add(array(), $input_password->show());
$table->add(array(), $input_read_only->show());
$table->add(array(), $input_submit);
$boxcontent .= html::div(array('class' => 'carddav_headline'), $this->gettext('settings_server_form'));
$boxcontent .= html::div(array('class' => 'carddav_container'), $table->show());;
}
else
{
$rcmail->output->show_message($this->gettext('settings_curl_not_installed'), 'error');
}
$output = html::div(
array('class' => 'box carddav'),
html::div(array('class' => 'boxtitle'), $this->gettext('settings')).
html::div(array('class' => 'boxcontent', 'id' => 'carddav_server_list'), $this->get_carddav_server_list()).
html::div(array('class' => 'boxcontent'), $boxcontent).
html::div(array('class' => 'boxcontent'), $this->get_carddav_url_list())
);
return $output;
}
/**
* Render a CardDAV server example URL list
*
* @return string $content HTML CardDAV server example URL list
*/
public function get_carddav_url_list()
{
$content = null;
$table = new html_table(array(
'cols' => 2,
'class' => 'carddav_server_list'
));
$table->add(array(), 'DAViCal');
$table->add(array(), 'https://example.com/{resource|principal|username}/{collection}/');
$table->add(array(), 'Apple Addressbook Server');
$table->add(array(), 'https://example.com/addressbooks/users/{resource|principal|username}/{collection}/');
$table->add(array(), 'memotoo');
$table->add(array(), 'https://sync.memotoo.com/cardDAV/');
$table->add(array(), 'SabreDAV');
$table->add(array(), 'https://example.com/addressbooks/{resource|principal|username}/{collection}/');
$table->add(array(), 'ownCloud');
$table->add(array(), 'https://example.com/remote.php/carddav/addressbooks/{resource|principal|username}/{collection}/');
$table->add(array(), 'SOGo');
$table->add(array(), 'https://example.com/SOGo/dav/{resource|principal|username}/Contacts/{collection}/');
$content .= html::div(array('class' => 'carddav_headline example_server_list'), $this->gettext('settings_example_server_list'));
$content .= html::div(array('class' => 'carddav_container'), $table->show());
return $content;
}
/**
* Save CardDAV server and execute first CardDAV contact sync
*
* @return void
*/
public function carddav_server_save()
{
$rcmail = rcmail::get_instance();
if ($this->carddav_server_check_connection())
{
$user_id = $rcmail->user->data['user_id'];
$url = parse_input_value(base64_decode($_POST['_server_url']));
$username = parse_input_value(base64_decode($_POST['_username']));
$password = parse_input_value(base64_decode($_POST['_password']));
$label = parse_input_value(base64_decode($_POST['_label']));
$read_only = (int) parse_input_value(base64_decode($_POST['_read_only']));
$query = "
INSERT INTO
".$rcmail->db->table_name('carddav_server')." (user_id, url, username, password, label, read_only)
VALUES
(?, ?, ?, ?, ?, ?)
";
$rcmail->db->query($query, $user_id, $url, $username, $rcmail->encrypt($password), $label, $read_only);
if ($rcmail->db->affected_rows())
{
$this->carddav_addressbook_sync($rcmail->db->insert_id(), false);
$rcmail->output->command('plugin.carddav_server_message', array(
'server_list' => $this->get_carddav_server_list(),
'message' => $this->gettext('settings_saved'),
'check' => true
));
}
else
{
$rcmail->output->command('plugin.carddav_server_message', array(
'message' => $this->gettext('settings_save_failed'),
'check' => false
));
}
}
else
{
$rcmail->output->command('plugin.carddav_server_message', array(
'message' => $this->gettext('settings_no_connection'),
'check' => false
));
}
}
/**
* Delete CardDAV server and all related local contacts
*
* @return void
*/
public function carddav_server_delete()
{
$rcmail = rcmail::get_instance();
$user_id = $rcmail->user->data['user_id'];
$carddav_server_id = parse_input_value(base64_decode($_POST['_carddav_server_id']));
$query = "
DELETE FROM
".$rcmail->db->table_name('carddav_server')."
WHERE
user_id = ?
AND
carddav_server_id = ?
";
$rcmail->db->query($query, $user_id, $carddav_server_id);
if ($rcmail->db->affected_rows())
{
$rcmail->output->command('plugin.carddav_server_message', array(
'server_list' => $this->get_carddav_server_list(),
'message' => $this->gettext('settings_deleted'),
'check' => true
));
}
else
{
$rcmail->output->command('plugin.carddav_server_message', array(
'message' => $this->gettext('settings_delete_failed'),
'check' => false
));
}
}
/**
* Extended write log with pre defined logfile name and add version before the message content
*
* @param string $message Error log message
* @return void
*/
public function write_log($message)
{
rcmail::write_log('CardDAV', 'v' . self::VERSION . ' | ' . $message);
}
}