summaryrefslogtreecommitdiff
path: root/webmail/plugins/carddav/cronjob/synchronize.php
blob: 52cfff9efa85e3a26edd9494b23bab4b35ba9d71 (plain)
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
<?php

/**
 * Roundcube CardDAV synchronization
 *
 * @author Christian Putzke <christian.putzke@graviox.de>
 * @copyright Christian Putzke @ Graviox Studios
 * @since 31.03.2012
 * @link http://www.graviox.de/
 * @link https://twitter.com/graviox/
 * @version 0.5
 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
 *
 */
class carddav_synchronization_cronjob
{
	/**
	 * @var	string	$doc_root	Roundcubes document root
	 */
	private	$doc_root;

	/**
	 * @var	string	$rc_inset	Roundcubes iniset script
	 */
	private	$rc_iniset = 'program/include/iniset.php';

	/**
	 * Init CardDAV synchronization cronjob
	 *
	 * @param	boolean	$init	Initialization
	 */
	public function __construct($init = true)
	{
		if ($init === true)
		{
			$this->init();
		}
	}

	/**
	 * Init CardDAV synchronization cronjob
	 *
	 * @return	void
	 */
	public function init()
	{
		$this->detect_document_root();
		$this->include_rc_iniset();
	}

	/**
	 * Detect Roundcubes real document root
	 *
	 * @return	void
	 */
	private function detect_document_root()
	{
		$dir = dirname(__FILE__);
		$dir = str_replace('plugins\\carddav\\cronjob', null, $dir);
		$this->doc_root = str_replace('plugins/carddav/cronjob', null, $dir);
		define('INSTALL_PATH', $this->doc_root);
	}

	/**
	 * Include Roundcubes initset so that the internal Roundcube functions can be used inside this cronjob
	 *
	 * @return	void
	 */
	private  function include_rc_iniset()
	{
		if (file_exists($this->doc_root . $this->rc_iniset))
		{
			chdir($this->doc_root);
			require_once $this->doc_root . '/program/include/iniset.php';
		}
		else
		{
			die('Can\'t detect file path correctly! I got this as Roundcubes document root: '. $this->doc_root);
		}
	}

	/**
	 * Get all CardDAV servers
	 *
	 * @return	array	$servers	CardDAV server array with label, url, username, password (encrypted)
	 */
	private function get_carddav_servers()
	{
		$servers	= array();
		$rcmail		= rcmail::get_instance();

		$query = "
			SELECT
				*
			FROM
				".get_table_name('carddav_server')."
		";

		$result = $rcmail->db->query($query);

		while($server = $rcmail->db->fetch_assoc($result))
		{
			$servers[] = $server;
		}

		return $servers;
	}

	/**
	 * Synchronize all available CardDAV servers
	 *
	 * @return	void
	 */
	public function synchronize()
	{
		$servers	= $this->get_carddav_servers();
		$rcmail		= rcmail::get_instance();

		carddav::write_log('CRONJOB: Starting automatic CardDAV synchronization!');

		if (!empty($servers))
		{
			foreach ($servers as $server)
			{
				$rcmail->user->data['user_id'] = $server['user_id'];
				$carddav_addressbook = new carddav_addressbook($server['carddav_server_id'], $server['label'], false);
				$carddav_addressbook->carddav_addressbook_sync($server);
			}
		}

		carddav::write_log('CRONJOB: Automatic CardDAV synchronization finished!');
	}
}

$cronjob = new carddav_synchronization_cronjob();
$cronjob->synchronize();