~ubuntu-branches/ubuntu/intrepid/moin/intrepid-updates

« back to all changes in this revision

Viewing changes to wiki/htdocs/applets/FCKeditor/editor/filemanager/browser/default/connectors/py/connector.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-02-14 16:09:24 UTC
  • mfrom: (0.2.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20060214160924-fyrx3gvknzqvt4vj
Tags: 1.5.2-1ubuntu1
Drop python2.3 package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""
 
4
FCKeditor - The text editor for internet
 
5
Copyright (C) 2003-2005 Frederico Caldeira Knabben
 
6
 
 
7
Licensed under the terms of the GNU Lesser General Public License:
 
8
                http://www.opensource.org/licenses/lgpl-license.php
 
9
 
 
10
For further information visit:
 
11
                http://www.fckeditor.net/
 
12
 
 
13
"Support Open Source software. What about a donation today?"
 
14
 
 
15
File Name: connector.py
 
16
        Connector for Python.
 
17
        
 
18
        Tested With:
 
19
        Standard:
 
20
                Python 2.3.3
 
21
        Zope:
 
22
                Zope Version: (Zope 2.8.1-final, python 2.3.5, linux2)
 
23
                Python Version: 2.3.5 (#4, Mar 10 2005, 01:40:25) 
 
24
                        [GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)]
 
25
                System Platform: linux2 
 
26
 
 
27
File Authors:
 
28
                Andrew Liu (andrew@liuholdings.com)
 
29
"""
 
30
 
 
31
"""
 
32
Author Notes (04 December 2005):
 
33
This module has gone through quite a few phases of change.  Obviously,
 
34
I am only supporting that part of the code that I use.  Initially
 
35
I had the upload directory as a part of zope (ie. uploading files
 
36
directly into Zope), before realising that there were too many 
 
37
complex intricacies within Zope to deal with.  Zope is one ugly piece
 
38
of code.  So I decided to complement Zope by an Apache server (which
 
39
I had running anyway, and doing nothing).  So I mapped all uploads
 
40
from an arbitrary server directory to an arbitrary web directory.
 
41
All the FCKeditor uploading occurred this way, and I didn't have to
 
42
stuff around with fiddling with Zope objects and the like (which are
 
43
terribly complex and something you don't want to do - trust me).
 
44
 
 
45
Maybe a Zope expert can touch up the Zope components.  In the end, 
 
46
I had FCKeditor loaded in Zope (probably a bad idea as well), and
 
47
I replaced the connector.py with an alias to a server module.
 
48
Right now, all Zope components will simple remain as is because
 
49
I've had enough of Zope.
 
50
 
 
51
See notes right at the end of this file for how I aliased out of Zope.
 
52
 
 
53
Anyway, most of you probably wont use Zope, so things are pretty
 
54
simple in that regard.
 
55
 
 
56
Typically, SERVER_DIR is the root of WEB_DIR (not necessarily).
 
57
Most definitely, SERVER_USERFILES_DIR points to WEB_USERFILES_DIR.
 
58
"""
 
59
 
 
60
import cgi
 
61
import re
 
62
import os
 
63
import string
 
64
 
 
65
"""
 
66
escape
 
67
 
 
68
Converts the special characters '<', '>', and '&'.
 
69
 
 
70
RFC 1866 specifies that these characters be represented
 
71
in HTML as &lt; &gt; and &amp; respectively. In Python
 
72
1.5 we use the new string.replace() function for speed.
 
73
"""
 
74
def escape(text, replace=string.replace):
 
75
    text = replace(text, '&', '&amp;') # must be done 1st
 
76
    text = replace(text, '<', '&lt;')
 
77
    text = replace(text, '>', '&gt;')
 
78
    text = replace(text, '"', '&quot;')
 
79
    return text
 
80
 
 
81
"""
 
82
getFCKeditorConnector
 
83
 
 
84
Creates a new instance of an FCKeditorConnector, and runs it
 
85
"""
 
86
def getFCKeditorConnector(context=None):
 
87
        # Called from Zope.  Passes the context through
 
88
        connector = FCKeditorConnector(context=context)
 
89
        return connector.run()
 
90
 
 
91
 
 
92
"""
 
93
FCKeditorRequest
 
94
 
 
95
A wrapper around the request object
 
96
Can handle normal CGI request, or a Zope request
 
97
Extend as required
 
98
"""
 
99
class FCKeditorRequest(object):
 
100
        def __init__(self, context=None):
 
101
                if (context is not None):
 
102
                        r = context.REQUEST
 
103
                else:
 
104
                        r = cgi.FieldStorage()
 
105
                self.context = context
 
106
                self.request = r
 
107
 
 
108
        def isZope(self):
 
109
                if (self.context is not None):
 
110
                        return True
 
111
                return False
 
112
 
 
113
        def has_key(self, key):
 
114
                return self.request.has_key(key)
 
115
 
 
116
        def get(self, key, default=None):
 
117
                value = None
 
118
                if (self.isZope()):
 
119
                        value = self.request.get(key, default)
 
120
                else:
 
121
                        if key in self.request.keys():
 
122
                                value = self.request[key].value
 
123
                        else:
 
124
                                value = default
 
125
                return value
 
126
 
 
127
"""
 
128
FCKeditorConnector
 
129
 
 
130
The connector class
 
131
"""
 
132
class FCKeditorConnector(object):
 
133
        # Configuration for FCKEditor
 
134
        # can point to another server here, if linked correctly
 
135
        #WEB_HOST = "http://127.0.0.1/" 
 
136
        WEB_HOST = ""
 
137
        SERVER_DIR = "/var/www/html/"
 
138
 
 
139
        WEB_USERFILES_FOLDER = WEB_HOST + "upload/"
 
140
        SERVER_USERFILES_FOLDER = SERVER_DIR + "upload/"
 
141
 
 
142
        # Allow access (Zope)
 
143
        __allow_access_to_unprotected_subobjects__ = 1
 
144
        # Class Attributes
 
145
        parentFolderRe = re.compile("[\/][^\/]+[\/]?$")
 
146
 
 
147
        """
 
148
        Constructor
 
149
        """
 
150
        def __init__(self, context=None):
 
151
                # The given root path will NOT be shown to the user
 
152
                # Only the userFilesPath will be shown
 
153
 
 
154
                # Instance Attributes
 
155
                self.context = context
 
156
                self.request = FCKeditorRequest(context=context)
 
157
                self.rootPath = self.SERVER_DIR
 
158
                self.userFilesFolder = self.SERVER_USERFILES_FOLDER
 
159
                self.webUserFilesFolder = self.WEB_USERFILES_FOLDER
 
160
 
 
161
                # Enables / Disables the connector
 
162
                self.enabled = False # Set to True to enable this connector
 
163
 
 
164
                # These are instance variables
 
165
                self.zopeRootContext = None
 
166
                self.zopeUploadContext = None
 
167
 
 
168
                # Copied from php module =)
 
169
                self.allowedExtensions = {
 
170
                                "File": None,
 
171
                                "Image": None,
 
172
                                "Flash": None,
 
173
                                "Media": None
 
174
                                }
 
175
                self.deniedExtensions = {
 
176
                                "File": [ "php", "php3", "php5", "phtml", "asp", "aspx", "ascx", "jsp", "cfm", "cfc", "pl", "bat", "exe", "dll", "reg", "cgi" ],
 
177
                                "Image": [ "php", "php3", "php5", "phtml", "asp", "aspx", "ascx", "jsp", "cfm", "cfc", "pl", "bat", "exe", "dll", "reg", "cgi" ],
 
178
                                "Flash": [ "php", "php3", "php5", "phtml", "asp", "aspx", "ascx", "jsp", "cfm", "cfc", "pl", "bat", "exe", "dll", "reg", "cgi" ],
 
179
                                "Media": [ "php", "php3", "php5", "phtml", "asp", "aspx", "ascx", "jsp", "cfm", "cfc", "pl", "bat", "exe", "dll", "reg", "cgi" ]
 
180
                                }
 
181
 
 
182
        """
 
183
        Zope specific functions
 
184
        """
 
185
        def isZope(self):
 
186
                # The context object is the zope object
 
187
                if (self.context is not None):
 
188
                        return True
 
189
                return False
 
190
 
 
191
        def getZopeRootContext(self):
 
192
                if self.zopeRootContext is None:
 
193
                        self.zopeRootContext = self.context.getPhysicalRoot()
 
194
                return self.zopeRootContext
 
195
 
 
196
        def getZopeUploadContext(self):
 
197
                if self.zopeUploadContext is None:
 
198
                        folderNames = self.userFilesFolder.split("/")
 
199
                        c = self.getZopeRootContext()
 
200
                        for folderName in folderNames:
 
201
                                if (folderName <> ""):
 
202
                                        c = c[folderName]
 
203
                        self.zopeUploadContext = c
 
204
                return self.zopeUploadContext
 
205
 
 
206
        """
 
207
        Generic manipulation functions
 
208
        """
 
209
        def getUserFilesFolder(self):
 
210
                return self.userFilesFolder
 
211
 
 
212
        def getWebUserFilesFolder(self):
 
213
                return self.webUserFilesFolder
 
214
 
 
215
        def getAllowedExtensions(self, resourceType):
 
216
                return self.allowedExtensions[resourceType]
 
217
 
 
218
        def getDeniedExtensions(self, resourceType):
 
219
                return self.deniedExtensions[resourceType]
 
220
 
 
221
        def removeFromStart(self, string, char):
 
222
                return string.lstrip(char)
 
223
 
 
224
        def removeFromEnd(self, string, char):
 
225
                return string.rstrip(char)
 
226
 
 
227
        def convertToXmlAttribute(self, value):
 
228
                if (value is None):
 
229
                        value = ""
 
230
                return escape(value)
 
231
 
 
232
        def convertToPath(self, path):
 
233
                if (path[-1] <> "/"):
 
234
                        return path + "/"
 
235
                else:
 
236
                        return path
 
237
 
 
238
        def getUrlFromPath(self, resourceType, path):
 
239
                if (resourceType is None) or (resourceType == ''):
 
240
                        url = "%s%s" % (
 
241
                                        self.removeFromEnd(self.getUserFilesFolder(), '/'),
 
242
                                        path
 
243
                                        )
 
244
                else:
 
245
                        url = "%s%s%s" % (
 
246
                                        self.getUserFilesFolder(),
 
247
                                        resourceType,
 
248
                                        path
 
249
                                        )
 
250
                return url
 
251
 
 
252
        def getWebUrlFromPath(self, resourceType, path):
 
253
                if (resourceType is None) or (resourceType == ''):
 
254
                        url = "%s%s" % (
 
255
                                        self.removeFromEnd(self.getWebUserFilesFolder(), '/'),
 
256
                                        path
 
257
                                        )
 
258
                else:
 
259
                        url = "%s%s%s" % (
 
260
                                        self.getWebUserFilesFolder(),
 
261
                                        resourceType,
 
262
                                        path
 
263
                                        )
 
264
                return url
 
265
 
 
266
        def removeExtension(self, fileName):
 
267
                index = fileName.rindex(".")
 
268
                newFileName = fileName[0:index]
 
269
                return newFileName
 
270
 
 
271
        def getExtension(self, fileName):
 
272
                index = fileName.rindex(".") + 1
 
273
                fileExtension = fileName[index:]
 
274
                return fileExtension
 
275
                
 
276
        def getParentFolder(self, folderPath):
 
277
                parentFolderPath = self.parentFolderRe.sub('', folderPath)
 
278
                return parentFolderPath
 
279
        
 
280
        """
 
281
        serverMapFolder
 
282
 
 
283
        Purpose: works out the folder map on the server
 
284
        """
 
285
        def serverMapFolder(self, resourceType, folderPath):
 
286
                # Get the resource type directory
 
287
                resourceTypeFolder = "%s%s/" % (
 
288
                                self.getUserFilesFolder(),
 
289
                                resourceType
 
290
                                )
 
291
                # Ensure that the directory exists
 
292
                self.createServerFolder(resourceTypeFolder)
 
293
 
 
294
                # Return the resource type directory combined with the
 
295
                # required path
 
296
                return "%s%s" % (
 
297
                                resourceTypeFolder,
 
298
                                self.removeFromStart(folderPath, '/')
 
299
                                )
 
300
 
 
301
        """
 
302
        createServerFolder
 
303
 
 
304
        Purpose: physically creates a folder on the server
 
305
        """
 
306
        def createServerFolder(self, folderPath):
 
307
                # Check if the parent exists
 
308
                parentFolderPath = self.getParentFolder(folderPath)
 
309
                if not(os.path.exists(parentFolderPath)):
 
310
                        errorMsg = self.createServerFolder(parentFolderPath)
 
311
                        if errorMsg is not None:
 
312
                                return errorMsg
 
313
                # Check if this exists
 
314
                if not(os.path.exists(folderPath)):
 
315
                        os.mkdir(folderPath)
 
316
                        os.chmod(folderPath, 0755)
 
317
                        errorMsg = None
 
318
                else:
 
319
                        if os.path.isdir(folderPath):
 
320
                                errorMsg = None
 
321
                        else:
 
322
                                raise "createServerFolder: Non-folder of same name already exists"
 
323
                return errorMsg
 
324
 
 
325
 
 
326
        """
 
327
        getRootPath
 
328
 
 
329
        Purpose: returns the root path on the server
 
330
        """
 
331
        def getRootPath(self):
 
332
                return self.rootPath
 
333
                
 
334
        """
 
335
        setXmlHeaders
 
336
 
 
337
        Purpose: to prepare the headers for the xml to return
 
338
        """
 
339
        def setXmlHeaders(self):
 
340
                #now = self.context.BS_get_now()
 
341
                #yesterday = now - 1
 
342
                self.setHeader("Content-Type", "text/xml")
 
343
                #self.setHeader("Expires", yesterday)
 
344
                #self.setHeader("Last-Modified", now)
 
345
                #self.setHeader("Cache-Control", "no-store, no-cache, must-revalidate")
 
346
                self.printHeaders()
 
347
                return
 
348
 
 
349
        def setHeader(self, key, value):
 
350
                if (self.isZope()):
 
351
                        self.context.REQUEST.RESPONSE.setHeader(key, value)
 
352
                else:
 
353
                        print "%s: %s" % (key, value)
 
354
                return
 
355
 
 
356
        def printHeaders(self):
 
357
                # For non-Zope requests, we need to print an empty line
 
358
                # to denote the end of headers
 
359
                if (not(self.isZope())):
 
360
                        print ""
 
361
 
 
362
        """
 
363
        createXmlFooter
 
364
 
 
365
        Purpose: returns the xml header
 
366
        """
 
367
        def createXmlHeader(self, command, resourceType, currentFolder):
 
368
                self.setXmlHeaders()
 
369
                s = ""
 
370
                # Create the XML document header
 
371
                s += """<?xml version="1.0" encoding="utf-8" ?>"""
 
372
                # Create the main connector node
 
373
                s += """<Connector command="%s" resourceType="%s">""" % (
 
374
                                command,
 
375
                                resourceType
 
376
                                )
 
377
                # Add the current folder node
 
378
                s += """<CurrentFolder path="%s" url="%s" />""" % (
 
379
                                self.convertToXmlAttribute(currentFolder),
 
380
                                self.convertToXmlAttribute(
 
381
                                        self.getWebUrlFromPath(
 
382
                                                resourceType, 
 
383
                                                currentFolder
 
384
                                                )
 
385
                                        ),
 
386
                                )
 
387
                return s
 
388
 
 
389
        """
 
390
        createXmlFooter
 
391
 
 
392
        Purpose: returns the xml footer
 
393
        """
 
394
        def createXmlFooter(self):
 
395
                s = """</Connector>"""
 
396
                return s
 
397
 
 
398
        """
 
399
        sendError
 
400
 
 
401
        Purpose: in the event of an error, return an xml based error
 
402
        """
 
403
        def sendError(self, number, text):
 
404
                self.setXmlHeaders()
 
405
                s = ""
 
406
                # Create the XML document header
 
407
                s += """<?xml version="1.0" encoding="utf-8" ?>"""
 
408
                s += """<Connector>"""
 
409
                s += """<Error number="%s" text="%s" />""" % (number, text)
 
410
                s += """</Connector>"""
 
411
                return s
 
412
 
 
413
        """
 
414
        getFolders
 
415
 
 
416
        Purpose: command to recieve a list of folders
 
417
        """
 
418
        def getFolders(self, resourceType, currentFolder):
 
419
                if (self.isZope()):
 
420
                        return self.getZopeFolders(resourceType, currentFolder)
 
421
                else:
 
422
                        return self.getNonZopeFolders(resourceType, currentFolder)
 
423
 
 
424
        def getZopeFolders(self, resourceType, currentFolder):
 
425
                # Open the folders node
 
426
                s = ""
 
427
                s += """<Folders>"""
 
428
                zopeFolder = self.findZopeFolder(resourceType, currentFolder)
 
429
                for (name, o) in zopeFolder.objectItems(["Folder"]):
 
430
                        s += """<Folder name="%s" />""" % (
 
431
                                        self.convertToXmlAttribute(name)
 
432
                                        )
 
433
                # Close the folders node
 
434
                s += """</Folders>"""
 
435
                return s
 
436
 
 
437
        def getNonZopeFolders(self, resourceType, currentFolder):
 
438
                # Map the virtual path to our local server
 
439
                serverPath = self.serverMapFolder(resourceType, currentFolder)
 
440
                # Open the folders node
 
441
                s = ""
 
442
                s += """<Folders>"""
 
443
                for someObject in os.listdir(serverPath):
 
444
                        someObjectPath = os.path.join(serverPath, someObject)
 
445
                        if os.path.isdir(someObjectPath):
 
446
                                s += """<Folder name="%s" />""" % (
 
447
                                                self.convertToXmlAttribute(someObject)
 
448
                                                )
 
449
                # Close the folders node
 
450
                s += """</Folders>"""
 
451
                return s
 
452
                
 
453
        """
 
454
        getFoldersAndFiles
 
455
 
 
456
        Purpose: command to recieve a list of folders and files
 
457
        """
 
458
        def getFoldersAndFiles(self, resourceType, currentFolder):
 
459
                if (self.isZope()):
 
460
                        return self.getZopeFoldersAndFiles(resourceType, currentFolder)
 
461
                else:
 
462
                        return self.getNonZopeFoldersAndFiles(resourceType, currentFolder)
 
463
 
 
464
        def getNonZopeFoldersAndFiles(self, resourceType, currentFolder):
 
465
                # Map the virtual path to our local server
 
466
                serverPath = self.serverMapFolder(resourceType, currentFolder)
 
467
                # Open the folders / files node
 
468
                folders = """<Folders>"""
 
469
                files = """<Files>"""
 
470
                for someObject in os.listdir(serverPath):
 
471
                        someObjectPath = os.path.join(serverPath, someObject)
 
472
                        if os.path.isdir(someObjectPath):
 
473
                                folders += """<Folder name="%s" />""" % (
 
474
                                                self.convertToXmlAttribute(someObject)
 
475
                                                )
 
476
                        elif os.path.isfile(someObjectPath):
 
477
                                size = os.path.getsize(someObjectPath)
 
478
                                files += """<File name="%s" size="%s" />""" % (
 
479
                                                self.convertToXmlAttribute(someObject),
 
480
                                                os.path.getsize(someObjectPath)
 
481
                                                )
 
482
                # Close the folders / files node
 
483
                folders += """</Folders>"""
 
484
                files += """</Files>"""
 
485
                # Return it
 
486
                s = folders + files
 
487
                return s
 
488
 
 
489
        def getZopeFoldersAndFiles(self, resourceType, currentFolder):
 
490
                folders = self.getZopeFolders(resourceType, currentFolder)
 
491
                files = self.getZopeFiles(resourceType, currentFolder)
 
492
                s = folders + files
 
493
                return s
 
494
 
 
495
        def getZopeFiles(self, resourceType, currentFolder):
 
496
                # Open the files node
 
497
                s = ""
 
498
                s += """<Files>"""
 
499
                zopeFolder = self.findZopeFolder(resourceType, currentFolder)
 
500
                for (name, o) in zopeFolder.objectItems(["File","Image"]):
 
501
                        s += """<File name="%s" size="%s" />""" % (
 
502
                                        self.convertToXmlAttribute(name),
 
503
                                        ((o.get_size() / 1024) + 1)
 
504
                                        )
 
505
                # Close the files node
 
506
                s += """</Files>"""
 
507
                return s
 
508
                
 
509
        def findZopeFolder(self, resourceType, folderName):
 
510
                # returns the context of the resource / folder
 
511
                zopeFolder = self.getZopeUploadContext()
 
512
                folderName = self.removeFromStart(folderName, "/")
 
513
                folderName = self.removeFromEnd(folderName, "/")
 
514
                if (resourceType <> ""):
 
515
                        try:
 
516
                                zopeFolder = zopeFolder[resourceType]
 
517
                        except:
 
518
                                zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=resourceType, title=resourceType)
 
519
                                zopeFolder = zopeFolder[resourceType]
 
520
                if (folderName <> ""):
 
521
                        folderNames = folderName.split("/")
 
522
                        for folderName in folderNames:
 
523
                                zopeFolder = zopeFolder[folderName]
 
524
                return zopeFolder
 
525
 
 
526
        """
 
527
        createFolder
 
528
 
 
529
        Purpose: command to create a new folder
 
530
        """
 
531
        def createFolder(self, resourceType, currentFolder):
 
532
                if (self.isZope()):
 
533
                        return self.createZopeFolder(resourceType, currentFolder)
 
534
                else:
 
535
                        return self.createNonZopeFolder(resourceType, currentFolder)
 
536
 
 
537
        def createZopeFolder(self, resourceType, currentFolder):
 
538
                # Find out where we are
 
539
                zopeFolder = self.findZopeFolder(resourceType, currentFolder)
 
540
                errorNo = 0
 
541
                errorMsg = ""
 
542
                if self.request.has_key("NewFolderName"):
 
543
                        newFolder = self.request.get("NewFolderName", None)
 
544
                        zopeFolder.manage_addProduct["OFSP"].manage_addFolder(id=newFolder, title=newFolder)
 
545
                else:
 
546
                        errorNo = 102
 
547
                error = """<Error number="%s" originalDescription="%s" />""" % (
 
548
                                errorNo,
 
549
                                self.convertToXmlAttribute(errorMsg)
 
550
                                )
 
551
                return error
 
552
 
 
553
        def createNonZopeFolder(self, resourceType, currentFolder):
 
554
                errorNo = 0
 
555
                errorMsg = ""
 
556
                if self.request.has_key("NewFolderName"):
 
557
                        newFolder = self.request.get("NewFolderName", None)
 
558
                        currentFolderPath = self.serverMapFolder(
 
559
                                        resourceType, 
 
560
                                        currentFolder
 
561
                                        )
 
562
                        try:
 
563
                                newFolderPath = currentFolderPath + newFolder
 
564
                                errorMsg = self.createServerFolder(newFolderPath)
 
565
                                if (errorMsg is not None):
 
566
                                        errorNo = 110
 
567
                        except:
 
568
                                errorNo = 103
 
569
                else:
 
570
                        errorNo = 102
 
571
                error = """<Error number="%s" originalDescription="%s" />""" % (
 
572
                                errorNo,
 
573
                                self.convertToXmlAttribute(errorMsg)
 
574
                                )
 
575
                return error
 
576
 
 
577
        """
 
578
        getFileName
 
579
 
 
580
        Purpose: helper function to extrapolate the filename
 
581
        """
 
582
        def getFileName(self, filename):
 
583
                for splitChar in ["/", "\\"]:
 
584
                        array = filename.split(splitChar)
 
585
                        if (len(array) > 1):
 
586
                                filename = array[-1]
 
587
                return filename
 
588
 
 
589
        """
 
590
        fileUpload
 
591
 
 
592
        Purpose: command to upload files to server
 
593
        """
 
594
        def fileUpload(self, resourceType, currentFolder):
 
595
                if (self.isZope()):
 
596
                        return self.zopeFileUpload(resourceType, currentFolder)
 
597
                else:
 
598
                        return self.nonZopeFileUpload(resourceType, currentFolder)
 
599
 
 
600
        def zopeFileUpload(self, resourceType, currentFolder, count=None):
 
601
                zopeFolder = self.findZopeFolder(resourceType, currentFolder)
 
602
                file = self.request.get("NewFile", None)
 
603
                fileName = self.getFileName(file.filename)
 
604
                fileNameOnly = self.removeExtension(fileName)
 
605
                fileExtension = self.getExtension(fileName).lower()
 
606
                if (count):
 
607
                        nid = "%s.%s.%s" % (fileNameOnly, count, fileExtension)
 
608
                else:
 
609
                        nid = fileName
 
610
                title = nid
 
611
                try:
 
612
                        zopeFolder.manage_addProduct['OFSP'].manage_addFile(
 
613
                                        id=nid,
 
614
                                        title=title,
 
615
                                        file=file.read()
 
616
                                        )
 
617
                except:
 
618
                        if (count):
 
619
                                count += 1
 
620
                        else:
 
621
                                count = 1
 
622
                        self.zopeFileUpload(resourceType, currentFolder, count)
 
623
                return
 
624
                
 
625
        def nonZopeFileUpload(self, resourceType, currentFolder):
 
626
                errorNo = 0
 
627
                errorMsg = ""
 
628
                if self.request.has_key("NewFile"):
 
629
                        # newFile has all the contents we need
 
630
                        newFile = self.request.get("NewFile", "")
 
631
                        # Get the file name
 
632
                        newFileName = newFile.filename
 
633
                        newFileNameOnly = self.removeExtension(newFileName)
 
634
                        newFileExtension = self.getExtension(newFileName).lower()
 
635
                        allowedExtensions = self.getAllowedExtensions(resourceType)
 
636
                        deniedExtensions = self.getDeniedExtensions(resourceType)
 
637
                        if (allowedExtensions is not None):
 
638
                                # Check for allowed
 
639
                                isAllowed = False
 
640
                                if (newFileExtension in allowedExtensions):
 
641
                                        isAllowed = True
 
642
                        elif (deniedExtensions is not None):
 
643
                                # Check for denied
 
644
                                isAllowed = True
 
645
                                if (newFileExtension in deniedExtensions):
 
646
                                        isAllowed = False
 
647
                        else:
 
648
                                # No extension limitations
 
649
                                isAllowed = True
 
650
 
 
651
                        if (isAllowed):
 
652
                                if (self.isZope()):
 
653
                                        # Upload into zope
 
654
                                        self.zopeFileUpload(resourceType, currentFolder)
 
655
                                else:
 
656
                                        # Upload to operating system
 
657
                                        # Map the virtual path to the local server path
 
658
                                        currentFolderPath = self.serverMapFolder(
 
659
                                                        resourceType, 
 
660
                                                        currentFolder
 
661
                                                        )
 
662
                                        i = 0
 
663
                                        while (True):
 
664
                                                newFilePath = "%s%s" % (
 
665
                                                                currentFolderPath,
 
666
                                                                newFileName
 
667
                                                                )
 
668
                                                if os.path.exists(newFilePath):
 
669
                                                        i += 1
 
670
                                                        newFilePath = "%s%s(%s).%s" % (
 
671
                                                                        currentFolderPath,
 
672
                                                                        newFileNameOnly,
 
673
                                                                        i,
 
674
                                                                        newFileExtension
 
675
                                                                        )
 
676
                                                        errorNo = 201
 
677
                                                        break
 
678
                                                else:
 
679
                                                        fileHandle = open(newFilePath,'w')
 
680
                                                        linecount = 0
 
681
                                                        while (1):
 
682
                                                                #line = newFile.file.readline()
 
683
                                                                line = newFile.readline()
 
684
                                                                if not line: break
 
685
                                                                fileHandle.write("%s" % line)
 
686
                                                                linecount += 1
 
687
                                                        os.chmod(newFilePath, 0777)
 
688
                                                        break
 
689
                        else:
 
690
                                newFileName = "Extension not allowed"
 
691
                                errorNo = 203
 
692
                else:
 
693
                        newFileName = "No File"
 
694
                        errorNo = 202
 
695
        
 
696
                string = """
 
697
<script type="text/javascript">
 
698
window.parent.frames["frmUpload"].OnUploadCompleted(%s,"%s");
 
699
</script>
 
700
                                """ % (
 
701
                                                errorNo,
 
702
                                                newFileName.replace('"',"'")
 
703
                                                )
 
704
                return string
 
705
 
 
706
        def run(self):
 
707
                s = ""
 
708
                try:
 
709
                        # Check if this is disabled
 
710
                        if not(self.enabled):
 
711
                                return self.sendError(1, "This connector is disabled.  Please check the connector configurations and try again")
 
712
                        # Make sure we have valid inputs
 
713
                        if not(
 
714
                                        (self.request.has_key("Command")) and 
 
715
                                        (self.request.has_key("Type")) and 
 
716
                                        (self.request.has_key("CurrentFolder"))
 
717
                                        ):
 
718
                                return 
 
719
                        # Get command
 
720
                        command = self.request.get("Command", None)
 
721
                        # Get resource type
 
722
                        resourceType = self.request.get("Type", None)
 
723
                        # folder syntax must start and end with "/"
 
724
                        currentFolder = self.request.get("CurrentFolder", None)
 
725
                        if (currentFolder[-1] <> "/"):
 
726
                                currentFolder += "/"
 
727
                        if (currentFolder[0] <> "/"):
 
728
                                currentFolder = "/" + currentFolder
 
729
                        # Check for invalid paths
 
730
                        if (".." in currentFolder):
 
731
                                return self.sendError(102, "")
 
732
                        # File upload doesn't have to return XML, so intercept
 
733
                        # her:e
 
734
                        if (command == "FileUpload"):
 
735
                                return self.fileUpload(resourceType, currentFolder)
 
736
                        # Begin XML
 
737
                        s += self.createXmlHeader(command, resourceType, currentFolder)
 
738
                        # Execute the command
 
739
                        if (command == "GetFolders"):
 
740
                                f = self.getFolders
 
741
                        elif (command == "GetFoldersAndFiles"):
 
742
                                f = self.getFoldersAndFiles
 
743
                        elif (command == "CreateFolder"):
 
744
                                f = self.createFolder
 
745
                        else:
 
746
                                f = None
 
747
                        if (f is not None):
 
748
                                s += f(resourceType, currentFolder)
 
749
                        s += self.createXmlFooter()
 
750
                except Exception, e:
 
751
                        s = "ERROR: %s" % e
 
752
                return s
 
753
                        
 
754
# Running from command line
 
755
if __name__ == '__main__':
 
756
        # To test the output, uncomment the standard headers
 
757
        #print "Content-Type: text/html"
 
758
        #print ""
 
759
        print getFCKeditorConnector()
 
760
 
 
761
"""
 
762
Running from zope, you will need to modify this connector. 
 
763
If you have uploaded the FCKeditor into Zope (like me), you need to 
 
764
move this connector out of Zope, and replace the "connector" with an
 
765
alias as below.  The key to it is to pass the Zope context in, as
 
766
we then have a like to the Zope context.
 
767
 
 
768
## Script (Python) "connector.py"
 
769
##bind container=container
 
770
##bind context=context
 
771
##bind namespace=
 
772
##bind script=script
 
773
##bind subpath=traverse_subpath
 
774
##parameters=*args, **kws
 
775
##title=ALIAS
 
776
##
 
777
import Products.connector as connector
 
778
return connector.getFCKeditorConnector(context=context).run()
 
779
"""
 
780
                        
 
781