Skip to content

Commit

Permalink
Move script-related UI methods out of Ingo:: and into Ingo_Script_Util
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Feb 5, 2014
1 parent 2b67268 commit ff058c5
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 135 deletions.
13 changes: 5 additions & 8 deletions ingo/lib/Api.php
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 2 additions & 4 deletions ingo/lib/Basic/Blacklist.php
Expand Up @@ -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'))) {
Expand Down Expand Up @@ -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());
}
Expand Down
10 changes: 4 additions & 6 deletions ingo/lib/Basic/Filters.php
Expand Up @@ -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;

Expand Down
6 changes: 2 additions & 4 deletions ingo/lib/Basic/Forward.php
Expand Up @@ -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'))) {
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 5 additions & 7 deletions ingo/lib/Basic/Rule.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions ingo/lib/Basic/Script.php
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions ingo/lib/Basic/Spam.php
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down
7 changes: 3 additions & 4 deletions ingo/lib/Basic/Vacation.php
Expand Up @@ -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'))) {
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions ingo/lib/Basic/Whitelist.php
Expand Up @@ -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'))) {
Expand All @@ -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());
Expand Down
86 changes: 3 additions & 83 deletions ingo/lib/Ingo.php
Expand Up @@ -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);
}

/**
Expand All @@ -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.
*
Expand Down
8 changes: 5 additions & 3 deletions ingo/lib/Script/Maildrop.php
Expand Up @@ -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'
);
Expand Down
8 changes: 5 additions & 3 deletions ingo/lib/Script/Procmail/Recipe.php
Expand Up @@ -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" \\';
Expand Down
9 changes: 6 additions & 3 deletions ingo/lib/Script/Sieve/Action/Vacation.php
Expand Up @@ -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']
)
)
. '";';
}

Expand Down

0 comments on commit ff058c5

Please sign in to comment.