⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connector.py

📁 FCKeditor,此HTML文本编辑器可以让web程序拥有如MS Word这样强大的编辑功能.支持当前流行的浏览器如IE 5.5+, Firefox 1.0+, Mozilla 1.3+与Netsc
💻 PY
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/env python

"""
FCKeditor - The text editor for Internet - http://www.fckeditor.net
Copyright (C) 2003-2007 Frederico Caldeira Knabben

== BEGIN LICENSE ==

Licensed under the terms of any of the following licenses at your
choice:

 - GNU General Public License Version 2 or later (the "GPL")
   http://www.gnu.org/licenses/gpl.html

 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
   http://www.gnu.org/licenses/lgpl.html

 - Mozilla Public License Version 1.1 or later (the "MPL")
   http://www.mozilla.org/MPL/MPL-1.1.html

== END LICENSE ==

File Name: connector.py
	Connector for Python.
	
	Tested With:
	Standard:
		Python 2.3.3
	Zope:
		Zope Version: (Zope 2.8.1-final, python 2.3.5, linux2)
		Python Version: 2.3.5 (#4, Mar 10 2005, 01:40:25) 
			[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)]
		System Platform: linux2 

File Authors:
		Andrew Liu (andrew@liuholdings.com)
"""

"""
Author Notes (04 December 2005):
This module has gone through quite a few phases of change.  Obviously,
I am only supporting that part of the code that I use.  Initially
I had the upload directory as a part of zope (ie. uploading files
directly into Zope), before realising that there were too many 
complex intricacies within Zope to deal with.  Zope is one ugly piece
of code.  So I decided to complement Zope by an Apache server (which
I had running anyway, and doing nothing).  So I mapped all uploads
from an arbitrary server directory to an arbitrary web directory.
All the FCKeditor uploading occurred this way, and I didn't have to
stuff around with fiddling with Zope objects and the like (which are
terribly complex and something you don't want to do - trust me).

Maybe a Zope expert can touch up the Zope components.  In the end, 
I had FCKeditor loaded in Zope (probably a bad idea as well), and
I replaced the connector.py with an alias to a server module.
Right now, all Zope components will simple remain as is because
I've had enough of Zope.

See notes right at the end of this file for how I aliased out of Zope.

Anyway, most of you probably wont use Zope, so things are pretty
simple in that regard.

Typically, SERVER_DIR is the root of WEB_DIR (not necessarily).
Most definitely, SERVER_USERFILES_DIR points to WEB_USERFILES_DIR.
"""

import cgi
import re
import os
import string

"""
escape

Converts the special characters '<', '>', and '&'.

RFC 1866 specifies that these characters be represented
in HTML as &lt; &gt; and &amp; respectively. In Python
1.5 we use the new string.replace() function for speed.
"""
def escape(text, replace=string.replace):
    text = replace(text, '&', '&amp;') # must be done 1st
    text = replace(text, '<', '&lt;')
    text = replace(text, '>', '&gt;')
    text = replace(text, '"', '&quot;')
    return text

"""
getFCKeditorConnector

Creates a new instance of an FCKeditorConnector, and runs it
"""
def getFCKeditorConnector(context=None):
	# Called from Zope.  Passes the context through
	connector = FCKeditorConnector(context=context)
	return connector.run()


"""
FCKeditorRequest

A wrapper around the request object
Can handle normal CGI request, or a Zope request
Extend as required
"""
class FCKeditorRequest(object):
	def __init__(self, context=None):
		if (context is not None):
			r = context.REQUEST
		else:
			r = cgi.FieldStorage()
		self.context = context
		self.request = r

	def isZope(self):
		if (self.context is not None):
			return True
		return False

	def has_key(self, key):
		return self.request.has_key(key)

	def get(self, key, default=None):
		value = None
		if (self.isZope()):
			value = self.request.get(key, default)
		else:
			if key in self.request.keys():
				value = self.request[key].value
			else:
				value = default
		return value

