Author: Michael Slusarz (slusarz@horde.org)
Ejemplo n.º 1
0
 /**
  */
 public function __get($name)
 {
     global $injector;
     switch ($name) {
         case 'max_rules':
             return $injector->getInstance('Horde_Core_Perms')->hasAppPermission(Ingo_Perms::getPerm('max_rules'));
     }
 }
Ejemplo n.º 2
0
 /**
  */
 public function setForwardAddresses($data)
 {
     $addr = $this->_addressList($data);
     $max = $GLOBALS['injector']->getInstance('Horde_Core_Perms')->hasAppPermission(Ingo_Perms::getPerm('max_forward'));
     if ($max !== true && !empty($max)) {
         $addr_count = count($addr);
         if ($addr_count > $max) {
             throw new Ingo_Exception(sprintf(_("Maximum number of forward addresses exceeded (Total addresses: %s, Maximum addresses: %s)."), $addr_count, $max));
         }
     }
     $this->_addr = $addr;
 }
Ejemplo n.º 3
0
 /**
  * Sets the list of blacklisted addresses.
  *
  * @param mixed $data  The list of addresses (array or string).
  *
  * @throws Ingo_Exception
  */
 public function setBlacklist($data)
 {
     global $injector;
     $addr = $this->_addressList($data);
     $max = $injector->getInstance('Horde_Core_Perms')->hasAppPermission(Ingo_Perms::getPerm('max_blacklist'));
     if ($max !== true && !empty($max)) {
         $addr_count = count($addr);
         if ($addr_count > $max) {
             throw new Ingo_Exception(sprintf(_("Maximum number of blacklisted addresses exceeded (Total addresses: %s, Maximum addresses: %s).  Could not add new addresses to blacklist."), $addr_count, $max));
         }
     }
     $this->_addr = $addr;
 }
Ejemplo n.º 4
0
 /**
  * Add addresses to the current address list.
  *
  * @param mixed $add  Addresses to add.
  *
  * @throws Ingo_Exception
  */
 public function addAddresses($to_add)
 {
     global $injector;
     $addr = clone $this->_addr;
     $addr->add($to_add);
     $addr->unique();
     $max = is_null($this->_perm) ? false : $injector->getInstance('Horde_Core_Perms')->hasAppPermission(Ingo_Perms::getPerm($this->_perm));
     if ($max !== true && !empty($max)) {
         $addr_count = count($addr);
         if ($addr_count > $max) {
             throw $this->_setAddressesException($addr_count, $max);
         }
     }
     $this->_addr = $addr;
 }
