class ViewTemplateFromPageTemplate(PageTemplate, Acquisition.Explicit): """A way to make a TTW created template work as a z3 style view template. This opens a potential security hole and is just a preliminary proof-of-concept. DO NOT USE!!!""" def __init__(self, template, context): self._text = template._text self.context = context if hasattr(template, 'id'): self.id = template.id if hasattr(template, 'title'): self.title = template.title # A trivial _getContext method as we always know how we are wrapped def _getContext(self): return getattr(self, 'aq_parent', None) # Borrow from Five (the methods from PageTemplateFile are inherited from # PageTemplate directly) _cook = rebindFunction(PageTemplate._cook, getEngine=getEngine) pt_render = rebindFunction(PageTemplate.pt_render, getEngine=getEngine) _pt_getContext = ZopeTwoPageTemplateFile._pt_getContext.im_func pt_getContext = ZopeTwoPageTemplateFile.pt_getContext.im_func def getId(self): return self.id def __call__(self, *args, **kwargs): """Add the zope user to the security context, as done in PageTemplateFile""" if not kwargs.has_key('args'): kwargs['args'] = args bound_names = {'options': kwargs} security = AccessControl.getSecurityManager() bound_names['user'] = security.getUser() return self.pt_render(extra_context=bound_names)
def run(): if not PLONE25: # we don't need to do this w/ Z2.10, which uses Z3 PT engine return log('Applying Five patches...') from Products.Five.browser.ReuseUtils import rebindFunction from Products.Five.browser.pagetemplatefile import ZopeTwoPageTemplateFile \ as Z2PTF from Products.Five.browser.TrustedExpression import getEngine # CacheFu expects to find the original method on the class as # __CacheSetup_PageTemplate_<METHOD_NAME> orig_method = getattr(Z2PTF, 'pt_render') Z2PTF.__CacheSetup_PageTemplate_pt_render__ = orig_method new_pt_render = rebindFunction(patch_cmf.PT_pt_render, getEngine=getEngine) Z2PTF.pt_render = new_pt_render
class ZopeTwoPageTemplateFile(PageTemplateFile): """A strange hybrid between Zope 2 and Zope 3 page template. Uses Zope 2's engine, but with security disabled and with some initialization and API from Zope 3. """ def __init__(self, filename, _prefix=None, content_type=None): # XXX doesn't use content_type yet self.ZBindings_edit(self._default_bindings) path = self.get_path_from_prefix(_prefix) self.filename = os.path.join(path, filename) if not os.path.isfile(self.filename): raise ValueError("No such file", self.filename) basepath, ext = os.path.splitext(self.filename) self.__name__ = os.path.basename(basepath) # required for the ajung-zpt-final-integration branch try: PageTemplateFile.__init__(self, self.filename, _prefix) except: pass def get_path_from_prefix(self, _prefix): if isinstance(_prefix, str): path = _prefix else: if _prefix is None: _prefix = sys._getframe(2).f_globals path = package_home(_prefix) return path _cook = rebindFunction(PageTemplateFile._cook, getEngine=getEngine) pt_render = rebindFunction(PageTemplateFile.pt_render, getEngine=getEngine) def pt_getContext(self): try: root = self.getPhysicalRoot() except AttributeError: root = self.context.getPhysicalRoot() # Even if the context isn't a view (when would that be exaclty?), # there shouldn't be any dange in applying a view, because it # won't be used. However assuming that a lack of getPhysicalRoot # implies a missing view causes problems. view = self._getContext() here = self.context.aq_inner request = getattr(root, 'REQUEST', None) c = { 'template': self, 'here': here, 'context': here, 'container': here, 'nothing': None, 'options': {}, 'root': root, 'request': request, 'modules': ModuleImporter, } if view is not None: c['view'] = view c['views'] = ViewMapper(here, request) return c
ob = traverse(ob, path, getSecurityManager()) return ob class PathExpr(PathExpr): __init__ = rebindFunction(PathExpr.__init__.im_func, SubPathExpr=SubPathExpr, ) class StringExpr(StringExpr): __init__ = rebindFunction(StringExpr.__init__.im_func, PathExpr=PathExpr, ) installHandlers = rebindFunction(installHandlers, PathExpr=PathExpr, StringExpr=StringExpr, PythonExpr=PythonExpr, ) def installHandlers2(engine): installHandlers(engine) engine.registerType('provider', ProviderExpr) _engine=None getEngine = rebindFunction(getEngine, _engine=_engine, installHandlers=installHandlers2 )
class ZopeTwoPageTemplateFile(PageTemplateFile): """A strange hybrid between Zope 2 and Zope 3 page template. Uses Zope 2's engine, but with security disabled and with some initialization and API from Zope 3. """ def __init__(self, filename, _prefix=None, content_type=None): # XXX doesn't use content_type yet self.ZBindings_edit(self._default_bindings) path = self.get_path_from_prefix(_prefix) self.filename = os.path.join(path, filename) if not os.path.isfile(self.filename): raise ValueError("No such file", self.filename) basepath, ext = os.path.splitext(self.filename) self.__name__ = os.path.basename(basepath) def get_path_from_prefix(self, _prefix): if isinstance(_prefix, str): path = _prefix else: if _prefix is None: _prefix = sys._getframe(2).f_globals path = package_home(_prefix) return path _cook = rebindFunction(PageTemplateFile._cook, getEngine=getEngine) pt_render = rebindFunction(PageTemplateFile.pt_render, getEngine=getEngine) def _pt_getContext(self): view = self._getContext() try: root = self.getPhysicalRoot() here = view.context except AttributeError: # self has no attribute getPhysicalRoot. # This typically happens when the template has # no proper acquisition context. That means it has no view, # since that's the normal context for a template in Five. /regebro root = self.context.getPhysicalRoot() here = self.context view = None request = getattr(root, 'REQUEST', None) c = { 'template': self, 'here': here, 'context': here, 'container': self._getContainer(here), 'nothing': None, 'options': {}, 'root': root, 'request': request, 'modules': ModuleImporter, } if view: c['view'] = view c['views'] = ViewMapper(here, request) return c pt_getContext = rebindFunction(_pt_getContext, SecureModuleImporter=ModuleImporter) def _getContainer(self, obj=None): # Utility for bindcode. if obj is None: obj = self while 1: obj = obj.aq_inner.aq_parent if not getattr(obj, '_is_wrapperish', None): parent = getattr(obj, 'aq_parent', None) inner = getattr(obj, 'aq_inner', None) container = getattr(inner, 'aq_parent', None) try: getSecurityManager().validate(parent, container, '', obj) except Unauthorized: return UnauthorizedBinding('container', obj) return obj
class ZopeTwoPageTemplateFile(PageTemplateFile): """A strange hybrid between Zope 2 and Zope 3 page template. Uses Zope 2's engine, but with security disabled and with some initialization and API from Zope 3. """ def __init__(self, filename, _prefix=None, content_type=None): # XXX doesn't use content_type yet self.ZBindings_edit(self._default_bindings) path = self.get_path_from_prefix(_prefix) self.filename = os.path.join(path, filename) if not os.path.isfile(self.filename): raise ValueError("No such file", self.filename) basepath, ext = os.path.splitext(self.filename) self.__name__ = os.path.basename(basepath) def get_path_from_prefix(self, _prefix): if isinstance(_prefix, str): path = _prefix else: if _prefix is None: _prefix = sys._getframe(2).f_globals path = package_home(_prefix) return path _cook = rebindFunction(PageTemplateFile._cook, getEngine=getEngine) pt_render = rebindFunction(PageTemplateFile.pt_render, getEngine=getEngine) def _pt_getContext(self): try: root = self.getPhysicalRoot() view = self._getContext() except AttributeError: # self has no attribute getPhysicalRoot. This typically happens # when the template has no proper acquisition context. # That also means it has no view. /regebro root = self.context.getPhysicalRoot() view = None here = self.context.aq_inner request = getattr(root, 'REQUEST', None) c = {'template': self, 'here': here, 'context': here, 'container': here, 'nothing': None, 'options': {}, 'root': root, 'request': request, 'modules': ModuleImporter, } if view: c['view'] = view c['views'] = ViewMapper(here, request) return c pt_getContext = rebindFunction(_pt_getContext, SecureModuleImporter=ModuleImporter)