A more elaborate example of vacation script


1 <?php
2 /*
3
4 $Id: vacationset-sieve.php,v 1.1 2003/11/06 11:25:30 avel Exp $
5
6 vacationset-sieve.phtml is a PHP script that sets up a vacation message
7 via Cyrus sieve
8
9 Note: This script was written as part of work done for the Cooperative
10 Housing Federation of Canada <http://www.chfc.ca/> where I help manage
11 email and other related LAN services.
12
13 Copyright (C) 2003 Russell McOrmond <http://www.flora.ca/russell/>
14
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 http://www.gnu.org/copyleft/gpl.html
26
27
28 ---cut---
29
30 This script uses a library from
31 http://sourceforge.net/projects/sieve-php/
32
33 And interacts with the sieve server part of Cyrus IMAPD
34 http://asg.web.cmu.edu/cyrus/imapd/
35
36 */
37
38 // sieve-php library mentioned above
39 include "./sieve-php.lib";
40
41 // Domain this script is being used on.
42 $domain = "chfc.ca";
43 $sieveserver = "localhost";
44 $sieveport = 2000;
45
46 // Directory writeable by HTTP server, stores vacation messages/etc.
47 $tempdir = "temp-sieve/";
48
49 function authenticate() {
50 Header("WWW-Authenticate: Basic realm=\"Email Username/password\"");
51 Header("HTTP/1.0 401 Unauthorized");
52 echo "You didn't authenticate - sorry\n";
53 exit;
54 }
55
56 function setvacation() {
57 global $userid $password $message $aliases $sieve
58
59 $alias = preg_split ("/[\s,]+/", $aliases);
60
61 $scriptaliases="";
62 foreach ($alias as $result) {
63 if ($result == "") continue;
64 if ($scriptaliases != "") $scriptaliases .=", ";
65 $scriptaliases .= "\"$result\"";
66 }
67 if ($scriptaliases != "")
68 $scriptalias = ":addresses [$scriptaliases]";
69 $vacationscript="# Set by vacationset-sieve.phtml
70 require \"vacation\";
71 vacation $scriptalias
72 \"" . addslashes($message) . "\";";
73
74 // echo "<PRE>$vacationscript</PRE>";
75
76 return $sieve->sieve_sendscript("vacationset",$vacationscript);
77 }
78
79 function read_aliases() {
80 global $aliases $userid $tempdir
81
82 $aliases="";
83
84 $filename = "$tempdir/$userid.vacation.aliases";
85 if ($fd = @fopen ($filename, "r")) {
86 while ($fd && !feof ($fd)) {
87 $aliases .= fgets($fd, 4096);
88 }
89 fclose ($fd);
90 }
91 cleanup_aliases();
92 }
93
94 function cleanup_aliases() {
95 global $aliases $userid $emailaddr
96
97 if ($aliases == "") {
98 $aliases="$emailaddr\n";
99 } else if (preg_match ("/\S/",$aliases)) {
100 $alias = preg_split ("/[\s,]+/", $aliases);
101 reset ($alias);
102 $aliases="$emailaddr\n";
103 while (list(, $email) = each ($alias)) {
104 // Ignore main email address if it is already in the list.
105 if($email == $emailaddr) continue;
106 // Ignore lines that don't look like Email addresses
107 if(eregi(
108 "^[a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$",$email))
109 $aliases .= "$email\n";
110 }
111 }
112 }
113
114 function save_aliases() {
115 global $aliases $userid $tempdir
116
117 cleanup_aliases();
118 $filename = "$tempdir/$userid.vacation.aliases";
119 if ($fd = @fopen ($filename, "w")) {
120 @fwrite($fd,$aliases);
121 fclose($fd);
122 }
123 }
124
125 function html_head() {
126 ?>
127
128 <HTML>
129 <HEAD>
130 <TITLE>CHF Canada: auto-responder for e-mail</TITLE>
131 </HEAD>
132 <BODY BGCOLOR="#FFFFFF">
133 <H1>CHF Canada e-mail auto-responder</H1>
134
135 <B>Note:</B><BR>
136 This is the new vacation system that is part of the new email server
137 software. Your old vacation messages may need to be reset.
138
139
140 <?php
141
142 }
143
144 function html_foot() {
145 ?>
146 </BODY>
147 </HTML>
148 <?php
149 }
150
151 function ask_active_script($scriptname) {
152 global $deactivatescript
153
154 html_head();
155 ?><H2>Other script is active</H2>
156
157 Another script named "<?php echo $scriptname;?>" is currently active.
158 This version of the vacation system does not modify existing scripts,
159 it just sets a new script. If you wish to use this vacation system you
160 must deactivate current script.
161
162 <P>
163 <A HREF="<?php echo $PHP_SELF . "?deactivatescript=yes"; ?>">Deactivate
164 script?</A>
165
166 <?php
167 html_foot();
168 exit;
169 }
170
171
172
173 // Start of Body of code...
174
175 // Make sure user is authenticated...
176 if(!isset($PHP_AUTH_USER)) {
177 authenticate();
178 }
179
180 $userid=$PHP_AUTH_USER;
181 $password=$PHP_AUTH_PW;
182 $emailaddr = "$userid@$domain";
183
184 // Connect to sieve server
185 $sieve=new sieve($sieveserver, $sieveport, $userid, $password);
186
187 // Right password for this user?
188 if(!$sieve->sieve_login()) {
189 authenticate();
190 }
191
192 // If the user requested, deactivate current script
193 if($deactivatescript) {
194 $sieve->sieve_setactivescript("");
195 }
196
197 // Verify that other script is not active
198 if($sieve->sieve_listscripts())
199 if(isset($sieve->response["ACTIVE"]))
200 if($sieve->response["ACTIVE"] != "vacationset")
201 ask_active_script($sieve->response["ACTIVE"]);
202
203 if(!isset($message)) {
204 read_aliases();
205 html_head();
206 ?>
207 <FORM METHOD="POST" ACTION="<?php echo $PHP_SELF;?>">
208
209 <OL TYPE="A">
210
211
212 <LI><H2>Enable/Disable</H2>
213
214 <blockquote>
215 <INPUT TYPE="radio" NAME="mode" VALUE="set" checked>
216 Enable auto-response message
217 <BR>
218 <INPUT TYPE="radio" NAME="mode" VALUE="unset">
219 Disable auto-response message
220 </blockquote>
221
222
223
224 <LI><H2>Message</H2>
225
226 <?php
227 $filename="$tempdir/$userid.vacation.msg";
228 if($fd = @fopen ($filename, "r")) {
229 $vacationmsg = fread ($fd, filesize ($filename));
230 fclose ($fd);
231 ?>
232
233 <P>
234 Here is your existing outgoing message.
235 <BR>
236 <TEXTAREA NAME="message" ROWS="12" COLS="70">
237 <?php echo htmlspecialchars($vacationmsg);?>
238 </TEXTAREA>
239 <?php
240
241 } else {
242 ?>
243
244 You had no outgoing message. Here is the default to start with.
245 <BR>
246 <TEXTAREA NAME="message" ROWS="12" COLS="70">
247 Hello,
248
249 I am gone for a while and will read your message upon my return.
250
251 </TEXTAREA>
252 <?php
253 }
254
255
256 ?>
257 <p>You should edit the body of the message to indicate when
258 you will return and perhaps pass on other instructions.
259
260 <P>The subject of your reply will be "Re " and whatever they put as the
261 subject.
262
263 <BR><HR><BR>
264
265 <INPUT TYPE="reset" VALUE="Reset">
266 If you have made any changes to the above text and wish to start over,
267 press reset to clear your changes.
268
269 <P>
270
271 <LI><H2>Aliases</H2>
272
273 <P> If you have other e-mail accounts that you have forwarded to your main
274 CHF address &lt;<?php echo $emailaddr; ?>&gt;, you may want to have the
275 auto-responder reply to messages addressed to those accounts as well.
276
277 <P>
278
279 Please add to the box below any addresses that are forwarded to this mailbox, for
280 which you wish the auto-responder program to also send replies.
281
282 <P>
283
284 <TEXTAREA NAME="aliases" ROWS="5" COLS="40">
285 <?php echo htmlspecialchars($aliases);?>
286 </TEXTAREA>
287
288
289 <P>
290 <LI><H2>Finished</H2>
291
292
293 When you are satisfied with the above choices, select Submit.
294
295 <P>
296 <INPUT TYPE="submit" VALUE="Submit">
297
298 </OL>
299
300 </FORM>
301
302
303 <?php
304 // $message was set
305 } else {
306 save_aliases();
307
308 $filename="$tempdir/$userid.vacation.msg";
309 if($fd = @fopen ($filename, "w")) {
310 @fwrite($fd,stripslashes(chop($message))."\n");
311 fclose($fd);
312 }
313
314 html_head();
315
316 if (setvacation()) {
317 if ($mode == "set") $sieve->sieve_setactivescript("vacationset");
318 else $sieve->sieve_setactivescript("");
319 ?>
320 <P>
321 <H2>Your choices have been saved.</H2>
322 <?php
323
324 } else {
325
326 ?>
327 <P>
328 <H2>Error setting vacation.</H2>
329
330 <?php
331 echo "Error: " . $sieve->error . "<BR>\n";
332 foreach($sieve->error_raw as $errorline)
333 print "errorline: $errorline<br>";
334
335 }
336
337 ?>
338 <P>
339 <A HREF="/">Back to server homepage</A><BR>
340 <A HREF="<?php echo $PHP_SELF;?>">Look at your
341 auto-responder settings again</A>
342
343 <?php
344 }
345
346 html_foot();
347 ?>

Documentation generated on Thu, 18 Dec 2003 15:18:54 +0200 by phpDocumentor 1.2.3