1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11: class Ingo_Script_Sieve_Action_Vacation extends Ingo_Script_Sieve_Action
12: {
13: 14: 15: 16: 17:
18: public function __construct($vars = array())
19: {
20: $this->_vars = array_merge(array(
21: 'days' => '',
22: 'addresses' => '',
23: 'subject' => '',
24: 'reason' => '',
25: 'start' => '',
26: 'start_year' => '',
27: 'start_month' => '',
28: 'start_day' => '',
29: 'end' => '',
30: 'end_year' => '',
31: 'end_month' => '',
32: 'end_day' => ''
33: ), $vars);
34: }
35:
36: 37: 38: 39: 40:
41: public function toCode()
42: {
43: $start_year = $this->_vars['start_year'];
44: $start_month = $this->_vars['start_month'];
45: $start_day = $this->_vars['start_day'];
46:
47: $end_year = $this->_vars['end_year'];
48: $end_month = $this->_vars['end_month'];
49: $end_day = $this->_vars['end_day'];
50:
51: $code = '';
52:
53: if (empty($this->_vars['start']) || empty($this->_vars['end'])) {
54: return $this->_vacationCode();
55: } elseif ($end_year > $start_year + 1) {
56: $code .= $this->_yearCheck($start_year + 1, $end_year - 1)
57: . $this->_vacationCode()
58: . "\n}\n"
59: . $this->_yearCheck($start_year, $start_year);
60: if ($start_month < 12) {
61: $code .= $this->_monthCheck($start_month + 1, 12)
62: . $this->_vacationCode()
63: . "\n}\n";
64: }
65: $code .= $this->_monthCheck($start_month, $start_month)
66: . $this->_dayCheck($start_day, 31)
67: . $this->_vacationCode()
68: . "\n}\n}\n}\n"
69: . $this->_yearCheck($end_year, $end_year);
70: if ($end_month > 1) {
71: $code .= $this->_monthCheck(1, $end_month - 1)
72: . $this->_vacationCode()
73: . "\n}\n";
74: }
75: $code .= $this->_monthCheck($end_month, $end_month)
76: . $this->_dayCheck(1, $end_day)
77: . $this->_vacationCode()
78: . "\n}\n}\n}\n";
79: } elseif ($end_year == $start_year + 1) {
80: $code .= $this->_yearCheck($start_year, $start_year);
81: if ($start_month < 12) {
82: $code .= $this->_monthCheck($start_month + 1, 12)
83: . $this->_vacationCode()
84: . "\n}\n";
85: }
86: $code .= $this->_monthCheck($start_month, $start_month)
87: . $this->_dayCheck($start_day, 31)
88: . $this->_vacationCode()
89: . "\n}\n}\n}\n"
90: . $this->_yearCheck($end_year, $end_year);
91: if ($end_month > 1) {
92: $code .= $this->_monthCheck(1, $end_month - 1)
93: . $this->_vacationCode()
94: . "\n}\n";
95: }
96: $code .= $this->_monthCheck($end_month, $end_month)
97: . $this->_dayCheck(1, $end_day)
98: . $this->_vacationCode()
99: . "\n}\n}\n}\n";
100: } elseif ($end_year == $start_year) {
101: $code .= $this->_yearCheck($start_year, $start_year);
102: if ($end_month > $start_month) {
103: if ($end_month > $start_month + 1) {
104: $code .= $this->_monthCheck($start_month + 1, $end_month - 1)
105: . $this->_vacationCode()
106: . "\n}\n";
107: }
108: $code .= $this->_monthCheck($start_month, $start_month)
109: . $this->_dayCheck($start_day, 31)
110: . $this->_vacationCode()
111: . "\n}\n}\n"
112: . $this->_monthCheck($end_month, $end_month)
113: . $this->_dayCheck(1, $end_day)
114: . $this->_vacationCode()
115: . "\n}\n}\n";
116: } elseif ($end_month == $start_month) {
117: $code .= $this->_monthCheck($start_month, $start_month)
118: . $this->_dayCheck($start_day, $end_day)
119: . $this->_vacationCode()
120: . "\n}\n}\n";
121: }
122: $code .= "}\n";
123: }
124:
125: return $code;
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function check()
135: {
136: return empty($this->_vars['reason'])
137: ? _("Missing reason in vacation.")
138: : true;
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function requires()
148: {
149: return array('vacation', 'regex');
150: }
151:
152: 153:
154: protected function _vacationCode()
155: {
156: $code = 'vacation :days ' . $this->_vars['days'] . ' ';
157: $addresses = $this->_vars['addresses'];
158: $stringlist = '';
159: if (count($addresses) > 1) {
160: foreach ($addresses as $address) {
161: $address = trim($address);
162: if (!empty($address)) {
163: $stringlist .= empty($stringlist) ? '"' : ', "';
164: $stringlist .= Ingo_Script_Sieve::escapeString($address) . '"';
165: }
166: }
167: $stringlist = "[" . $stringlist . "] ";
168: } elseif (count($addresses) == 1) {
169: $stringlist = '"' . Ingo_Script_Sieve::escapeString($addresses[0]) . '" ';
170: }
171:
172: if (!empty($stringlist)) {
173: $code .= ':addresses ' . $stringlist;
174: }
175:
176: if (!empty($this->_vars['subject'])) {
177: $code .= ':subject "' . Horde_Mime::encode(Ingo_Script_Sieve::escapeString($this->_vars['subject']), 'UTF-8') . '" ';
178: }
179: return $code
180: . '"' . Ingo_Script_Sieve::escapeString($this->_vars['reason'])
181: . '";';
182: }
183:
184: 185:
186: protected function _yearCheck($begin, $end)
187: {
188: $code = 'if header :regex "Received" "^.*(' . $begin;
189: for ($i = $begin + 1; $i <= $end; $i++) {
190: $code .= '|' . $i;
191: }
192: return $code
193: . ') (\\\\(.*\\\\) )?..:..:.. (\\\\(.*\\\\) )?((\\\\+|\\\\-)[[:digit:]]{4}|.{1,5})( \\\\(.*\\\\))?$" {'
194: . "\n ";
195: }
196:
197: 198:
199: protected function _monthCheck($begin, $end)
200: {
201: $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
202: 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
203: $code = 'if header :regex "Received" "^.*(' . $months[$begin - 1];
204: for ($i = $begin + 1; $i <= $end; $i++) {
205: $code .= '|' . $months[$i - 1];
206: }
207: return $code
208: . ') (\\\\(.*\\\\) )?.... (\\\\(.*\\\\) )?..:..:.. (\\\\(.*\\\\) )?((\\\\+|\\\\-)[[:digit:]]{4}|.{1,5})( \\\\(.*\\\\))?$" {'
209: . "\n ";
210: }
211:
212: 213:
214: protected function _dayCheck($begin, $end)
215: {
216: $code = 'if header :regex "Received" "^.*(' . str_repeat('[0 ]', 2 - strlen($begin)) . $begin;
217: for ($i = $begin + 1; $i <= $end; $i++) {
218: $code .= '|' . str_repeat('[0 ]', 2 - strlen($i)) . $i;
219: }
220: return $code
221: . ') (\\\\(.*\\\\) )?... (\\\\(.*\\\\) )?.... (\\\\(.*\\\\) )?..:..:.. (\\\\(.*\\\\) )?((\\\\+|\\\\-)[[:digit:]]{4}|.{1,5})( \\\\(.*\\\\))?$" {'
222: . "\n ";
223: }
224:
225: }
226: