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
|
<?php
/**
* $Horde: turba/lib/Turba.php,v 1.59.4.39 2008/06/13 09:59:43 jan Exp $
*
* @package Turba
*/
/** The virtual path to use for VFS data. */
define('TURBA_VFS_PATH', '.horde/turba/documents');
/**
* Turba Base Class.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jon Parise <jon@horde.org>
* @package Turba
*/
class Turba {
function formatEmailAddresses($data, $name)
{
global $registry;
static $batchCompose;
if (!isset($batchCompose)) {
$batchCompose = $registry->hasMethod('mail/batchCompose');
}
require_once 'Horde/MIME.php';
$array = is_array($data);
if (!$array) {
$data = array($data);
}
$addresses = array();
foreach ($data as $i => $email_vals) {
$email_vals = explode(',', $email_vals);
foreach ($email_vals as $j => $email_val) {
$email_val = trim($email_val);
// Format the address according to RFC822.
$mailbox_host = explode('@', $email_val);
if (!isset($mailbox_host[1])) {
$mailbox_host[1] = '';
}
$address = MIME::rfc822WriteAddress($mailbox_host[0], $mailbox_host[1], $name);
// Get rid of the trailing @ (when no host is included in
// the email address).
$addresses[$i . ':' . $j] = array('to' => addslashes(str_replace('@>', '>', $address)));
if (!$batchCompose) {
$addresses[$i . ':' . $j] = $GLOBALS['registry']->call('mail/compose', $addresses[$i . ':' . $j]);
}
}
}
if ($batchCompose) {
$addresses = $GLOBALS['registry']->call('mail/batchCompose', array($addresses));
}
foreach ($data as $i => $email_vals) {
$email_vals = explode(',', $email_vals);
$email_values = false;
foreach ($email_vals as $j => $email_val) {
if (!is_a($addresses, 'PEAR_Error')) {
$mail_link = $addresses[$i . ':' . $j];
if (is_a($mail_link, 'PEAR_Error')) {
$mail_link = 'mailto:' . urlencode($email_val);
}
} else {
$mail_link = 'mailto:' . urlencode($email_val);
}
$email_value = Horde::link($mail_link) . htmlspecialchars($email_val) . '</a>';
if ($email_values) {
$email_values .= ', ' . $email_value;
} else {
$email_values = $email_value;
}
}
}
if ($array) {
return $email_values[0];
} else {
return $email_values;
}
}
/**
* Get all the address books the user has the requested permissions to and
* return them in the user's preferred order.
*
* @param integer $permission The PERMS_* constant to filter on.
*
* @return array The filtered, ordered $cfgSources entries.
*/
function getAddressBooks($permission = PERMS_READ)
{
$addressbooks = array();
foreach (array_keys(Turba::getAddressBookOrder()) as $addressbook) {
$addressbooks[$addressbook] = $GLOBALS['cfgSources'][$addressbook];
}
if (!$addressbooks) {
$addressbooks = $GLOBALS['cfgSources'];
}
return Turba::permissionsFilter($addressbooks, $permission);
}
/**
* Get the order the user selected for displaying address books.
*
* @return array An array describing the order to display the address books.
*/
function getAddressBookOrder()
{
$i = 0;
$lines = explode("\n", $GLOBALS['prefs']->getValue('addressbooks'));
$temp = $lines;
$addressbooks = array();
foreach ($lines as $line) {
$line = trim($line);
if ($line && isset($GLOBALS['cfgSources'][$line])) {
$addressbooks[$line] = $i++;
}
}
return $addressbooks;
}
/**
* Returns the current user's default address book.
*
* @return string The default address book name.
*/
function getDefaultAddressBook()
{
$lines = explode("\n", $GLOBALS['prefs']->getValue('addressbooks'));
foreach ($lines as $line) {
$line = trim($line);
if ($line && isset($GLOBALS['cfgSources'][$line])) {
return $line;
}
}
reset($GLOBALS['cfgSources']);
return key($GLOBALS['cfgSources']);
}
/**
* Returns the sort order selected by the user
*/
function getPreferredSortOrder($columns)
{
global $prefs;
$order = @unserialize($prefs->getValue('sortorder'));
if ($prefs->getValue('name_format') == 'last_first') {
for ($i = 0, $i_max = count($order); $i < $i_max; ++$i) {
if ($order[$i]['field'] == 'name') {
$order[$i]['field'] = 'lastname';
}
}
}
return $order;
}
/**
* Retrieves a column's field name
*/
function getColumnName($i, $columns)
{
if ($i == 0) {
if ($GLOBALS['prefs']->getValue('name_format') == 'first_last') {
return 'name';
} else {
return 'lastname';
}
} else {
return $columns[$i - 1];
}
}
/**
*/
function getColumns()
{
$columns = array();
$lines = explode("\n", $GLOBALS['prefs']->getValue('columns'));
foreach ($lines as $line) {
$line = trim($line);
if ($line) {
$cols = explode("\t", $line);
if (count($cols) > 1) {
$source = array_splice($cols, 0, 1);
$columns[$source[0]] = $cols;
}
}
}
return $columns;
}
/**
* Returns a best guess at the lastname in a string.
*
* @param string $name String contain the full name.
*
* @return string String containing the last name.
*/
function guessLastname($name)
{
$name = trim(preg_replace('|\s|', ' ', $name));
if (!empty($name)) {
/* Assume that last names are always before any commas. */
if (is_int(strpos($name, ','))) {
$name = String::substr($name, 0, strpos($name, ','));
}
/* Take out anything in parentheses. */
$name = trim(preg_replace('|\(.*\)|', '', $name));
$namelist = explode(' ', $name);
$name = $namelist[($nameindex = (count($namelist) - 1))];
while (!empty($name) && String::length($name) < 5 &&
strspn($name[(String::length($name) - 1)], '.:-') &&
!empty($namelist[($nameindex - 1)])) {
$nameindex--;
$name = $namelist[$nameindex];
}
}
return $name;
}
/**
* Formats the name according to the user's preference.
*
* @param Turba_Object $ob The object to get a name from.
*
* @return string The formatted name, either "Firstname Lastname"
* or "Lastname, Firstname" depending on the user's
* preference.
*/
function formatName($ob)
{
global $prefs;
static $name_format;
if (!isset($name_format)) {
$name_format = $prefs->getValue('name_format');
}
/* if no formatting, return original name */
if ($name_format != 'first_last' && $name_format != 'last_first') {
return $ob->getValue('name');
}
/* See if we have the name fields split out explicitly. */
if ($ob->hasValue('firstname') && $ob->hasValue('lastname')) {
if ($name_format == 'last_first') {
return $ob->getValue('lastname') . ', ' . $ob->getValue('firstname');
} else {
return $ob->getValue('firstname') . ' ' . $ob->getValue('lastname');
}
} else {
/* One field, we'll have to guess. */
$name = $ob->getValue('name');
$lastname = Turba::guessLastname($name);
if ($name_format == 'last_first' &&
!is_int(strpos($name, ',')) &&
String::length($name) > String::length($lastname)) {
$name = preg_replace('/\s+' . preg_quote($lastname, '/') . '/', '', $name);
$name = $lastname . ', ' . $name;
}
if ($name_format == 'first_last' &&
is_int(strpos($name, ',')) &&
String::length($name) > String::length($lastname)) {
$name = preg_replace('/' . preg_quote($lastname, '/') . ',\s*/', '', $name);
$name = $name . ' ' . $lastname;
}
return $name;
}
}
/**
* Returns the real name, if available, of a user.
*
* @since Turba 2.2
*/
function getUserName($uid)
{
static $names = array();
if (!isset($names[$uid])) {
require_once 'Horde/Identity.php';
$ident = &Identity::singleton('none', $uid);
$ident->setDefault($ident->getDefault());
$names[$uid] = $ident->getValue('fullname');
if (empty($names[$uid])) {
$names[$uid] = $uid;
}
}
return $names[$uid];
}
/**
* Gets extended permissions on an address book.
*
* @since Turba 2.1
*
* @param Turba_Driver $addressBook The address book to get extended permissions for.
* @param string $permission What extended permission to get.
*
* @return mixed The requested extended permissions value, or true if it doesn't exist.
*/
function getExtendedPermission($addressBook, $permission)
{
global $perms;
// We want to check the base source as extended permissions
// are enforced per backend, not per share.
$key = $addressBook->name . ':' . $permission;
if (!$GLOBALS['perms']->exists('turba:sources:' . $key)) {
return true;
}
$allowed = $GLOBALS['perms']->getPermissions('turba:sources:' . $key);
if (is_array($allowed)) {
switch ($permission) {
case 'max_contacts':
$allowed = max($allowed);
break;
}
}
return $allowed;
}
/**
* Filters data based on permissions.
*
* @param array $in The data we want filtered.
* @param string $filter What type of data we are filtering.
* @param integer $permission The PERMS_* constant we will filter on.
*
* @return array The filtered data.
*/
function permissionsFilter($in, $permission = PERMS_READ)
{
$out = array();
foreach ($in as $sourceId => $source) {
$driver = &Turba_Driver::singleton($sourceId);
if (is_a($driver, 'PEAR_Error')) {
continue;
}
if ($driver->hasPermission($permission)) {
$out[$sourceId] = $source;
}
}
return $out;
}
/**
* Replaces all share-enabled sources in a source list with all shares
* from this source that the current user has access to.
*
* This will only sync shares that are unique to Horde (basically, a SQL
* driver source for now). Any backend that supports ACLs or similar
* mechanism should be configured from within sources.php or
* _horde_hook_share_* calls.
*
* @param array $sources The default $cfgSources array.
*
* @return array The $cfgSources array.
*/
function getConfigFromShares($sources)
{
global $notification;
$shares = Turba::listShares();
// Notify the user if we failed, but still return the $cfgSource array.
if (is_a($shares, 'PEAR_Error')) {
$notification->push($shares, 'horde.error');
return $sources;
}
$sortedShares = $defaults = $vbooks = array();
foreach (array_keys($shares) as $name) {
if (isset($sources[$name])) {
continue;
}
$params = @unserialize($shares[$name]->get('params'));
if (isset($params['type']) && $params['type'] == 'vbook') {
// We load vbooks last in case they're based on other shares.
$params['share'] = &$shares[$name];
$vbooks[$name] = $params;
} elseif (!empty($params['source']) &&
!empty($sources[$params['source']]['use_shares'])) {
if (empty($params['name'])) {
$params['name'] = $name;
$shares[$name]->set('params', serialize($params));
$shares[$name]->save();
}
// Default share?
if (empty($defaults[$params['source']])) {
$driver = &Turba_Driver::singleton($params['source']);
if (!is_a($driver, 'PEAR_Error')) {
$defaults[$params['source']] =
$driver->checkDefaultShare(
$shares[$name],
$sources[$params['source']]);
} else {
$notification->push($driver, 'horde.error');
}
}
$share = $sources[$params['source']];
$share['params']['config'] = $sources[$params['source']];
$share['params']['config']['params']['share'] = &$shares[$name];
$share['params']['config']['params']['name'] = $params['name'];
$share['title'] = $shares[$name]->get('name');
$share['type'] = 'share';
$share['use_shares'] = false;
$sortedSources[$params['source']][$name] = $share;
}
}
// Check for the user's default share and built new source list.
$newSources = array();
foreach (array_keys($sources) as $source) {
if (empty($sources[$source]['use_shares'])) {
$newSources[$source] = $sources[$source];
continue;
}
if (isset($sortedSources[$source])) {
$newSources = array_merge($newSources, $sortedSources[$source]);
}
if (Auth::getAuth() && empty($defaults[$source])) {
// User's default share is missing.
$driver = &Turba_Driver::singleton($source);
if (!is_a($driver, 'PEAR_Error')) {
$sourceKey = md5(mt_rand());
$share = &$driver->createShare(
$sourceKey,
array('params' => array('source' => $source,
'default' => true,
'name' => Auth::getAuth())));
if (is_a($share, 'PEAR_Error')) {
Horde::logMessage($share, __FILE__, __LINE__, PEAR_LOG_ERR);
continue;
}
$source_config = $sources[$source];
$source_config['params']['share'] = &$share;
$newSources[$sourceKey] = $source_config;
} else {
$notification->push($driver, 'horde.error');
}
}
}
// Add vbooks now that all available address books are loaded.
foreach ($vbooks as $name => $params) {
$newSources[$name] = array(
'title' => $shares[$name]->get('name'),
'type' => 'vbook',
'params' => $params,
'export' => true,
'browse' => true,
'map' => $newSources[$params['source']]['map'],
'search' => $newSources[$params['source']]['search'],
'strict' => $newSources[$params['source']]['strict'],
'use_shares' => false,
);
}
return $newSources;
}
/**
* Retrieve a new source config entry based on a Turba share.
*
* @param Horde_Share object The share to base config on.
*
* @since Turba 2.2
*/
function getSourceFromShare(&$share)
{
// Require a fresh config file.
require TURBA_BASE . '/config/sources.php';
$params = @unserialize($share->get('params'));
$newConfig = $cfgSources[$params['source']];
$newConfig['params']['config'] = $cfgSources[$params['source']];
$newConfig['params']['config']['params']['share'] = &$share;
$newConfig['params']['config']['params']['name'] = $params['name'];
$newConfig['title'] = $share->get('name');
$newConfig['type'] = 'share';
$newConfig['use_shares'] = false;
return $newConfig;
}
/**
* Returns all shares the current user has specified permissions to.
*
* @param boolean $owneronly Only return address books owned by the user?
* Defaults to false.
* @param integer $permission Permissions to filter by.
*
* @return array Shares the user has the requested permissions to.
*/
function listShares($owneronly = false, $permission = PERMS_READ)
{
if (empty($_SESSION['turba']['has_share'])) {
// No backends are configured to provide shares
return array();
}
$sources = $GLOBALS['turba_shares']->listShares(
Auth::getAuth(), $permission,
$owneronly ? Auth::getAuth() : null);
if (is_a($sources, 'PEAR_Error')) {
Horde::logMessage($sources, __FILE__, __LINE__, PEAR_LOG_ERR);
return array();
}
return $sources;
}
/**
* Create a new Turba share.
*
* @param string $share_id The id for the new share.
* @param array $params Parameters for the new share.
*
* @return mixed The new share object or PEAR_Error
*/
function &createShare($share_id, $params)
{
if (!isset($params['name'])) {
/* Sensible default for empty display names */
require_once 'Horde/Identity.php';
$identity = &Identity::singleton();
$name = $identity->getValue('fullname');
if (trim($name) == '') {
$name = Auth::removeHook(Auth::getAuth());
}
$name = sprintf(_("%s's Address Book"), $name);
} else {
$name = $params['name'];
unset($params['name']);
}
/* Generate the new share. */
$share = &$GLOBALS['turba_shares']->newShare($share_id);
if (is_a($share, 'PEAR_Error')) {
return $share;
}
/* Set the display name for this share. */
$share->set('name', $name);
/* Now any other params. */
foreach ($params as $key => $value) {
if (!is_scalar($value)) {
$value = serialize($value);
}
$share->set($key, $value);
}
$result = $GLOBALS['turba_shares']->addShare($share);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
$result = $share->save();
if (is_a($result, 'PEAR_Error')) {
return $result;
}
/* Update share_id as backends like Kolab change it to the IMAP folder
* name. */
$share_id = $share->getId();
/* Add the new addressbook to the user's list of visible
* address books. */
$prefs = explode("\n", $GLOBALS['prefs']->getValue('addressbooks'));
if (array_search($share_id, $prefs) === false) {
$GLOBALS['prefs']->setValue('addressbooks', $GLOBALS['prefs']->getValue('addressbooks') . "\n" . $share_id);
}
return $share;
}
/**
* Build Turba's list of menu items.
*/
function getMenu($returnType = 'object')
{
require_once 'Horde/Menu.php';
$menu = new Menu();
if (!empty($_SESSION['turba']['has_share'])) {
$menu->add(Horde::applicationUrl('addressbooks/index.php'), _("_My Address Books"), 'turba.png');
}
if ($GLOBALS['browse_source_count']) {
$menu->add(Horde::applicationUrl('browse.php'), _("_Browse"), 'menu/browse.png', null, null, null, (($GLOBALS['prefs']->getValue('initial_page') == 'browse.php' && basename($_SERVER['PHP_SELF']) == 'index.php' && basename(dirname($_SERVER['PHP_SELF'])) != 'addressbooks') || (basename($_SERVER['PHP_SELF']) == 'browse.php' && Util::getFormData('key') != '**search')) ? 'current' : '__noselection');
}
if (count($GLOBALS['addSources'])) {
$menu->add(Horde::applicationUrl('add.php'), _("_New Contact"), 'menu/new.png');
}
$menu->add(Horde::applicationUrl('search.php'), _("_Search"), 'search.png', $GLOBALS['registry']->getImageDir('horde'), null, null, (($GLOBALS['prefs']->getValue('initial_page') == 'search.php' && basename($_SERVER['PHP_SELF']) == 'index.php' && strpos($_SERVER['PHP_SELF'], 'addressbooks/index.php') === false) || (basename($_SERVER['PHP_SELF']) == 'browse.php' && Util::getFormData('key') == '**search')) ? 'current' : null);
/* Import/Export */
if ($GLOBALS['conf']['menu']['import_export']) {
$menu->add(Horde::applicationUrl('data.php'), _("_Import/Export"), 'data.png', $GLOBALS['registry']->getImageDir('horde'));
}
/* Print. */
if (isset($GLOBALS['print_link'])) {
$menu->add($GLOBALS['print_link'], _("_Print"), 'print.png', $GLOBALS['registry']->getImageDir('horde'), '_blank', 'return !popup(this.href);', '__noselection');
}
if ($returnType == 'object') {
return $menu;
} else {
return $menu->render();
}
}
/**
* Checks for any maintenance to run
*
* @return mixed True || PEAR_Error
*/
function doMaintenance()
{
global $prefs;
// Kinda hackish way of indicating what tasks need to run, until
// a more general mechanism is available.
$needed_tasks = array('upgradeprefs', 'upgradelists');
$successful = array();
$existing = @unserialize($GLOBALS['prefs']->getValue('turba_maintenance_tasks'));
if (empty($existing)) {
$existing = array();
}
foreach ($needed_tasks as $taskname) {
if (array_search($taskname, $existing) === false) {
include dirname(__FILE__) . '/Maintenance/Task/' . basename($taskname) . '.php';
$class = 'Maintenance_Task_Turba_' . $taskname;
if (class_exists($class)) {
$task = &new $class();
$result = $task->doMaintenance();
if (is_a($result, 'PEAR_Error')) {
// Mark any successful taks before failing.
$prefs->setValue('turba_maintenance_tasks', serialize(array_merge($existing, $successful)));
return $result;
} elseif ($result) {
$successful[] = $taskname;
}
} else {
$prefs->setValue('turba_maintenance_tasks', serialize(array_merge($existing, $successful)));
return PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class));
}
}
}
$prefs->setValue('turba_maintenance_tasks', serialize(array_merge($existing, $successful)));
return true;
}
}
|