public static function getConfigFromShares(array $sources, $owner = false)
{
global $notification, $registry, $conf, $injector, $prefs;
try {
$shares = self::listShares($owner);
} catch (Horde_Share_Exception $e) {
// Notify the user if we failed, but still return the $cfgSource
// array.
$notification->push($e, 'horde.error');
return $sources;
}
/* See if any of our sources are configured to handle all otherwise
* unassigned Horde_Share objects. */
$all_shares = null;
foreach ($sources as $key => $cfg) {
if (!empty($cfg['all_shares'])) {
// Indicate the source handler that catches unassigned shares.
$all_shares = $key;
}
}
$auth_user = $registry->getAuth();
$sortedSources = $vbooks = array();
$personal = false;
foreach ($shares as $name => &$share) {
if (isset($sources[$name])) {
continue;
}
$personal |= $share->get('owner') == $auth_user;
$params = @unserialize($share->get('params'));
if (empty($params['source']) && !empty($all_shares)) {
$params['source'] = $all_shares;
}
if (isset($params['type']) && $params['type'] == 'vbook') {
// We load vbooks last in case they're based on other shares.
$params['share'] = $share;
$vbooks[$name] = $params;
} elseif (!empty($params['source']) && !empty($sources[$params['source']]['use_shares'])) {
if (empty($params['name'])) {
$params['name'] = $name;
$share->set('params', serialize($params));
try {
$share->save();
} catch (Horde_Share_Exception $e) {
Horde::log($e, 'ERR');
}
}
$info = $sources[$params['source']];
$info['params']['config'] = $sources[$params['source']];
$info['params']['config']['params']['share'] = $share;
$info['params']['config']['params']['name'] = $params['name'];
$info['title'] = $share->get('name');
if ($share->get('owner') != $auth_user) {
$info['title'] .= ' [' . $registry->convertUsername($share->get('owner'), false) . ']';
}
$info['type'] = 'share';
$info['use_shares'] = false;
$sortedSources[$params['source']][$name] = $info;
}
}
// 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 (!empty($conf['share']['auto_create']) && $auth_user && !$personal) {
// User's default share is missing.
try {
$driver = $injector->getInstance('Turba_Factory_Driver')->create($source);
} catch (Turba_Exception $e) {
$notification->push($e->getMessage(), 'horde.error');
continue;
}
$sourceKey = strval(new Horde_Support_Randomid());
try {
$share = $driver->createShare($sourceKey, array('params' => array('source' => $source, 'default' => true, 'name' => $auth_user)));
$source_config = $sources[$source];
$source_config['params']['share'] = $share;
$newSources[$sourceKey] = $source_config;
$personal = true;
$prefs->setValue('default_dir', $share->getName());
} catch (Horde_Share_Exception $e) {
Horde::log($e, 'ERR');
}
}
}
// Add vbooks now that all available address books are loaded.
foreach ($vbooks as $name => $params) {
if (isset($newSources[$params['source']])) {
$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);
} else {
$notification->push(sprintf(_("Removing the virtual address book \"%s\" because the parent source has disappeared."), $shares[$name]->get('name')), 'horde.message');
try {
$injector->getInstance('Turba_Shares')->removeShare($shares[$name]);
} catch (Horde_Share_Exception $e) {
Horde::log($e, 'ERR');
}
}
}
return $newSources;
}