1#!/usr/bin/env python
2
3"""
4FCKeditor - The text editor for Internet - http://www.fckeditor.net
5Copyright (C) 2003-2007 Frederico Caldeira Knabben
6
7== BEGIN LICENSE ==
8
9Licensed under the terms of any of the following licenses at your
10choice:
11
12- GNU General Public License Version 2 or later (the "GPL")
13http://www.gnu.org/licenses/gpl.html
14
15- GNU Lesser General Public License Version 2.1 or later (the "LGPL")
16http://www.gnu.org/licenses/lgpl.html
17
18- Mozilla Public License Version 1.1 or later (the "MPL")
19http://www.mozilla.org/MPL/MPL-1.1.html
20
21== END LICENSE ==
22
23Base Connector for Python (CGI and WSGI).
24
25See config.py for configuration settings
26
27"""
28import cgi, os
29
30from fckutil import *
31from fckcommands import * 	# default command's implementation
32from fckoutput import * 	# base http, xml and html output mixins
33import config as Config
34
35class FCKeditorConnectorBase( object ):
36	"The base connector class. Subclass it to extend functionality (see Zope example)"
37
38	def __init__(self, environ=None):
39		"Constructor: Here you should parse request fields, initialize variables, etc."
40		self.request = FCKeditorRequest(environ) # Parse request
41		self.headers = []						# Clean Headers
42		if environ:
43			self.environ = environ
44		else:
45			self.environ = os.environ
46
47	# local functions
48
49	def setHeader(self, key, value):
50		self.headers.append ((key, value))
51		return
52
53class FCKeditorRequest(object):
54	"A wrapper around the request object"
55	def __init__(self, environ):
56		if environ: # WSGI
57			self.request = cgi.FieldStorage(fp=environ['wsgi.input'],
58							environ=environ,
59							keep_blank_values=1)
60			self.environ = environ
61		else: # plain old cgi
62			self.environ = os.environ
63			self.request = cgi.FieldStorage()
64		if 'REQUEST_METHOD' in self.environ and 'QUERY_STRING' in self.environ:
65			if self.environ['REQUEST_METHOD'].upper()=='POST':
66				# we are in a POST, but GET query_string exists
67				# cgi parses by default POST data, so parse GET QUERY_STRING too
68				self.get_request = cgi.FieldStorage(fp=None,
69							environ={
70							'REQUEST_METHOD':'GET',
71							'QUERY_STRING':self.environ['QUERY_STRING'],
72							},
73							)
74		else:
75			self.get_request={}
76
77	def has_key(self, key):
78		return self.request.has_key(key) or self.get_request.has_key(key)
79
80	def get(self, key, default=None):
81		if key in self.request.keys():
82			field = self.request[key]
83		elif key in self.get_request.keys():
84			field = self.get_request[key]
85		else:
86			return default
87		if hasattr(field,"filename") and field.filename: #file upload, do not convert return value
88			return field
89		else:
90			return field.value
91