#!/usr/bin/python2.4

"""
*****************************
IP@LL IP addressmanagement
Copyright 2007 racyAPz
http://www.racyapz.at
*****************************
"""

import Html as HTML
import DBmy
import IpallUser
from Sessionclass import Session
import os
import cgi
import cgitb; cgitb.enable()
from Configobj import ConfigObj
from Cookie import SimpleCookie
from time import asctime


def main():
    """entry point for executing IPALL"""

    ### definitions of variables
    global conn

    ### create database connection object
    cfg = ConfigObj("ipall.cfg")
    db_host = cfg['Database']['db_host']
    db_user = cfg['Database']['db_user']
    db_pw = cfg['Database']['db_pw']
    db = cfg['Database']['db']
    conn = DBmy.db(db_host, db_user, db_pw, db)

    s = Session(conn)
    current_user = s.check_user()
    #company = s.check_cookie()
    HTML.simple_header()
    if current_user == "":
        HTML.simple_redirect_header(cfg['Server']['web_dir'])
        return
    else:
        HTML.body()

    user = IpallUser.User(current_user)
    group, company_id  = user.get_group_id()
    rights = user.get_rights()
    cgi_dir = cfg['Server']['cgi_dir']
    ipall_dir = cfg['Server']['ipall_dir']
    
    if group == 0 and rights[1] != 1:
        HTML.restriction_message()
        return

    print """<br>"""
    print """<div id="main">"""
    print """<table border=0 class=table_main>"""
    print """<tr class=lightPurple3>"""
    print """<td colspan=2 align=center class=TextPurpleBoldBig>&nbsp;</td>"""
    print """</tr>"""
    print """<tr class=lightPurple3>"""
    if rights[1] == 1 or rights[2] == 1:
        print """<td height=20 class=TextPurpleBold>IP@LL user management</td>"""
        print """<td width=200 align=right><a href="%s/new_user.cgi" class=linkPurpleBold>new user</a></td>""" % cgi_dir
    else:
        print """<td height=20 class=TextPurpleBold colspan=2 align=center> IP@LL user management </td>"""
    print """<tr><td colspan=2>&nbsp;</td></tr>"""
    print """<tr><td colspan=2>"""
    print """<table class=table_dotted_row>"""

    if group == 1:
        sql_companies = """SELECT id, name FROM companies ORDER BY name"""
        companies = conn.get_data(sql_companies)
        for c in companies:
            print """<tr><td colspan=3 class=textPurpleBold>%s</td></tr>""" % c[1]
            sql_users = """SELECT persons_id, surname, forename, username FROM persons WHERE companies_id=%u ORDER BY surname""" % int(c[0])
            users = conn.get_data(sql_users)
            for u in users:
                print """<tr><td width=20px align=right>&nbsp;</td>
                    <td class=textPurple>%s %s [%s]</td>""" % (u[1], u[2], u[3])
                print """<td width=200 align=right>"""
                print """<img src="%s/images/editOff.png" id="edit" onMouseOver="mouseoverImage(this, '%s');"  
                    onMouseOut="mouseoverImage(this, '%s');" onClick="redirect_to_url('%s/user_edit.cgi?%s');" class=handlers />""" \
                        % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
                print """<img src="%s/images/deleteOff.png" id="delete" onMouseOver="mouseoverImage(this, '%s');"  
                    onMouseOut="mouseoverImage(this, '%s');" onClick="confirm_and_redirect('Do you really want to delete this user?', '%s/user_delete.cgi?%s');" class=handlers />""" \
                        % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
                print """</td></tr>"""
    elif rights[1] == 1:
        sql_users = """SELECT persons_id, surname, forename, username FROM persons WHERE companies_id=%u ORDER BY surname""" % company_id
        users = conn.get_data(sql_users)
        for u in users:
            print """<tr><td width=20px align=right>&nbsp;</td>
                <td class=textPurple>%s %s [%s]</td>""" % (u[1], u[2], u[3])
            print """<td width=200 align=right>"""
            print """<img src="%s/images/editOff.png" id="edit" onMouseOver="mouseoverImage(this, '%s');"  
                onMouseOut="mouseoverImage(this, '%s');" onClick="redirect_to_url('%s/user_edit.cgi?%s');" class=handlers />""" \
                    % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
            print """<img src="%s/images/deleteOff.png" id="delete" onMouseOver="mouseoverImage(this, '%s');"  
                onMouseOut="mouseoverImage(this, '%s');" onClick="confirm_and_redirect('Do you really want to delete this user?', '%s/user_delete.cgi?%s');" class=handlers />""" \
                    % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
            print """</td></tr>"""
    else:
        sql_users = """SELECT persons_id, surname, forename FROM persons WHERE username='%s' """ % current_user
        user = conn.get_data(sql_users)[0]
        print """<tr><td width=20px align=right>&nbsp;</td>
                <td class=textPurple>%s %s [%s]</td>""" % (user[1], user[2], current_user)
        print """<td width=200 align=right>"""
        print """<img src="%s/images/editOff.png" id="edit" onMouseOver="mouseoverImage(this, '%s');"  
            onMouseOut="mouseoverImage(this, '%s');" onClick="redirect_to_url('%s/user_edit.cgi?%s');" class=handlers />""" \
                % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(user[0]) )
        print """</td></tr>"""
    print """</table>"""
    print """</td></tr>"""
    print """<tr><td colspan=2>&nbsp;</td></tr>"""
    print """<tr><td colspan=2 align=right><a href="%s/mgmt.cgi" class=linkPurpleBold title="back"> << back </a></td></tr>""" % cgi_dir
    print """</table>"""
    print """</div>"""

    HTML.main_footer()

main()
473aeaf55f7f5c7c04a9a9fe7186cf0d90f09a90
#!/usr/bin/python2.4

