Skip to content

Commit

Permalink
Merge pull request #4 from midahp/horde_add-transport-params-hook
Browse files Browse the repository at this point in the history
add transport_params hook for overriding Ingo_Transport params
  • Loading branch information
ralflang authored Mar 28, 2024
2 parents 460e5bf + fcbe079 commit 08269f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
23 changes: 23 additions & 0 deletions config/hooks.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ class Ingo_Hooks
// return true;
// }

/**
* A hook to override the transport params based on the driver and the current params
* @param string $driver The driver name (array key from backends.php).
* @param array $currentParams The currently used params
*
* @return array|null The params that should be overriden for this transport driver.
* If the return value is not an array nothing will be overriden.
*/
// public function transport_params($driver, $currentParams)
// {
// // in this example we use a different sieve server for certain users based on the domin in their username
// // we also increase the port by one. This might not be a very realistic example, but it shows that you are very flexible with this hook
// $params = [];
// switch ($driver) {
// case 'timsieved':
// $userDomain = $GLOBALS['registry']->getAuth('domain');
// if ($userDomain === 'new-server.localhost') {
// $params['hostspec'] = 'mail.new-server.localhost';
// $params['port'] = $currentParams['port'] + 1;
// }
// }
// return $params;
// }

/**
* Set the default addresses used for the vacation module.
Expand Down
2 changes: 1 addition & 1 deletion doc/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
v4.0.0alpha7
------------


[mpa] Add transport_params hook to for changing transport params based on current user or other factors


------------
Expand Down
23 changes: 18 additions & 5 deletions lib/Factory/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ public function create(array $transport)
{
global $registry;

$coreHooks = $GLOBALS['injector']->getInstance('Horde_Core_Hooks');
$transportDriver = $transport['driver'];
$transportParams = $transport['params'];

/* Get authentication parameters. */
try {
$auth = $GLOBALS['injector']->getInstance('Horde_Core_Hooks')
->callHook('transport_auth', 'ingo', array($transport['driver']));
$auth = $coreHooks->callHook('transport_auth', 'ingo', [$transportDriver]);
} catch (Horde_Exception_HookNotSet $e) {
$auth = null;
}

if (!is_array($auth)) {
$auth = array();
$auth = [];
}

if (!isset($auth['password'])) {
Expand All @@ -57,9 +60,19 @@ public function create(array $transport)
$auth['euser'] = Ingo::getUser(false);
}

$class = 'Ingo_Transport_' . ucfirst($transport['driver']);
// Get transport parameters.
try {
$customParams = $coreHooks->callHook('transport_params', 'ingo', [$transportDriver, $transportParams]);
} catch (Horde_Exception_HookNotSet $e) {
$customParams = null;
}
if (is_array($customParams)){
$transportParams = array_merge($transportParams, $customParams);
}

$class = 'Ingo_Transport_' . ucfirst($transportDriver);
if (class_exists($class)) {
return new $class(array_merge($auth, $transport['params']));
return new $class(array_merge($auth, $transportParams));
}

throw new Ingo_Exception(sprintf(_("Unable to load the transport driver \"%s\"."), $class));
Expand Down

0 comments on commit 08269f5

Please sign in to comment.