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_';
29 * Initialization method, needs to be implemented by the plugin itself
33 public function init()
36 $this->add_hook('addressbooks_list', [$this, 'addressbooks_list']);
37 $this->add_hook('addressbook_get', [$this, 'addressbook_get']);
39 $this->rcube = rcube::get_instance();
41 $this->db = rcube_db::factory(
42 $this->rcube->config->get('nextcloud_sql_addressbook_dsn')
44 $this->db->set_debug((bool) $this->rcube->config->get('sql_debug'));
46 $this->prefix = $this->rcube->config->get(
47 'nextcloud_sql_addressbook_dbtableprefix', 'oc_'
50 // use this address books for autocompletion queries
51 $config = rcmail::get_instance()->config;
52 $sources = (array) $config->get(
53 'autocomplete_addressbooks', array('sql')
55 foreach ($this->listAddressbooks() as $addressBook) {
56 if (!in_array($addressBook['id'], $sources)) {
57 $sources[] = $addressBook['id'];
60 $config->set('autocomplete_addressbooks', $sources);
64 * Load the nextcloud address book names
66 * The "id" may not contain any "-" because that would break "_cid",
67 * the "contact IDs" which are "$contactid-$addressbookid".
68 * See rcmail_get_cids()
70 * @param array $arguments Unknown data, with a "sources" key that we have
73 * @return array Arguments with our address books added to the "sources" key
75 public function addressbooks_list($arguments)
77 $arguments['sources'] = array_merge(
78 $arguments['sources'], $this->listAddressbooks()
84 * Build a list of address books for the user
86 * @return array Array of arrays with the following keys:
87 * id, name, groups, readonly, undelete, autocomplete
89 protected function listAddressbooks()
91 $principalUri = 'principals/users/'
92 . $this->rcube->user->data['username'];
94 $sql = 'SELECT id, displayname'
95 . ' FROM ' . $this->prefix . 'addressbooks'
96 . ' WHERE principaluri = ?'
97 . ' ORDER BY displayname';
98 $stmt = $this->db->query($sql, [$principalUri]);
100 foreach ($stmt as $row) {
102 'id' => 'nextcloud_' . $row['id'],
103 'name' => $row['displayname'] . ' (Nextcloud)',
107 'autocomplete' => true,
110 return $addressBooks;
114 * Return a adress book object for the given address book ID
116 * @param array $arguments Some data with an "id" key that contains the
119 * @return array $arguments with added "instance" key
121 public function addressbook_get($arguments)
123 $parts = explode('_', $arguments['id'], 2);
124 if (count($parts) != 2 || $parts[0] != 'nextcloud') {
129 //FIXME: security check if this ID really belongs to the user
131 $arguments['instance'] = new nextcloud_sql_addressbook_backend(
132 $id, $this->db, $this->prefix