add links
[roundcube-nextcloud_sql_addressbook.git] / nextcloud_sql_addressbook.php
1 <?php
2 require_once __DIR__ . '/nextcloud_sql_addressbook_backend.php';
3
4 /**
5  * Make a user's Nextcloud address books available in Roundcube.
6  *
7  * Directly accesses the database, which is much faster than using CardDAV.
8  *
9  * @author  Christian Weiske <cweiske@cweiske.de>
10  * @license AGPLv3+ http://www.gnu.org/licenses/agpl.html
11  */
12 class nextcloud_sql_addressbook extends rcube_plugin
13 {
14     /**
15      * Main roundcube instance
16      *
17      * @var rcube
18      */
19     protected $rcube;
20
21     /**
22      * Database table prefix
23      *
24      * @var string
25      */
26     protected $prefix = 'oc_';
27     
28     /**
29      * Initialization method, needs to be implemented by the plugin itself
30      *
31      * @return void
32      */
33     public function init()
34     {
35         $this->load_config();
36         $this->add_hook('addressbooks_list', [$this, 'addressbooks_list']);
37         $this->add_hook('addressbook_get', [$this, 'addressbook_get']);
38
39         $this->rcube = rcube::get_instance();
40         
41         $this->db = rcube_db::factory(
42             $this->rcube->config->get('nextcloud_sql_addressbook_dsn')
43         );
44         $this->db->set_debug((bool) $this->rcube->config->get('sql_debug'));
45         
46         $this->prefix = $this->rcube->config->get(
47             'nextcloud_sql_addressbook_dbtableprefix', 'oc_'
48         );
49
50         // use this address books for autocompletion queries
51         $config = rcmail::get_instance()->config;
52         $sources = (array) $config->get(
53             'autocomplete_addressbooks', array('sql')
54         );
55         foreach ($this->listAddressbooks() as $addressBook) {
56             if (!in_array($addressBook['id'], $sources)) {
57                 $sources[] = $addressBook['id'];
58             }
59         }
60         $config->set('autocomplete_addressbooks', $sources);
61     }
62
63     /**
64      * Load the nextcloud address book names
65      *
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()
69      *
70      * @param array $arguments Unknown data, with a "sources" key that we have
71      *                         to modify
72      *
73      * @return array Arguments with our address books added to the "sources" key
74      */
75     public function addressbooks_list($arguments)
76     {
77         $arguments['sources'] = array_merge(
78             $arguments['sources'], $this->listAddressbooks()
79         );
80         return $arguments;
81     }
82
83     /**
84      * Build a list of address books for the user
85      *
86      * @return array Array of arrays with the following keys:
87      *               id, name, groups, readonly, undelete, autocomplete
88      */
89     protected function listAddressbooks()
90     {
91         $principalUri = 'principals/users/'
92             . $this->rcube->user->data['username'];
93
94         $sql = 'SELECT id, displayname'
95              . ' FROM ' . $this->prefix . 'addressbooks'
96              . ' WHERE principaluri = ?'
97              . ' ORDER BY displayname';
98         $stmt = $this->db->query($sql, [$principalUri]);
99         $addressBooks = [];
100         foreach ($stmt as $row) {
101             $addressBooks[] = [
102                 'id'           => 'nextcloud_' . $row['id'],
103                 'name'         => $row['displayname'] . ' (Nextcloud)',
104                 'groups'       => false,
105                 'readonly'     => true,
106                 'undelete'     => false,
107                 'autocomplete' => true,
108             ];
109         }
110         return $addressBooks;
111     }
112
113     /**
114      * Return a adress book object for the given address book ID
115      *
116      * @param array $arguments Some data with an "id" key that contains the
117      *                         address book ID
118      *
119      * @return array $arguments with added "instance" key
120      */
121     public function addressbook_get($arguments)
122     {
123         $parts = explode('_', $arguments['id'], 2);
124         if (count($parts) != 2 || $parts[0] != 'nextcloud') {
125             return $arguments;
126         }
127
128         $id = $parts[1];
129         //FIXME: security check if this ID really belongs to the user
130
131         $arguments['instance'] = new nextcloud_sql_addressbook_backend(
132             $id, $this->db, $this->prefix
133         );
134         
135         return $arguments;
136     }
137 }
138 ?>