Ejemplo n.º 5
0
 /**
  */
 protected function _init()
 {
     global $conf, $injector, $notification, $page_output;
     /* Check rule permissions. */
     $max = $injector->getInstance('Horde_Core_Perms')->hasAppPermission(Ingo_Perms::getPerm('max_rules'));
     if ($max === 0) {
         Horde::permissionDeniedError('ingo', 'allow_rules', _("You are not allowed to create or edit custom rules."));
         Ingo_Basic_Filters::url()->redirect();
     }
     if (!Ingo::hasSharePermission(Horde_Perms::EDIT)) {
         $notification->push(_("You do not have permission to edit filter rules."), 'horde.error');
         Ingo_Basic_Filters::url()->redirect();
     }
     /* Load the Ingo_Script:: driver. */
     $ingo_script = $injector->getInstance('Ingo_Factory_Script')->create(Ingo::RULE_FILTER);
     /* Redirect if no rules are available. */
     $availActions = $ingo_script->availableActions();
     if (empty($availActions)) {
         $notification->push(_("Individual rules are not supported in the current filtering driver."), 'horde.error');
         Ingo_Basic_Filters::url()->redirect();
     }
     /* This provides the $ingo_fields array. */
     $config = new Horde_Registry_LoadConfig('ingo', 'fields.php', 'ingo_fields');
     $ingo_fields = $config->config['ingo_fields'];
     /* Get the current rules. */
     $ingo_storage = $injector->getInstance('Ingo_Factory_Storage')->create();
     $filters = $ingo_storage->retrieve(Ingo_Storage::ACTION_FILTERS);
     if ($this->_assertMaxRules($max, $filters)) {
         Ingo_Basic_Filters::url()->redirect();
     }
     /* Token checking. */
     $actionID = $this->_checkToken(array('rule_save', 'rule_delete'));
     /* Update the current rules before performing any action. */
     if (isset($this->vars->action)) {
         $rule = array('action' => $this->vars->action, 'combine' => $this->vars->combine, 'conditions' => array(), 'flags' => 0, 'id' => $this->vars->id, 'name' => $this->vars->name, 'stop' => $this->vars->stop);
     } else {
         $rule = isset($this->vars->edit) ? $filters->getRule($this->vars->edit) : $filters->getDefaultRule();
     }
     if (!$rule) {
         $notification->push(_("Filter not found."), 'horde.error');
         Ingo_Basic_Filters::url()->redirect();
     }
     if ($ingo_script->hasFeature('case_sensitive')) {
         $casesensitive = $this->vars->case;
     }
     foreach (array_filter(isset($this->vars->field) ? $this->vars->field : array()) as $key => $val) {
         $condition = array();
         $f_label = null;
         if ($val == Ingo::USER_HEADER) {
             $condition['field'] = empty($this->vars->userheader[$key]) ? '' : $this->vars->userheader[$key];
             $condition['type'] = Ingo_Storage::TYPE_HEADER;
         } elseif (!isset($ingo_fields[$val])) {
             $condition['field'] = $val;
             $condition['type'] = Ingo_Storage::TYPE_HEADER;
         } else {
             $condition['field'] = $val;
             $f_label = $ingo_fields[$val]['label'];
             $condition['type'] = $ingo_fields[$val]['type'];
         }
         $condition['match'] = isset($this->vars->match[$key]) ? $this->vars->match[$key] : '';
         if ($actionID == 'rule_save' && empty($this->vars->value[$key]) && !in_array($condition['match'], array('exists', 'not exist'))) {
             $notification->push(sprintf(_("You cannot create empty conditions. Please fill in a value for \"%s\"."), is_null($f_label) ? $condition['field'] : $f_label), 'horde.error');
             $actionID = null;
         }
         $condition['value'] = isset($this->vars->value[$key]) ? $this->vars->value[$key] : '';
         if (isset($casesensitive)) {
             $condition['case'] = isset($casesensitive[$key]) ? $casesensitive[$key] : '';
         }
         $rule['conditions'][] = $condition;
     }
     if ($this->vars->action) {
         switch ($ingo_storage->getActionInfo($this->vars->action)->type) {
             case 'folder':
                 if ($actionID == 'rule_save') {
                     try {
                         $rule['action-value'] = $this->validateMbox('actionvalue');
                     } catch (Ingo_Exception $e) {
                         $notification->push($e, 'horde.error');
                         $actionID = null;
                     }
                 } else {
                     $rule['action-value'] = $this->vars->actionvalue;
                     if (!$this->vars->actionvalue && isset($this->vars->actionvalue_new)) {
                         $page_output->addInlineScript(array('IngoNewFolder.setNewFolder("actionvalue", ' . Horde_Serialize::serialize($this->vars->actionvalue_new, Horde_Serialize::JSON) . ')'), true);
                     }
                 }
                 break;
             default:
                 $rule['action-value'] = $this->vars->actionvalue;
                 break;
         }
     }
     $flags = empty($this->vars->flags) ? array() : $this->vars->flags;
     foreach ($flags as $val) {
         $rule['flags'] |= $val;
     }
     /* Run through action handlers. */
     switch ($actionID) {
         case 'rule_save':
             if (empty($rule['conditions'])) {
                 $notification->push(_("You need to select at least one field to match."), 'horde.error');
                 break;
             }
             if (!isset($this->vars->edit)) {
                 if ($this->_assertMaxRules($max, $filters)) {
                     break;
                 }
                 $filters->addRule($rule);
             } else {
                 $filters->updateRule($rule, $this->vars->edit);
             }
             $ingo_storage->store($filters);
             $notification->push(_("Changes saved."), 'horde.success');
             try {
                 Ingo_Script_Util::update();
             } catch (Ingo_Exception $e) {
                 $notification->push($e, 'horde.error');
             }
             Ingo_Basic_Filters::url()->redirect();
         case 'rule_delete':
             if (isset($this->vars->conditionnumber)) {
                 unset($rule['conditions'][intval($this->vars->conditionnumber)]);
                 $rule['conditions'] = array_values($rule['conditions']);
             }
             break;
     }
     /* Add new, blank condition. */
     $rule['conditions'][] = array();
     /* Prepare the view. */
     $view = new Horde_View(array('templatePath' => INGO_TEMPLATES . '/basic/rule'));
     $view->addHelper('Horde_Core_View_Helper_Help');
     $view->addHelper('Horde_Core_View_Helper_Image');
     $view->addHelper('Horde_Core_View_Helper_Label');
     $view->addHelper('FormTag');
     $view->addHelper('Tag');
     $view->addHelper('Text');
     $view->avail_types = $ingo_script->availableTypes();
     $view->edit = $this->vars->edit;
     $view->fields = $ingo_fields;
     $view->formurl = $this->_addToken(self::url());
     $view->rule = $rule;
     $view->special = $ingo_script->specialTypes();
     $view->userheader = !empty($conf['rules']['userheader']);
     $filter = array();
     $lastcond = count($rule['conditions']) - 1;
     /* Display the conditions. */
     foreach ($rule['conditions'] as $cond_num => $condition) {
         $tmp = array('cond_num' => intval($cond_num), 'field' => isset($condition['field']) ? $condition['field'] : '', 'lastfield' => $lastcond == $cond_num);
         if ($view->userheader && isset($condition['type']) && $condition['type'] == Ingo_Storage::TYPE_HEADER && !isset($ingo_fields[$tmp['field']])) {
             $tmp['userheader'] = $tmp['field'];
         }
         if ($tmp['lastfield']) {
             $filter[] = $tmp;
             continue;
         }
         /* Create the match listing. */
         if (!isset($condition['field']) || $condition['field'] == Ingo::USER_HEADER || !isset($ingo_fields[$condition['field']]['tests'])) {
             $avail_tests = $ingo_script->availableTests();
         } else {
             $avail_tests = $ingo_fields[$condition['field']]['tests'];
         }
         $tmp['matchtest'] = array();
         $selected_test = empty($condition['match']) ? null : $condition['match'];
         foreach ($avail_tests as $test) {
             if (is_null($selected_test)) {
                 $selected_test = $test;
             }
             $tmp['matchtest'][] = array('label' => $ingo_storage->getTestInfo($test)->label, 'selected' => isset($condition['match']) && $test == $condition['match'], 'value' => $test);
         }
         if (!in_array($selected_test, array('exists', 'not exist'))) {
             $tmp['match_value'] = isset($condition['value']) ? $condition['value'] : '';
         }
         $testOb = $ingo_storage->getTestInfo(!empty($condition['match']) ? $condition['match'] : 'contains');
         switch ($testOb->type) {
             case 'text':
                 if ($ingo_script->hasFeature('case_sensitive')) {
                     $tmp['case_sensitive'] = !empty($condition['case']);
                 }
                 break;
         }
         $filter[] = $tmp;
     }
     $view->filter = $filter;
     /* Get the action select output. */
     $actions = array();
     $current_action = false;
     foreach ($availActions as $val) {
         $action = $ingo_storage->getActionInfo($val);
         $actions[] = array('label' => $action->label, 'selected' => $val == $rule['action'], 'value' => $val);
         if ($val == $rule['action']) {
             $current_action = $action;
         }
     }
     $view->actions = $actions;
     /* Get the action value output. */
     if ($current_action) {
         switch ($current_action->type) {
             case 'folder':
                 $view->actionvaluelabel = _("Select target folder");
                 $view->actionvalue = Ingo_Flist::select($rule['action-value']);
                 break;
             case 'text':
             case 'int':
                 $view->actionvaluelabel = _("Value");
                 $view->actionvalue = '<input id="actionvalue" name="actionvalue" size="40" value="' . htmlspecialchars($rule['action-value']) . '" />';
                 break;
         }
         $view->flags = $current_action->flags && $ingo_script->hasFeature('imap_flags');
     }
     $view->stop = $ingo_script->hasFeature('stop_script');
     $page_output->addScriptFile('rule.js');
     $page_output->addInlineJsVars(array('IngoRule.filtersurl' => strval(Ingo_Basic_Filters::url()->setRaw(true))));
     $this->header = $rule['name'];
     $this->output = $view->render('rule');
 }
