Overview

Packages

  • Horde
    • Icalendar
      • UnitTests
  • Ingo
    • UnitTests
  • None

Classes

  • Horde_Core_Ui_VarRenderer_Ingo
  • Ingo
  • Ingo_Api
  • Ingo_Exception
  • Ingo_Exception_Pear
  • Ingo_LoginTasks_SystemTask_Upgrade
  • Ingo_Script
  • Ingo_Script_Imap
  • Ingo_Script_Imap_Api
  • Ingo_Script_Imap_Live
  • Ingo_Script_Maildrop
  • Ingo_Script_Maildrop_Comment
  • Ingo_Script_Maildrop_Recipe
  • Ingo_Script_Maildrop_Variable
  • Ingo_Script_Procmail
  • Ingo_Script_Procmail_Comment
  • Ingo_Script_Procmail_Recipe
  • Ingo_Script_Procmail_Variable
  • Ingo_Script_Sieve
  • Ingo_Script_Sieve_Action
  • Ingo_Script_Sieve_Action_Addflag
  • Ingo_Script_Sieve_Action_Discard
  • Ingo_Script_Sieve_Action_Fileinto
  • Ingo_Script_Sieve_Action_Flag
  • Ingo_Script_Sieve_Action_Keep
  • Ingo_Script_Sieve_Action_Notify
  • Ingo_Script_Sieve_Action_Redirect
  • Ingo_Script_Sieve_Action_Reject
  • Ingo_Script_Sieve_Action_Removeflag
  • Ingo_Script_Sieve_Action_Stop
  • Ingo_Script_Sieve_Action_Vacation
  • Ingo_Script_Sieve_Comment
  • Ingo_Script_Sieve_Else
  • Ingo_Script_Sieve_Elsif
  • Ingo_Script_Sieve_If
  • Ingo_Script_Sieve_Test
  • Ingo_Script_Sieve_Test_Address
  • Ingo_Script_Sieve_Test_Allof
  • Ingo_Script_Sieve_Test_Anyof
  • Ingo_Script_Sieve_Test_Body
  • Ingo_Script_Sieve_Test_Exists
  • Ingo_Script_Sieve_Test_False
  • Ingo_Script_Sieve_Test_Header
  • Ingo_Script_Sieve_Test_Not
  • Ingo_Script_Sieve_Test_Relational
  • Ingo_Script_Sieve_Test_Size
  • Ingo_Script_Sieve_Test_True
  • Ingo_Storage
  • Ingo_Storage_Blacklist
  • Ingo_Storage_Filters
  • Ingo_Storage_Filters_Sql
  • Ingo_Storage_Forward
  • Ingo_Storage_Mock
  • Ingo_Storage_Prefs
  • Ingo_Storage_Rule
  • Ingo_Storage_Spam
  • Ingo_Storage_Sql
  • Ingo_Storage_Vacation
  • Ingo_Storage_VacationTest
  • Ingo_Storage_Whitelist
  • Ingo_Test
  • Ingo_Transport
  • Ingo_Transport_Ldap
  • Ingo_Transport_Null
  • Ingo_Transport_Sivtest
  • Ingo_Transport_Timsieved
  • Ingo_Transport_Vfs
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Ingo_Script_Maildrop:: class represents a maildrop script generator.
  4:  *
  5:  * Copyright 2005-2007 Matt Weyland <mathias@weyland.ch>
  6:  *
  7:  * See the enclosed file LICENSE for license information (ASL).  If you
  8:  * did not receive this file, see http://www.horde.org/licenses/apache.
  9:  *
 10:  * @author  Matt Weyland <mathias@weyland.ch>
 11:  * @package Ingo
 12:  */
 13: class Ingo_Script_Maildrop extends Ingo_Script
 14: {
 15: 
 16:     /* Additional storage action since maildrop does not support the
 17:      * "c-flag" as in procmail. */
 18:     const MAILDROP_STORAGE_ACTION_STOREANDFORWARD = 100;
 19: 
 20:     /**
 21:      * The list of actions allowed (implemented) for this driver.
 22:      *
 23:      * @var array
 24:      */
 25:     protected $_actions = array(
 26:         Ingo_Storage::ACTION_KEEP,
 27:         Ingo_Storage::ACTION_MOVE,
 28:         Ingo_Storage::ACTION_DISCARD,
 29:         Ingo_Storage::ACTION_REDIRECT,
 30:         Ingo_Storage::ACTION_REDIRECTKEEP,
 31:         Ingo_Storage::ACTION_REJECT,
 32:     );
 33: 
 34:     /**
 35:      * The categories of filtering allowed.
 36:      *
 37:      * @var array
 38:      */
 39:     protected $_categories = array(
 40:         Ingo_Storage::ACTION_BLACKLIST,
 41:         Ingo_Storage::ACTION_WHITELIST,
 42:         Ingo_Storage::ACTION_VACATION,
 43:         Ingo_Storage::ACTION_FORWARD,
 44:         Ingo_Storage::ACTION_SPAM,
 45:     );
 46: 
 47:     /**
 48:      * The types of tests allowed (implemented) for this driver.
 49:      *
 50:      * @var array
 51:      */
 52:     protected $_types = array(
 53:         Ingo_Storage::TYPE_HEADER,
 54:     );
 55: 
 56:     /**
 57:      * The list of tests allowed (implemented) for this driver.
 58:      *
 59:      * @var array
 60:      */
 61:     protected $_tests = array(
 62:         'contains', 'not contain',
 63:         'is', 'not is',
 64:         'begins with','not begins with',
 65:         'ends with', 'not ends with',
 66:         'regex',
 67:         'matches', 'not matches',
 68:         'exists', 'not exist',
 69:         'less than', 'less than or equal to',
 70:         'equal', 'not equal',
 71:         'greater than', 'greater than or equal to',
 72:     );
 73: 
 74:     /**
 75:      * Can tests be case sensitive?
 76:      *
 77:      * @var boolean
 78:      */
 79:     protected $_casesensitive = true;
 80: 
 81:     /**
 82:      * Does the driver support the stop-script option?
 83:      *
 84:      * @var boolean
 85:      */
 86:     protected $_supportStopScript = false;
 87: 
 88:     /**
 89:      * Does the driver require a script file to be generated?
 90:      *
 91:      * @var boolean
 92:      */
 93:     protected $_scriptfile = true;
 94: 
 95:     /**
 96:      * The recipes that make up the code.
 97:      *
 98:      * @var array
 99:      */
100:     protected $_recipes = array();
101: 
102:     /**
103:      * The vacation reason to be saved in vacation.msg.
104:      *
105:      * @var string
106:      */
107:     protected $_reason;
108: 
109:     /**
110:      * Returns a script previously generated with generate().
111:      *
112:      * @return string  The maildrop script.
113:      */
114:     public function toCode()
115:     {
116:         $code = '';
117:         foreach ($this->_recipes as $item) {
118:             $code .= $item->generate() . "\n";
119:         }
120:         return rtrim($code);
121:     }
122: 
123:     /**
124:      * Generates the maildrop script to do the filtering specified in
125:      * the rules.
126:      *
127:      * @return string  The maildrop script.
128:      */
129:     public function generate()
130:     {
131:         $filters = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_FILTERS);
132: 
133:         $this->addItem(new Ingo_Script_Maildrop_Comment(_("maildrop script generated by Ingo") . ' (' . date('F j, Y, g:i a') . ')'));
134: 
135:         /* Add variable information, if present. */
136:         if (!empty($this->_params['variables']) &&
137:             is_array($this->_params['variables'])) {
138:             foreach ($this->_params['variables'] as $key => $val) {
139:                 $this->addItem(new Ingo_Script_Maildrop_Variable(array('name' => $key, 'value' => $val)));
140:             }
141:         }
142: 
143:         foreach ($filters->getFilterList() as $filter) {
144:             switch ($filter['action']) {
145:             case Ingo_Storage::ACTION_BLACKLIST:
146:                 $this->generateBlacklist(!empty($filter['disable']));
147:                 break;
148: 
149:             case Ingo_Storage::ACTION_WHITELIST:
150:                 $this->generateWhitelist(!empty($filter['disable']));
151:                 break;
152: 
153:             case Ingo_Storage::ACTION_FORWARD:
154:                 $this->generateForward(!empty($filter['disable']));
155:                 break;
156: 
157:             case Ingo_Storage::ACTION_VACATION:
158:                 $this->generateVacation(!empty($filter['disable']));
159:                 break;
160: 
161:             case Ingo_Storage::ACTION_SPAM:
162:                 $this->generateSpamfilter(!empty($filter['disable']));
163:                 break;
164: 
165:             default:
166:                 if (in_array($filter['action'], $this->_actions)) {
167:                     /* Create filter if using AND. */
168:                     $recipe = new Ingo_Script_Maildrop_Recipe($filter, $this->_params);
169:                     foreach ($filter['conditions'] as $condition) {
170:                         $recipe->addCondition($condition);
171:                     }
172:                     $this->addItem(new Ingo_Script_Maildrop_Comment($filter['name'], !empty($filter['disable']), true));
173:                     $this->addItem($recipe);
174:                 }
175:             }
176:         }
177: 
178:         return $this->toCode();
179:     }
180: 
181:     /**
182:      * Generates the maildrop script to handle the blacklist specified in
183:      * the rules.
184:      *
185:      * @param boolean $disable  Disable the blacklist?
186:      */
187:     public function generateBlacklist($disable = false)
188:     {
189:         $blacklist = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_BLACKLIST);
190:         $bl_addr = $blacklist->getBlacklist();
191:         $bl_folder = $blacklist->getBlacklistFolder();
192: 
193:         $bl_type = empty($bl_folder)
194:             ? Ingo_Storage::ACTION_DISCARD
195:             : Ingo_Storage::ACTION_MOVE;
196: 
197:         if (!empty($bl_addr)) {
198:             $this->addItem(new Ingo_Script_Maildrop_Comment(_("Blacklisted Addresses"), $disable, true));
199:             $params = array('action-value' => $bl_folder,
200:                             'action' => $bl_type,
201:                             'disable' => $disable);
202: 
203:             foreach ($bl_addr as $address) {
204:                 if (!empty($address)) {
205:                     $recipe = new Ingo_Script_Maildrop_Recipe($params, $this->_params);
206:                     $recipe->addCondition(array('field' => 'From', 'value' => $address));
207:                     $this->addItem($recipe);
208:                 }
209:             }
210:         }
211:     }
212: 
213:     /**
214:      * Generates the maildrop script to handle the whitelist specified in
215:      * the rules.
216:      *
217:      * @param boolean $disable  Disable the whitelist?
218:      */
219:     public function generateWhitelist($disable = false)
220:     {
221:         $whitelist = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_WHITELIST);
222:         $wl_addr = $whitelist->getWhitelist();
223: 
224:         if (!empty($wl_addr)) {
225:             $this->addItem(new Ingo_Script_Maildrop_Comment(_("Whitelisted Addresses"), $disable, true));
226:             foreach ($wl_addr as $address) {
227:                 if (!empty($address)) {
228:                     $recipe = new Ingo_Script_Maildrop_Recipe(array('action' => Ingo_Storage::ACTION_KEEP, 'disable' => $disable), $this->_params);
229:                     $recipe->addCondition(array('field' => 'From', 'value' => $address));
230:                     $this->addItem($recipe);
231:                 }
232:             }
233:         }
234:     }
235: 
236:     /**
237:      * Generates the maildrop script to handle mail forwards.
238:      *
239:      * @param boolean $disable  Disable forwarding?
240:      */
241:     public function generateForward($disable = false)
242:     {
243:         $forward = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_FORWARD);
244:         $addresses = $forward->getForwardAddresses();
245: 
246:         if (!empty($addresses)) {
247:             $this->addItem(new Ingo_Script_Maildrop_Comment(_("Forwards"), $disable, true));
248:             $params = array('action' => Ingo_Storage::ACTION_FORWARD,
249:                             'action-value' => $addresses,
250:                             'disable' => $disable);
251:             if ($forward->getForwardKeep()) {
252:                 $params['action'] = self::MAILDROP_STORAGE_ACTION_STOREANDFORWARD;
253:             }
254:             $recipe = new Ingo_Script_Maildrop_Recipe($params, $this->_params);
255:             $recipe->addCondition(array('field' => 'From', 'value' => ''));
256:             $this->addItem($recipe);
257:         }
258:     }
259: 
260:     /**
261:      * Generates the maildrop script to handle vacation messages.
262:      *
263:      * @param boolean $disable  Disable forwarding?
264:      */
265:     public function generateVacation($disable = false)
266:     {
267:         $vacation = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_VACATION);
268:         $addresses = $vacation->getVacationAddresses();
269:         $actionval = array('addresses' => $addresses,
270:                            'subject' => $vacation->getVacationSubject(),
271:                            'days' => $vacation->getVacationDays(),
272:                            'ignorelist' => $vacation->getVacationIgnorelist(),
273:                            'excludes' => $vacation->getVacationExcludes(),
274:                            'start' => $vacation->getVacationStart(),
275:                            'end' => $vacation->getVacationEnd());
276: 
277:         if (!empty($addresses)) {
278:             $this->addItem(new Ingo_Script_Maildrop_Comment(_("Vacation"), $disable, true));
279:             $params = array('action' => Ingo_Storage::ACTION_VACATION,
280:                             'action-value' => $actionval,
281:                             'disable' => $disable);
282:             $recipe = new Ingo_Script_Maildrop_Recipe($params, $this->_params);
283:             $this->addItem($recipe);
284:             $this->_reason = $vacation->getVacationReason();
285:         }
286:     }
287: 
288:     /**
289:      * Generates the maildrop script to handle spam as identified by
290:      * SpamAssassin.
291:      *
292:      * @param boolean $disable  Disable the spam-filter?
293:      */
294:     public function generateSpamfilter($disable = false)
295:     {
296:         $spam = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_SPAM);
297:         if ($spam == false) {
298:             return;
299:         }
300: 
301:         $spam_folder = $spam->getSpamFolder();
302:         $spam_action = (empty($spam_folder)) ? Ingo_Storage::ACTION_DISCARD : Ingo_Storage::ACTION_MOVE;
303: 
304:         $this->addItem(new Ingo_Script_Maildrop_Comment(_("Spam Filter"), $disable, true));
305: 
306:         $params = array('action-value' => $spam_folder,
307:                         'action' => $spam_action,
308:                         'disable' => $disable);
309:         $recipe = new Ingo_Script_Maildrop_Recipe($params, $this->_params);
310:         if ($this->_params['spam_compare'] == 'numeric') {
311:             $recipe->addCondition(array('match' => 'greater than or equal to',
312:                                         'field' => $this->_params['spam_header'],
313:                                         'value' => $spam->getSpamLevel()));
314:         } elseif ($this->_params['spam_compare'] == 'string') {
315:             $recipe->addCondition(array('match' => 'contains',
316:                                         'field' => $this->_params['spam_header'],
317:                                         'value' => str_repeat($this->_params['spam_char'], $spam->getSpamLevel())));
318:         }
319: 
320:         $this->addItem($recipe);
321:     }
322: 
323:     /**
324:      * Returns any additional scripts that need to be sent to the transport
325:      * layer.
326:      *
327:      * @return array  A list of scripts with script names as keys and script
328:      *                code as values.
329:      */
330:     public function additionalScripts()
331:     {
332:         return array('vacation.msg' => $this->_reason);
333:     }
334: 
335:     /**
336:      * Adds an item to the recipe list.
337:      *
338:      * @param object $item  The item to add to the recipe list.
339:      *                      The object should have a generate() function.
340:      */
341:     public function addItem($item)
342:     {
343:         $this->_recipes[] = $item;
344:     }
345: 
346: }
347: 
API documentation generated by ApiGen