1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Ingo_Script_Maildrop extends Ingo_Script
14: {
15:
16: 17:
18: const MAILDROP_STORAGE_ACTION_STOREANDFORWARD = 100;
19:
20: 21: 22: 23: 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: 36: 37: 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: 49: 50: 51:
52: protected $_types = array(
53: Ingo_Storage::TYPE_HEADER,
54: );
55:
56: 57: 58: 59: 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: 76: 77: 78:
79: protected $_casesensitive = true;
80:
81: 82: 83: 84: 85:
86: protected $_supportStopScript = false;
87:
88: 89: 90: 91: 92:
93: protected $_scriptfile = true;
94:
95: 96: 97: 98: 99:
100: protected $_recipes = array();
101:
102: 103: 104: 105: 106:
107: protected $_reason;
108:
109: 110: 111: 112: 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: 125: 126: 127: 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:
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:
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: 183: 184: 185: 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: 215: 216: 217: 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: 238: 239: 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: 262: 263: 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: 290: 291: 292: 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: 325: 326: 327: 328: 329:
330: public function additionalScripts()
331: {
332: return array('vacation.msg' => $this->_reason);
333: }
334:
335: 336: 337: 338: 339: 340:
341: public function addItem($item)
342: {
343: $this->_recipes[] = $item;
344: }
345:
346: }
347: