|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * include required carddav classes |
| 5 | + */ |
| 6 | +require_once dirname(__FILE__).'/carddav_backend.php'; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Roundcube cardDAV implementation |
| 11 | + * |
| 12 | + * This is a cardDAV implementation for roundcube 0.6 or higher. It allows every user to add |
| 13 | + * multiple cardDAV server in their settings. The cardDAV contacts will be synchronized |
| 14 | + * automaticly with their addressbook. |
| 15 | + * |
| 16 | + * |
| 17 | + * @author Christian Putzke <[email protected]> |
| 18 | + * @copyright Graviox Studios |
| 19 | + * @since 06.09.2011 |
| 20 | + * @version 0.1 |
| 21 | + * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later |
| 22 | + * |
| 23 | + */ |
| 24 | +class carddav extends rcube_plugin |
| 25 | +{ |
| 26 | + public $task = 'settings|addressbook'; |
| 27 | + |
| 28 | + public function init() |
| 29 | + { |
| 30 | + $rcmail = rcmail::get_instance(); |
| 31 | + $this->add_texts('localization/', true); |
| 32 | + |
| 33 | + switch ($rcmail->task) |
| 34 | + { |
| 35 | + case 'settings': |
| 36 | + $this->register_action('plugin.carddav-server', array($this, 'carddav_server')); |
| 37 | + $this->register_action('plugin.carddav-server-check', array($this, 'carddav_server_check')); |
| 38 | + $this->register_action('plugin.carddav-server-save', array($this, 'carddav_server_save')); |
| 39 | + $this->register_action('plugin.carddav-server-delete', array($this, 'carddav_server_delete')); |
| 40 | + $this->include_script('carddav_settings.js'); |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + protected function get_carddav_server() |
| 46 | + { |
| 47 | + $servers = array(); |
| 48 | + $rcmail = rcmail::get_instance(); |
| 49 | + $user_id = $rcmail->user->data['user_id']; |
| 50 | + |
| 51 | + $query = " |
| 52 | + SELECT |
| 53 | + * |
| 54 | + FROM |
| 55 | + ".get_table_name('carddav_server')." |
| 56 | + WHERE |
| 57 | + user_id = ? |
| 58 | + "; |
| 59 | + |
| 60 | + $result = $rcmail->db->query($query, $user_id); |
| 61 | + |
| 62 | + while($server = $rcmail->db->fetch_assoc($result)) |
| 63 | + { |
| 64 | + $servers[] = $server; |
| 65 | + } |
| 66 | + |
| 67 | + return $servers; |
| 68 | + } |
| 69 | + |
| 70 | + public function carddav_server() |
| 71 | + { |
| 72 | + $this->register_handler('plugin.body', array($this, 'carddav_server_form')); |
| 73 | + |
| 74 | + $rcmail = rcmail::get_instance(); |
| 75 | + $rcmail->output->set_pagetitle($this->gettext('settings')); |
| 76 | + $rcmail->output->send('plugin'); |
| 77 | + } |
| 78 | + |
| 79 | + public function carddav_server_check() |
| 80 | + { |
| 81 | + $rcmail = rcmail::get_instance(); |
| 82 | + $url = get_input_value('_server_url', RCUBE_INPUT_POST); |
| 83 | + $username = get_input_value('_username', RCUBE_INPUT_POST); |
| 84 | + $password = get_input_value('_password', RCUBE_INPUT_POST); |
| 85 | + |
| 86 | + $carddav_backend = new carddav_backend($url); |
| 87 | + $carddav_backend->set_auth($username, $password); |
| 88 | + |
| 89 | + if ($carddav_backend->check_connection() === true) |
| 90 | + { |
| 91 | + $rcmail->output->command('plugin.carddav_server_check', array('check' => 'true')); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + $rcmail->output->command('plugin.carddav_server_check', array('check' => 'false')); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public function carddav_server_form() |
| 100 | + { |
| 101 | + $rcmail = rcmail::get_instance(); |
| 102 | + $servers = $this->get_carddav_server(); |
| 103 | + |
| 104 | + $table = new html_table(array('cols' => 5)); |
| 105 | + $table->add('title', $this->gettext('settings_label')); |
| 106 | + $table->add('title', $this->gettext('server')); |
| 107 | + $table->add('title', $this->gettext('username')); |
| 108 | + $table->add('title', $this->gettext('password')); |
| 109 | + $table->add(null, null); |
| 110 | + |
| 111 | + if (!empty($servers)) |
| 112 | + { |
| 113 | + foreach ($servers as $server) |
| 114 | + { |
| 115 | + $table->add(null, $server['label']); |
| 116 | + $table->add(null, $server['url']); |
| 117 | + $table->add(null, $server['username']); |
| 118 | + $table->add(null, '**********'); |
| 119 | + $table->add(null, html::a(array('href' => './?_task=settings&_action=plugin.carddav-server-delete&id='.$server['carddav_server_id']), 'delete')); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + $input_label = new html_inputfield(array( |
| 124 | + 'name' => '_label', |
| 125 | + 'id' => '_label', |
| 126 | + 'size' => '20' |
| 127 | + )); |
| 128 | + |
| 129 | + $input_server_url = new html_inputfield(array( |
| 130 | + 'name' => '_server_url', |
| 131 | + 'id' => '_server_url', |
| 132 | + 'size' => '50' |
| 133 | + )); |
| 134 | + |
| 135 | + $input_username = new html_inputfield(array( |
| 136 | + 'name' => '_username', |
| 137 | + 'id' => '_username', |
| 138 | + 'size' => '20' |
| 139 | + )); |
| 140 | + |
| 141 | + $input_password = new html_passwordfield(array( |
| 142 | + 'name' => '_password', |
| 143 | + 'id' => '_password', |
| 144 | + 'size' => '20' |
| 145 | + )); |
| 146 | + |
| 147 | + $input_submit = $rcmail->output->button(array( |
| 148 | + 'command' => 'plugin.carddav-server-save', |
| 149 | + 'type' => 'input', |
| 150 | + 'class' => 'button mainaction', |
| 151 | + 'label' => 'save' |
| 152 | + )); |
| 153 | + |
| 154 | + $table->add(null, $input_label->show()); |
| 155 | + $table->add(null, $input_server_url->show()); |
| 156 | + $table->add(null, $input_username->show()); |
| 157 | + $table->add(null, $input_password->show()); |
| 158 | + $table->add(null, $input_submit); |
| 159 | + |
| 160 | + $rcmail->output->add_gui_object('carddavserverform', 'carddav-server-form'); |
| 161 | + |
| 162 | + $out = html::div( |
| 163 | + array('class' => 'box'), |
| 164 | + html::div(array('class' => 'boxtitle'), $this->gettext('settings')). |
| 165 | + html::div(array('class' => 'boxcontent'), $table->show()) |
| 166 | + ); |
| 167 | + |
| 168 | + return $rcmail->output->form_tag(array( |
| 169 | + 'id' => 'carddav-server-form', |
| 170 | + 'name' => 'carddav-server-form', |
| 171 | + 'method' => 'post', |
| 172 | + 'action' => './?_task=settings&_action=plugin.carddav-server-save', |
| 173 | + ), $out); |
| 174 | + } |
| 175 | + |
| 176 | + public function carddav_server_save() |
| 177 | + { |
| 178 | + $rcmail = rcmail::get_instance(); |
| 179 | + $user_id = $rcmail->user->data['user_id']; |
| 180 | + $url = get_input_value('_server_url', RCUBE_INPUT_POST); |
| 181 | + $username = get_input_value('_username', RCUBE_INPUT_POST); |
| 182 | + $password = get_input_value('_password', RCUBE_INPUT_POST); |
| 183 | + $label = get_input_value('_label', RCUBE_INPUT_POST); |
| 184 | + |
| 185 | + $query = " |
| 186 | + INSERT INTO |
| 187 | + ".get_table_name('carddav_server')." (user_id, url, username, password, label) |
| 188 | + VALUES |
| 189 | + (?, ?, ?, ?, ?) |
| 190 | + "; |
| 191 | + |
| 192 | + $rcmail->db->query($query, $user_id, $url, $username, $rcmail->encrypt($password), $label); |
| 193 | + |
| 194 | + if ($rcmail->db->affected_rows()) |
| 195 | + { |
| 196 | + $rcmail->output->show_message($this->gettext('settings_saved'), 'confirmation'); |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + $rcmail->output->show_message($this->gettext('settings_save_failed'), 'error'); |
| 201 | + } |
| 202 | + |
| 203 | + return $this->carddav_server(); |
| 204 | + } |
| 205 | + |
| 206 | + public function carddav_server_delete() |
| 207 | + { |
| 208 | + $rcmail = rcmail::get_instance(); |
| 209 | + $user_id = $rcmail->user->data['user_id']; |
| 210 | + $carddav_server_id = $_GET['id']; |
| 211 | + |
| 212 | + $query = " |
| 213 | + DELETE FROM |
| 214 | + ".get_table_name('carddav_server')." |
| 215 | + WHERE |
| 216 | + user_id = ? |
| 217 | + AND |
| 218 | + carddav_server_id = ? |
| 219 | + "; |
| 220 | + |
| 221 | + $rcmail->db->query($query, $user_id, $carddav_server_id); |
| 222 | + |
| 223 | + if ($rcmail->db->affected_rows()) |
| 224 | + { |
| 225 | + $rcmail->output->show_message($this->gettext('settings_deleted'), 'confirmation'); |
| 226 | + } |
| 227 | + else |
| 228 | + { |
| 229 | + $rcmail->output->show_message($this->gettext('settings_delete_failed'), 'error'); |
| 230 | + } |
| 231 | + |
| 232 | + return $this->carddav_server(); |
| 233 | + } |
| 234 | +} |
0 commit comments