forked from chilek/google_addressbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_addressbook.php
210 lines (181 loc) · 7.46 KB
/
google_addressbook.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
<?php
/**
* Google Addressbook
*
* Plugin to use google contacts in roundcube mail.
*
* @version 1.0
* @author Stefan L. Wagner
* @url https://github.com/stwa/google-addressbook
*/
require_once(dirname(__FILE__) . '/google_addressbook_backend.php');
require_once(dirname(__FILE__) . '/google_func.php');
class google_addressbook extends rcube_plugin
{
public $task = 'mail|addressbook|settings';
private $abook_id = 'google_addressbook';
private $abook_name = 'Google Addressbook';
function init()
{
$rcmail = rcmail::get_instance();
$this->add_texts('localization/', true);
$this->load_config('config.inc.php.dist');
$this->load_config('config.inc.php');
// register actions
$this->register_action('plugin.google_addressbook.auth', array($this, 'handle_auth_requests'));
$this->register_action('plugin.google_addressbook.sync', array($this, 'handle_sync_requests'));
// register hooks
$this->add_hook('preferences_list', array($this, 'preferences_list'));
$this->add_hook('preferences_save', array($this, 'preferences_save'));
$this->add_hook('addressbooks_list', array($this, 'addressbooks_list'));
$this->add_hook('addressbook_get', array($this, 'addressbook_get'));
$this->add_hook('contact_create', array($this, 'contact_create'));
$this->add_hook('contact_update', array($this, 'contact_update'));
$this->add_hook('contact_delete', array($this, 'contact_delete'));
// add google addressbook to autocomplete addressbooks
$sources = (array) $rcmail->config->get('autocomplete_addressbooks', 'sql');
$sources[] = $this->abook_id;
$rcmail->config->set('autocomplete_addressbooks', $sources);
$this->include_script('google_addressbook.js');
// only call command when in ajax action 'list'
if ($rcmail->output->type == 'js' && $rcmail->action == 'list') {
if($this->is_enabled() && $this->is_autosync() && !isset($_SESSION['google_addressbook_synced'])) {
$rcmail->output->command('plugin.google_addressbook.autosync', array('message' => $this->gettext('done')));
}
}
}
function get_current_token()
{
return google_func::get_current_token(rcmail::get_instance()->user);
}
function save_current_token($token)
{
return google_func::save_current_token(rcmail::get_instance()->user, $token);
}
function is_enabled()
{
return google_func::is_enabled(rcmail::get_instance()->user);
}
function is_autosync()
{
return google_func::is_autosync(rcmail::get_instance()->user);
}
function handle_auth_requests(){
$rcmail = rcmail::get_instance();
if (isset($_GET['error'])){
$rcmail->output->show_message(htmlspecialchars($_GET['error']), 'error');
return;
}
$auth_code = $_GET['code'];
$user = $rcmail->user;
$prefs = array(google_func::$settings_key_auth_code => $auth_code, google_func::$settings_key_token => null);
if(!$user->save_prefs($prefs)) {
rcmail_action::display_server_error('errorsaving');
return;
}
$client = google_func::get_client();
$res = google_func::google_authenticate($client, $user);
$rcmail->output->show_message($res['message'], $res['success'] ? 'confirmation' : 'error');
}
function handle_sync_requests()
{
$this->sync_contacts();
rcmail::get_instance()->output->command('plugin.google_addressbook.finished', array('message' => $this->gettext('done')));
}
function preferences_list($params)
{
$rcmail = rcmail::get_instance();
if($params['section'] == 'addressbook') {
$params['blocks'][$this->ID]['name'] = $this->abook_name;
$field_id = 'rc_use_plugin';
$checkbox = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
$params['blocks'][$this->ID]['options'][$field_id] = array(
'title' => html::label($field_id, $this->gettext('use').$this->abook_name),
'content' => $checkbox->show($rcmail->config->get(google_func::$settings_key_use_plugin))
);
$field_id = 'rc_google_autosync';
$checkbox = new html_checkbox(array('name' => $field_id, 'id' => $field_id, 'value' => 1));
$params['blocks'][$this->ID]['options'][$field_id] = array(
'title' => html::label($field_id, $this->gettext('autosync')),
'content' => $checkbox->show($rcmail->config->get(google_func::$settings_key_auto_sync))
);
$auth_link = array('target' => '_top');
if (!google_func::has_redirect()){
$field_id = 'rc_google_authcode';
$input_auth = new html_inputfield(array('name' => $field_id, 'id' => $field_id, 'size' => 45));
$params['blocks'][$this->ID]['options'][$field_id] = array(
'title' => html::label($field_id, $this->gettext('authcode')),
'content' => $input_auth->show($rcmail->config->get(google_func::$settings_key_auth_code))
);
$auth_link['target'] = '_blank';
}
$auth_link['href'] = google_func::get_client()->createAuthUrl();
$params['blocks'][$this->ID]['options']['link'] = array(
'title' => html::span('', ''),
'content' => html::a($auth_link, $this->gettext('authcodelink'))
);
}
return $params;
}
function preferences_save($params)
{
if($params['section'] == 'addressbook') {
if (!google_func::has_redirect()){
$old_prefs = rcmail::get_instance()->user->get_prefs();
$new_code = rcube_utils::get_input_value('rc_google_authcode', rcube_utils::INPUT_POST);
if($old_prefs[google_func::$settings_key_auth_code] != $new_code) {
// token is no longer valid, so delete it
$this->save_current_token(null);
}
$params['prefs'][google_func::$settings_key_auth_code] = $new_code;
}
$params['prefs'][google_func::$settings_key_use_plugin] = isset($_POST['rc_use_plugin']);
$params['prefs'][google_func::$settings_key_auto_sync] = isset($_POST['rc_google_autosync']);
}
return $params;
}
// roundcube collects information about available addressbooks
function addressbooks_list($params)
{
if($this->is_enabled()) {
$params['sources'][] = array('id' => $this->abook_id,
'name' => $this->abook_name,
'groups' => false,
'readonly' => true,
'autocomplete' => true);
}
return $params;
}
// user opens addressbook
function addressbook_get($params)
{
$rcmail = rcmail::get_instance();
if($params['id'] == $this->abook_id) {
$params['instance'] = new google_addressbook_backend($rcmail->get_dbh(), $rcmail->user->ID);
$params['writable'] = false;
}
return $params;
}
function sync_contacts()
{
$rcmail = rcmail::get_instance();
$_SESSION['google_addressbook_synced'] = true;
$res = google_func::google_sync_contacts($rcmail->user);
$rcmail->output->show_message($res['message'], $res['success'] ? 'confirmation' : 'error');
}
function contact_create($params)
{
rcube::write_log('google_addressbook', 'contact_create: '.print_r($params, true));
// TODO: not supported right now
}
function contact_update($params)
{
rcube::write_log('google_addressbook', 'contact_update: '.print_r($params, true));
// TODO: not supported right now
}
function contact_delete($params)
{
rcube::write_log('google_addressbook', 'contact_delete: '.print_r($params, true));
// TODO: not supported right now
}
}