2 require_once __DIR__ . '/nextcloud_sql_addressbook_backend.php';
5 * Make a user's Nextcloud address books available in Roundcube.
7 * Directly accesses the database, which is much faster than using CardDAV.
9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @license AGPLv3+ http://www.gnu.org/licenses/agpl.html
12 class nextcloud_sql_addressbook extends rcube_plugin
15 * Main roundcube instance
22 * Database table prefix
26 protected $prefix = 'oc_';
34 * Initialization method, needs to be implemented by the plugin itself
38 public function init()
41 $this->add_hook('addressbooks_list', [$this, 'addressbooks_list']);
42 $this->add_hook('addressbook_get', [$this, 'addressbook_get']);
44 $this->rcube = rcube::get_instance();
46 $this->db = rcube_db::factory(
47 $this->rcube->config->get('nextcloud_sql_addressbook_dsn')
49 $this->db->set_debug((bool) $this->rcube->config->get('sql_debug'));
51 $this->prefix = $this->rcube->config->get(
52 'nextcloud_sql_addressbook_dbtableprefix', 'oc_'
55 // use this address books for autocompletion queries
56 $config = rcmail::get_instance()->config;
57 $sources = (array) $config->get(
58 'autocomplete_addressbooks', array('sql')
60 foreach ($this->listAddressbooks() as $addressBook) {
61 if (!in_array($addressBook['id'], $sources)) {
62 $sources[] = $addressBook['id'];
65 $config->set('autocomplete_addressbooks', $sources);
69 * Load the nextcloud address book names
71 * The "id" may not contain any "-" because that would break "_cid",
72 * the "contact IDs" which are "$contactid-$addressbookid".
73 * See rcmail_get_cids()
75 * @param array $arguments Unknown data, with a "sources" key that we have
78 * @return array Arguments with our address books added to the "sources" key
80 public function addressbooks_list($arguments)
82 $arguments['sources'] = array_merge(
83 $arguments['sources'], $this->listAddressbooks()
89 * Build a list of address books for the user
91 * @return array Array of arrays with the following keys:
92 * id, name, groups, readonly, undelete, autocomplete
94 protected function listAddressbooks()
96 if (!isset($this->rcube->user->data)) {
100 $principalUri = 'principals/users/'
101 . $this->rcube->user->data['username'];
103 $sql = 'SELECT id, displayname'
104 . ' FROM ' . $this->prefix . 'addressbooks'
105 . ' WHERE principaluri = ?'
106 . ' ORDER BY displayname';
107 $stmt = $this->db->query($sql, [$principalUri]);
109 foreach ($stmt as $row) {
111 'id' => 'nextcloud_' . $row['id'],
112 'name' => $row['displayname'] . ' (Nextcloud)',
116 'autocomplete' => true,
119 return $addressBooks;
123 * Return a adress book object for the given address book ID
125 * @param array $arguments Some data with an "id" key that contains the
128 * @return array $arguments with added "instance" key
130 public function addressbook_get($arguments)
132 if (!isset($arguments['id'])) {
135 $parts = explode('_', $arguments['id'], 2);
136 if (count($parts) != 2 || $parts[0] != 'nextcloud') {
141 //FIXME: security check if this ID really belongs to the user
143 $arguments['instance'] = new nextcloud_sql_addressbook_backend(
144 $id, $this->db, $this->prefix