diff --git a/ingo/lib/Api.php b/ingo/lib/Api.php index 0ffa5d26b0a..fd5220effaa 100644 --- a/ingo/lib/Api.php +++ b/ingo/lib/Api.php @@ -95,7 +95,7 @@ public function blacklistFrom($addresses) try { $bl = $injector->getInstance('Ingo_Factory_Storage')->create()->retrieve(Ingo_Storage::ACTION_BLACKLIST)->getBlacklist(); Ingo::updateListFilter(array_merge($bl, $addresses), Ingo_Storage::ACTION_BLACKLIST); - Ingo::updateScript(); + Ingo_Script_Util::update(false); foreach ($addresses as $from) { $notification->push(sprintf(_("The address \"%s\" has been added to your blacklist."), $from)); } @@ -117,7 +117,7 @@ public function whitelistFrom($addresses) try { $wl = $injector->getInstance('Ingo_Factory_Storage')->create()->retrieve(Ingo_Storage::ACTION_WHITELIST)->getWhitelist(); Ingo::updateListFilter(array_merge($wl, $addresses), Ingo_Storage::ACTION_WHITELIST); - Ingo::updateScript(); + Ingo_Script_Util::update(false); foreach ($addresses as $from) { $notification->push(sprintf(_("The address \"%s\" has been added to your whitelist."), $from)); } @@ -226,9 +226,8 @@ public function setVacation($info, $enable = true) $filters->ruleDisable($vacation_id); } $ingo_storage->store($filters); - if ($GLOBALS['prefs']->getValue('auto_update')) { - Ingo::updateScript(); - } + + Ingo_Script_Util::update(); /* Update the timestamp for the rules. */ $GLOBALS['session']->set('ingo', 'change', time()); @@ -270,9 +269,7 @@ public function disableVacation() $vacation_id = $filters->findRuleId(Ingo_Storage::ACTION_VACATION); $filters->ruleDisable($vacation_id); $ingo_storage->store($filters); - if ($GLOBALS['prefs']->getValue('auto_update')) { - Ingo::updateScript(); - } + Ingo_Script_Util::update(); /* Update the timestamp for the rules. */ $GLOBALS['session']->set('ingo', 'change', time()); diff --git a/ingo/lib/Basic/Blacklist.php b/ingo/lib/Basic/Blacklist.php index 97e39ea61b4..d1a7046920c 100644 --- a/ingo/lib/Basic/Blacklist.php +++ b/ingo/lib/Basic/Blacklist.php @@ -27,7 +27,7 @@ class Ingo_Basic_Blacklist extends Ingo_Basic_Base */ protected function _init() { - global $injector, $notification, $page_output, $prefs, $session; + global $injector, $notification, $page_output, $session; /* Redirect if blacklist is not available. */ if (!in_array(Ingo_Storage::ACTION_BLACKLIST, $session->get('ingo', 'script_categories'))) { @@ -69,9 +69,7 @@ protected function _init() $blacklist->setBlacklistFolder($folder); $ingo_storage->store($blacklist); $notification->push(_("Changes saved."), 'horde.success'); - if ($prefs->getValue('auto_update')) { - Ingo::updateScript(); - } + Ingo_Script_Util::update(); } catch (Ingo_Exception $e) { $notification->push($e->getMessage(), $e->getCode()); } diff --git a/ingo/lib/Basic/Filters.php b/ingo/lib/Basic/Filters.php index e4210d8a7d4..23e90b461b0 100644 --- a/ingo/lib/Basic/Filters.php +++ b/ingo/lib/Basic/Filters.php @@ -126,12 +126,10 @@ protected function _init() /* Save changes */ $ingo_storage->store($filters); - if ($prefs->getValue('auto_update')) { - try { - Ingo::updateScript(); - } catch (Ingo_Exception $e) { - $notification->push($e->getMessage(), 'horde.error'); - } + try { + Ingo_Script_Util::update(); + } catch (Ingo_Exception $e) { + $notification->push($e->getMessage(), 'horde.error'); } break; diff --git a/ingo/lib/Basic/Forward.php b/ingo/lib/Basic/Forward.php index ab4ee072c3b..23fa0dcb266 100644 --- a/ingo/lib/Basic/Forward.php +++ b/ingo/lib/Basic/Forward.php @@ -27,7 +27,7 @@ class Ingo_Basic_Forward extends Ingo_Basic_Base */ protected function _init() { - global $injector, $notification, $prefs, $session; + global $injector, $notification, $session; /* Redirect if forward is not available. */ if (!in_array(Ingo_Storage::ACTION_FORWARD, $session->get('ingo', 'script_categories'))) { @@ -68,9 +68,7 @@ protected function _init() $notification->push(_("Rule Disabled"), 'horde.success'); $fwd_rule['disable'] = true; } - if ($prefs->getValue('auto_update')) { - Ingo::updateScript(); - } + Ingo_Script_Util::update(); } catch (Ingo_Exception $e) { $notification->push($e); } diff --git a/ingo/lib/Basic/Rule.php b/ingo/lib/Basic/Rule.php index 48b273caa36..9cf0372fb07 100644 --- a/ingo/lib/Basic/Rule.php +++ b/ingo/lib/Basic/Rule.php @@ -27,7 +27,7 @@ class Ingo_Basic_Rule extends Ingo_Basic_Base */ protected function _init() { - global $conf, $injector, $notification, $page_output, $prefs, $session; + global $conf, $injector, $notification, $page_output, $session; /* Check rule permissions. */ $perms = $injector->getInstance('Horde_Core_Perms'); @@ -196,12 +196,10 @@ protected function _init() $ingo_storage->store($filters); $notification->push(_("Changes saved."), 'horde.success'); - if ($prefs->getValue('auto_update')) { - try { - Ingo::updateScript(); - } catch (Ingo_Exception $e) { - $notification->push($e, 'horde.error'); - } + try { + Ingo_Script_Util::update(); + } catch (Ingo_Exception $e) { + $notification->push($e, 'horde.error'); } Ingo_Basic_Filters::url()->redirect(); diff --git a/ingo/lib/Basic/Script.php b/ingo/lib/Basic/Script.php index f3e99d16f01..d37bdd2957c 100644 --- a/ingo/lib/Basic/Script.php +++ b/ingo/lib/Basic/Script.php @@ -53,7 +53,7 @@ protected function _init() case 'action_activate': if (!empty($scripts)) { try { - Ingo::activateScripts($scripts, false); + Ingo_Script_Util::activate($scripts); } catch (Ingo_Exception $e) { $notification->push($e); } @@ -62,7 +62,7 @@ protected function _init() case 'action_deactivate': try { - Ingo::activateScripts('', true); + Ingo_Script_Util::activate('', true); } catch (Ingo_Exception $e) { $notification->push($e); } diff --git a/ingo/lib/Basic/Spam.php b/ingo/lib/Basic/Spam.php index bb37b7e691c..9a98fdd76c3 100644 --- a/ingo/lib/Basic/Spam.php +++ b/ingo/lib/Basic/Spam.php @@ -28,7 +28,7 @@ class Ingo_Basic_Spam extends Ingo_Basic_Base */ protected function _init() { - global $injector, $notification, $session, $prefs; + global $injector, $notification, $session; if (!in_array(Ingo_Storage::ACTION_SPAM, $session->get('ingo', 'script_categories'))) { $notification->push(_("Simple spam filtering is not supported in the current filtering driver."), 'horde.error'); @@ -81,9 +81,7 @@ protected function _init() $notification->push(_("Rule Disabled"), 'horde.success'); $spam_rule['disable'] = true; } - if ($prefs->getValue('auto_update')) { - Ingo::updateScript(); - } + Ingo_Script_Util::update(); } catch (Ingo_Exception $e) { $notification->push($e); } diff --git a/ingo/lib/Basic/Vacation.php b/ingo/lib/Basic/Vacation.php index a528453b7d7..3ba421a4a63 100644 --- a/ingo/lib/Basic/Vacation.php +++ b/ingo/lib/Basic/Vacation.php @@ -27,7 +27,7 @@ class Ingo_Basic_Vacation extends Ingo_Basic_Base */ protected function _init() { - global $injector, $notification, $prefs, $session; + global $injector, $notification, $session; /* Redirect if vacation is not available. */ if (!in_array(Ingo_Storage::ACTION_VACATION, $session->get('ingo', 'script_categories'))) { @@ -82,9 +82,8 @@ protected function _init() $notification->push(_("Rule Disabled"), 'horde.success'); $vac_rule['disable'] = true; } - if ($prefs->getValue('auto_update')) { - Ingo::updateScript(); - } + + Ingo_Script_Util::update(); } catch (Ingo_Exception $e) { $notification->push($e); } diff --git a/ingo/lib/Basic/Whitelist.php b/ingo/lib/Basic/Whitelist.php index 15cca9f8598..608d9fb174f 100644 --- a/ingo/lib/Basic/Whitelist.php +++ b/ingo/lib/Basic/Whitelist.php @@ -28,7 +28,7 @@ class Ingo_Basic_Whitelist extends Ingo_Basic_Base */ protected function _init() { - global $injector, $notification, $page_output, $prefs, $session; + global $injector, $notification, $page_output, $session; /* Redirect if whitelist not available. */ if (!in_array(Ingo_Storage::ACTION_WHITELIST, $session->get('ingo', 'script_categories'))) { @@ -48,9 +48,7 @@ protected function _init() try { Ingo::updateListFilter($this->vars->whitelist, Ingo_Storage::ACTION_WHITELIST); $notification->push(_("Changes saved."), 'horde.success'); - if ($prefs->getValue('auto_update')) { - Ingo::updateScript(); - } + Ingo_Script_Util::update(); /* Update the timestamp for the rules. */ $session->set('ingo', 'change', time()); diff --git a/ingo/lib/Ingo.php b/ingo/lib/Ingo.php index 0deb3804a31..f162fc39dc2 100644 --- a/ingo/lib/Ingo.php +++ b/ingo/lib/Ingo.php @@ -85,59 +85,9 @@ static public function getDomain() $user = self::getUser(true); $pos = strpos($user, '@'); - return ($pos !== false) - ? substr($user, $pos + 1) - : false; - } - - /** - * Connects to the backend, uploads the scripts and sets them active. - * - * @param array $scripts A list of scripts to set active. - * @param boolean $deactivate If true, notification will identify the - * script as deactivated instead of activated. - * - * @throws Ingo_Exception - */ - static public function activateScripts($scripts, $deactivate = false) - { - foreach ($scripts as $script) { - try { - $GLOBALS['injector'] - ->getInstance('Ingo_Factory_Transport') - ->create($script['transport']) - ->setScriptActive($script); - } catch (Ingo_Exception $e) { - $msg = $deactivate - ? _("There was an error deactivating the script.") - : _("There was an error activating the script."); - throw new Ingo_Exception(sprintf(_("%s The driver said: %s"), $msg, $e->getMessage())); - } - } - - $msg = ($deactivate) - ? _("Script successfully deactivated.") - : _("Script successfully activated."); - $GLOBALS['notification']->push($msg, 'horde.success'); - } - - /** - * Does all the work in updating the script on the server. - * - * @throws Ingo_Exception - */ - static public function updateScript() - { - foreach ($GLOBALS['injector']->getInstance('Ingo_Factory_Script')->createAll() as $script) { - if ($script->hasFeature('script_file')) { - try { - /* Generate and activate the script. */ - self::activateScripts($script->generate()); - } catch (Ingo_Exception $e) { - throw new Ingo_Exception(sprintf(_("Script not updated: %s"), $e->getMessage())); - } - } - } + return ($pos === false) + ? false + : substr($user, $pos + 1); } /** @@ -159,36 +109,6 @@ static public function hasSharePermission($mask = null) : true; } - /** - * Returns the vacation reason with all placeholder replaced. - * - * @param string $reason The vacation reason including placeholders. - * @param integer $start The vacation start timestamp. - * @param integer $end The vacation end timestamp. - * - * @return string The vacation reason suitable for usage in the filter - * scripts. - */ - static public function getReason($reason, $start, $end) - { - $identity = $GLOBALS['injector'] - ->getInstance('Horde_Core_Factory_Identity') - ->create(Ingo::getUser()); - $format = $GLOBALS['prefs']->getValue('date_format'); - - return str_replace(array('%NAME%', - '%EMAIL%', - '%SIGNATURE%', - '%STARTDATE%', - '%ENDDATE%'), - array($identity->getName(), - $identity->getDefaultFromAddress(), - $identity->getValue('signature'), - $start ? strftime($format, $start) : '', - $end ? strftime($format, $end) : ''), - $reason); - } - /** * Updates a list (blacklist/whitelist) filter. * diff --git a/ingo/lib/Script/Maildrop.php b/ingo/lib/Script/Maildrop.php index b4e376c0a7c..19e419dba9b 100644 --- a/ingo/lib/Script/Maildrop.php +++ b/ingo/lib/Script/Maildrop.php @@ -265,9 +265,11 @@ public function generateVacation($disable = false) $this->_addItem( Ingo::RULE_VACATION, new Ingo_Script_String( - Ingo::getReason($vacation->getVacationReason(), - $vacation->getVacationStart(), - $vacation->getVacationEnd()) + Ingo_Script_Util::vacationReason( + $vacation->getVacationReason(), + $vacation->getVacationStart(), + $vacation->getVacationEnd() + ) ), 'vacation.msg' ); diff --git a/ingo/lib/Script/Procmail/Recipe.php b/ingo/lib/Script/Procmail/Recipe.php index 074dffd471d..f9089ed3194 100644 --- a/ingo/lib/Script/Procmail/Recipe.php +++ b/ingo/lib/Script/Procmail/Recipe.php @@ -192,9 +192,11 @@ public function __construct($params = array(), $scriptparams = array()) $this->_action[] = ' | (formail -rI"Precedence: junk" \\'; $this->_action[] = ' -a"From: <' . $address . '>" \\'; $this->_action[] = ' -A"X-Loop: ' . $address . '" \\'; - $reason = Ingo::getReason($params['action-value']['reason'], - $params['action-value']['start'], - $params['action-value']['end']); + $reason = Ingo_Script_Util::vacationReason( + $params['action-value']['reason'], + $params['action-value']['start'], + $params['action-value']['end'] + ); if (Horde_Mime::is8bit($reason)) { $this->_action[] = ' -i"Subject: ' . Horde_Mime::encode($params['action-value']['subject'] . ' (Re: $SUBJECT)') . '" \\'; $this->_action[] = ' -i"Content-Transfer-Encoding: quoted-printable" \\'; diff --git a/ingo/lib/Script/Sieve/Action/Vacation.php b/ingo/lib/Script/Sieve/Action/Vacation.php index 2e22050a1b3..395535a4061 100644 --- a/ingo/lib/Script/Sieve/Action/Vacation.php +++ b/ingo/lib/Script/Sieve/Action/Vacation.php @@ -192,9 +192,12 @@ protected function _vacationCode() return $code . '"' . Ingo_Script_Sieve::escapeString( - Ingo::getReason($this->_vars['reason'], - $this->_vars['start'], - $this->_vars['end'])) + Ingo_Script_Util::vacationReason( + $this->_vars['reason'], + $this->_vars['start'], + $this->_vars['end'] + ) + ) . '";'; } diff --git a/ingo/lib/Script/Util.php b/ingo/lib/Script/Util.php new file mode 100644 index 00000000000..564dc0d8426 --- /dev/null +++ b/ingo/lib/Script/Util.php @@ -0,0 +1,121 @@ + + * @category Horde + * @copyright 2014 Horde LLC + * @license http://www.horde.org/licenses/apache ASL + * @package Ingo + */ +class Ingo_Script_Util +{ + /** + * Connects to the backend, uploads the scripts and sets them active. + * + * @param array $scripts A list of scripts to set active. + * @param boolean $deactivate If true, notification will identify the + * script as deactivated instead of activated. + * + * @throws Ingo_Exception + */ + static public function activate($scripts, $deactivate = false) + { + global $injector, $notification; + + foreach ($scripts as $script) { + try { + $injector->getInstance('Ingo_Factory_Transport') + ->create($script['transport']) + ->setScriptActive($script); + } catch (Ingo_Exception $e) { + $msg = $deactivate + ? _("There was an error deactivating the script.") + : _("There was an error activating the script."); + throw new Ingo_Exception( + sprintf(_("%s The driver said: %s"), $msg, $e) + ); + } + } + + $msg = $deactivate + ? _("Script successfully deactivated.") + : _("Script successfully activated."); + $notification->push($msg, 'horde.success'); + } + + /** + * Does all the work in updating the script on the server. + * + * @param boolean $auto_update Only update if auto_update is active? + * + * @throws Ingo_Exception + */ + static public function update($auto_update = true) + { + global $injector, $prefs; + + if ($auto_update && !$prefs->getValue('auto_update')) { + return; + } + + foreach ($injector->getInstance('Ingo_Factory_Script')->createAll() as $script) { + if ($script->hasFeature('script_file')) { + try { + /* Generate and activate the script. */ + self::activate($script->generate()); + } catch (Ingo_Exception $e) { + throw new Ingo_Exception( + sprintf(_("Script not updated: %s"), $e) + ); + } + } + } + } + + /** + * Returns the vacation reason with all placeholder replaced. + * + * @param string $reason The vacation reason including placeholders. + * @param integer $start The vacation start timestamp. + * @param integer $end The vacation end timestamp. + * + * @return string The vacation reason suitable for usage in the filter + * scripts. + */ + static public function vacationReason($reason, $start, $end) + { + global $injector, $prefs; + + $format = $prefs->getValue('date_format'); + $identity = $injector->getInstance('Horde_Core_Factory_Identity') + ->create(Ingo::getUser()); + + $replace = array( + '%NAME%' => $identity->getName(), + '%EMAIL%' => $identity->getDefaultFromAddress(), + '%SIGNATURE%' => $identity->getValue('signature'), + '%STARTDATE%' => $start ? strftime($format, $start) : '', + '%ENDDATE%' => $end ? strftime($format, $end) : '' + ); + + return str_replace( + array_keys($replace), + array_values($replace), + $reason + ); + } + +} diff --git a/ingo/package.xml b/ingo/package.xml index 4a7473e9b37..2f332374f1a 100644 --- a/ingo/package.xml +++ b/ingo/package.xml @@ -182,6 +182,7 @@ + @@ -705,6 +706,7 @@ +