-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHaloRadio.cgi
executable file
·173 lines (141 loc) · 5.08 KB
/
HaloRadio.cgi
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
#!/usr/bin/python
#
# Copyright (C) 2004 Philip J Freeman
#
# This file is part of halo_radio
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import HaloRadio
import HaloRadio.Session as Session
from HaloRadio.Util import do_authorize
from simpletal import simpleTAL, simpleTALES
# from simpletalutils import HTMLStructureCleaner
import os, cgi, string, sys, StringIO
import cgitb; cgitb.enable()
#logfile = open("/tmp/.newhaloradio.log","w+")
#
#def log( text ):
# logfile.write( "%s\n" % ( text ) )
#
#log( "startup")
#log( repr(os.environ) )
####
#
# Auth
#
def get_session():
import Cookie
if os.environ.has_key('HTTP_COOKIE'):
C=Cookie.SimpleCookie(os.environ['HTTP_COOKIE'])
else:
C=Cookie.SimpleCookie()
if C.has_key('SessionID'):
try:
s=Session.Session(C['SessionID'].value)
s.UpdateActivity()
u = s.GetUser()
u.UpdateActivity()
except:
s=Session.Session(None)
C['SessionID']=s.id
print C.output()
else:
s=Session.Session(None)
C['SessionID']=s.id
print C.output()
env = os.environ
if env.has_key('REMOTE_HOST'):
s.SetHost(env['REMOTE_HOST'])
elif env.has_key('REMOTE_ADDR'):
s.SetHost(env['REMOTE_ADDR'])
return s
def do_template(action, session):
# Create the context that is used by the template
context = simpleTALES.Context( )
fortunestr = ""
if HaloRadio.conf["general.do_fortune"] == "1":
f = os.popen("/usr/games/fortune -s")
fortunestr = f.read()
f.close()
context.addGlobal ("qotd", fortunestr)
context.addGlobal ("menu", get_menuitems(session))
templateFile = open ("%s/WebRoot/%s.html" % (HaloRadio.conf["general.webroot"],action), 'r')
template = simpleTAL.compileHTMLTemplate (templateFile )
templateFile.close()
return (context, template)
def get_menuitems(session):
from_dir= "%s/WebRoot" % (HaloRadio.conf["general.webroot"] )
files = os.listdir(from_dir)
menuitems = []
for file in files:
if file[-4:] == "html" and file[0] in string.uppercase:
action = file[0:-5]
if os.access("%s/WebRoot/%s.py" % (HaloRadio.conf["general.webroot"], action), os.F_OK):
plugin = my_import("WebRoot.%s"%action)
object = plugin.plugin(form, session)
u = User.User(session.userid)
try:
reqs = object.GetReqs()
except AttributeError:
reqs = ""
if do_authorize(u.rights,reqs):
menuitems.append({"name": file[0:-5], "link": "%s?action=%s" % (HaloRadio.conf["general.cgi_url"],file[0:-5])})
else:
continue
else:
menuitems.append({"name": file[0:-5], "link": "%s?action=%s" % (HaloRadio.conf["general.cgi_url"],file[0:-5])})
#menuitems=[ {"name": "Welcome", "link": "NewHaloRadio.cgi?action=Welcome"}, ]
menuitems.sort()
return menuitems
def my_import(name):
mod = __import__(name)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
import HaloRadio.User as User
form = cgi.FieldStorage()
session = get_session()
print "Content-type: text/html\n"
u = User.User(session.userid)
if form.has_key("action"):
action = form['action'].value
else:
action = 'Welcome'
if not int(HaloRadio.conf["general.guest_access"]) and session.userid == 1 and action != "login" and action != "newUser" and action != "newUserPost" and action != "verify" and action != "forgotPassword" and action != "forgotPasswordPost":
action="Login"
if not ( os.access("%s/WebRoot/%s.html" % (HaloRadio.conf["general.webroot"], action), os.F_OK) or os.access("%s/WebRoot/%s.py" % (HaloRadio.conf["general.webroot"], action), os.F_OK) ):
(context, template) = do_template('error', session )
context.addGlobal ("error", "Invalid Action Specified")
context.addGlobal ("style", u.GetStyle())
template.expand (context, sys.stdout)
sys.exit()
if not u.GetAccess():
rights = u.GetDisplayRights()
if 'guest' not in rights.split(", "):
(context, template) = do_template('error', session )
context.addGlobal ("error", "User Disabled")
context.addGlobal ("style", u.GetStyle())
template.expand (context, sys.stdout)
sys.exit()
# do header here ( automaticly generated menu :- )
(context, template) = do_template(action, session)
context.addGlobal ("style", u.GetStyle())
if os.access("%s/WebRoot/%s.py" % (HaloRadio.conf["general.webroot"], action), os.F_OK):
plugin = my_import("WebRoot.%s"%action)
object = plugin.plugin(form, session)
object.LoadAuth()
object.handler(context)
template.expand (context, sys.stdout)
# do footer here