这描述了如何嵌入 FCK 编辑器并启用图像上传。
首先,您需要编辑 fckconfig.js 以将图像上传 URL 更改为指向服务器内的某个 URL。
FCKConfig.ImageUploadURL = "/myapp/root/imageUploader";
这将指向服务器相对 URL 以接收上传。FCK 将使用使用 multipart/form-data 编码的 CGI 变量名称“NewFile”将上传的文件发送到该处理程序。不幸的是,您将不得不实现 /myapp/root/imageUploader,因为我认为 FCK 分发的东西不能轻易地适应其他框架。
imageUploader 应该提取 NewFile 并将其存储在服务器上的某个位置。/myapp/root/imageUploader 生成的响应应该模拟 /editor/.../fckoutput.py 中构造的 HTML。像这样的东西(气味模板格式)
{{env
whiff.content_type: "text/html",
whiff.headers: [
["Expires","Mon, 26 Jul 1997 05:00:00 GMT"],
["Cache-Control","no-store, no-cache, must-revalidate"],
["Cache-Control","post-check=0, pre-check=0"],
["Pragma","no-cache"]
]
/}}
<script>
//alert("!! RESPONSE RECIEVED");
errorNumber = 0;
fileUrl = "fileurl.png";
fileName = "filename.png";
customMsg = "";
window.parent.OnUploadCompleted(errorNumber, fileUrl, fileName, customMsg);
</script>
顶部的 {{env ...}} 表示内容类型和建议发送的 HTTP 标头。fileUrl 应该是用于在服务器上查找图像的 Url。
以下是获取生成 FCK 编辑器小部件的 html 片段的基本步骤。唯一棘手的部分是您必须将正确的客户端标识放入 os.environ - 这很丑陋,但这是 FCK 库现在的工作方式(我提交了错误报告)。
import fckeditor # you must have the fck editor python support installed to use this module
import os
inputName = "myInputName" # the name to use for the input element in the form
basePath = "/server/relative/path/to/fck/installation/" # the location of FCK static files
if basePath[-1:]!="/":
basePath+="/" # basepath must end in slash
oFCKeditor = fckeditor.FCKeditor(inputName)
oFCKeditor.BasePath = basePath
oFCKeditor.Height = 300 # the height in pixels of the editor
oFCKeditor.Value = "<h1>initial html to be editted</h1>"
os.environ["HTTP_USER_AGENT"] = "Mozilla/5.0 (Macintosh; U;..." # or whatever
# there must be some way to figure out the user agent in Django right?
htmlOut = oFCKeditor.Create()
# insert htmlOut into your page where you want the editor to appear
return htmlOut
以上未经测试,但它基于以下经过测试。
以下是使用 mod-wsgi 使用 FCK 编辑器的方法: 从技术上讲,它使用了 WHIFF 的几个功能(请参阅
WHIFF.sourceforge.net)——实际上它是 WHIFF 发行版的一部分——但 WHIFF 功能很容易被删除。
我不知道如何在 Django 中安装它,但如果 Django 允许轻松安装 wsgi 应用程序,你应该可以做到。
注意:FCK 允许客户端向 HTML 页面注入几乎任何东西——您需要过滤返回值以防止恶意攻击。(例如:有关如何执行此操作的示例,请参见 whiff.middleware.TestSafeHTML 中间件)。
"""
引入 FCK 编辑器输入元素。(需要 FCKeditor http://www.fckeditor.net/)。
注意:这个实现可以生成包含代码注入攻击的值,如果你
不要过滤为邪恶标签和值生成的输出。
"""
import fckeditor # 你必须安装 fck 编辑器 python 支持才能使用这个模块
从 whiff.middleware 导入杂项
导入操作系统
FCKInput 类(misc.utility):
def __init__(self,
inputName, # 输入元素的名称
basePath, # 用于 FCK HTTP 安装的服务器相对 URL 根
value = ""): # 输入的初始值
self.inputName = 输入名称
self.basePath = basePath
自我价值=价值
def __call__(self, env, start_response):
inputName = self.param_value(self.inputName, env).strip()
basePath = self.param_value(self.basePath, env).strip()
如果 basePath[-1:]!="/":
基本路径+="/"
value = self.param_value(self.value, env)
oFCKeditor = fckeditor.FCKeditor(inputName)
oFCKeditor.BasePath = basePath
oFCKeditor.Height = 300 # 这应该是必需的!
oFCKeditor.Value = 值
# 破解 fck python 库中的一个错误:需要将用户代理放入 os.environ
# XXX 这个 hack 对于多线程服务器是不安全的(理论上)...需要锁定 os.env
os_environ = os.environ
new_os_env = os_environ.copy()
new_os_env.update(env)
尝试:
os.environ = new_os_env
htmlOut = oFCKeditor.Create()
最后:
# 恢复旧的 os.environ
os.environ = os_environ
start_response("200 OK", [('Content-Type', 'text/html')])
返回 [htmlOut]
__middleware__ = FCKInput
定义测试():
环境 = {
“HTTP_USER_AGENT”:
“Mozilla/5.0(Macintosh;U;Intel Mac OS X;en-US;rv:1.8.1.14)Gecko/20080404 Firefox/2.0.0.14”
}
f = FCKInput("INPUTNAME", "/MY/BASE/PATH", "开始的HTML值")
r = f(env, misc.ignore)
打印“测试结果”
打印“”。加入(列表(r))
如果 __name__=="__main__":
测试()
例如,在
http://aaron.oirt.rutgers.edu/myapp/docs/W1500.whyIsWhiffCool看到这个工作。
顺便说一句:谢谢。无论如何,我需要调查一下。