"""
FCKeditorConnector

The connector class
"""
class FCKeditorConnector(object):
	# Configuration for FCKEditor
	# can point to another server here, if linked correctly
	#WEB_HOST = "http://127.0.0.1/" 
	WEB_HOST = ""
	SERVER_DIR = "/var/www/html/"

	WEB_USERFILES_FOLDER = WEB_HOST + "upload/"
	SERVER_USERFILES_FOLDER = SERVER_DIR + "upload/"

	# Allow access (Zope)
	__allow_access_to_unprotected_subobjects__ = 1
	# Class Attributes
	parentFolderRe = re.compile("[\/][^\/]+[\/]?$")

	"""
	Constructor
	"""
	def __init__(self, context=None):
		# The given root path will NOT be shown to the user
		# Only the userFilesPath will be shown

		# Instance Attributes
		self.context = context
		self.request = FCKeditorRequest(context=context)
		self.rootPath = self.SERVER_DIR
		self.userFilesFolder = self.SERVER_USERFILES_FOLDER
		self.webUserFilesFolder = self.WEB_USERFILES_FOLDER

		# Enables / Disables the connector
		self.enabled = False # Set to True to enable this connector

		# These are instance variables
		self.zopeRootContext = None
		self.zopeUploadContext = None

		# Copied from php module =)
		self.allowedExtensions = {
				"File": None,
				"Image": None,
				"Flash": None,
				"Media": None
				}
		self.deniedExtensions = {
				"File": [ "html","htm","php","php2","php3","php4","php5","phtml","pwml","inc","asp","aspx","ascx","jsp","cfm","cfc","pl","bat","exe","com","dll","vbs","js","reg","cgi","htaccess","asis" ],
				"Image": [ "html","htm","php","php2","php3","php4","php5","phtml","pwml","inc","asp","aspx","ascx","jsp","cfm","cfc","pl","bat","exe","com","dll","vbs","js","reg","cgi","htaccess","asis" ],
				"Flash": [ "html","htm","php","php2","php3","php4","php5","phtml","pwml","inc","asp","aspx","ascx","jsp","cfm","cfc","pl","bat","exe","com","dll","vbs","js","reg","cgi","htaccess","asis" ],
				"Media": [ "html","htm","php","php2","php3","php4","php5","phtml","pwml","inc","asp","aspx","ascx","jsp","cfm","cfc","pl","bat","exe","com","dll","vbs","js","reg","cgi","htaccess","asis" ]
				}

	"""
	Zope specific functions
	"""
	def isZope(self):
		# The context object is the zope object
		if (self.context is not None):
			return True
		return False

	def getZopeRootContext(self):
		if self.zopeRootContext is None:
			self.zopeRootContext = self.context.getPhysicalRoot()
		return self.zopeRootContext

	def getZopeUploadContext(self):
		if self.zopeUploadContext is None:
			folderNames = self.userFilesFolder.split("/")
			c = self.getZopeRootContext()
			for folderName in folderNames:
				if (folderName <> ""):
					c = c[folderName]
			self.zopeUploadContext = c
		return self.zopeUploadContext

	"""
	Generic manipulation functions
	"""
	def getUserFilesFolder(self):
		return self.userFilesFolder

	def getWebUserFilesFolder(self):
		return self.webUserFilesFolder

	def getAllowedExtensions(self, resourceType):
		return self.allowedExtensions[resourceType]

	def getDeniedExtensions(self, resourceType):
		return self.deniedExtensions[resourceType]

	def removeFromStart(self, string, char):
		return string.lstrip(char)

	def removeFromEnd(self, string, char):
		return string.rstrip(char)

	def convertToXmlAttribute(self, value):
		if (value is None):
			value = ""
		return escape(value)

	def convertToPath(self, path):
		if (path[-1] <> "/"):
			return path + "/"
		else:
			return path

	def getUrlFromPath(self, resourceType, path):
		if (resourceType is None) or (resourceType == ''):
			url = "%s%s" % (
					self.removeFromEnd(self.getUserFilesFolder(), '/'),
					path
					)
		else:
			url = "%s%s%s" % (
					self.getUserFilesFolder(),
					resourceType,
					path
					)
		return url

	def getWebUrlFromPath(self, resourceType, path):
		if (resourceType is None) or (resourceType == ''):
			url = "%s%s" % (
					self.removeFromEnd(self.getWebUserFilesFolder(), '/'),
					path
					)
		else:
			url = "%s%s%s" % (
					self.getWebUserFilesFolder(),
					resourceType,
					path
					)
		return url

	def removeExtension(self, fileName):
		index = fileName.rindex(".")
		newFileName = fileName[0:index]
		return newFileName

	def getExtension(self, fileName):
		index = fileName.rindex(".") + 1
		fileExtension = fileName[index:]
		return fileExtension
		
	def getParentFolder(self, folderPath):
		parentFolderPath = self.parentFolderRe.sub('', folderPath)
		return parentFolderPath
	
	"""
	serverMapFolder

	Purpose: works out the folder map on the server
	"""
	def serverMapFolder(self, resourceType, folderPath):
		# Get the resource type directory
		resourceTypeFolder = "%s%s/" % (
				self.getUserFilesFolder(),
				resourceType
				)
		# Ensure that the directory exists
		self.createServerFolder(resourceTypeFolder)

		# Return the resource type directory combined with the
		# required path
		return "%s%s" % (
				resourceTypeFolder,
				self.removeFromStart(folderPath, '/')
				)

	"""
	createServerFolder

	Purpose: physically creates a folder on the server
	"""
	def createServerFolder(self, folderPath):
		# Check if the parent exists
		parentFolderPath = self.getParentFolder(folderPath)
		if not(os.path.exists(parentFolderPath)):
			errorMsg = self.createServerFolder(parentFolderPath)
			if errorMsg is not None:
				return errorMsg
		# Check if this exists
		if not(os.path.exists(folderPath)):
			os.mkdir(folderPath)
			os.chmod(folderPath, 0755)
			errorMsg = None
		else:
			if os.path.isdir(folderPath):
				errorMsg = None
			else:
				raise "createServerFolder: Non-folder of same name already exists"
		return errorMsg


	"""
	getRootPath

	Purpose: returns the root path on the server
	"""
	def getRootPath(self):
		return self.rootPath
		
	"""
	setXmlHeaders

	Purpose: to prepare the headers for the xml to return
	"""
	def setXmlHeaders(self):
		#now = self.context.BS_get_now()
		#yesterday = now - 1
		self.setHeader("Content-Type", "text/xml")
		#self.setHeader("Expires", yesterday)
		#self.setHeader("Last-Modified", now)
		#self.setHeader("Cache-Control", "no-store, no-cache, must-revalidate")
		self.printHeaders()
		return

	def setHeader(self, key, value):
		if (self.isZope()):
			self.context.REQUEST.RESPONSE.setHeader(key, value)
		else:
			print "%s: %s" % (key, value)
		return

	def printHeaders(self):
		# For non-Zope requests, we need to print an empty line
		# to denote the end of headers
		if (not(self.isZope())):
			print ""

	"""
	createXmlFooter

	Purpose: returns the xml header
	"""
	def createXmlHeader(self, command, resourceType, currentFolder):
		self.setXmlHeaders()
		s = ""
		# Create the XML document header
		s += """<?xml version="1.0" encoding="utf-8" ?>"""
		# Create the main connector node
		s += """<Connector command="%s" resourceType="%s">""" % (
				command,
				resourceType
				)
		# Add the current folder node
		s += """<CurrentFolder path="%s" url="%s" />""" % (
				self.convertToXmlAttribute(currentFolder),
				self.convertToXmlAttribute(
					self.getWebUrlFromPath(
						resourceType, 
						currentFolder
						)
					),
				)
		return s

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -