'''
Created on 19.09.2010
@author: Sergey Morozov
'''
import sys, os, cgitb, cgi
from wsgiref.util import application_uri
cgitb.enable()
serverdir = os.path.dirname(__file__)
sys.path.append(serverdir)
sys.path.append(serverdir + os.sep + 'include')
import config
from siphon import user
def genHeader(environ):
output = '\n'\
'\n'\
'
\n'\
' \n'\
' Change password - Siphon Server\n'\
'\n'\
'\n'\
' Fill the form to change password
\n'
return(output)
def genForm(environ, submitUrl, printOldPassword = True):
output = ' \n'
return(output)
def genAlert(environ, alertMessage):
output = ' \n'\
' \n'\
' \n'\
' ' + alertMessage + '\n'\
' | \n'\
'
\n'\
'
\n'
return(output)
def genFooter(environ):
output = '\n'
return(output)
def application(environ, start_response):
output = genHeader(environ)
#webForm = cgi.FieldStorage(environ=os.environ,keep_blank_values=1)
webForm = cgi.FieldStorage(fp = environ['wsgi.input'], environ = environ, keep_blank_values = 1)
printOldPassword = True
if webForm:
if webForm.has_key('email') and webForm.has_key('old_password') and webForm.has_key('new_password') and webForm.has_key('retype_new_password'):
if len(webForm.getfirst('email')) > 4 and len(webForm.getfirst('old_password')) > 1 and len(webForm.getfirst('new_password')) > 1 and webForm.getfirst('new_password') == webForm.getfirst('retype_new_password'):
form = {'email': webForm.getfirst('email'),
'password': webForm.getfirst('old_password')}
u = user(form)
if u.status['retval'] == 0:
authstatus = u.auth()
if authstatus['retval'] == 0:
output += genAlert(environ, u.setPass(webForm.getfirst('new_password'))['alert_message'])
else:
output += genAlert(environ, authstatus['alert_message'])
else:
output += genAlert(environ, u.status['alert_message'])
else:
output += genAlert(environ, 'Please, fill the form')
elif webForm.has_key('user') and webForm.has_key('key') and webForm.has_key('new_password') and webForm.has_key('retype_new_password'):
if len(webForm.getfirst('new_password')) > 1 and webForm.getfirst('new_password') == webForm.getfirst('retype_new_password') and len(webForm.getfirst('key')) == 20:
form = {'email': webForm.getfirst('user'),
'password': webForm.getfirst('new_password')}
u = user(form)
if u.status['retval'] ==0:
if webForm.getfirst('key') == u.getKey():
output += genAlert(environ, u.setPass(webForm.getfirst('new_password'))['alert_message'])
else:
output += genAlert(environ, 'Error: key missmatch.')
else:
output += genAlert(environ, authstatus['alert_message'])
else:
output += genAlert(environ, 'Please, fill the form')
elif webForm.has_key('user') and webForm.has_key('key'):
printOldPassword = False
submitUrl = application_uri(environ)
if webForm and webForm.has_key('key') and webForm.has_key('user'):
submitUrl += '?key=' + webForm.getfirst('key') + '&user=' + webForm.getfirst('user')
output += genForm(environ, submitUrl, printOldPassword)
output += genFooter(environ)
status = '200 OK'
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return([output])