Ejemplo n.º 6
0
 /**
  * Add additional items to the sidebar.
  *
  * @param Horde_View_Sidebar $sidebar  The sidebar object.
  */
 public function sidebar($sidebar)
 {
     global $injector, $session;
     $actions = array();
     foreach ($injector->getInstance('Ingo_Factory_Script')->createAll() as $script) {
         $actions = array_merge($actions, $script->availableActions());
     }
     $filters = $injector->getInstance('Ingo_Factory_Storage')->create()->retrieve(Ingo_Storage::ACTION_FILTERS)->getFilterList();
     if (!empty($actions)) {
         $max = $injector->getInstance('Horde_Core_Perms')->hasAppPermission(Ingo_Perms::getPerm('max_rules'));
         if ($max === true || $max > count($filters)) {
             $sidebar->addNewButton(_("New Rule"), Ingo_Basic_Rule::url());
         }
     }
     if ($injector->getInstance('Ingo_Shares') && count($all_rulesets = $this->_listRulesets()) > 1) {
         $url = Ingo_Basic_Filters::url();
         $current = $session->get('ingo', 'current_share');
         $sidebar->containers['rulesets'] = array('header' => array('id' => 'ingo-toggle-rules', 'label' => _("Ruleset"), 'collapsed' => false));
         foreach ($all_rulesets as $id => $ruleset) {
             $row = array('selected' => $current == $id, 'url' => $url->add('ruleset', $id), 'label' => $ruleset->get('name'), 'type' => 'radiobox');
             $sidebar->addRow($row, 'rulesets');
         }
     }
 }
