2002-03-17 18:13:59 +01:00
#!/usr/bin/env python
import sys
import MySQLdb
2002-03-19 23:33:45 +01:00
import os
2002-03-17 18:13:59 +01:00
import cgi
import re
import string
import smtplib
from whrandom import choice
# specify the filename of the template file
2002-04-08 12:01:42 +02:00
scripturl = " http://eressea.upb.de/cgi-bin/eressea/eressea-register.py "
2002-03-17 18:13:59 +01:00
HTMLTemplate = " eressea.html "
MailTemplate = " register.mail "
DefaultTitle = " Eressea Anmeldung "
dbname = " eressea "
From = " accounts@eressea-pbem.de "
2002-03-19 23:33:45 +01:00
locale = " de "
2002-03-17 18:13:59 +01:00
smtpserver = ' localhost '
db = None
# define a new function called Display
# it takes one parameter - a string to Display
def Display ( Content , Title = DefaultTitle ) :
TemplateHandle = open ( HTMLTemplate , " r " ) # open in read only mode
# read the entire file as a string
TemplateInput = TemplateHandle . read ( )
TemplateHandle . close ( ) # close the file
# this defines an exception string in case our
# template file is messed up
BadTemplateException = " There was a problem with the HTML template. "
SubResult = re . subn ( " <!-- INSERT TITLE HERE --> " , Title , TemplateInput )
SubResult = re . subn ( " <!-- INSERT CONTENT HERE --> " , Content , SubResult [ 0 ] )
if SubResult [ 1 ] == 0 :
raise BadTemplateException
print " Content-Type: text/html \n \n "
print SubResult [ 0 ]
return
def Send ( email , custid , firstname , password , position ) :
2002-03-19 23:33:45 +01:00
TemplateHandle = open ( MailTemplate + " . " + locale , " r " ) # open in read only mode
2002-03-17 18:13:59 +01:00
# read the entire file as a string
TemplateInput = TemplateHandle . read ( )
TemplateHandle . close ( ) # close the file
SubResult = re . subn ( " <FIRSTNAME> " , firstname , TemplateInput )
SubResult = re . subn ( " <PASSWORD> " , password , SubResult [ 0 ] )
SubResult = re . subn ( " <POSITION> " , str ( int ( position ) ) , SubResult [ 0 ] )
SubResult = re . subn ( " <CUSTID> " , str ( int ( custid ) ) , SubResult [ 0 ] )
2002-04-03 00:09:40 +02:00
subject = { " de " : " Eressea Anmeldung " , " en " : " Eressea Registration " }
Msg = " From: " + From + " \n To: " + email + " \n Subject: " + subject [ locale ] + " \n \n "
2002-03-17 18:13:59 +01:00
Msg = Msg + SubResult [ 0 ]
server = smtplib . SMTP ( smtpserver )
server . sendmail ( From , email , Msg )
server . close ( )
return
def GetKey ( Form , key ) :
if Form . has_key ( key ) :
value = Form [ key ] . value
if value != " " :
return value
return None
2002-03-19 23:33:45 +01:00
def ValidEmail ( email ) :
if string . find ( email , " @ " ) == - 1 :
return 0
elif string . find ( email , " " ) != - 1 :
return 0
return 1
2002-03-17 18:13:59 +01:00
2002-04-07 11:11:48 +02:00
def genpasswd ( ) :
newpasswd = " "
2002-03-17 18:13:59 +01:00
chars = string . letters + string . digits
2002-04-07 11:11:48 +02:00
for i in range ( 8 ) :
newpasswd = newpasswd + choice ( chars )
return newpasswd
2002-03-17 18:13:59 +01:00
Form = cgi . FieldStorage ( )
email = GetKey ( Form , " email " )
firstname = GetKey ( Form , " firstname " )
lastname = GetKey ( Form , " lastname " )
info = GetKey ( Form , " info " )
address = GetKey ( Form , " address " )
city = GetKey ( Form , " city " )
country = GetKey ( Form , " country " )
phone = GetKey ( Form , " phone " )
race = GetKey ( Form , " race " )
2002-03-19 23:33:45 +01:00
locale = GetKey ( Form , " locale " )
2002-03-17 18:13:59 +01:00
2002-03-19 23:33:45 +01:00
referrer = GetKey ( Form , " referrer " )
firsttime = GetKey ( Form , " firsttime " )
2002-04-07 11:11:48 +02:00
bonus = GetKey ( Form , " bonus " )
2002-03-19 23:33:45 +01:00
if ( locale == None ) or ( lastname == None ) or ( race == None ) or ( firstname == None ) or ( address == None ) or ( city == None ) :
2002-03-17 18:13:59 +01:00
output = " <p>Um Dich zu Eressea anzumelden musst Du das Formular vollst<73> ndig ausf<73> llen. \n "
for key in Form . keys ( ) :
2002-03-19 23:33:45 +01:00
output = output + " <br> " + key + " : " + Form [ key ] . value + " \n "
Display ( output )
elif ValidEmail ( email ) == 0 :
output = " <p>Um Dich zu Eressea anzumelden musst Du eine g<> ltige Email-Adresse angeben. \n "
2002-03-17 18:13:59 +01:00
Display ( output )
else :
db = MySQLdb . connect ( db = dbname )
cursor = db . cursor ( )
2002-04-03 00:09:40 +02:00
exist = cursor . execute ( " select id from users where email= ' " + email + " ' and (status= ' WAITING ' or status= ' CONFIRMED ' ) " )
if exist :
text = { " de " : " Du stehst bereits auf der Warteliste " , " en " : " You are already on the waiting list " }
Display ( ' <p> ' + text [ locale ] )
2002-03-17 18:13:59 +01:00
else :
2002-04-03 00:09:40 +02:00
bans = cursor . execute ( ' select regex, reason from bannedemails ' )
while bans :
bans = bans - 1
regexp , reason = cursor . fetchone ( )
if ( re . match ( regexp , email , re . IGNORECASE ) ) != None :
Display ( ' Deine Email-Adresse ist f<> r Eressea nicht zugelassen. ' + reason )
sys . exit ( 0 )
2002-04-08 12:01:42 +02:00
# create a new user record
2002-03-17 18:13:59 +01:00
password = genpasswd ( )
2002-03-19 23:33:45 +01:00
fields = " firstname, lastname, locale, email, address, city, status, password "
values = " ' " + firstname + " ' , ' " + lastname + " ' , ' " + locale + " ' , ' " + email + " ' , ' " + address + " ' , ' " + city + " ' , ' WAITING ' , ' " + password + " ' "
2002-03-17 18:13:59 +01:00
if phone != None :
fields = fields + " , phone "
values = values + " , ' " + phone + " ' "
if info != None :
fields = fields + " , info "
values = values + " , ' " + info + " ' "
if country != None :
fields = fields + " , country "
values = values + " , " + country + " "
2002-03-19 23:33:45 +01:00
if referrer != None :
fields = fields + " , referrer "
values = values + " , ' " + referrer + " ' "
if firsttime != None :
fields = fields + " , firsttime "
if firsttime == ' yes ' :
values = values + " , 1 "
else :
values = values + " , 0 "
2002-03-17 18:13:59 +01:00
cursor . execute ( " insert into users ( " + fields + " ) VALUES ( " + values + " ) " )
cursor . execute ( " SELECT LAST_INSERT_ID() from dual " )
custid = cursor . fetchone ( ) [ 0 ]
2002-04-08 12:01:42 +02:00
# track IP addresses
2002-04-03 00:09:40 +02:00
ip = None
2002-03-19 23:33:45 +01:00
if os . environ . has_key ( ' REMOTE_ADDR ' ) :
ip = os . environ [ ' REMOTE_ADDR ' ]
2002-04-03 00:09:40 +02:00
if ip != None :
2002-03-19 23:33:45 +01:00
cursor . execute ( " REPLACE userips (ip, user) VALUES ( ' " + ip + " ' , " + str ( int ( custid ) ) + " ) " )
2002-04-08 12:01:42 +02:00
# add a subscription record
values = " ' PENDING ' "
fields = " status "
if bonus != None :
fields = fields + " , bonus "
if bonus == ' yes ' :
values = values + " , 1 "
else :
values = values + " , 0 "
cursor . execute ( " insert into subscriptions (user, race, game, " + fields + " ) VALUES ( " + str ( int ( custid ) ) + " , ' " + race + " ' , 0, " + values + " ) " )
2002-03-23 13:49:18 +01:00
cursor . execute ( " select count(*) from users where status= ' WAITING ' or status= ' CONFIRMED ' " )
2002-03-17 18:13:59 +01:00
Send ( email , custid , firstname , password , cursor . fetchone ( ) [ 0 ] )
2002-04-03 00:09:40 +02:00
text = { " de " : " Deine Anmeldung wurde bearbeitet. Eine EMail mit Hinweisen ist unterwegs zu Dir " , " en " : " Your application was processed. An email containing further instructions is being sent to you " }
Display ( " <p> " + text [ locale ] + " . " )
2002-03-17 18:13:59 +01:00
db . close ( )