1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Ingo_Script_Procmail extends Ingo_Script
15: {
16: 17: 18: 19: 20:
21: protected $_actions = array(
22: Ingo_Storage::ACTION_KEEP,
23: Ingo_Storage::ACTION_MOVE,
24: Ingo_Storage::ACTION_DISCARD,
25: Ingo_Storage::ACTION_REDIRECT,
26: Ingo_Storage::ACTION_REDIRECTKEEP,
27: Ingo_Storage::ACTION_REJECT
28: );
29:
30: 31: 32: 33: 34:
35: protected $_categories = array(
36: Ingo_Storage::ACTION_BLACKLIST,
37: Ingo_Storage::ACTION_WHITELIST,
38: Ingo_Storage::ACTION_VACATION,
39: Ingo_Storage::ACTION_FORWARD
40: );
41:
42: 43: 44: 45: 46:
47: protected $_types = array(
48: Ingo_Storage::TYPE_HEADER,
49: Ingo_Storage::TYPE_BODY
50: );
51:
52: 53: 54: 55: 56:
57: protected $_special_types = array(
58: 'Destination',
59: );
60:
61: 62: 63: 64: 65:
66: protected $_tests = array(
67: 'contains',
68: 'not contain',
69: 'begins with',
70: 'not begins with',
71: 'ends with',
72: 'not ends with',
73: 'regex'
74: );
75:
76: 77: 78: 79: 80:
81: protected $_casesensitive = true;
82:
83: 84: 85: 86: 87:
88: protected $_supportStopScript = true;
89:
90: 91: 92: 93: 94:
95: protected $_scriptfile = true;
96:
97: 98: 99: 100: 101:
102: protected $_recipes = array();
103:
104: 105: 106: 107: 108:
109: public function __construct($params = array())
110: {
111: parent::__construct($params);
112:
113:
114:
115: if ($this->_params['path_style'] == 'maildir') {
116: if (!isset($this->_params['variables']['DEFAULT'])) {
117: $this->_params['variables']['DEFAULT'] = '$HOME/Maildir/';
118: }
119: if (!isset($this->_params['variables']['MAILDIR'])) {
120: $this->_params['variables']['MAILDIR'] = '$HOME/Maildir';
121: }
122: }
123: }
124:
125: 126: 127: 128: 129:
130: public function toCode()
131: {
132: $code = '';
133: foreach ($this->_recipes as $item) {
134: $code .= $item->generate() . "\n";
135: }
136:
137:
138:
139: if (isset($this->_params['delivery_agent'])) {
140: $code .= ":0 w\n";
141: $code .= isset($this->_params['delivery_mailbox_prefix']) ?
142: '| ' . $this->_params['delivery_agent'] . ' ' . $this->_params['delivery_mailbox_prefix'] . '$DEFAULT' :
143: '| ' . $this->_params['delivery_agent'] . ' $DEFAULT';
144: }
145:
146: return rtrim($code) . "\n";
147: }
148:
149: 150: 151: 152: 153: 154:
155: public function generate()
156: {
157: $filters = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_FILTERS);
158:
159: $this->addItem(new Ingo_Script_Procmail_Comment(_("procmail script generated by Ingo") . ' (' . date('F j, Y, g:i a') . ')'));
160:
161:
162: if (!empty($this->_params['variables']) &&
163: is_array($this->_params['variables'])) {
164: foreach ($this->_params['variables'] as $key => $val) {
165: $this->addItem(new Ingo_Script_Procmail_Variable(array('name' => $key, 'value' => $val)));
166: }
167: }
168:
169: foreach ($filters->getFilterList() as $filter) {
170: switch ($filter['action']) {
171: case Ingo_Storage::ACTION_BLACKLIST:
172: $this->generateBlacklist(!empty($filter['disable']));
173: break;
174:
175: case Ingo_Storage::ACTION_WHITELIST:
176: $this->generateWhitelist(!empty($filter['disable']));
177: break;
178:
179: case Ingo_Storage::ACTION_VACATION:
180: $this->generateVacation(!empty($filter['disable']));
181: break;
182:
183: case Ingo_Storage::ACTION_FORWARD:
184: $this->generateForward(!empty($filter['disable']));
185: break;
186:
187: default:
188: if (in_array($filter['action'], $this->_actions)) {
189:
190: if ($filter['combine'] == Ingo_Storage::COMBINE_ALL) {
191: $recipe = new Ingo_Script_Procmail_Recipe($filter, $this->_params);
192: if (!$filter['stop']) {
193: $recipe->addFlag('c');
194: }
195: foreach ($filter['conditions'] as $condition) {
196: $recipe->addCondition($condition);
197: }
198: $this->addItem(new Ingo_Script_Procmail_Comment($filter['name'], !empty($filter['disable']), true));
199: $this->addItem($recipe);
200: } else {
201:
202: $this->addItem(new Ingo_Script_Procmail_Comment($filter['name'], !empty($filter['disable']), true));
203: $loop = 0;
204: foreach ($filter['conditions'] as $condition) {
205: $recipe = new Ingo_Script_Procmail_Recipe($filter, $this->_params);
206: if ($loop++) {
207: $recipe->addFlag('E');
208: }
209: if (!$filter['stop']) {
210: $recipe->addFlag('c');
211: }
212: $recipe->addCondition($condition);
213: $this->addItem($recipe);
214: }
215: }
216: }
217: }
218: }
219:
220: return $this->toCode();
221: }
222:
223: 224: 225: 226: 227: 228:
229: public function generateBlacklist($disable = false)
230: {
231: $blacklist = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_BLACKLIST);
232: $bl_addr = $blacklist->getBlacklist();
233: $bl_folder = $blacklist->getBlacklistFolder();
234:
235: $bl_type = empty($bl_folder)
236: ? Ingo_Storage::ACTION_DISCARD
237: : Ingo_Storage::ACTION_MOVE;
238:
239: if (!empty($bl_addr)) {
240: $this->addItem(new Ingo_Script_Procmail_Comment(_("Blacklisted Addresses"), $disable, true));
241: $params = array('action-value' => $bl_folder,
242: 'action' => $bl_type,
243: 'disable' => $disable);
244:
245: foreach ($bl_addr as $address) {
246: if (!empty($address)) {
247: $recipe = new Ingo_Script_Procmail_Recipe($params, $this->_params);
248: $recipe->addCondition(array('field' => 'From', 'value' => $address, 'match' => 'address'));
249: $this->addItem($recipe);
250: }
251: }
252: }
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function generateWhitelist($disable = false)
262: {
263: $whitelist = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_WHITELIST);
264: $wl_addr = $whitelist->getWhitelist();
265:
266: if (!empty($wl_addr)) {
267: $this->addItem(new Ingo_Script_Procmail_Comment(_("Whitelisted Addresses"), $disable, true));
268: foreach ($wl_addr as $address) {
269: if (!empty($address)) {
270: $recipe = new Ingo_Script_Procmail_Recipe(array('action' => Ingo_Storage::ACTION_KEEP, 'disable' => $disable), $this->_params);
271: $recipe->addCondition(array('field' => 'From', 'value' => $address, 'match' => 'address'));
272: $this->addItem($recipe);
273: }
274: }
275: }
276: }
277:
278: 279: 280: 281: 282:
283: public function generateVacation($disable = false)
284: {
285: $vacation = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_VACATION);
286: $addresses = $vacation->getVacationAddresses();
287: $actionval = array(
288: 'addresses' => $addresses,
289: 'subject' => $vacation->getVacationSubject(),
290: 'days' => $vacation->getVacationDays(),
291: 'reason' => $vacation->getVacationReason(),
292: 'ignorelist' => $vacation->getVacationIgnorelist(),
293: 'excludes' => $vacation->getVacationExcludes(),
294: 'start' => $vacation->getVacationStart(),
295: 'end' => $vacation->getVacationEnd(),
296: );
297:
298: if (!empty($addresses)) {
299: $this->addItem(new Ingo_Script_Procmail_Comment(_("Vacation"), $disable, true));
300: $params = array('action' => Ingo_Storage::ACTION_VACATION,
301: 'action-value' => $actionval,
302: 'disable' => $disable);
303: $recipe = new Ingo_Script_Procmail_Recipe($params, $this->_params);
304: $this->addItem($recipe);
305: }
306: }
307:
308: 309: 310: 311: 312:
313: public function generateForward($disable = false)
314: {
315: $forward = $GLOBALS['ingo_storage']->retrieve(Ingo_Storage::ACTION_FORWARD);
316: $addresses = $forward->getForwardAddresses();
317:
318: if (!empty($addresses)) {
319: $this->addItem(new Ingo_Script_Procmail_Comment(_("Forwards"), $disable, true));
320: $params = array('action' => Ingo_Storage::ACTION_FORWARD,
321: 'action-value' => $addresses,
322: 'disable' => $disable);
323: $recipe = new Ingo_Script_Procmail_Recipe($params, $this->_params);
324: if ($forward->getForwardKeep()) {
325: $recipe->addFlag('c');
326: }
327: $this->addItem($recipe);
328: }
329: }
330:
331: 332: 333: 334: 335: 336: 337:
338: public function additionalScripts()
339: {
340: if (isset($this->_params['forward_file']) &&
341: isset($this->_params['forward_string'])) {
342: return array($this->_params['forward_file'] => $this->_params['forward_string']);
343: }
344: return array();
345: }
346:
347: 348: 349: 350: 351: 352:
353: public function addItem($item)
354: {
355: $this->_recipes[] = $item;
356: }
357:
358: }
359: