Turba::getConfigFromShares PHP Method

getConfigFromShares() public static method

This will only sync shares that are unique to Horde (such as a SQL or Kolab sources). Any backend that supports ACLs or similar mechanism should be configured from within backends.local.php or via Horde's share_* hooks.
public static getConfigFromShares ( array $sources, boolean $owner = false ) : array
$sources array The default $cfgSources array.
$owner boolean Only return shares that the current user owns?
return array The $cfgSources array.
    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;
    }

Usage Example

Example #1
0
 /**
  * Prepare the Turba setup.
  *
  * @return NULL
  */
 public function prepareTurba()
 {
     $world =& $this->prepareBasicSetup();
     $this->assertTrue($world['auth']->authenticate('*****@*****.**', array('password' => 'none')));
     $GLOBALS['registry']->pushApp('turba', array('check_perms' => false));
     // Turba base libraries.
     require_once TURBA_BASE . '/lib/Turba.php';
     require_once TURBA_BASE . '/lib/Driver.php';
     require_once TURBA_BASE . '/lib/Object.php';
     // Turba source and attribute configuration.
     include TURBA_BASE . '/config/attributes.php';
     $cfgSources = Turba::availableSources();
     unset($cfgSources['kolab_global']);
     $this->prepareNewFolder($world['storage'], 'Contacts', 'contact', true);
     $this->prepareNewFolder($world['storage'], 'test2', 'contact');
     $GLOBALS['session']->set('turba', 'has_share', true);
     $GLOBALS['cfgSources'] = Turba::getConfigFromShares($cfgSources);
 }
All Usage Examples Of Turba::getConfigFromShares