+
+
+ /**
+ * Return instance of the internal address book class
+ *
+ * @param string Address book identifier
+ * @param boolean True if the address book needs to be writeable
+ * @return rcube_contacts Address book object
+ */
+ public function get_address_book($id, $writeable = false)
+ {
+ $contacts = null;
+ $ldap_config = (array)$this->config->get('ldap_public');
+ $abook_type = strtolower($this->config->get('address_book_type'));
+
+ $plugin = $this->plugins->exec_hook('addressbook_get', array('id' => $id, 'writeable' => $writeable));
+
+ // plugin returned instance of a rcube_addressbook
+ if ($plugin['instance'] instanceof rcube_addressbook) {
+ $contacts = $plugin['instance'];
+ }
+ else if ($id && $ldap_config[$id]) {
+ $contacts = new rcube_ldap($ldap_config[$id], $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host']));
+ }
+ else if ($id === '0') {
+ $contacts = new rcube_contacts($this->db, $this->user->ID);
+ }
+ else if ($abook_type == 'ldap') {
+ // Use the first writable LDAP address book.
+ foreach ($ldap_config as $id => $prop) {
+ if (!$writeable || $prop['writable']) {
+ $contacts = new rcube_ldap($prop, $this->config->get('ldap_debug'), $this->config->mail_domain($_SESSION['imap_host']));
+ break;
+ }
+ }
+ }
+ else { // $id == 'sql'
+ $contacts = new rcube_contacts($this->db, $this->user->ID);
+ }
+
+ // add to the 'books' array for shutdown function
+ if (!in_array($contacts, $this->books))
+ $this->books[] = $contacts;
+
+ return $contacts;
+ }
+
+
+ /**
+ * Return address books list
+ *
+ * @param boolean True if the address book needs to be writeable
+ * @return array Address books array
+ */
+ public function get_address_sources($writeable = false)
+ {
+ $abook_type = strtolower($this->config->get('address_book_type'));
+ $ldap_config = $this->config->get('ldap_public');
+ $autocomplete = (array) $this->config->get('autocomplete_addressbooks');
+ $list = array();
+
+ // We are using the DB address book
+ if ($abook_type != 'ldap') {
+ $contacts = new rcube_contacts($this->db, null);
+ $list['0'] = array(
+ 'id' => 0,
+ 'name' => rcube_label('personaladrbook'),
+ 'groups' => $contacts->groups,
+ 'readonly' => false,
+ 'autocomplete' => in_array('sql', $autocomplete)
+ );
+ }
+
+ if ($ldap_config) {
+ $ldap_config = (array) $ldap_config;
+ foreach ($ldap_config as $id => $prop)
+ $list[$id] = array(
+ 'id' => $id,
+ 'name' => $prop['name'],
+ 'groups' => false,
+ 'readonly' => !$prop['writable'],
+ 'autocomplete' => in_array('sql', $autocomplete)
+ );
+ }
+
+ $plugin = $this->plugins->exec_hook('addressbooks_list', array('sources' => $list));
+ $list = $plugin['sources'];
+
+ if ($writeable && !empty($list)) {
+ foreach ($list as $idx => $item) {
+ if ($item['readonly']) {
+ unset($list[$idx]);
+ }
+ }
+ }
+
+ return $list;
+ }
+
+