File: mailform.pike

package info (click to toggle)
libroxen-mailform 0.1-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36 kB
  • sloc: makefile: 36
file content (304 lines) | stat: -rw-r--r-- 10,083 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

/////////////////////////////////////////////////////////////////
//                                                             //
//  MAILFORM tag v 0.2                                         //
//  Form to Mail roxen module. You can dump Forms to Mail      //
//  David Farre polak@polakilandia.org                         //
//  License GPL                                                //
//                                                             //
/////////////////////////////////////////////////////////////////



#include <config.h>
#include <module.h>
inherit "module";
inherit "roxenlib";

constant cvs_version = "$Id: mailform.pike,v 0.1 dff Exp $";

#ifdef TAGDOCUMENTATION
TAGDOCUMENTATION;
#ifdef manual
    constant tagdoc=([
    "mailform":#"<desc cont>Form to Mail module. You can dump Forms to Mail.</desc>

<attr name=help >
 Show this help.
</attr>

<attr name=user value=string>
 destination email address.
</attr>

<attr name=return value=url>
 Url that this mails show after sending mail.
</attr>

<attr name=required value=string>
 Comma list separed string with the requireds fields.
</attr>

<attr name=sender value=string>
 Email address for from & return-path mail.
</attr>

<attr name=sender-var value=string>
 Name of any field contained in the form.It value is used for from & return-path mail.
</attr>


         "]);
#endif
#endif


mixed js_error(string pcamp,string error)
{
    string out="";
    error=replace(error,({"%FIELD%"}),({pcamp}));
    out+="<html>\n";
    out+="<script language='JavaScript'>\n";
    out+="function err(){\n";
    out+="\talert(\""+error+"\");\n";
    out+="\thistory.go(-1);\n";
    out+="}\n";
    out+="</script>\n";
    out+="<body bgcolor='white' onload='err();'>\n";
    out+="</body>\n";
    out+="</html>\n";
 
    return http_string_answer(out,"text/html");
}



void start(int level, Configuration _conf)
{
}

#if __ROXEN_VERSION__ < 2.0
array register_module()
{ return ({ MODULE_LOCATION | MODULE_PARSER,
       "MAILFORM Tag",
       ( "Form to Mail module. You can dump Forms to Mail."),
       0,
       1 });
}
#else

constant module_type = MODULE_LOCATION | MODULE_PARSER;
constant module_name = "MAILFORM Tag";
constant module_doc  = "Form to Mail module. You can dump Forms to Mail.";

#endif

void create()
{
	defvar("mountpoint", "/internal_mailform", "Form ACTION / MOUNTPOINT",
		TYPE_LOCATION, "Destination location for GET/POST." );
	defvar("method", "POST", "Form METHOD", 
		TYPE_STRING_LIST, "HTTP FORM method.",
		( {"POST", "GET"} ) );
	defvar("Vuser", "__user", "Destination Email Address",
		TYPE_STRING, "Variable name for de email address destination." );
	defvar("Sender", "nobody@localhost", "From Email Address.",
		TYPE_STRING, "From mail address. This can be overwritten if tag has var-sender or sender attributes" );
	defvar("Vpage", "page", "Referer URL" , 
		TYPE_STRING, "Variable name for current page URL." );
	defvar("VretURL", "retURL", "Return URL",
		TYPE_STRING, "Variable name for thanx page." );
	defvar("VSender", "__sender", "Sender mail",
		TYPE_STRING, "Variable name for sender mail." );
	defvar("VVar_Sender", "__var_sender", "Sender mail",
		TYPE_STRING, "Variable name for form variable that contains sender mail." );
	defvar("Vorder", "internal_order", "Order of variables",
               TYPE_STRING, "Variable name for variables order." );
	defvar("VSenderProg", "/usr/lib/sendmail -i -t -fnobody@localhost", "MTA command line",
               TYPE_STRING, "The command line for sending mails." );

        defvar("VUserInfo", 1, "Format:Include User Info",
		TYPE_FLAG, "Include in thee mail the user host, address and agent." );
        defvar("Vemptyfield", "The required field %FIELD% is empty.", "Format:Error empty field",
		TYPE_STRING, "The error in javascript alert that shows when a required field is empty. %FIELD% will be the empty field name's." );
        defvar("VBadMail", "The email '%FIELD%' is a invalid email.", "Format:Error bad email",
		TYPE_STRING, "The error in javascript alert that shows when a email field contains a invalid email. %FIELD% will be the invalid email." );
	defvar("VFormatVar", "%VARIABLE% = %VALUE%\n", "Format:Format of mail lines",
		TYPE_STRING, "The format of the lines that produce this module." );
        defvar("VHeaderMail",  "= [User] ==================================", "Format:Format of mail header ",
		TYPE_TEXT_FIELD, "The format of the mail header." );
	defvar("VSepOrd",   "= [Required Vars] =========================", "Format:Format of ordered vars header",
		TYPE_STRING, "The format of the header in the ordered vars section." );
	defvar("VSepOther", "= [Other Vars] ============================", "Format:Format of other vars header",
		TYPE_STRING, "The format of the header in the other vars section." );
        defvar("VEndMail", "= [End] ===================================", "Format:Format of end of mail",
		TYPE_TEXT_FIELD, "The format of end of mail." );
        defvar("VSubject", "FORM from %PAGE%", "Format:Format of subject",
		TYPE_STRING, "The format of subject mail's. %PAGE% will be replaced by referer page." );

}



string query_location() { return query("mountpoint"); }

string mailtime()
{
  int t = time();
  mapping lt = localtime(t);
  string wday, month;
  if (sscanf(ctime(t), "%s %s %*s", wday, month) < 2)
    return "";
  return sprintf("%s, %02d %s %d %02d:%02d:%02d %s%04d",
		 wday, lt->mday, month, lt->year+1900, lt->hour, lt->min,
		 lt->sec, -lt->timezone >=0 ? "+":"", -lt->timezone*100/3600);
}

mapping find_file( string f, object id ){
    string out="";
    string texte="";
    string sender="";
    array vars=indices(id->variables);
    vars-=({query("Vuser"),query("Vorder"), query("VretURL"), query("Vpage"),query("VSender"),query("VVar_Sender")});
    if(id->variables[query("Vorder")] && sizeof(id->variables[query("Vorder")])){
        foreach((id->variables[query("Vorder")]/","),string s){
            s-=" ";
            vars-=({s});
            if(id->variables[s])
            {
                if(!sizeof(id->variables[s]))
                    return js_error(s,query("Vemptyfield"));
                out+=replace(query("VFormatVar"),({"%VARIABLE%","%VALUE%"}),({s,id->variables[s]}))+"\n";
            }
        }
    }
    out+="\n"+query("VSepOther")+"\n";
    foreach(sort(vars),string s)
        out+=replace(query("VFormatVar"),({"%VARIABLE%","%VALUE%"}),({s,id->variables[s]}))+"\n";
    texte+="\n"+query("VHeaderMail")+"\n";
    if(query("VUserInfo"))
    {
        texte+="ADDR={"+id->remoteaddr+"}\n";
        texte+="HOST={"+id->request_headers["host"]+"}\n";
        texte+="USER-AGENT={"+id->request_headers["user-agent"]+"}\n";
    }
    texte+="\n"+query("VSepOrd")+"\n";
    texte+=out;
    texte+="\n"+query("VEndMail")+"\n";


    object mail = MIME.Message("",
                               (["MIME-Version" : "1.0",
                                 "Content-Type" : "text/plain; charset=iso-8859-1",
                                 "X-Mailer" : "Form2Mail" ]) );

    mail->headers["to"]  = id->variables[query("Vuser")];


    if(sizeof(query("Sender")))
        sender=query("Sender");
    if(sizeof(query("VSender")) && id->variables[query("VSender")] && sizeof(id->variables[query("VSender")]))
        sender=id->variables[query("VSender")];
    if(sizeof(query("VVar_Sender")) && id->variables[query("VVar_Sender")] && sizeof(id->variables[query("VVar_Sender")]) && id->variables[id->variables[query("VVar_Sender")]] && sizeof(id->variables[id->variables[query("VVar_Sender")]]))
    {
        sender=id->variables[id->variables[query("VVar_Sender")]];
        if((sender-"@"==sender) || (sender-"."==sender))
            return js_error(sender,query("VBadMail"));
    }
    mail->headers["from"] =sender;
    mail->headers["Reply-to"] =sender;
    mail->headers["subject"] = replace(query("VSubject"),({"%PAGE%"}),({id->variables[query("Vpage")]}));
    mail->headers["date"] = mailtime();
    mail->setencoding("8bit");


    mail->setdata(texte);
    object(Stdio.File) in=Stdio.File("stdout");
    object out=in->pipe();
    Process.spawn(query("VSenderProg"),out,0,out);
    in->write((string)mail);
    in->close();


     return http_redirect( id->variables[query("VretURL")],id );
}

string simpletag_mailform( string tag, mapping m,string cont, object got)
{
	string username="";
	string pagename="";
        string retURL="";
        string order="";
        string sender="";
        string var_sender="";
	string tmp, out;

	foreach( indices(m), tmp )
	{
		switch(tmp)
		{
		case "user":
			username=m[tmp];
			break;
		case "return":
			retURL=m[tmp];
			break;
		case "required":
			order=m[tmp];
			break;
		case "sender":
			sender=m[tmp];
			break;
		case "sender-var":
			var_sender=m[tmp];
			break;
		}
	}


        pagename=got->conf->query("MyWorldLocation");
        sscanf(pagename,"http://%s/",pagename);
        pagename="http://"+pagename+got->not_query;
	if(!sizeof(username) )
	{
            return "<h1>Error:</h1><h3>The field user is required.</h3><br><br>";
	}

	if(!strlen(retURL))
	{
		retURL = got->not_query;
	}

	
	out =	"<FORM ACTION=" + query("mountpoint") +
		" METHOD=" + query("method") +">\n" +
		"<INPUT TYPE=HIDDEN NAME="+ query("Vuser") +
		" VALUE='" + username + "'>\n" ;
	
	if( sizeof(query("Vpage")) )
		out +=  "<INPUT TYPE=HIDDEN NAME="+ query("Vpage") +
			" VALUE='" + pagename + "'>\n";

	if( sizeof(query("VSender")) && sizeof(sender) )
		out +=  "<INPUT TYPE=HIDDEN NAME="+ query("VSender") +
			" VALUE='" + sender + "'>\n";

	if( sizeof(query("VVar_Sender")) && sizeof(var_sender) )
		out +=  "<INPUT TYPE=HIDDEN NAME="+ query("VVar_Sender") +
			" VALUE='" + var_sender + "'>\n";

	if( sizeof(query("Vorder")) )
		out +=  "<INPUT TYPE=HIDDEN NAME="+ query("Vorder") +
			" VALUE='" + order + "'>\n";

        if( sizeof(query("VretURL")) )
            out +=  "<INPUT TYPE=HIDDEN NAME="+ query("VretURL") +
			" VALUE='" + retURL + "'>\n";
	
	return(out+cont+"\n</FORM>\n");
}

string query_name()
{
	return(	"MAILFORM Tag" );
}