| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | require_once(dirname(__FILE__) . '/rcube_kolab_contacts.php'); |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * Kolab address book |
|---|
| 7 | * |
|---|
| 8 | * Sample plugin to add a new address book source with data from Kolab storage |
|---|
| 9 | * |
|---|
| 10 | * This is work-in-progress for the Roundcube+Kolab integration. |
|---|
| 11 | * The library part is to be moved into a separate PEAR package or plugin |
|---|
| 12 | * that this and other Kolab-related plugins will depend on. |
|---|
| 13 | * |
|---|
| 14 | * @author Thomas Bruederli <roundcube@gmail.com> |
|---|
| 15 | * |
|---|
| 16 | */ |
|---|
| 17 | class kolab_addressbook extends rcube_plugin |
|---|
| 18 | { |
|---|
| 19 | private $kolab; |
|---|
| 20 | private $folders; |
|---|
| 21 | private $sources; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Required startup method of a Roundcube plugin |
|---|
| 25 | */ |
|---|
| 26 | public function init() |
|---|
| 27 | { |
|---|
| 28 | // load local config |
|---|
| 29 | $this->load_config(); |
|---|
| 30 | |
|---|
| 31 | $this->add_hook('addressbooks_list', array($this, 'address_sources')); |
|---|
| 32 | $this->add_hook('addressbook_get', array($this, 'get_address_book')); |
|---|
| 33 | $this->add_hook('imap_init', array($this, 'imap_init')); |
|---|
| 34 | |
|---|
| 35 | // extend include path to load bundled Horde classes |
|---|
| 36 | $include_path = $this->home . '/lib' . PATH_SEPARATOR . ini_get('include_path'); |
|---|
| 37 | set_include_path($include_path); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Handler for the addressbooks_list hook. |
|---|
| 42 | * |
|---|
| 43 | * This will add all instances of available Kolab-based address books |
|---|
| 44 | * to the list of address sources of Roundcube. |
|---|
| 45 | * |
|---|
| 46 | * @param array Hash array with hook parameters |
|---|
| 47 | * @return array Hash array with modified hook parameters |
|---|
| 48 | */ |
|---|
| 49 | public function address_sources($p) |
|---|
| 50 | { |
|---|
| 51 | // setup Kolab backend |
|---|
| 52 | rcube_kolab::setup(); |
|---|
| 53 | |
|---|
| 54 | // get all folders that have "contact" type |
|---|
| 55 | $this->kolab = Kolab_List::singleton(); |
|---|
| 56 | $this->folders = $this->kolab->getByType('contact'); |
|---|
| 57 | |
|---|
| 58 | if (PEAR::isError($this->folders)) { |
|---|
| 59 | raise_error(array( |
|---|
| 60 | 'code' => 600, 'type' => 'php', |
|---|
| 61 | 'file' => __FILE__, 'line' => __LINE__, |
|---|
| 62 | 'message' => "Failed to list contact folders from Kolab server:" . $this->folders->getMessage()), |
|---|
| 63 | true, false); |
|---|
| 64 | } |
|---|
| 65 | else { |
|---|
| 66 | foreach ($this->folders as $c_folder) { |
|---|
| 67 | // create instance of rcube_contacts |
|---|
| 68 | $abook_id = strtolower(asciiwords(strtr($c_folder->name, '/.', '--'))); |
|---|
| 69 | $abook = new rcube_kolab_contacts($c_folder->name); |
|---|
| 70 | $this->sources[$abook_id] = $abook; |
|---|
| 71 | |
|---|
| 72 | // register this address source |
|---|
| 73 | $p['sources'][$abook_id] = array( |
|---|
| 74 | 'id' => $abook_id, |
|---|
| 75 | 'name' => $c_folder->name, |
|---|
| 76 | 'readonly' => $abook->readonly, |
|---|
| 77 | 'groups' => $abook->groups, |
|---|
| 78 | ); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return $p; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Getter for the rcube_addressbook instance |
|---|
| 88 | */ |
|---|
| 89 | public function get_address_book($p) |
|---|
| 90 | { |
|---|
| 91 | if ($this->sources[$p['id']]) { |
|---|
| 92 | $p['instance'] = $this->sources[$p['id']]; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | return $p; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Make sure the X-Kolab-Type headers are also fetched when listing messages |
|---|
| 101 | */ |
|---|
| 102 | function imap_init($p) |
|---|
| 103 | { |
|---|
| 104 | $p['fetch_headers'] = strtoupper('X-Kolab-Type'); |
|---|
| 105 | return $p; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|