#!/usr/bin/python
# -*- coding: utf-8 -*-
#For debugging purposes only
#import cgitb
#cgitb.enable()
import cgi, MySQLdb, sys, urllib
#Needs to send these headers
print "Content-Type: text/html; charset=UTF-8"
#Default data
Quser = ""
who=True
whom=True
#Handle args
f = cgi.FieldStorage()
if f.has_key('user'): Quser = f['user'].value
if f.has_key('who'): who = False
if f.has_key('whom'): whom = False
#DBdata
file=open('/data/project/rightstool/replica.my.cnf', 'r')
lista=[l.strip("\n") for l in file.readlines()[1:]]
file.close()
SQLuser=lista[0].split("=")[1].strip("'")
SQLpasswd=lista[1].split("=")[1].strip("'")
#Generate some html
if not who: checked1=u" checked "
else: checked1=u" "
if not whom: checked2=u" checked "
else: checked2=u" "
print """
Rights log search
""" % (sys.argv[0].replace("/data/project/rightstool/public_html//cgi-bin/", "").replace("py", ""), Quser, checked1, checked2)
#Do only if the user has been specified
if Quser!="":
#Query and printing
if not who and not whom:
print """
You cannot check both boxes!
"""
else:
print """
User rights logs search for "%s"
""" % Quser
try:
db = MySQLdb.connect(db='metawiki_p', host="metawiki.labsdb", user=SQLuser, passwd=SQLpasswd)
cursor = db.cursor()
Quser_=Quser.strip(" ").replace(" ", "_")
if not whom: sqlcond="log_user in (select user_id from user where user_name like '%%%s%%')" % Quser_
elif not who: sqlcond="log_title like '%%%s%%%" % Quser_
else: sqlcond="(log_user in (select user_id from user where user_name like '%%%s%%') or log_title like '%%%s%%')" % (Quser_, Quser_)
# who, whom, parameters, comment, timestamp
sql="select user_name, log_title, log_params, log_comment, log_timestamp from logging join user on user_id=log_user where log_type='rights' and %s order by log_timestamp desc;" % sqlcond
cursor.execute(sql)
results = cursor.fetchall()
except: results=()
if results!=():
print """"""
for i in results:
if i[3]=='': comment=" "
else: comment=i[3].replace("<", "<").replace(">", ">")
t=i[4]
time="%s/%s/%s %s:%s:%s" % (t[:4], t[4:6], t[6:8], t[8:10], t[10:12], t[12:])
par=i[2].split("\n")
for p in range(len(par)):
if not par[p]: par[p]="(none)"
if len(par)!=2: params="".join(par)
else: params="from %s to %s" % tuple(par)
print """""" % (urllib.quote(i[0]), i[0].replace("_", " "), urllib.quote(i[1]), i[1].replace("_", " "), params, comment, time)
print """