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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
|
<?php
/**
* Turba external API interface.
*
* $Horde: turba/lib/api.php,v 1.120.2.4 2005/01/24 11:01:27 jan Exp $
*
* This file defines Turba's external API interface. Other
* applications can interact with Turba through this API.
*
* @package Turba
*/
$_services['perms'] = array(
'args' => array(),
'type' => '{urn:horde}stringArray'
);
$_services['show'] = array(
'link' => '%application%/display.php?source=|source|&key=|key|&uid=|uid|',
);
$_services['browse'] = array(
'args' => array('path' => 'string'),
'type' => '{urn:horde}stringArray',
);
$_services['sources'] = array(
'args' => array('writeable' => 'boolean'),
'type' => '{urn:horde}stringArray',
);
$_services['fields'] = array(
'args' => array('source' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['list'] = array(
'args' => array(),
'type' => '{urn:horde}stringArray',
);
$_services['listBy'] = array(
'args' => array('action' => 'string', 'timestamp' => 'int'),
'type' => '{urn:horde}stringArray',
);
$_services['import'] = array(
'args' => array('content' => 'string', 'contentType' => 'string', 'source' => 'string'),
'type' => 'string',
);
$_services['export'] = array(
'args' => array('uid' => 'string', 'contentType' => 'string'),
'type' => 'string',
);
$_services['delete'] = array(
'args' => array('uid' => 'string'),
'type' => 'boolean',
);
$_services['replace'] = array(
'args' => array('uid' => 'string', 'content' => 'string', 'contentType' => 'string'),
'type' => 'boolean',
);
$_services['search'] = array(
'args' => array('addresses' => '{urn:horde}stringArray', 'sources' => '{urn:horde}stringArray', 'fields' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['getContact'] = array(
'args' => array('source' => 'string', 'objectId' => 'string'),
'type' => '{urn:horde}stringArray',
);
$_services['getContacts'] = array(
'args' => array('source' => 'string', 'objectIds' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['addField'] = array(
'args' => array('address' => 'string', 'name' => 'string', 'field' => 'string', 'value' => 'string', 'source' => 'string'),
'type' => '{urn:horde}stringArray',
);
$_services['deleteField'] = array(
'args' => array('address' => 'string', 'field' => 'string', 'sources' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['getField'] = array(
'args' => array('address' => 'string', 'field' => 'string', 'sources' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['getAllAttributeValues'] = array(
'args' => array('field' => 'string', 'sources' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['getClientSource'] = array(
'checkperms' => false,
'args' => array(),
'type' => 'string',
);
$_services['getClient'] = array(
'checkperms' => false,
'args' => array('objectId' => 'string'),
'type' => '{urn:horde}stringArray',
);
$_services['getClients'] = array(
'checkperms' => false,
'args' => array('objectIds' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
$_services['addClient'] = array(
'args' => array('attributes' => '{urn:horde}stringArray'),
'type' => 'string',
);
$_services['updateClient'] = array(
'args' => array('objectId' => 'string', 'attributes' => '{urn:horde}stringArray'),
'type' => 'string',
);
$_services['deleteClient'] = array(
'args' => array('objectId' => 'string'),
'type' => '{urn:horde}stringArray',
);
$_services['searchClients'] = array(
'checkperms' => false,
'args' => array('names' => '{urn:horde}stringArray', 'fields' => '{urn:horde}stringArray'),
'type' => '{urn:horde}stringArray',
);
function _turba_perms()
{
static $perms = array();
if (!empty($perms)) {
return $perms;
}
@define('TURBA_BASE', dirname(__FILE__) . '/..');
require_once TURBA_BASE . '/lib/base.php';
global $cfgSources;
$perms['tree']['turba']['sources'] = false;
$perms['title']['turba:sources'] = _("Sources");
// Run through every contact source.
foreach ($cfgSources as $source => $curSource) {
$perms['tree']['turba']['sources'][$source] = false;
$perms['title']['turba:sources:' . $source] = $curSource['title'];
}
return $perms;
}
function _turba_sources($writeable = false)
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
$sources = array();
foreach ($cfgSources as $key => $entry) {
if (!$writeable || (!$entry['readonly'] ||
(isset($entry['admin']) && in_array(Auth::getAuth(), $entry['admin'])))) {
$sources[$key] = $entry['title'];
}
}
return $sources;
}
function _turba_fields($source = '')
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources, $attributes;
if (empty($source) || !isset($cfgSources[$source])) {
return PEAR::raiseError(_("Invalid address book"), 'horde.error', null, null, $source);
}
$fields = array();
foreach ($cfgSources[$source]['map'] as $field_name => $null) {
if (substr($field_name, 0, 2) != '__') {
$fields[$field_name] = array('name' => $field_name,
'type' => $attributes[$field_name]['type'],
'label' => $attributes[$field_name]['label'],
'search' => in_array($field_name, $cfgSources[$source]['search']));
}
}
return $fields;
}
/**
* Browse through Turba's object tree.
*
* @param string $path The level of the tree to browse.
*
* @return array The contents of $path
*/
function _turba_browse($path = '')
{
require_once dirname(__FILE__) . '/base.php';
global $registry, $cfgSources;
if (substr($path, 0, 5) == 'turba') {
$path = substr($path, 5);
}
if (substr($path, 0, 1) == '/') {
$path = substr($path, 1);
}
if (substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
$addressbooks = $registry->callByPackage('turba', 'sources');
if (is_a($addressbooks, 'PEAR_Error')) {
return $addressbooks;
}
if (empty($path)) {
$results = array();
foreach ($addressbooks as $addressbook => $name) {
$results['turba/' . $addressbook] =
array('name' => $name,
'icon' => $registry->getImageDir() . '/turba.png',
'browseable' => true);
}
} elseif (isset($addressbooks[$path])) {
/* Load the Turba driver. */
$driver = &Turba_Driver::singleton($path, $cfgSources[$path]);
$contacts = $driver->search(array());
if (is_a($contacts, 'PEAR_Error')) {
return $contacts;
}
$results = array();
$contacts->reset();
while ($contact = $contacts->next()) {
$results['turba/' . $contact->getSource() . '/' . $contact->getValue('__key')] =
array('name' => Turba::formatName($contact),
'browseable' => false);
}
} else {
return PEAR::raiseError($path . ' does not exist or permission denied');
}
return $results;
}
/**
* Returns an array of UIDs for all contacts that the current user is
* authorized to see.
*
* @return array An array of UIDs for all contacts the user can access.
*/
function _turba_list()
{
}
/**
* Returns an array of UIDs for contacts that have had $action happen
* since $timestamp.
*
* @param string $action The action to check for - add, modify, or delete.
* @param integer $timestamp The time to start the search.
*
* @return array An array of UIDs matching the action and time criteria.
*/
function &_turba_listBy($action, $timestamp)
{
require_once dirname(__FILE__) . '/base.php';
require_once 'Horde/History.php';
$history = &Horde_History::singleton();
$histories = $history->getByTimestamp('>', $timestamp, array(array('op' => '=', 'field' => 'action', 'value' => $action)), 'turba');
if (is_a($histories, 'PEAR_Error')) {
return $histories;
}
return array_keys($histories);
}
/**
* Import a contact represented in the specified contentType.
*
* @param string $content The content of the contact.
* @param string $contentType What format is the data in? Currently supports:
* array
* text/x-vcard
* @param string $sourcet The source into which the contact will be
* imported.
*
* @return string The new UID, or false on failure.
*/
function _turba_import($content, $contentType = 'array', $source = '')
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
/* Check existance of and permissions on the specified source. */
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources) || !isset($cfgSources[$source])) {
return PEAR::raiseError(_("Invalid address book"), 'horde.warning');
}
if ($cfgSources[$source]['readonly']
&& (!isset($cfgSources[$source]['admin'])
|| !in_array(Auth::getAuth(), $cfgSources[$source]['admin']))) {
return PEAR::raiseError(_("Permission Denied"), 'horde.error', null, null, $source);
}
if (!is_a($content, 'Horde_iCalendar_vcard')) {
switch ($contentType) {
case 'array':
break;
case 'text/x-vcard':
require_once 'Horde/iCalendar.php';
$iCal = &new Horde_iCalendar();
if (!$iCal->parsevCalendar($content)) {
return PEAR::raiseError(_("There was an error importing the iCalendar data."));
}
switch ($iCal->getComponentCount()) {
case 0:
return PEAR::raiseError(_("No vCard data was found."));
case 1:
$content = $iCal->getComponent(0);
break;
default:
foreach ($iCal->getComponents() as $c) {
if (is_a($content, 'Horde_iCalendar_vcard')) {
$result = _turba_import($content, $contentType, $source);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
}
return true;
}
break;
default:
return PEAR::raiseError(_("Invalid Content-Type"));
}
}
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($content, 'Horde_iCalendar_vcard')) {
$content = $driver->toHash($content);
}
$result = $driver->search($content);
if (is_a($result, 'PEAR_Error')) {
return $result;
} elseif ($result->count() > 0) {
return PEAR::raiseError(_("Already Exists"), 'horde.message', null, null, $source);
}
$result = $driver->add($content);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
$object = &$driver->getObject($result);
return $object->getValue('__key');
}
/**
* Export a contact, identified by UID, in the requested contentType.
*
* @param string $uid Identify the contact to export.
* @param string $contentType What format should the data be in? Currently supports:
* text/x-icalendar
* text/x-vcalendar
*
* @return string The requested data.
*/
function _turba_export($uid, $contentType)
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (is_array($contentType)) {
$options = $contentType;
$contentType = $options['ContentType'];
unset($options['ContentType']);
} else {
$options = array();
}
// Need to reference some sort of mapping here to turn the UID
// into a source id and contact id:
//
// $source = $uid['addressbook'];
// $objectId = $uid['contactId'];
return PEAR::raiseError('Turba needs a UID map');
if (empty($source) || !isset($cfgSources[$source])) {
return PEAR::raiseError(_("Invalid address book"), 'horde.error', null, null, $source);
}
if (empty($objectId)) {
return PEAR::raiseError(_("Invalid ID"), 'horde.error', null, null, $source);
}
if ($cfgSources[$source]['readonly']
&& (!isset($cfgSources[$source]['admin'])
|| !in_array(Auth::getAuth(), $cfgSources[$source]['admin']))) {
return PEAR::raiseError(_("Permission Denied"), 'horde.error', null, null, $source);
}
// If the objectId isn't in $source in the first place, just return
// true. Otherwise, try to delete it and return success or
// failure.
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
$result = $driver->search(array('__key' => $objectId));
if (is_a($result, 'PEAR_Error')) {
return $result;
} elseif ($result->count() == 0) {
return PEAR::raiseError(_("Object not found"), 'horde.error', null, null, $source);
return true;
} elseif ($result->count() > 1) {
return PEAR::raiseError("Internal Horde Error: multiple turba objects with same objectId.", 'horde.error', null, null, $source);
}
switch ($contentType) {
case 'text/x-vcard':
require_once 'Horde/iCalendar.php';
$iCal = &new Horde_iCalendar();
$export = '';
foreach ($result->objects as $obj) {
$vcard = $driver->tovCard($obj);
$vcard->setParameter('FN', $options);
$vcard->setParameter('ORG', $options);
$vcard->setParameter('ADR', $options);
$vcard->setParameter('LABEL', $options);
$vcard->setParameter('NICKNAME', $options);
$vcard->setParameter('TITLE', $options);
$vcard->setParameter('NOTE', $options);
$vcard->setParameter('N', $options);
// vCards are not enclosed in
// BEGIN:VCALENDAR..END:VCALENDAR so export the individual
// cards instead:
$export .= $vcard->exportvCalendar();
$iCal->addComponent($vcard);
}
return $export;
default:
return PEAR::raiseError(_("Unsupported Content-Type."));
}
}
/**
* Delete a contact identified by UID.
*
* @param string | array $uid Identify the contact to delete, either a
* single UID or an array.
*
* @return boolean Success or failure.
*/
function _turba_delete($uid)
{
// Handle an arrray of UIDs for convenience of deleting multiple
// contacts at once.
if (is_array($uid)) {
foreach ($uid as $g) {
$result = _turba_delete($uid);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
return true;
}
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
// Need to reference some sort of mapping here to turn the UID
// into a source id and contact id:
//
// $source = $uid['addressbook'];
// $objectId = $uid['contactId'];
return PEAR::raiseError('Turba needs a UID map');
if (empty($source) || !isset($cfgSources[$source])) {
return PEAR::raiseError(_("Invalid address book"), 'horde.error', null, null, $source);
}
if (empty($objectId)) {
return PEAR::raiseError(_("Invalid ID"), 'horde.error', null, null, $source);
}
if ($cfgSources[$source]['readonly']
&& (!isset($cfgSources[$source]['admin'])
|| !in_array(Auth::getAuth(), $cfgSources[$source]['admin']))) {
return PEAR::raiseError(_("Permission Denied"), 'horde.error', null, null, $source);
}
// If the objectId isn't in $source in the first place, just return
// true. Otherwise, try to delete it and return success or
// failure.
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
$result = $driver->search(array('__key' => $objectId));
if (is_a($result, 'PEAR_Error')) {
return $result;
} elseif ($result->count() == 0) {
return true;
} else {
return $driver->delete($objectId);
}
}
/**
* Replace the contact identified by UID with the content represented
* in the specified contentType.
*
* @param string $uid Idenfity the contact to replace.
* @param string $content The content of the contact.
* @param string $contentType What format is the data in? Currently supports:
* array
* text/x-vcard
*
* @return boolean Success or failure.
*/
function _turba_replace($uid, $content, $contentType)
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
// Need to reference some sort of mapping here to turn the UID
// into a source id and contact id:
//
// $source = $uid['addressbook'];
// $objectId = $uid['contactId'];
return PEAR::raiseError('Turba needs a UID map');
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources) || !isset($cfgSources[$source])) {
return PEAR::raiseError(_("Invalid address book"), 'horde.warning');
}
if (empty($objectId)) {
return PEAR::raiseError(_("Invalid objectId"), 'horde.error', null, null, $source);
}
/* Check permissions. */
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
$object = $driver->getObject($objectId);
if (is_a($object, 'PEAR_Error')) {
return $object;
} elseif (!Turba::hasPermission($object, 'object', PERMS_EDIT)) {
return PEAR::raiseError(_("Permission Denied"), 'horde.warning');
}
switch ($contentType) {
case 'array':
break;
default:
return PEAR::raiseError(_("Invalid Content-Type"));
}
foreach ($content as $attribute => $value) {
if ($attribute != '__key') {
$object->setValue($attribute, $value);
}
}
return $object->store();
}
function _turba_search($names = array(), $sources = array(), $fields = array())
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources, $attributes;
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
if (count($sources) == 0) {
$sources = array(key($cfgSources));
}
$results = array();
$seen = array();
foreach ($sources as $source) {
if (!isset($cfgSources[$source])) {
continue;
}
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($driver, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Connection failed: %s"), $driver->getMessage()), 'horde.error', null, null, $source);
}
foreach ($names as $name) {
$criteria = array();
if (isset($fields[$source])) {
foreach ($fields[$source] as $field) {
$criteria[$field] = trim($name);
}
}
if (count($criteria) == 0) {
$criteria['name'] = trim($name);
}
$res = $driver->search($criteria, 'lastname', 'OR');
if (!is_a($res, 'Turba_List')) {
continue;
}
while ($ob = $res->next()) {
if (!$ob->isGroup()) {
/* Not a group. */
$att = $ob->getAttributes();
$email = null;
foreach (array_keys($att) as $key) {
if ($ob->getValue($key) && isset($attributes[$key]) &&
$attributes[$key]['type'] == 'email') {
$email = $ob->getValue($key);
break;
}
}
if (!is_null($email)) {
$seen_key = trim(String::lower($ob->getValue('name'))) . '/' . trim(String::lower($email));
if (!empty($seen[$seen_key])) {
continue;
}
$seen[$seen_key] = true;
}
if (!isset($results[$name])) {
$results[$name] = array();
}
$results[$name][] = array_merge($att,
array('id' => $att['__key'],
'name' => $ob->getValue('name'),
'email' => $email,
'__type' => 'Object',
'source' => $source));
} else {
/* Is a distribution list. */
$listatt = $ob->getAttributes();
$seeninlist = array();
$members = $ob->listMembers();
if (is_a($members, 'Turba_List')) {
if (!isset($results[$name])) {
$results[$name] = array();
}
if ($members->count() == 1) {
$ob = $members->next();
$att = $ob->getAttributes();
$email = '';
foreach ($att as $key => $value) {
if (!empty($value) && isset($attributes[$key]) &&
$attributes[$key]['type'] == 'email') {
$email = $value;
}
}
$results[$name][] = array('name' => $listatt['name'] . ' - ' . $att['name'], 'email' => $email, 'id' => $att['__key'], 'source' => $source );
} else {
$email = '';
while ($ob = $members->next()) {
$att = $ob->getAttributes();
foreach ($att as $key => $value) {
if (!empty($value) && isset($attributes[$key]) &&
$attributes[$key]['type'] == 'email' &&
empty($seeninlist[trim(String::lower($att['name'])) . trim(String::lower($value))])) {
$email .= ($email == '') ? '' : ', ';
$email .= '"' . $att['name'] . '" <' . $value . '>';
$seeninlist[trim(String::lower($att['name'])) . trim(String::lower($value))] = true;
}
}
}
$results[$name][] = array('name' => $listatt['name'], 'email' => $email, 'id' => $listatt['__key'], 'source' => $source);
}
}
}
}
}
}
return $results;
}
function _turba_getContact($source = '', $objectId = '')
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
if (isset($cfgSources[$source])) {
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($driver, 'PEAR_Error')) {
return $driver;
}
$object = $driver->getObject($objectId);
if (is_a($object, 'PEAR_Error')) {
return $object;
}
/* Check permissions. */
if (!Turba::hasPermission($object, 'object', PERMS_READ)) {
return PEAR::raiseError(_("Permission Denied"), 'horde.error', null, null, $source);
}
$attributes = array();
foreach ($cfgSources[$source]['map'] as $field => $map) {
$attributes[$field] = $object->getValue($field);
}
return $attributes;
}
return array();
}
function _turba_getContacts($source = '', $objectIds = array())
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
$results = array();
if (!is_array($objectIds)) {
$objectIds = array($objectIds);
}
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
if (isset($cfgSources[$source])) {
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($driver, 'PEAR_Error')) {
return $driver;
}
$objects = $driver->getObjects($objectIds);
if (is_a($objects, 'PEAR_Error')) {
return $objects;
}
foreach ($objects as $object) {
/* Check permissions on this object. */
if (!Turba::hasPermission($object, 'object', PERMS_READ)) {
continue;
}
$attributes = array();
foreach ($cfgSources[$source]['map'] as $field => $map) {
$attributes[$field] = $object->getValue($field);
}
$results[] = $attributes;
}
}
return $results;
}
function _turba_getAllAttributeValues($field = '', $sources = array())
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
if (!count($sources)) {
$sources = array(key($cfgSources));
}
$results = array();
foreach ($sources as $source) {
if (isset($cfgSources[$source])) {
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($driver, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Connection failed: %s"), $driver->getMessage()), 'horde.error', null, null, $source);
}
$res = $driver->search(array());
if (!is_a($res, 'Turba_List')) {
return PEAR::raiseError(_("Search failed"), 'horde.error', null, null, $source);
}
while ($ob = $res->next()) {
if ($ob->hasValue($field)) {
$results[$ob->getValue('source') . ':' . $ob->getValue('__key')] = array(
'name' => $ob->getValue('name'),
'email' => $ob->getValue('email'),
$field => $ob->getValue($field));
}
}
}
}
return $results;
}
function _turba_getClientSource()
{
return !empty($GLOBALS['conf']['client']['addressbook']) ? $GLOBALS['conf']['client']['addressbook'] : false;
}
function _turba_getClient($objectId = '')
{
return $GLOBALS['registry']->call('clients/getContact', array('source' => $GLOBALS['conf']['client']['addressbook'],
'objectId' => $objectId));
}
function _turba_getClients($objectIds = array())
{
return $GLOBALS['registry']->call('clients/getContacts', array('source' => $GLOBALS['conf']['client']['addressbook'],
'objectIds' => $objectIds));
}
function _turba_addClient($attributes = array())
{
return $GLOBALS['registry']->call('clients/import', array('content' => $attributes,
'contentType' => 'array',
'source' => $GLOBALS['registry']->call('clients/getClientSource')));
}
function _turba_updateClient($objectId = '', $attributes = array())
{
return $GLOBALS['registry']->call('clients/replace', array('uid' => $GLOBALS['registry']->call('clients/getClientSource') . ':' . $objectId,
'content' => $attributes,
'contentType' => 'array'));
}
function _turba_deleteClient($objectId = '')
{
return $GLOBALS['registry']->call('clients/delete', array($GLOBALS['registry']->call('clients/getClientSource') . ':' . $objectId));
}
function _turba_searchClients($names = array(), $fields = array())
{
return $GLOBALS['registry']->call('clients/search', array('names' => $names,
'sources' => array($GLOBALS['conf']['client']['addressbook']),
'fields' => $fields));
}
function _turba_addField($address = '', $name = '', $field = '', $value = '', $source = '')
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (empty($source) || !isset($cfgSources[$source])) {
return PEAR::raiseError(_("Invalid address book"), 'horde.error', null, null, $source);
}
if (empty($address)) {
return PEAR::raiseError(_("Invalid email"), 'horde.error', null, null, $source);
}
if (empty($name)) {
return PEAR::raiseError(_("Invalid name"), 'horde.error', null, null, $source);
}
if (empty($value)) {
return PEAR::raiseError(_("Invalid entry"), 'horde.error', null, null, $source);
}
if ($cfgSources[$source]['readonly']
&& (!isset($cfgSources[$source]['admin'])
|| !in_array(Auth::getAuth(), $cfgSources[$source]['admin']))) {
return PEAR::raiseError(_("Permission Denied"), 'horde.error', null, null, $source);
}
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
$res = $driver->search(array('email' => trim($address)), null, 'AND');
if (is_a($res, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Search failed: %s"), $res->getMessage()), 'horde.message', null, null, $source);
}
if ($res->count() > 1) {
$res2 = $driver->search(array('email' => trim($address), 'name' => trim($name)), null, 'AND');
if (is_a($res2, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Search failed: %s"), $res2->getMessage()), 'horde.message', null, null, $source);
}
if (!$res2->count()) {
return PEAR::raiseError(sprintf(_("Multiple persons with address [%s], but none with name [%s] already exist"), trim($address), trim($name)), 'horde.message', null, null, $source);
}
$res3 = $driver->search(array('email' => $address, 'name' => $name, $field => $value));
if (is_a($res3, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Search failed: %s"), $res3->getMessage()), 'horde.message', null, null, $source);
}
if ($res3->count()) {
return PEAR::raiseError(sprintf(_("This person already has a %s entry in the address book"), $field), 'horde.message', null, null, $source);
}
$ob = $res2->next();
$ob->setValue($field, $value);
$ob->store();
} elseif ($res->count() == 1) {
$res4 = $driver->search(array('email' => $address, $field => $value));
if (is_a($res4, 'PEAR_Error')) {
return PEAR::raiseError(sprintf(_("Search failed: %s"), $res4->getMessage()), 'horde.message', null, null, $source);
}
if ($res4->count()) {
return PEAR::raiseError(sprintf(_("This person already has a %s entry in the address book"), $field), 'horde.message', null, null, $source);
}
$ob = $res->next();
$ob->setValue($field, $value);
$ob->store();
} else {
return $driver->add(array('email' => $address, 'name' => $name, $field => $value, '__owner' => Auth::getAuth()));
}
return;
}
function _turba_getField($address = '', $field = '', $sources = array())
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (empty($address)) {
return PEAR::raiseError(_("Invalid email"), 'horde.error');
}
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
if (!count($sources)) {
if (!count($cfgSources)) {
return PEAR::raiseError(_("No address books found."));
}
// backport security patch from Turba 2.1.5 for sarge
reset($cfgSources);
$sources = array(key($cfgSources));
}
$count = 0;
foreach ($sources as $source) {
if (!isset($cfgSources[$source])) {
continue;
}
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($driver, 'PEAR_Error')) {
continue;
}
$list = $driver->search(array('email' => $address));
if (!is_a($list, 'Turba_List')) {
continue;
}
while ($ob = $list->next()) {
if ($ob->hasValue($field)) {
$count++;
$result = $ob->getValue($field);
}
}
}
if ($count > 1) {
return PEAR::raiseError(_("More than 1 entry found"), 'horde.warning', null, null, $source);
} elseif (!isset($result)) {
return PEAR::raiseError(sprintf(_("No %s entry found for %s"), $field, $address), 'horde.warning', null, null, $source);
}
return $result;
}
function _turba_deleteField($address = '', $field = '', $sources = array())
{
require_once dirname(__FILE__) . '/base.php';
global $cfgSources;
if (empty($address)) {
return PEAR::raiseError(_("Invalid email"), 'horde.error');
}
if (!isset($cfgSources) || !is_array($cfgSources) || !count($cfgSources)) {
return array();
}
if (count($sources) == 0) {
$sources = array(key($cfgSources));
}
$success = false;
foreach ($sources as $source) {
if (isset($cfgSources[$source])) {
$driver = &Turba_Driver::singleton($source, $cfgSources[$source]);
if (is_a($driver, 'PEAR_Error')) {
continue;
}
// backport security patch from Turba 2.1.5 for sarge
if (!Turba::hasPermission($object, 'object', PERMS_EDIT)) {
continue;
}
$res = $driver->search(array('email' => $address));
if (is_a($res, 'Turba_List')) {
if ($res->count() > 1) {
continue;
}
$ob = $res->next();
if (is_object($ob) && $ob->hasValue($field)) {
$ob->setValue($field, '');
$ob->store();
$success = true;
}
}
}
}
if (!$success) {
return PEAR::raiseError(sprintf(_("No %s entry found for %s"), $field, $address), 'horde.error');
}
return;
}
|