-
Notifications
You must be signed in to change notification settings - Fork 0
/
_pathhandler.py
360 lines (327 loc) · 15.6 KB
/
_pathhandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
################################################################################
# _pathhandler.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
from OFS.CopySupport import absattr
import copy
# Product Imports.
import _blobfields
import _confmanager
import _fileutil
import _globals
# ------------------------------------------------------------------------------
# _pathhandler.validateId:
#
# Validates id against list of possible declarative id.
# ------------------------------------------------------------------------------
def validateId(self, id, REQUEST):
langs = []
lang = REQUEST.get( 'lang')
if lang is None:
for lang in self.getLanguages():
req={ 'lang': lang, 'preview': REQUEST.get('preview','')}
decl_id = self.getDeclId( req)
if id == decl_id:
langs.append( lang)
if len( langs) == 1:
self.REQUEST.set( 'lang', langs[0])
else:
decl_id = self.getDeclId( REQUEST)
if id == decl_id:
langs.append( lang)
if len( langs) > 0:
return True
return False
# ------------------------------------------------------------------------------
# _pathhandler.filterId:
#
# Filters object by id.
# ------------------------------------------------------------------------------
def filterId(self, id, REQUEST):
obs = self.objectValues( self.dGlobalAttrs.keys())
filtered_obs = filter( lambda x: x.id == id, obs)
if len( filtered_obs) > 0:
return filtered_obs[0]
elif self.getConfProperty( 'ZMS.pathhandler', 0) != 0:
req = { 'lang': REQUEST.get('lang'), 'preview': REQUEST.get('preview')}
filtered_obs = filter( lambda x: x.isVisible( req) and x.isPage() and validateId( x, id, req), obs)
if len( filtered_obs) > 0:
return filtered_obs[0]
return None
# ------------------------------------------------------------------------------
# _pathhandler.handleBlobAttrs:
#
# If the object has blob-fields find by filename and display data.
# ------------------------------------------------------------------------------
def handleBlobAttrs(self, name, REQUEST):
langs = self.getLangIds()
name_without_lang_suffix = name
if len(langs) == 1 and name.find('_%s.'%langs[0]) > 0:
name_without_lang_suffix = name.replace('_%s.'%langs[0],'.')
for ob in [self]+self.filteredChildNodes(REQUEST,['ZMSFile']):
for key in ob.getObjAttrs().keys():
obj_attr = ob.getObjAttr(key)
datatype = obj_attr['datatype_key']
if datatype in _globals.DT_BLOBS:
lang = ob.getLanguageFromName( REQUEST['URL'])
REQUEST.set( 'lang', lang)
value = ob.getObjProperty( key, REQUEST)
if value is not None:
href = value.getHref( REQUEST)
langfilename = href.split( '/')[ -1]
if langfilename.find( '?') > 0:
langfilename = langfilename[ :langfilename.find( '?')]
if langfilename == name or \
langfilename == _globals.url_encode(name) or \
langfilename == name_without_lang_suffix or \
langfilename == _globals.url_encode(name_without_lang_suffix):
return value
return None
################################################################################
################################################################################
###
### class PathHandler:
###
### Based on the Zope-Product PathHandler
### http://www.zope.org/Members/NIP/PathHandler).
###
################################################################################
################################################################################
class PathHandler:
# --------------------------------------------------------------------------
# PathHandler.base_url
# --------------------------------------------------------------------------
def base_url(self):
if self.getConfProperty( 'ZMS.pathcoherence', 1) == 1:
# return self.REQUEST.get('BASE0') + '/'.join( list( self.getPhysicalPath()))
return '/'.join( list( self.getPhysicalPath()))
else:
return self.absolute_url()
# --------------------------------------------------------------------------
# PathHandler.__bobo_traverse__
# --------------------------------------------------------------------------
def __bobo_traverse__(self, TraversalRequest, name):
# If this is the first time this __bob_traverse__ method has been called
# in handling this traversal request, store the path_to_handle
req = self.REQUEST
url = self.REQUEST.get('URL','')
zmi = url.find('/manage') >= 0
if not TraversalRequest.has_key('path_to_handle'):
# Make a reversed copy of the TraversalRequestNameStack
TraversalRequestNameStackReversed=copy.copy(TraversalRequest['TraversalRequestNameStack'])
TraversalRequestNameStackReversed.reverse()
# Set path_to_handle in the TraversalRequest.
TraversalRequest['path_to_handle']=[name]+TraversalRequestNameStackReversed
# Set path_to_handle for VirtualHosts.
if '/' in TraversalRequest[ 'path_to_handle']:
path_physical = req.get( 'path_physical', list( self.getDocumentElement().getPhysicalPath())[1:])
if len( path_physical) > 1:
b = TraversalRequest[ 'path_to_handle'].index( '/')
TraversalRequest[ 'path_to_handle'] = TraversalRequest[ 'path_to_handle'][ b+1:]
for i in range( 1, len( path_physical)):
TraversalRequest[ 'path_to_handle'].insert( i-1, path_physical[ i])
# Set language.
lang = req.get( 'lang')
if lang is None:
lang = self.getLanguageFromName( TraversalRequest['path_to_handle'][-1])
if lang is not None:
req.set( 'lang', lang)
# If the name is in the list of attributes, call it.
ob = getattr( self, name, None)
if ob is None or getattr(ob,'meta_type',None) == 'Folder':
obContext = filterId( self, name, req)
if obContext is not None:
ob = obContext
if ob is not None:
meta_type = getattr(ob,'meta_type','?')
if zmi or (meta_type.startswith('ZMS') and meta_type not in self.dGlobalAttrs.keys()):
_confmanager.set_zmi_theme(self)
_confmanager.set_zmi_theme(ob)
try:
for child in self.objectValues(self.dGlobalAttrs.keys()):
_confmanager.set_zmi_theme(child)
except:
pass
if not zmi and TraversalRequest['path_to_handle'][-1] == name:
lang = req.get( 'lang')
if lang is None:
lang = self.getHttpAcceptLanguage( self.REQUEST)
if lang is not None:
self.REQUEST.set( 'lang', lang)
return ob
# otherwise do some 'magic'
else:
_globals.writeLog( self, '[__bobo_traverse__]: otherwise do some magic')
# Package-Home.
if name == '$ZMS_HOME':
i = TraversalRequest['path_to_handle'].index(name)
filepath = self.localfs_package_home()+'/'+'/'.join(TraversalRequest['path_to_handle'][i+1:])
data, mt, enc, size = _fileutil.readFile(filepath)
filename = TraversalRequest['path_to_handle'][-1]
f = self.FileFromData( data, filename, mt)
f.aq_parent = self
f.key = 'None'
f.lang = req.get('lang', self.getPrimaryLanguage())
return f
# Pathhandler-Hook.
if 'pathhandler' in self.getMetaobjAttrIds( self.meta_id, types=['method','py']):
if name == TraversalRequest['path_to_handle'][-1]:
req.set( 'path_', '/'.join(req.get('path_',[])+[name]))
return self.getObjProperty('pathhandler',req)
else:
req.set( 'path_', req.get('path_',[])+[name])
return self
if not zmi or req.get( 'ZMS_PATH_HANDLER', False):
# Recursive inclusions.
thisOb = _globals.nvl( filterId( self, name, req), self)
if thisOb.meta_type == 'ZMSLinkElement':
recursive = thisOb.isEmbeddedRecursive( req)
if recursive:
ob = thisOb.getRefObj()
proxy = thisOb.initProxy( thisOb.aq_parent, thisOb.absolute_url(), ob, recursive)
c = 0
l = TraversalRequest[ 'path_to_handle']
if thisOb.id in l:
i = l.index( thisOb.id) + 1
elif thisOb.getDeclId( req) in l:
i = l.index( thisOb.getDeclId( req)) + 1
for k in range( i, len(l)):
newOb = None
obs = ob.getChildNodes( req)
filtered_obs = filter( lambda x: ( x.id == l[k] or x.getDeclId( req) == l[k]), obs)
if len( filtered_obs) == 1:
newOb = filtered_obs[0]
try:
if newOb.meta_type not in self.dGlobalAttrs.keys():
newOb = None
except:
pass
if newOb is None:
break
ob = newOb
proxy = thisOb.initProxy( proxy, proxy.absolute_url()+'/'+ob.id, ob, recursive)
c += 1
if c > 0:
req.set( 'ZMS_PROXY_%s'%self.id, proxy)
if req.get( 'ZMS_PROXY_%s'%self.id) and req.get( 'ZMS_PROXY_%s'%self.id).id != TraversalRequest[ 'path_to_handle'][-1]:
v = handleBlobAttrs( req.get( 'ZMS_PROXY_%s'%self.id).proxy, TraversalRequest[ 'path_to_handle'][-1], req)
if v is not None:
return v
return thisOb
# Declarative Urls.
ob = self.pathob([name],self.REQUEST)
if ob is not None:
return ob
# If the object is record-set and has blob-fields find by filename and
# display data.
if name.find( '@') == 0:
if self.getType()=='ZMSRecordSet':
try:
i = int( name[1:])
r = self.getObjProperty( self.getMetaobj( self.meta_id)['attrs'][0]['id'], self.REQUEST)
d = r[i]
for key in d.keys():
value = d[key]
if isinstance(value,_blobfields.MyImage) or isinstance(value,_blobfields.MyFile):
value = value._getCopy()
value.aq_parent = self
value.key = key
value.lang = req.get('lang', self.getPrimaryLanguage())
langfilename = value.getHref( req).split( '/')[ -1]
if langfilename.find( '?') > 0:
langfilename = langfilename[ :langfilename.find( '?')]
if langfilename == TraversalRequest['path_to_handle'][-1]:
return value
except:
_globals.writeError( self, '[__bobo_traverse__]')
else:
try:
i = int( name[1:])
obj_attrs = self.getObjAttrs()
for key in self.getObjAttrs().keys():
obj_attr = obj_attrs[ key]
if obj_attr['datatype_key'] == _globals.DT_LIST and \
obj_attr['repetitive']:
lp = [ self.REQUEST.get('preview')]
if lp[ 0] != 'preview':
lp.append( 'preview')
par = None
for ip in lp:
try:
self.REQUEST.set( 'preview', ip)
r = self.getObjProperty( key, self.REQUEST, par)
value = r[i]
value = value._getCopy()
value.aq_parent = self
value.key = key
value.lang = req.get('lang', self.getPrimaryLanguage())
langfilename = value.getHref( req).split( '/')[ -1]
if langfilename.find( '?') > 0:
langfilename = langfilename[ :langfilename.find( '?')]
if langfilename == TraversalRequest['path_to_handle'][-1]:
return value
except:
_globals.writeError( self, '[__bobo_traverse__]: ip=%s'%str(ip))
par = {'fetchReqBuff':0}
self.REQUEST.set( 'preview', lp[ 0])
except:
_globals.writeError( self, '[__bobo_traverse__]')
# If the object has blob-fields find by filename and display data.
v = handleBlobAttrs( self, name, req)
if v is not None: return v
# Skins
if name == TraversalRequest['path_to_handle'][-1]:
l = name
i = l.rfind('_')
j = l.rfind('.')
if i > 0 and j > 0:
lang = l[i+1:j]
if lang in self.getLangIds():
auth_user = self.REQUEST.get('AUTHENTICATED_USER')
zms_skin = l[:i]
zms_ext = l[j+1:]
if zms_skin in map(lambda x:x.strip(),self.getConfProperty('ZMS.skins','index,sitemap,mobile').split(',')):
access = (auth_user is None and self.hasPublicAccess()) or \
(auth_user is not None and auth_user.has_permission( 'View', self) in [ 1, True])
if access:
self.REQUEST.set('ZMS_SKIN',zms_skin)
self.REQUEST.set('ZMS_EXT',zms_ext)
self.REQUEST.set('lang',lang)
return self
# If there's no more names left to handle, return the path handling
# method to the traversal machinery so it gets called next
exc_value='<h2>Site-Error</h2><b>Sorry, there is no web page matching your request.</b> It is possible you typed the address incorrectly, or that the page no longer exists.<hr><b>Resource<b> <i>'+name+'</i> '+''.join(map(lambda x: x+'/',TraversalRequest['path_to_handle']))+' GET'
return self.standard_error_message( self,
exc_type='Resource not found',
exc_value=exc_value,
REQUEST=self.REQUEST)
# --------------------------------------------------------------------------
# PathHandler.pathob
# --------------------------------------------------------------------------
def pathob(self, path_to_handle, REQUEST):
path_ob = self
path_index = 0
while True:
if path_index == len(path_to_handle):
return path_ob
path_item = path_to_handle[path_index]
obs = path_ob.objectValues( self.dGlobalAttrs.keys())
path_ob = filterId( path_ob, path_item, REQUEST)
if path_ob is None:
return path_ob
path_index += 1
################################################################################