/[theodore]/bunnyblog/admin.cgi


UCC Code Repository

Contents of /bunnyblog/admin.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (hide annotations) (download)
Fri Feb 22 06:06:51 2008 UTC (12 years, 9 months ago) by theodore
File size: 11035 byte(s)
Fixed more delete article stuff


1 svn-admin 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 theodore 22 httpstatuscode = "200 OK"
31 svn-admin 1
32     """
33     Here we retrieve the blog and any arguments
34     Defaults are assigned here
35     """
36     form = cgi.FieldStorage()
37     myblog = blog.Blog()
38     perpage = 10
39     pagenum = 1
40    
41     """
42     Stuff for login tools
43     """
44     userdir = 'data/users/'
45     script = 'admin.cgi' #os.environ['SCRIPT_NAME']
46     formdict = getform(['page'], form)
47     loginreturn = login(form, userdir, action=formdict['page'])
48     try:
49     action, userconfig = loginreturn
50     except:
51     pass
52    
53     def listing(directory):
54     list = ""
55     for dir in os.listdir(directory):
56     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
57     continue
58     if dir == myblog.config['template']:
59     selected = "selected='selected'"
60     else:
61     selected = ""
62     dir = dir.replace(".pyhtml","")
63     list += "<option value='%s' %s>%s</option>" % (dir,selected,dir)
64     return list
65    
66     def content_replace(main, filename="", content="", pagetitle="", bb_selected="checked='checked'",html_selected="", filetype=""):
67     main = main.replace("<@[email protected]>" , filename )
68     main = main.replace("<@[email protected]>" , content )
69     main = main.replace("<@[email protected]>" , pagetitle )
70     main = main.replace("<@[email protected]>" , bb_selected )
71     main = main.replace("<@[email protected]>" , html_selected )
72     main = main.replace("<@[email protected]>" , filetype )
73     return main
74    
75     def makemeta(url="<@[email protected]>index.cgi", delay = 5):
76     return "<meta http-equiv='refresh' content='%s;url=%s' />" % (delay,url)
77    
78    
79     def checkcontent(form,action="new"):
80     checkedcontent = ""
81     error = ""
82     filename = ""
83     pageexists = False
84     bb_selected = "checked='checked'"
85     html_selected = ""
86     if form.has_key('filename'):
87     filename = form['filename'].value
88     if not filename.replace("_", "0").isalnum():
89     error += "<li>Filename contains disallowed letters, try again</li>"
90     try:
91     open('templates/cms/'+ filename + '.pyhtml', 'r')
92     pageexists = True
93     except:
94     pageexists = False
95    
96     if action == "new" and pageexists:
97    
98     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."
99     options = """
100     <p>Alternatively, you can either:<br />
101     <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>
102     <h3>OR</h3>
103     Create a new page with the details you provided, and change the filename</a>
104     </p>
105     """
106     else:
107     error += "<li>No Filename</li>"
108     if form.has_key('pagetitle'):
109     pagetitle = form['pagetitle'].value
110     else:
111     pagetitle = filename
112     if form.has_key('html'):
113     html_or_bbcode = form['html'].value
114     if html_or_bbcode == "html":
115     html_selected = bb_selected
116     bb_selected = ""
117     if form.has_key('content'):
118     content = form['content'].value
119     else:
120     error = "<li>No page content</li>"
121    
122     if error == "": #Everything is good
123     if bb_selected != "":
124     filetype = "bbcode"
125     else:
126     filetype = "html"
127     blog.savenewpage(filename,content,pagetitle, filetype)
128     meta = makemeta("%sindex.cgi?page=%s"%(myblog.config['site_url'],filename))
129     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)
130     else:
131     error = "<h3>The following errors occured while trying to save the page:</h3><ul>"+error+"</ul><hr />"
132     checkedcontent += error + options + blog.gettemplate('admin/%scontent' % action)
133    
134     checkedcontent = content_replace(checkedcontent, filename, content, pagetitle, bb_selected,html_selected)
135     return meta,checkedcontent
136    
137     if form.has_key('page'):
138     page = form['page'].value
139     if not form.has_key('delete'):
140     main += blog.gettemplate("admin/" + page)
141     if page == "config":
142 theodore 34 main = main.replace("<@[email protected]>",listing("./data/themes/"))
143 svn-admin 1 if page == "content":
144     main = main.replace("<@[email protected]>",listing("./data/cms/"))
145     if page == "newcontent":
146     main = content_replace(main)
147     if page == "editcontent":
148     if form.has_key('delete'):
149     action = "delete"
150     else:
151     action = "edit"
152     if form.has_key('filename'):
153     filename = form['filename'].value
154     if action == "delete":
155     try:
156 theodore 13 os.remove('data/cms/'+filename+".pyhtml")
157 svn-admin 1 main += "File ' %s ' removed" % filename
158     except:
159     main += blog.error("Delete Failed")
160     else:
161     (content,title,filetype,lastedited) = blog.loadcmspage(filename)
162     main = content_replace(main, filename,content,title,"","",filetype)
163     main = main.replace('<@[email protected]>',lastedited)
164     else:
165     error = "Filename of page to edit not specified"
166     if page == "editmenu" or page == "editshout":
167     import re
168     if page == "editmenu":
169 theodore 6 menu = blog.gettemplate('mainmenu', 'data')
170 theodore 19 menu = menu.replace("</li>","")
171     menu = menu.replace("<li>","")
172     htmlmenu = ""
173     blogtags = re.compile(r'<@[email protected]>index.cgi\?(page|title|index)=(.*?).><span>(.*?)</span>')
174     urltags = re.compile(r'<a href="(.*?)"><span>(.*?)</span>')
175     imgtags = re.compile(r'<@[email protected]>gallery.cgi\?image=(.*?)"><span>(.*?)</span>')
176 theodore 28 rsstags = re.compile(r'<@[email protected]>blog.rss"><span><img.*?/>')
177 theodore 19 for line in menu.splitlines()[1:-1]:
178     line = line.strip()
179     blogs = blogtags.findall(line)
180     urls = urltags.findall(line)
181     imgs = imgtags.findall(line)
182     if blogs:
183     line = '[blog=%s:%s]%s[/blog]' % blogs[0]
184     elif urls:
185     line = '[url="%s"]%s[/url]' % urls[0]
186     elif imgs:
187     line = '[blog=image:%s]%s[/blog]' % imgs[0]
188     htmlmenu += line + "\n"
189 theodore 28 htmlmenu = htmlmenu.replace('<a href = "<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" class="rss_link" alt="" /> Link to <@[email protected]> RSS Feed</span></a>', "[rss/]")
190     htmlmenu = htmlmenu.replace('<a href = "<@[email protected]>blog.rss"><span><img src="templates/blog/rss.png" class="rss_link" alt="" /> ',"[rss]")
191 theodore 19 htmlmenu = htmlmenu.replace('</span></a>', "[/rss]")
192     htmlmenu = blog.cleanse(htmlmenu)
193 svn-admin 1 elif page == "editshout":
194 theodore 19 htmlmenu = blog.gettemplate('shoutbox', 'data')
195 svn-admin 1 main = main.replace("<@[email protected]>", htmlmenu)
196    
197     elif form.has_key('action'):
198     myblog = blog.Blog(True)
199     action = form['action'].value
200     if action == "addarticle":
201     if form.has_key('title') and form.has_key('content'):
202     title = blog.cleanse(form['title'].value)
203     content = blog.cleanse(form['content'].value)
204     if len(myblog.articles):
205     index = myblog.articles[-1].index + 1
206     else:
207     index = 0
208     if myblog.addarticle(title, content, index):
209     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)
210     else:
211     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
212     else:
213     blog.error('addarticle failed')
214     elif action == "saveconfig":
215     config = dict()
216     if form.has_key('site_url'):
217     config['site_url'] = form['site_url'].value
218     if config['site_url'][-1] != "/":
219     config['site_url'] = config['site_url'] + '/'
220     if form.has_key('site_name'):
221     config['site_name'] = form['site_name'].value
222 theodore 7 else:
223     config['site_name'] = 'Bunny Blog'
224 svn-admin 1 if form.has_key('site_blurb'):
225     config['site_blurb'] = form['site_blurb'].value
226 theodore 7 else:
227     config['site_blurb'] = ''
228 svn-admin 1 if form.has_key('blog_author'):
229     config['blog_author'] = form['blog_author'].value
230 theodore 7 else:
231     config['blog_author'] = ''
232     if form.has_key('footer'):
233 svn-admin 1 config['footer'] = form['footer'].value
234 theodore 7 else:
235     config['footer'] = ''
236     config['template'] = form['template'].value
237 svn-admin 1 myblog.config.update(config)
238     myblog.saveconfig()
239     main += "Configuration file successfully updated"
240     elif action == "addcontent":
241     if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
242 theodore 13 if not os.path.exists("./data/cms/"+form['filename'].value +".pyhtml"):
243     meta,main = checkcontent(form)
244     else:
245     main = "A page with this filename already exists, please try again."
246 svn-admin 1 else:
247     main += blog.gettemplate('admin/newcontent')
248     elif action == "savecontent":
249     if form.has_key('filename') or form.has_key('html') or form.has_key('content'):
250     meta,main = checkcontent(form,'edit')
251     else:
252     main += blog.gettemplate('admin/editcontent')
253     elif action == "logout":
254     cookieheader = logout(userdir)
255     print cookieheader
256     elif action == "deletearticle":
257 theodore 8 try:
258 theodore 35 article_title = myblog.delarticle(form['index'].value)
259 theodore 41 if article_title:
260 theodore 35 main += "Deleted Article: %s" % (article_title)
261     else:
262     main += "Delete failed - article with index %s not found" % form['index'].value
263 theodore 9 except:
264 theodore 35 main += "Delete failed - Blog was unable to be updated, article may not have been deleted properly."
265 svn-admin 1 elif action == "savemenu" or action == "saveshout":
266     if form.has_key('menu'):
267     menu = form['menu'].value
268 theodore 21 else:
269     menu = ""
270     if action == "savemenu":
271     filename = "mainmenu.pyhtml"
272     updated = "Main Menu"
273     if blog.savemenu(menu, filename):
274     if menu:
275 theodore 19 main = updated + " update successful"
276     else:
277 theodore 21 main = updated + " changed to blank content successful."
278     else:
279     main = blog.error(updated + " update failed. Press back and try again.")
280     elif action == "saveshout":
281     filename = "shoutbox.pyhtml"
282     updated = "Shoutbox"
283     if blog.save(menu, 'data/'+filename):
284     if menu:
285 theodore 19 main = updated + " update successful"
286     else:
287 theodore 21 main = updated + " changed to blank content successful."
288 svn-admin 1 else:
289     main = blog.error(updated + " update failed. Press back and try again.")
290 theodore 11 else:
291 theodore 21 main = blog.error(updated + " update failed. Press back and try again.")
292 svn-admin 1 else:
293     main += "This is the main admin page"
294    
295 theodore 25 if blog.isadmin():
296     main += "<div id='logout_box'><a href='<@[email protected]>admin.cgi?login=logout'>Logout</a></div>"
297 theodore 22 print "Status: %s\nContent-Type: text/html\r\n\n" % httpstatuscode # HTML is following$
298     print 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