Ejemplo n.º 7
0
 /**
  */
 protected function _init()
 {
     global $injector, $notification, $page_output, $prefs, $session;
     /* Get the list of filter rules. */
     $ingo_storage = $injector->getInstance('Ingo_Factory_Storage')->create();
     $filters = $ingo_storage->retrieve(Ingo_Storage::ACTION_FILTERS);
     /* Load the Ingo_Script factory. */
     $factory = $injector->getInstance('Ingo_Factory_Script');
     /* Get permissions. */
     $edit_allowed = Ingo::hasSharePermission(Horde_Perms::EDIT);
     $delete_allowed = Ingo::hasSharePermission(Horde_Perms::DELETE);
     /* Permissions. */
     $perms = $injector->getInstance('Horde_Core_Perms');
     /* Token checking. */
     $actionID = $this->_checkToken(array('rule_copy', 'rule_delete', 'rule_disable', 'rule_enable'));
     /* Default to no mailbox filtering. */
     $mbox_search = null;
     /* Perform requested actions. */
     switch ($actionID) {
         case 'mbox_search':
             if (isset($this->vars->searchfield)) {
                 $mbox_search = array('exact' => $this->vars->get('searchexact', 1), 'query' => $this->vars->searchfield);
             }
             break;
         case 'rule_copy':
         case 'rule_delete':
         case 'rule_disable':
         case 'rule_enable':
             if (!$edit_allowed) {
                 $notification->push(_("You do not have permission to edit filter rules."), 'horde.error');
                 self::url()->redirect();
             }
             switch ($actionID) {
                 case 'rule_delete':
                     if (!$delete_allowed) {
                         $notification->push(_("You do not have permission to delete filter rules."), 'horde.error');
                         self::url()->redirect();
                     }
                     $tmp = $filters->getFilter($this->vars->rulenumber);
                     if ($filters->deleteRule($this->vars->rulenumber)) {
                         $notification->push(sprintf(_("Rule \"%s\" deleted."), $tmp['name']), 'horde.success');
                     }
                     break;
                 case 'rule_copy':
                     $max = $perms->hasAppPermission(Ingo_Perms::getPerm('max_rules'));
                     if ($max === 0) {
                         Horde::permissionDeniedError('ingo', 'max_rules', _("You are not allowed to create or edit custom rules."));
                         break 2;
                     } elseif ($max !== true && $max <= count($filters->getFilterList())) {
                         Horde::permissionDeniedError('ingo', 'max_rules', sprintf(_("You are not allowed to create more than %d rules."), $max));
                         break 2;
                     }
                     $tmp = $filters->getFilter($this->vars->rulenumber);
                     if ($filters->copyRule($this->vars->rulenumber)) {
                         $notification->push(sprintf(_("Rule \"%s\" copied."), $tmp['name']), 'horde.success');
                     }
                     break;
                 case 'rule_disable':
                     $tmp = $filters->getFilter($this->vars->rulenumber);
                     $filters->ruleDisable($this->vars->rulenumber);
                     $notification->push(sprintf(_("Rule \"%s\" disabled."), $tmp['name']), 'horde.success');
                     break;
                 case 'rule_enable':
                     $tmp = $filters->getFilter($this->vars->rulenumber);
                     $filters->ruleEnable($this->vars->rulenumber);
                     $notification->push(sprintf(_("Rule \"%s\" enabled."), $tmp['name']), 'horde.success');
                     break;
             }
             /* Save changes */
             $ingo_storage->store($filters);
             try {
                 Ingo_Script_Util::update();
             } catch (Ingo_Exception $e) {
                 $notification->push($e->getMessage(), 'horde.error');
             }
             break;
         case 'settings_save':
             if (!$edit_allowed) {
                 $notification->push(_("You do not have permission to edit filter rules."), 'horde.error');
                 self::url()->redirect();
             }
             $prefs->setValue('show_filter_msg', $this->vars->show_filter_msg);
             $prefs->setValue('filter_seen', $this->vars->filter_seen);
             $notification->push(_("Settings successfully updated."), 'horde.success');
             break;
         case 'apply_filters':
             $factory->perform();
             break;
     }
     /* Get the list of rules now. */
     $filter_list = $filters->getFilterList();
     /* Common URLs. */
     $filters_url = $this->_addToken(self::url());
     $rule_url = Ingo_Basic_Rule::url();
     $view = new Horde_View(array('templatePath' => INGO_TEMPLATES . '/basic/filters'));
     $view->addHelper('Horde_Core_View_Helper_Help');
     $view->addHelper('Horde_Core_View_Helper_Image');
     $view->addHelper('Horde_Core_View_Helper_Label');
     $view->addHelper('FormTag');
     $view->addHelper('Tag');
     $view->canapply = $factory->canPerform();
     $view->deleteallowed = $delete_allowed;
     $view->editallowed = $edit_allowed;
     $view->formurl = $filters_url;
     if (count($filter_list)) {
         $display = array();
         $s_categories = $session->get('ingo', 'script_categories');
         $view->can_copy = $edit_allowed && (($max_rules = $perms->hasAppPermission(Ingo_Perms::getPerm('max_rules'))) === true || $max_rules > count($filter_list));
         foreach ($filter_list as $rule_number => $filter) {
             /* Non-display categories. */
             if (!in_array($filter['action'], $s_categories)) {
                 $display[$rule_number] = false;
                 continue;
             }
             $copyurl = $delurl = $editurl = $name = null;
             $entry = array();
             $url = $filters_url->copy()->add('rulenumber', $rule_number);
             switch ($filter['action']) {
                 case Ingo_Storage::ACTION_BLACKLIST:
                     if (!is_null($mbox_search)) {
                         continue 2;
                     }
                     $editurl = Ingo_Basic_Blacklist::url();
                     $entry['filterimg'] = 'blacklist.png';
                     $name = _("Blacklist");
                     break;
                 case Ingo_Storage::ACTION_WHITELIST:
                     if (!is_null($mbox_search)) {
                         continue 2;
                     }
                     $editurl = Ingo_Basic_Whitelist::url();
                     $entry['filterimg'] = 'whitelist.png';
                     $name = _("Whitelist");
                     break;
                 case Ingo_Storage::ACTION_VACATION:
                     if (!is_null($mbox_search)) {
                         continue 2;
                     }
                     $editurl = Ingo_Basic_Vacation::url();
                     $entry['filterimg'] = 'vacation.png';
                     $name = _("Vacation");
                     break;
                 case Ingo_Storage::ACTION_FORWARD:
                     if (!is_null($mbox_search)) {
                         continue 2;
                     }
                     $editurl = Ingo_Basic_Forward::url();
                     $entry['filterimg'] = 'forward.png';
                     $name = _("Forward");
                     break;
                 case Ingo_Storage::ACTION_SPAM:
                     if (!is_null($mbox_search)) {
                         continue 2;
                     }
                     $editurl = Ingo_Basic_Spam::url();
                     $entry['filterimg'] = 'spam.png';
                     $name = _("Spam Filter");
                     break;
                 default:
                     if (!is_null($mbox_search)) {
                         if ($mbox_search['exact']) {
                             if (strcasecmp($filter['action-value'], $mbox_search['query']) !== 0) {
                                 continue 2;
                             }
                         } elseif (stripos($filter['action-value'], $mbox_search['query']) === false) {
                             continue 2;
                         }
                     }
                     $editurl = $rule_url->copy()->add(array('edit' => $rule_number));
                     $delurl = $url->copy()->add('actionID', 'rule_delete');
                     $copyurl = $url->copy()->add('actionID', 'rule_copy');
                     $name = $filter['name'];
                     break;
             }
             /* Create description. */
             if (!$edit_allowed) {
                 $entry['descriplink'] = htmlspecialchars($name);
             } elseif (!empty($filter['conditions'])) {
                 $entry['descriplink'] = Horde::linkTooltip($editurl, sprintf(_("Edit %s"), $name), null, null, null, $ingo_storage->ruleDescription($filter)) . htmlspecialchars($name) . '</a>';
             } else {
                 $entry['descriplink'] = Horde::link($editurl, sprintf(_("Edit %s"), $name)) . htmlspecialchars($name) . '</a>';
             }
             /* Create delete link. */
             if ($delete_allowed && !is_null($delurl)) {
                 $entry['dellink'] = Horde::link($delurl, sprintf(_("Delete %s"), $name), null, null, "return window.confirm('" . addslashes(_("Are you sure you want to delete this rule?")) . "');");
             }
             /* Create copy link. */
             if ($view->can_copy && !is_null($copyurl)) {
                 $entry['copylink'] = Horde::link($copyurl, sprintf(_("Copy %s"), $name));
             }
             /* Create disable/enable link. */
             if (empty($filter['disable'])) {
                 $entry['disabled'] = true;
                 if ($edit_allowed) {
                     $entry['disablelink'] = Horde::link($url->copy()->add('actionID', 'rule_disable'), sprintf(_("Disable %s"), $name));
                 }
             } elseif ($edit_allowed) {
                 $entry['enablelink'] = Horde::link($url->copy()->add('actionID', 'rule_enable'), sprintf(_("Enable %s"), $name));
             }
             $display[$rule_number] = $entry;
         }
         $view->filter = $display;
         $view->mbox_search = $mbox_search;
     }
     if ($edit_allowed && is_null($mbox_search)) {
         if ($factory->hasFeature('on_demand')) {
             $view->settings = true;
             $view->flags = $prefs->getValue('filter_seen');
             $view->show_filter_msg = $prefs->getValue('show_filter_msg');
         }
         $page_output->addScriptFile('hordecore.js', 'horde');
         $page_output->addScriptPackage('Horde_Core_Script_Package_Sortable');
     }
     $page_output->addScriptFile('stripe.js', 'horde');
     $page_output->addScriptFile('filters.js');
     $topbar = $injector->getInstance('Horde_View_Topbar');
     $topbar->search = true;
     $topbar->searchAction = self::url();
     $topbar->searchLabel = _("Mailbox Search");
     $topbar->searchParameters = array('actionID' => 'mbox_search', 'searchexact' => 0, 'page' => 'filters');
     $this->header = _("Filter Rules");
     $this->output = $view->render('filters');
 }