/[theodore]/bunnyblog/admin.cgi


UCC Code Repository

Contents of /bunnyblog/admin.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (show annotations) (download)
Fri Feb 1 09:31:49 2008 UTC (12 years, 10 months ago) by theodore
File size: 10718 byte(s)
More menu/shoutbox stuff.


1 #!/usr/bin/python
2 import cgi
3 import cgitb; cgitb.enable()
4 import blog
5 import os
6
7 """
8 Import modules from modules directory
9 """
10 os.sys.path.insert(0,'./modules')
11 from logintools import login
12 from logintools import logout
13 from logintools import isloggedin
14 from cgiutils import *
15 del os.sys.path[0]
16
17 os.environ['SCRIPT_NAME'] = "admin.cgi"
18
19 """
20 Admin Module Page
21 """
22
23 """
24 Here we set up all the variables for generating the page
25 """
26 main = ""
27 title = "Bunny Blog Admin Center"
28 header = title
29 meta = ""
30
31 """
32 Here we retrieve the blog and any arguments
33 Defaults are assigned here
34 """
35 form = cgi.FieldStorage()
36 myblog = blog.Blog()
37 perpage = 10
38 pagenum = 1
39
40 """
41 Stuff for login tools
42 """
43 userdir = 'data/users/'
44 script = 'admin.cgi' #os.environ['SCRIPT_NAME']
45 formdict = getform(['page'], form)
46 loginreturn = login(form, userdir, action=formdict['page'])
47 try:
48 action, userconfig = loginreturn
49 except:
50 pass
51
52 def listing(directory):
53 list = ""
54 for dir in os.listdir(directory):
55 if dir[-1] == '~' or dir[0] == ".": #Most likely Linux tmp Files, more than likely will not effect users, but makes dev work a little cleaner
56 continue
57 if dir == myblog.config['template']:
58 selected = "selected='selected'"
59 else:
60 selected = ""
61 dir = dir.replace(".pyhtml","")
62 list += "<option value='%s' %s>%s</option>" % (dir,selected,dir)
63 return list
64
65 def content_replace(main, filename="", content="", pagetitle="", bb_selected="checked='checked'",html_selected="", filetype=""):
66 main = main.replace("<@[email protected]>" , filename )
67 main = main.replace("<@[email protected]>" , content )
68 main = main.replace("<@[email protected]>" , pagetitle )
69 main = main.replace("<@[email protected]>" , bb_selected )
70 main = main.replace("<@[email protected]>" , html_selected )
71 main = main.replace("<@[email protected]>" , filetype )
72 return main
73
74 def makemeta(url="<@[email protected]>index.cgi", delay = 5):
75 return "<meta http-equiv='refresh' content='%s;url=%s' />" % (delay,url)
76
77
78 def checkcontent(form,action="new"):
79 checkedcontent = ""
80 error = ""
81 filename = ""
82 pageexists = False
83 bb_selected = "checked='checked'"
84 html_selected = ""
85 if form.has_key('filename'):
86 filename = form['filename'].value
87 if not filename.replace("_", "0").isalnum():
88 error += "<li>Filename contains disallowed letters, try again</li>"
89 try:
90 open('templates/cms/'+ filename + '.pyhtml', 'r')
91 pageexists = True
92 except:
93 pageexists = False
94
95 if action == "new" and pageexists:
96
97 error = "The filename you provided already exists.<br /><br />This error may occur if the request to add a new page submitted twice.<br />If you think this may have happened, open <a href='<@[email protected]>index.cgi?page=<@[email protected]>'>this link</a> in a new tab or window to see if the page exists.<br />If it does not, return here and try saving the page again, possibly with a new name."
98 options = """
99 <p>Alternatively, you can either:<br />
100 <a href="<@[email protected]>admin.cgi?page=editcontent&amp;page=<@[email protected]>">Edit the page with the file name you specifed, losing the current content you have provided.</a>
101 <h3>OR</h3>
102 Create a new page with the details you provided, and change the filename</a>
103 </p>
104 """
105 else:
106 error += "<li>No Filename</li>"
107 if form.has_key('pagetitle'):
108 pagetitle = form['pagetitle'].value
109 else:
110 pagetitle = filename
111 if form.has_key('html'):
112 html_or_bbcode = form['html'].value
113 if html_or_bbcode == "html":
114 html_selected = bb_selected
115 bb_selected = ""
116 if form.has_key('content'):
117 content = form['content'].value
118 else:
119 error = "<li>No page content</li>"
120
121 if error == "": #Everything is good
122 if bb_selected != "":
123 filetype = "bbcode"
124 else:
125 filetype = "html"
126 blog.savenewpage(filename,content,pagetitle, filetype)
127 meta = makemeta("%sindex.cgi?page=%s"%(myblog.config['site_url'],filename))
128 checkedcontent += "Successfully saved page '%s'<br />If this template and your browser support it you will be redirected to the page in 5 seconds. Otherwise, you can access this page at <a href='%sindex.cgi?page=%s'>%sindex.cgi?page=%s</a>.<br />" % (filename, myblog.config['site_url'],filename, myblog.config['site_url'] ,filename)
129 else:
130 error = "<h3>The following errors occured while trying to save the page:</h3><ul>"+error+"</ul><hr />"
131 checkedcontent += error + options + blog.gettemplate('admin/%scontent' % action)
132
133 checkedcontent = content_replace(checkedcontent, filename, content, pagetitle, bb_selected,html_selected)
134 return meta,checkedcontent
135
136 if form.has_key('page'):
137 page = form['page'].value
138 if not form.has_key('delete'):
139 main += blog.gettemplate("admin/" + page)
140 if page == "config":
141 main = main.replace("<@[email protected]>",listing("./templates/style/"))
142 if page == "content":
143 main = main.replace("<@[email protected]>",listing("./data/cms/"))
144 if page == "newcontent":
145 main = content_replace(main)
146 if page == "editcontent":
147 if form.has_key('delete'):
148 action = "delete"
149 else:
150 action = "edit"
151 if form.has_key('filename'):
152 filename = form['filename'].value
153 if action == "delete":
154 try:
155 os.remove('data/cms/'+filename+".pyhtml")
156 main += "File ' %s ' removed" % filename
157 except:
158 main += blog.error("Delete Failed")
159 else:
160 (content,title,filetype,lastedited) = blog.loadcmspage(filename)
161 main = content_replace(main, filename,content,title,"","",filetype)
162 main = main.replace('<@[email protected]>',lastedited)
163 else:
164 error = "Filename of page to edit not specified"
165 if page == "editmenu" or page == "editshout":
166 import re
167 if page == "editmenu":
168 menu = blog.gettemplate('mainmenu', 'data')
169 menu = menu.replace("</li>","")
170 menu = menu.replace("<li>","")
171 htmlmenu = ""
172 blogtags = re.compile(r'<@[email protected]>index.cgi\?(page|title|index)=(.*?).><span>(.*?)</span>')
173 urltags = re.compile(r'<a href="(.*?)"><span>(.*?)</span>')
174 imgtags = re.compile(r'<@[email protected]>gallery.cgi\?image=(.*?)"><span>(.*?)</span>')
175 rsstags = re.compile(r'<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" style="height:15px;width:15px" alt="" />')
176 for line in menu.splitlines()[1:-1]:
177 line = line.strip()
178 blogs = blogtags.findall(line)
179 urls = urltags.findall(line)
180 imgs = imgtags.findall(line)
181 if blogs:
182 line = '[blog=%s:%s]%s[/blog]' % blogs[0]
183 elif urls:
184 line = '[url="%s"]%s[/url]' % urls[0]
185 elif imgs:
186 line = '[blog=image:%s]%s[/blog]' % imgs[0]
187 htmlmenu += line + "\n"
188 htmlmenu = htmlmenu.replace('<a href = "<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" style="height:1em;width:1em" alt="" /> Link to <@[email protected]> RSS Feed</span></a>', "[rss/]")
189 htmlmenu = htmlmenu.replace('<a href = "<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" style="height:1em;width:1em" alt="" /> ',"[rss]")
190 htmlmenu = htmlmenu.replace('</span></a>', "[/rss]")
191 htmlmenu = blog.cleanse(htmlmenu)
192 elif page == "editshout":
193 htmlmenu = blog.gettemplate('shoutbox', 'data')
194 main = main.replace("<@[email protected]>", htmlmenu)
195
196 elif form.has_key('action'):
197 myblog = blog.Blog(True)
198 action = form['action'].value
199 if action == "addarticle":
200 if form.has_key('title') and form.has_key('content'):
201 title = blog.cleanse(form['title'].value)
202 content = blog.cleanse(form['content'].value)
203 if len(myblog.articles):
204 index = myblog.articles[-1].index + 1
205 else:
206 index = 0
207 if myblog.addarticle(title, content, index):
208 main += "<p>Successfully posted article: <span style='font-size:2em;'>&ldquo;</span> %s <span style='font-size:2em;'>&rdquo;</span><br /> A Link to the Article is at <a href='<@[email protected]>index.cgi?article=%d'><@[email protected]>index.cgi?article=%d</a></p>" % (title,index,index)
209 else:
210 main += "<p class='blog_engine_error'Save failed</br>Article name: %s was not saved. Click the back button in your browser and try again, if that fails there may be a problem with the server permissions" % title
211 else:
212 blog.error('addarticle failed')
213 elif action == "saveconfig":
214 config = dict()
215 if form.has_key('site_url'):
216 config['site_url'] = form['site_url'].value
217 if config['site_url'][-1] != "/":
218 config['site_url'] = config['site_url'] + '/'
219 if form.has_key('site_name'):
220 config['site_name'] = form['site_name'].value
221 else:
222 config['site_name'] = 'Bunny Blog'
223 if form.has_key('site_blurb'):
224 config['site_blurb'] = form['site_blurb'].value
225 else:
226 config['site_blurb'] = ''
227 if form.has_key('blog_author'):
228 config['blog_author'] = form['blog_author'].value
229 else:
230 config['blog_author'] = ''
231 if form.has_key('footer'):
232 config['footer'] = form['footer'].value
233 else:
234 config['footer'] = ''
235 config['template'] = form['template'].value
236 myblog.config.update(config)
237 myblog.saveconfig()
238 main += "Configuration file successfully updated"
239 elif action == "addcontent":
240 if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
241 if not os.path.exists("./data/cms/"+form['filename'].value +".pyhtml"):
242 meta,main = checkcontent(form)
243 else:
244 main = "A page with this filename already exists, please try again."
245 else:
246 main += blog.gettemplate('admin/newcontent')
247 elif action == "savecontent":
248 if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
249 meta,main = checkcontent(form,'edit')
250 else:
251 main += blog.gettemplate('admin/editcontent')
252 elif action == "logout":
253 cookieheader = logout(userdir)
254 print cookieheader
255 elif action == "deletearticle":
256 try:
257 title = myblog.delarticle(form['index'].value)
258 main += "Deleted Article: %s" % (article)
259 except:
260 main += "Delete failed"
261 elif action == "savemenu" or action == "saveshout":
262 if form.has_key('menu'):
263 menu = form['menu'].value
264 if action == "savemenu":
265 filename = "mainmenu.pyhtml"
266 updated = "Main Menu"
267 if blog.savemenu(menu, filename):
268 main = updated + " update successful"
269 else:
270 main = blog.error(updated + " update failed. Press back and try again.")
271 elif action == "saveshout":
272 filename = "shoutbox.pyhtml"
273 updated = "Shoutbox"
274 if blog.save(menu, 'data/'+filename):
275 main = updated + " update successful"
276 else:
277 main = blog.error(updated + " update failed. Press back and try again.")
278 else:
279 main = blog.error(updated + " update failed. Press back and try again.")
280 else:
281 menu = " "
282 if blog.savemenu(menu, filename):
283 main = updated + " update with blank content successful"
284 else:
285 main = blog.error(updated + " update failed. Press back and try again.")
286 else:
287 main += "This is the main admin page"
288
289
290
291
292 myblog.display(title,header,main,blog.gettemplate("admin/toolbar"), meta)

Properties

Name Value
svn:executable

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26