Back to index

moin  1.9.0~rc2
fckoutput.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 """
00004 FCKeditor - The text editor for Internet - http://www.fckeditor.net
00005 Copyright (C) 2003-2009 Frederico Caldeira Knabben
00006 
00007 == BEGIN LICENSE ==
00008 
00009 Licensed under the terms of any of the following licenses at your
00010 choice:
00011 
00012 - GNU General Public License Version 2 or later (the "GPL")
00013 http://www.gnu.org/licenses/gpl.html
00014 
00015 - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
00016 http://www.gnu.org/licenses/lgpl.html
00017 
00018 - Mozilla Public License Version 1.1 or later (the "MPL")
00019 http://www.mozilla.org/MPL/MPL-1.1.html
00020 
00021 == END LICENSE ==
00022 
00023 Connector for Python (CGI and WSGI).
00024 
00025 """
00026 
00027 from time import gmtime, strftime
00028 import string
00029 
00030 def escape(text, replace=string.replace):
00031        """
00032        Converts the special characters '<', '>', and '&'.
00033 
00034        RFC 1866 specifies that these characters be represented
00035        in HTML as &lt; &gt; and &amp; respectively. In Python
00036        1.5 we use the new string.replace() function for speed.
00037        """
00038        text = replace(text, '&', '&amp;') # must be done 1st
00039        text = replace(text, '<', '&lt;')
00040        text = replace(text, '>', '&gt;')
00041        text = replace(text, '"', '&quot;')
00042        return text
00043 
00044 def convertToXmlAttribute(value):
00045        if (value is None):
00046               value = ""
00047        return escape(value)
00048 
00049 class BaseHttpMixin(object):
00050        def setHttpHeaders(self, content_type='text/xml'):
00051               "Purpose: to prepare the headers for the xml to return"
00052               # Prevent the browser from caching the result.
00053               # Date in the past
00054               self.setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT')
00055               # always modified
00056               self.setHeader('Last-Modified',strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime()))
00057               # HTTP/1.1
00058               self.setHeader('Cache-Control','no-store, no-cache, must-revalidate')
00059               self.setHeader('Cache-Control','post-check=0, pre-check=0')
00060               # HTTP/1.0
00061               self.setHeader('Pragma','no-cache')
00062 
00063               # Set the response format.
00064               self.setHeader( 'Content-Type', content_type + '; charset=utf-8' )
00065               return
00066 
00067 class BaseXmlMixin(object):
00068        def createXmlHeader(self, command, resourceType, currentFolder, url):
00069               "Purpose: returns the xml header"
00070               self.setHttpHeaders()
00071               # Create the XML document header
00072               s =  """<?xml version="1.0" encoding="utf-8" ?>"""
00073               # Create the main connector node
00074               s += """<Connector command="%s" resourceType="%s">""" % (
00075                             command,
00076                             resourceType
00077                             )
00078               # Add the current folder node
00079               s += """<CurrentFolder path="%s" url="%s" />""" % (
00080                             convertToXmlAttribute(currentFolder),
00081                             convertToXmlAttribute(url),
00082                             )
00083               return s
00084 
00085        def createXmlFooter(self):
00086               "Purpose: returns the xml footer"
00087               return """</Connector>"""
00088 
00089        def sendError(self, number, text):
00090               "Purpose: in the event of an error, return an xml based error"
00091               self.setHttpHeaders()
00092               return ("""<?xml version="1.0" encoding="utf-8" ?>""" +
00093                             """<Connector>""" +
00094                             self.sendErrorNode (number, text) +
00095                             """</Connector>""" )
00096 
00097        def sendErrorNode(self, number, text):
00098               if number != 1:
00099                      return """<Error number="%s" />""" % (number)
00100               else:
00101                      return """<Error number="%s" text="%s" />""" % (number, convertToXmlAttribute(text))
00102 
00103 class BaseHtmlMixin(object):
00104        def sendUploadResults( self, errorNo = 0, fileUrl = '', fileName = '', customMsg = '' ):
00105               self.setHttpHeaders("text/html")
00106               "This is the function that sends the results of the uploading process"
00107 
00108               "Minified version of the document.domain automatic fix script (#1919)."
00109               "The original script can be found at _dev/domain_fix_template.js"
00110               return """<script type="text/javascript">
00111                      (function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();
00112 
00113                      window.parent.OnUploadCompleted(%(errorNumber)s,"%(fileUrl)s","%(fileName)s","%(customMsg)s");
00114                      </script>""" % {
00115                      'errorNumber': errorNo,
00116                      'fileUrl': fileUrl.replace ('"', '\\"'),
00117                      'fileName': fileName.replace ( '"', '\\"' ) ,
00118                      'customMsg': customMsg.replace ( '"', '\\"' ),
00119                      }