"""
*****************************
IP@LL IP addressmanagement
Copyright 2007 racyAPz
http://www.racyapz.at
*****************************
"""

import Html as HTML
import DBmy
import IpallUser
from Sessionclass import Session
import os
import cgi
import cgitb; cgitb.enable()
from Configobj import ConfigObj
from Cookie import SimpleCookie
from time import asctime


def main():
    """entry point for executing IPALL"""

    ### definitions of variables
    global conn

    ### create database connection object
    cfg = ConfigObj("ipall.cfg")
    db_host = cfg['Database']['db_host']
    db_user = cfg['Database']['db_user']
    db_pw = cfg['Database']['db_pw']
    db = cfg['Database']['db']
    conn = DBmy.db(db_host, db_user, db_pw, db)

    s = Session(conn)
    current_user = s.check_user()
    #company = s.check_cookie()
    HTML.simple_header()
    if current_user == "":
        HTML.simple_redirect_header(cfg['Server']['web_dir'])
        return
    else:
        HTML.body()

    user = IpallUser.User(current_user)
    group, company_id  = user.get_group_id()
    rights = user.get_rights()
    cgi_dir = cfg['Server']['cgi_dir']
    ipall_dir = cfg['Server']['ipall_dir']
    
    if group == 0 and rights[1] != 1:
        HTML.restriction_message()
        return

    print """<br>"""
    print """<div id="main">"""
    print """<table border=0 class=table_main>"""
    print """<tr class=lightPurple3>"""
    print """<td colspan=2 align=center class=TextPurpleBoldBig>&nbsp;</td>"""
    print """</tr>"""
    print """<tr class=lightPurple3>"""
    if rights[1] == 1 or rights[2] == 1:
        print """<td height=20 class=TextPurpleBold>IP@LL user management</td>"""
        print """<td width=200 align=right><a href="%s/new_user.cgi" class=linkPurpleBold>new user</a></td>""" % cgi_dir
    else:
        print """<td height=20 class=TextPurpleBold colspan=2 align=center> IP@LL user management </td>"""
    print """<tr><td colspan=2>&nbsp;</td></tr>"""
    print """<tr><td colspan=2>"""
    print """<table class=table_dotted_row>"""

    if group == 1:
        sql_companies = """SELECT id, name FROM companies ORDER BY name"""
        companies = conn.get_data(sql_companies)
        for c in companies:
            print """<tr><td colspan=3 class=textPurpleBold>%s</td></tr>""" % c[1]
            sql_users = """SELECT persons_id, surname, forename, username FROM persons WHERE companies_id=%u ORDER BY surname""" % int(c[0])
            users = conn.get_data(sql_users)
            for u in users:
                print """<tr><td width=20px align=right>&nbsp;</td>
                    <td class=textPurple>%s %s [%s]</td>""" % (u[1], u[2], u[3])
                print """<td width=200 align=right>"""
                print """<img src="%s/images/editOff.png" id="edit" onMouseOver="mouseoverImage(this, '%s');"  
                    onMouseOut="mouseoverImage(this, '%s');" onClick="redirect_to_url('%s/user_edit.cgi?%s');" class=handlers />""" \
                        % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
                print """<img src="%s/images/deleteOff.png" id="delete" onMouseOver="mouseoverImage(this, '%s');"  
                    onMouseOut="mouseoverImage(this, '%s');" onClick="confirm_and_redirect('Do you really want to delete this user?', '%s/user_delete.cgi?%s');" class=handlers />""" \
                        % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
                print """</td></tr>"""
    elif rights[1] == 1:
        sql_users = """SELECT persons_id, surname, forename, username FROM persons WHERE companies_id=%u ORDER BY surname""" % company_id
        users = conn.get_data(sql_users)
        for u in users:
            print """<tr><td width=20px align=right>&nbsp;</td>
                <td class=textPurple>%s %s [%s]</td>""" % (u[1], u[2], u[3])
            print """<td width=200 align=right>"""
            print """<img src="%s/images/editOff.png" id="edit" onMouseOver="mouseoverImage(this, '%s');"  
                onMouseOut="mouseoverImage(this, '%s');" onClick="redirect_to_url('%s/user_edit.cgi?%s');" class=handlers />""" \
                    % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
            print """<img src="%s/images/deleteOff.png" id="delete" onMouseOver="mouseoverImage(this, '%s');"  
                onMouseOut="mouseoverImage(this, '%s');" onClick="confirm_and_redirect('Do you really want to delete this user?', '%s/user_delete.cgi?%s');" class=handlers />""" \
                    % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(u[0]) )
            print """</td></tr>"""
    else:
        sql_users = """SELECT persons_id, surname, forename FROM persons WHERE username='%s' """ % current_user
        user = conn.get_data(sql_users)[0]
        print """<tr><td width=20px align=right>&nbsp;</td>
                <td class=textPurple>%s %s [%s]</td>""" % (user[1], user[2], current_user)
        print """<td width=200 align=right>"""
        print """<img src="%s/images/editOff.png" id="edit" onMouseOver="mouseoverImage(this, '%s');"  
            onMouseOut="mouseoverImage(this, '%s');" onClick="redirect_to_url('%s/user_edit.cgi?%s');" class=handlers />""" \
                % ( ipall_dir, ipall_dir, ipall_dir, cgi_dir, str(user[0]) )
        print """</td></tr>"""
    print """</table>"""
    print """</td></tr>"""
    print """<tr><td colspan=2>&nbsp;</td></tr>"""
    print """<tr><td colspan=2 align=right><a href="%s/mgmt.cgi" class=linkPurpleBold title="back"> << back </a></td></tr>""" % cgi_dir
    print """</table>"""
    print """</div>"""

    HTML.main_footer()

main()