1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
<?php
require_once 'Horde/Kolab.php';
require_once 'Horde/Data.php';
/**
* Horde Turba driver for the Kolab IMAP Server.
* Copyright 2004-2006 Horde Project (http://horde.org/)
*
* $Horde: turba/lib/Driver/kolab.php,v 1.5.10.8 2006/01/01 21:29:16 jan Exp $
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/asl.php.
*
* @author Stuart Binge <omicron@mighty.co.za>
* @package Turba
*/
class Turba_Driver_kolab extends Turba_Driver {
/**
* Our Kolab server connection.
*
* @var Kolab
*/
var $_kolab = null;
function _init()
{
if (isset($this->_kolab)) {
return true;
}
$this->_kolab = new Kolab();
return $this->_kolab->open($this->_params['share']);
}
function _disconnect()
{
$this->_kolab->close();
$this->_kolab = null;
}
function _buildContact()
{
$k = &$this->_kolab;
$contact = array(
'uid' => $k->getUID(),
'owner' => Auth::getAuth(),
'job-title' => $k->getStr('job-title'),
'organization' => $k->getStr('organization'),
'body' => $k->getStr('body'),
'web-page' => $k->getStr('web-page'),
'nick-name' => $k->getStr('nick-name'),
);
$name = &$k->getRootElem('name');
$contact['full-name'] = $k->getElemStr($name, 'full-name');
$contact['given-name'] = $k->getElemStr($name, 'given-name');
$contact['last-name'] = $k->getElemStr($name, 'last-name');
$email = &$k->getRootElem('email');
$contact['smtp-address'] = $k->getElemStr($email, 'smtp-address');
$phones = &$k->getAllRootElems('phone');
for ($i = 0, $j = count($phones); $i < $j; $i++) {
$phone = &$phones[$i];
$type = $k->getElemStr($phone, 'type');
switch ($type) {
case 'home1':
$contact['home1'] = $k->getElemStr($phone, 'number');
break;
case 'business1':
$contact['business1'] = $k->getElemStr($phone, 'number');
break;
case 'mobile':
$contact['mobile'] = $k->getElemStr($phone, 'number');
break;
case 'businessfax':
$contact['businessfax'] = $k->getElemStr($phone, 'number');
break;
}
}
$addresses = &$k->getAllRootElems('address');
for ($i = 0, $j = count($addresses); $i < $j; $i++) {
$address = &$addresses[$i];
$type = $k->getElemStr($address, 'type');
switch ($type) {
case 'home':
$contact['home-street'] = $k->getElemStr($address, 'street');
$contact['home-locality'] = $k->getElemStr($address, 'locality');
$contact['home-region'] = $k->getElemStr($address, 'region');
$contact['home-postal-code'] = $k->getElemStr($address, 'postal-code');
$contact['home-country'] = $k->getElemStr($address, 'country');
break;
case 'business':
$contact['business-street'] = $k->getElemStr($address, 'street');
$contact['business-locality'] = $k->getElemStr($address, 'locality');
$contact['business-region'] = $k->getElemStr($address, 'region');
$contact['business-postal-code'] = $k->getElemStr($address, 'postal-code');
$contact['business-country'] = $k->getElemStr($address, 'country');
break;
}
}
return $contact;
}
function _setPhone($type, &$phone, $attributes)
{
if (empty($attributes[$type])) {
$this->_kolab->delRootElem($phone);
} else {
if ($phone === false) {
$phone = &$this->_kolab->appendRootElem('phone');
$this->_kolab->setElemStr($phone, 'type', $type);
}
$this->_kolab->setElemStr($phone, 'number', $attributes[$type]);
}
}
function _setAddress($type, &$address, $attributes)
{
if (empty($attributes["$type-street"]) && empty($attributes["$type-locality"]) &&
empty($attributes["$type-region"]) && empty($attributes["$type-postal-code"]) &&
empty($attributes["$type-country"])) {
$this->_kolab->delRootElem($address);
} else {
if ($address === false) {
$address = &$this->_kolab->appendRootElem('address');
$this->_kolab->setElemStr($address, 'type', $type);
}
$this->_kolab->setElemStr($address, 'street', $attributes["$type-street"]);
$this->_kolab->setElemStr($address, 'locality', $attributes["$type-locality"]);
$this->_kolab->setElemStr($address, 'region', $attributes["$type-region"]);
$this->_kolab->setElemStr($address, 'postal-code', $attributes["$type-postal-code"]);
$this->_kolab->setElemStr($address, 'country', $attributes["$type-country"]);
}
}
function _createContact(&$xml, $attributes)
{
$k = &$this->_kolab;
$name = &$k->initRootElem('name');
$k->setElemStr($name, 'full-name', $attributes['full-name']);
$k->setElemStr($name, 'given-name', $attributes['given-name']);
$k->setElemStr($name, 'last-name', $attributes['last-name']);
$email = &$k->initRootElem('email');
$k->setElemStr($email, 'display-name', $attributes['full-name']);
$k->setElemStr($email, 'smtp-address', $attributes['smtp-address']);
$k->setStr('job-title', $attributes['job-title']);
$k->setStr('organization', $attributes['organization']);
$k->setStr('body', $attributes['body']);
$k->setStr('web-page', $attributes['web-page']);
$k->setStr('nick-name', $attributes['nick-name']);
// Phones
$phones = &$k->getAllRootElems('phone');
$home = false;
$bus = false;
$mob = false;
$fax = false;
for ($i = 0, $j = count($phones); $i < $j; $i++) {
$phone = &$phones[$i];
$type = $k->getElemStr($phone, 'type');
switch ($type) {
case 'home1':
$home = &$phone;
break;
case 'business1':
$bus = &$phone;
break;
case 'mobile':
$mob = &$phone;
break;
case 'businessfax':
$fax = &$phone;
break;
}
}
$this->_setPhone('home1', $home, $attributes);
$this->_setPhone('business1', $bus, $attributes);
$this->_setPhone('mobile', $mob, $attributes);
$this->_setPhone('businessfax', $fax, $attributes);
// Addresses
$home = false;
$bus = false;
$addresses = &$k->getAllRootElems('address');
for ($i = 0, $j = count($addresses); $i < $j; $i++) {
$address = &$addresses[$i];
$type = $k->getElemStr($address, 'type');
switch ($type) {
case 'home':
$home = &$address;
break;
case 'business':
$bus = &$address;
break;
}
}
$this->_setAddress('home', $home, $attributes);
$this->_setAddress('business', $bus, $attributes);
}
/**
* Searches the Kolab message store with the given criteria and returns a
* filtered list of results. If the criteria parameter is an empty
* array, all records will be returned.
*
* @param $criteria Array containing the search criteria.
* @param $fields List of fields to return.
*
* @return Hash containing the search results.
*/
function _search($criteria, $fields)
{
$query = $this->_buildIMAPSearch($criteria);
$results = array();
$msg_list = $this->_kolab->findObjects($query);
if (is_a($msg_list, 'PEAR_Error') || empty($msg_list)) {
return $results;
}
foreach ($msg_list as $msg) {
$result = $this->_kolab->loadObject($msg, true);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
$contact = $this->_buildContact();
$card = array();
foreach ($fields as $field) {
$card[$field] = (isset($contact[$field]) ? $contact[$field] : '');
}
$results[] = $card;
}
return $results;
}
/**
* Read the given data from the Kolab message store and returns the
* result's fields.
*
* @param $criteria Search criteria.
* @param $id Data identifier.
* @param $fields List of fields to return.
*
* @return Hash containing the search results.
*/
function _read($criteria, $id_list, $fields)
{
$results = array();
if ($criteria != 'uid') {
return $results;
}
if (!is_array($id_list)) {
$id_list = array($id_list);
}
foreach ($id_list as $id) {
$result = $this->_kolab->loadObject($id);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
$contact = $this->_buildContact($result);
$card = array();
foreach ($fields as $field) {
$card[$field] = (isset($contact[$field]) ? $contact[$field] : '');
}
$results[] = $card;
}
return $results;
}
/**
* Adds the specified object to the Kolab message store.
*/
function _add($attributes)
{
$xml = &$this->_kolab->newObject($attributes['uid']);
if (is_a($xml, 'PEAR_Error')) {
return $xml;
}
$this->_createContact($xml, $attributes);
return $this->_kolab->saveObject();
}
/**
* Removes the specified object from the Kolab message store.
*/
function _delete($object_key, $object_id)
{
if ($object_key != 'uid') {
return false;
}
return $this->_kolab->removeObjects($object_id);
}
/**
* Updates an existing object in the Kolab message store.
*
* @return string The object id, possibly updated.
*/
function _save($object_key, $object_id, $attributes)
{
if ($object_key != 'uid') {
return PEAR::raiseError('key must be uid');
}
$xml = &$this->_kolab->loadObject($object_id);
if (is_a($xml, 'PEAR_Error')) {
return $xml;
}
$this->_createContact($xml, $attributes);
$result = $this->_kolab->saveObject();
if (is_a($result, 'PEAR_Error')) {
return $result;
}
return $object_id;
}
/**
* Create an object key for a new object.
*
* @param array $attributes The attributes (in driver keys) of the
* object being added.
*
* @return string A unique ID for the new object.
*/
function _makeKey($attributes)
{
return $this->generateUID();
}
/**
* Converts Turba search criteria into a comparable IMAP search string
*
* @param array $criteria The search criteria.
*
* @return string The IMAP search string corresponding to $criteria.
*/
function _buildIMAPSearch($criteria)
{
$values = array_values($criteria);
$values = $values[0];
$query = 'HEADER "' . KOLAB_HEADER_TYPE . '" "' . $this->_kolab->getMimeType() . '" ';
for ($current = 0; $current < count($values); $current++) {
$temp = $values[$current];
while (!empty($temp) && !array_key_exists('field', $temp)) {
$temp = array_values($temp);
$temp = $temp[0];
}
if (empty($temp)) continue;
$searchkey = $temp['field'];
$searchval = $temp['test'];
switch ($searchkey) {
case 'owner':
$query .= 'FROM "' . $searchval . '" ';
break;
default:
if (!empty($searchkey)) {
$query .= 'BODY "' . $searchkey . '" ';
}
if (!empty($searchval)) {
$query .= 'BODY "' . $searchval . '" ';
}
}
}
return trim($query);
}
}
|