Index: kernel/common/ezoverride.php
===================================================================
--- kernel/common/ezoverride.php	(revision 20875)
+++ kernel/common/ezoverride.php	(working copy)
@@ -1,93 +0,0 @@
-<?php
-//
-// Definition of eZOverride class
-//
-// Created on: <31-Oct-2002 09:18:07 amos>
-//
-// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
-// SOFTWARE NAME: eZ Publish
-// SOFTWARE RELEASE: 4.0.x
-// COPYRIGHT NOTICE: Copyright (C) 1999-2007 eZ Systems AS
-// SOFTWARE LICENSE: GNU General Public License v2.0
-// NOTICE: >
-//   This program is free software; you can redistribute it and/or
-//   modify it under the terms of version 2.0  of the GNU General
-//   Public License as published by the Free Software Foundation.
-//
-//   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 version 2.0 of the GNU General
-//   Public License along with this program; if not, write to the Free
-//   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-//   MA 02110-1301, USA.
-//
-//
-// ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
-//
-
-/*! \file ezoverride.php
-*/
-
-/*!
-  \class eZOverride ezoverride.php
-  \brief The class eZOverride does
-
-*/
-
-class eZOverride
-{
-    static function selectFile( $matches, $matchKeys, &$matchedKeys, $regexpMatch )
-    {
-        $match = null;
-        foreach ( $matches as $templateMatch )
-        {
-            $templatePath = $templateMatch["file"];
-            $templateType = $templateMatch["type"];
-            if ( $templateType == "normal" )
-            {
-                if ( file_exists( $templatePath ) )
-                {
-                    $match = $templateMatch;
-                    break;
-                }
-            }
-            else if ( $templateType == "override" )
-            {
-                $foundOverrideFile = false;
-                if ( file_exists( $templatePath ) )
-                {
-                    $match = $templateMatch;
-                    $match["file"] = $templatePath;
-                    $foundOverrideFile = true;
-                }
-                if ( !$foundOverrideFile and
-                     count( $matchKeys ) == 0 )
-                    continue;
-                if ( !$foundOverrideFile and
-                     preg_match( $regexpMatch, $templatePath, $regs ) )// Check for dir/filebase_keyname_keyid.tpl, eg. content/view_section_1.tpl
-                {
-                    foreach ( $matchKeys as $matchKeyName => $matchKeyValue )
-                    {
-                        $file = $regs[1] . "/" . $regs[2] . "_$matchKeyName" . "_$matchKeyValue" . $regs[3];
-                        if ( file_exists( $file ) )
-                        {
-                            $match = $templateMatch;
-                            $match["file"] = $file;
-                            $foundOverrideFile = true;
-                            $matchedKeys[$matchKeyName] = $matchKeyValue;
-                            break;
-                        }
-                    }
-                }
-                if ( $match !== null )
-                    break;
-            }
-        }
-        return $match;
-    }
-}
-
-?>
Index: kernel/common/eztemplatedesignresource.php
===================================================================
--- kernel/common/eztemplatedesignresource.php	(revision 20875)
+++ kernel/common/eztemplatedesignresource.php	(working copy)
@@ -45,12 +45,11 @@
     /*!
      Initializes with a default resource name "design".
     */
-    function eZTemplateDesignResource( $name = "design", $onlyStandard = false )
+    function eZTemplateDesignResource( $name = "design" )
     {
         $this->eZTemplateFileResource( $name, true );
         $this->Keys = array();
         $this->KeyStack = array();
-        $this->OnlyStandard = $onlyStandard;
     }
 
     /*!
@@ -62,7 +61,7 @@
             return false;
 
         $file = $resourceData['template-name'];
-        $matchFileArray = eZTemplateDesignResource::overrideArray( $this->OverrideSiteAccess, $this->OnlyStandard );
+        $matchFileArray = eZTemplateDesignResource::overrideArray( $this->OverrideSiteAccess );
         $matchList = array();
         foreach ( $matchFileArray as $matchFile )
         {
@@ -239,64 +238,26 @@
     }
 
     /*!
-     \return the rules used for matching design elements. \a $element defines the element type.
+     \static
+
     */
-    static function fileMatchingRules( $element, $path, $onlyStandard = false )
+    static function fileMatch( $bases, $element, $path, &$triedFiles )
     {
-        eZDebug::accumulatorStart( 'matching_rules', 'override', 'Matching rules' );
+        foreach ( $bases as $base )
+        {
+            $resource = $element != '' ? "$base/$element" : $base;
+            $possibleMatchFile = $resource . '/' . $path;
 
-        $standardBase = eZTemplateDesignResource::designSetting( 'standard' );
-        if ( !$onlyStandard )
-            $siteBase = eZTemplateDesignResource::designSetting( 'site' );
+            $triedFiles[] = $possibleMatchFile;
 
-        $ini = eZINI::instance();
-        $additionalSiteDesignList = $ini->variable( "DesignSettings", "AdditionalSiteDesignList" );
-
-        //include_once( 'lib/ezutils/classes/ezextension.php' );
-        $extensionDirectory = eZExtension::baseDirectory();
-
-        $designINI = eZINI::instance( 'design.ini' );
-        $extensions = $designINI->variable( 'ExtensionSettings', 'DesignExtensions' );
-
-        $matches = array();
-
-        $elementText = '';
-        if ( $element !== false )
-            $elementText = $element . '/';
-
-        $designStartPath = eZTemplateDesignResource::designStartPath();
-
-        $designList = $additionalSiteDesignList;
-        array_unshift( $designList, $siteBase );
-        $designList[] = $standardBase;
-
-        if ( $onlyStandard )
-            $designList = array( $standardBase );
-
-
-        foreach ( $designList as $design )
-        {
-            foreach ( $extensions as $extension )
+            if ( file_exists( $possibleMatchFile ) )
             {
-                $matches[] = array( 'file' => "$extensionDirectory/$extension/$designStartPath/$design/override/$elementText$path",
-                                    'type' => 'override' );
+                return array( 'resource' => $resource,
+                              'path' => $possibleMatchFile );
             }
-            $matches[] = array( 'file' => "$designStartPath/$design/override/$elementText$path",
-                                'type' => 'override' );
         }
 
-        foreach ( $designList as $design )
-        {
-            foreach ( $extensions as $extension )
-            {
-                $matches[] = array( 'file' => "$extensionDirectory/$extension/$designStartPath/$design/$elementText$path",
-                                    'type' => 'normal' );
-            }
-            $matches[] = array( 'file' => "$designStartPath/$design/$elementText$path",
-                                'type' => 'normal' );
-        }
-        eZDebug::accumulatorStop( 'matching_rules' );
-        return $matches;
+        return false;
     }
 
     /*!
@@ -350,7 +311,7 @@
 //            $matchFileArray = false;
             if ( empty( $GLOBALS['eZTemplateOverrideArray_' . $this->OverrideSiteAccess] ) )
             {
-                $GLOBALS['eZTemplateOverrideArray_' . $this->OverrideSiteAccess] = eZTemplateDesignResource::overrideArray( $this->OverrideSiteAccess, $this->OnlyStandard );
+                $GLOBALS['eZTemplateOverrideArray_' . $this->OverrideSiteAccess] = eZTemplateDesignResource::overrideArray( $this->OverrideSiteAccess );
             }
             $matchFileArray = $GLOBALS['eZTemplateOverrideArray_' . $this->OverrideSiteAccess];
 
@@ -468,8 +429,7 @@
             $useOverrideCache = $ini->variable( 'OverrideSettings', 'Cache' ) == 'enabled';
 
         $standardBase = eZTemplateDesignResource::designSetting( 'standard' );
-        if ( !$this->OnlyStandard )
-            $siteBase = eZTemplateDesignResource::designSetting( 'site' );
+        $siteBase = eZTemplateDesignResource::designSetting( 'site' );
 
         $overrideKeys = $this->overrideKeys();
 
@@ -483,7 +443,7 @@
         if ( !$useOverrideCache or
              !file_exists( $overrideCacheFile ) )
         {
-            $matchFileArray = eZTemplateDesignResource::overrideArray( $this->OverrideSiteAccess, $this->OnlyStandard );
+            $matchFileArray = eZTemplateDesignResource::overrideArray( $this->OverrideSiteAccess );
 
             // Generate PHP compiled cache file.
             //include_once( 'lib/ezutils/classes/ezphpcreator.php' );
@@ -576,10 +536,6 @@
                 else
                 {
                     $phpCode .= "'". $matchFileArray[$matchKey]['base_dir'] . $matchKey . "'";
-                    // Plain matching without custom override
-//                    $phpCode .= "case  \"$matchKey\":\n    {\n
-//                           return '" .
-//                         $matchFileArray[$matchKey]['base_dir'] . $matchKey . "';}\nbreak;\n";
                 }
 
                 if ( $countMatchFiles < $numMatchFiles )
@@ -591,14 +547,7 @@
                     $phpCode .= ");\n";
                 }
             }
-//            $phpCode .= "default:\n {\n}break;\n}";
 
-//            $phpCode .= "}\n";
-
-//            $phpCode .= "function overrideFile( \$matchFile, \$matchKeys )\n{\n    ";
-//            $phpCode .= '  eval( "\$return = " . $GLOBALS[\'eZOverrideTemplateCacheMap\'][$matchFile] . ";" );' . "\n";
-//            $phpCode .= '  return $return;' . "\n}\n\n";
-
             $phpCache->addCodePiece( $phpCode );
             if ( $useOverrideCache and
                  $phpCache->store() )
@@ -613,7 +562,6 @@
                 }
                 $eZTemplateOverrideCacheNoPermission = 'nocache';
                 $overrideCacheFile = false;
-
             }
         }
 
@@ -648,11 +596,8 @@
             $standardBase = $ini->variable( "DesignSettings", "StandardDesign" );
             $keys[] = "siteaccess/$siteAccess";
             $keys[] = $standardBase;
-            if ( !$this->OnlyStandard )
-            {
-                $siteBase = $ini->variable( "DesignSettings", "SiteDesign" );
-                $keys[] = $siteBase;
-            }
+            $siteBase = $ini->variable( "DesignSettings", "SiteDesign" );
+            $keys[] = $siteBase;
         }
         else
         {
@@ -676,8 +621,7 @@
             $standardBase = eZTemplateDesignResource::designSetting( 'standard' );
             $keys[] = $standardBase;
             $siteBase = eZTemplateDesignResource::designSetting( 'site' );
-            if ( !$this->OnlyStandard )
-                $keys[] = $siteBase;
+            $keys[] = $siteBase;
         }
 
 
@@ -704,33 +648,93 @@
 
     /*!
      \static
-     \return Gives all knows bases for avialable sitedesign folders.
+     \return An array containing the names of the design extensions that are
+             currently active
     */
-    static function allDesignBases()
+    static function designExtensions()
     {
-        $ini = eZINI::instance();
-
-        //include_once( 'lib/ezutils/classes/ezextension.php' );
-        $extensionDirectory = eZExtension::baseDirectory();
         $designINI = eZINI::instance( 'design.ini' );
         $extensions = $designINI->variable( 'ExtensionSettings', 'DesignExtensions' );
+        return array_reverse( $extensions );
+    }
 
-        $bases = array();
+    /*!
+     \static
+     \return Gives all knows bases for avialable sitedesign folders.
+    */
+    static function allDesignBases( $siteAccess = false )
+    {
+        if ( $siteAccess )
+        {
+            if ( isset( $GLOBALS['eZTemplateDesignResourceSiteAccessBases'] ) )
+            {
+                if ( isset( $GLOBALS['eZTemplateDesignResourceSiteAccessBases'][$siteAccess] ) )
+                {
+                    return $GLOBALS['eZTemplateDesignResourceSiteAccessBases'][$siteAccess];
+                }
+            }
+            else
+            {
+                $GLOBALS['eZTemplateDesignResourceSiteAccessBases'] = array();
+            }
 
-        $std_base = eZTemplateDesignResource::designSetting( 'standard' );
-        $site_base = eZTemplateDesignResource::designSetting( 'site' );
-        $SiteDesignList = $ini->variable( 'DesignSettings', 'AdditionalSiteDesignList' );
-        array_unshift( $SiteDesignList, $site_base );
-        $SiteDesignList[] = $std_base;
+            $ini = eZINI::instance( 'site.ini', 'settings', null, null, true );
+            $ini->prependOverrideDir( "siteaccess/$siteAccess", false, 'siteaccess' );
+            eZExtension::prependExtensionSiteAccesses( $siteAccess, $ini, false, 'siteaccess' );
+            $ini->loadCache();
+
+            $standardDesign = $ini->variable( "DesignSettings", "StandardDesign" );
+            $siteDesign = $ini->variable( "DesignSettings", "SiteDesign" );
+        }
+        else
+        {
+            if ( isset( $GLOBALS['eZTemplateDesignResourceBases'] ) )
+            {
+                return $GLOBALS['eZTemplateDesignResourceBases'];
+            }
+
+            $ini = eZINI::instance();
+            $standardDesign = eZTemplateDesignResource::designSetting( 'standard' );
+            $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
+        }
+
+        $siteDesignList = $ini->variable( 'DesignSettings', 'AdditionalSiteDesignList' );
+
+        array_unshift( $siteDesignList, $siteDesign );
+        $siteDesignList[] = $standardDesign;
+
+        $bases = array();
+        $extensionDirectory = eZExtension::baseDirectory();
         $designStartPath = eZTemplateDesignResource::designStartPath();
-        foreach ( $SiteDesignList as $design )
+        $extensions = eZTemplateDesignResource::designExtensions();
+
+        foreach ( $siteDesignList as $design )
         {
-            $bases[] = "$designStartPath/$design";
-            foreach( $extensions as $extension )
+            foreach ( $extensions as $extension )
             {
-               $bases[] = "$extensionDirectory/$extension/$designStartPath/$design";
+                $path = "$extensionDirectory/$extension/$designStartPath/$design";
+                if ( file_exists( $path ) )
+                {
+                    $bases[] = $path;
+                }
             }
+
+            $path = "$designStartPath/$design";
+            if ( file_exists( $path ) )
+            {
+                $bases[] = $path;
+            }
         }
+
+        if ( $siteAccess )
+        {
+            $GLOBALS['eZTemplateDesignResourceSiteAccessBases'][$siteAccess] = $bases;
+        }
+        else
+        {
+            $GLOBALS['eZTemplateDesignResourceBases'] = $bases;
+        }
+
         return $bases;
     }
 
@@ -767,142 +771,88 @@
      \return an array of all the current templates and overrides for them.
              The current siteaccess is used if none is specified.
     */
-    static function overrideArray( $siteAccess = false, $onlyStandard = null )
+    static function overrideArray( $siteAccess = false )
     {
+        $bases = eZTemplateDesignResource::allDesignBases( $siteAccess );
+
         // fetch the override array from a specific siteacces
         if ( $siteAccess )
         {
-            // Get the design resources
-            $ini = eZINI::instance( 'site.ini', 'settings', null, null, true );
-            $ini->prependOverrideDir( "siteaccess/$siteAccess", false, 'siteaccess' );
-            eZExtension::prependExtensionSiteAccesses( $siteAccess, $ini, false, 'siteaccess' );
-            $ini->loadCache();
-
             $overrideINI = eZINI::instance( 'override.ini', 'settings', null, null, true );
             $overrideINI->prependOverrideDir( "siteaccess/$siteAccess", false, 'siteaccess' );
             eZExtension::prependExtensionSiteAccesses( $siteAccess, $overrideINI, false, 'siteaccess', false );
             $overrideINI->loadCache();
-
-            $standardBase = $ini->variable( "DesignSettings", "StandardDesign" );
-            if ( !$onlyStandard )
-                $siteBase = $ini->variable( "DesignSettings", "SiteDesign" );
         }
         else
         {
-            $ini = eZINI::instance();
             $overrideINI = eZINI::instance( 'override.ini' );
-            $standardBase = eZTemplateDesignResource::designSetting( 'standard' );
-            $siteBase = eZTemplateDesignResource::designSetting( 'site' );
         }
 
         $designStartPath = eZTemplateDesignResource::designStartPath();
 
-        $additionalSiteDesignList = $ini->variable( 'DesignSettings', 'AdditionalSiteDesignList' );
-        // Reverse additionalSiteDesignList so that most prefered design is last
-        $additionalSiteDesignList = array_reverse( $additionalSiteDesignList );
-
         // Generate match cache for all templates
-        //include_once( 'lib/ezfile/classes/ezdir.php' );
 
-        // Build arrays of available files, start with base design and end with most prefered design
+        // Build arrays of available files, start with standard design and end with most prefered design
         $matchFilesArray = array();
 
-        // For each override dir overwrite current default file
-        // TODO: fetch all resource repositories
-        $resourceArray[] = "$designStartPath/$standardBase/templates";
-        $resourceArray[] = "$designStartPath/$standardBase/override/templates";
+        $reverseBases = array_reverse( $bases );
 
-        // Add the additional sitedesigns
-        foreach ( $additionalSiteDesignList as $additionalSiteDesign )
+        foreach ( $reverseBases as $base )
         {
-            $resourceArray[] = "$designStartPath/$additionalSiteDesign/override/templates";
-            $resourceArray[] = "$designStartPath/$additionalSiteDesign/templates";
-        }
-
-        $resourceArray[] = "$designStartPath/$siteBase/override/templates";
-        $resourceArray[] = "$designStartPath/$siteBase/templates";
-
-        // Add extension paths
-        //include_once( 'lib/ezutils/classes/ezextension.php' );
-        $extensionDirectory = eZExtension::baseDirectory();
-
-        $designINI = eZINI::instance( 'design.ini' );
-        $extensions = $designINI->variable( 'ExtensionSettings', 'DesignExtensions' );
-
-        foreach ( $extensions as $extension )
-        {
-            // Look for standard design in extension
-            $resourceArray[] = "$extensionDirectory/$extension/$designStartPath/$standardBase/templates";
-            $resourceArray[] = "$extensionDirectory/$extension/$designStartPath/$standardBase/override/templates";
-
-            // Look for aditional sitedesigns in extension
-            foreach ( $additionalSiteDesignList as $additionalSiteDesign )
+            $templateResource = $base . '/templates';
+            $sourceFileArray = eZDir::recursiveFindRelative( $templateResource, "",  "tpl" );
+            foreach ( $sourceFileArray as $source )
             {
-                $resourceArray[] = "$extensionDirectory/$extension/$designStartPath/$additionalSiteDesign/override/templates";
-                $resourceArray[] = "$extensionDirectory/$extension/$designStartPath/$additionalSiteDesign/templates";
+                $matchFileArray[$source]['base_dir'] = $templateResource;
+                $matchFileArray[$source]['template'] = $source;
             }
-
-            // Look for site base in extention
-            $resourceArray[] = "$extensionDirectory/$extension/$designStartPath/$siteBase/override/templates";
-            $resourceArray[] = "$extensionDirectory/$extension/$designStartPath/$siteBase/templates";
         }
 
-        foreach ( $resourceArray as $resource )
-        {
-            $sourceFileArray = eZDir::recursiveFindRelative( $resource, "",  "tpl" );
-            foreach ( array_keys( $sourceFileArray ) as $sourceKey )
-            {
-                $matchFileArray[$sourceFileArray[$sourceKey]]['base_dir'] = $resource;
-                $matchFileArray[$sourceFileArray[$sourceKey]]['template'] = $sourceFileArray[$sourceKey];
-            }
-        }
-
-
-        // Load complex/custom override templates
-        $overrideSettingGroupArray = $overrideINI->groups();
+        // Load override templates
+        $overrideSettingGroups = $overrideINI->groups();
         if ( isset( $GLOBALS['eZDesignOverrides'] ) )
         {
-            $overrideSettingGroupArray = array_merge( $overrideSettingGroupArray, $GLOBALS['eZDesignOverrides'] );
+            $overrideSettingGroups = array_merge( $overrideSettingGroups, $GLOBALS['eZDesignOverrides'] );
         }
 
-        foreach ( array_keys( $overrideSettingGroupArray ) as $overrideSettingKey )
+        foreach ( $overrideSettingGroups as $overrideName => $overrideSetting )
         {
-            $overrideName = $overrideSettingKey;
-            $overrideSource = "/" . $overrideSettingGroupArray[$overrideSettingKey]['Source'];
+            $overrideSource = "/" . $overrideSetting['Source'];
+            $overrideMatchFile = $overrideSetting['MatchFile'];
 
-            $overrideMatchConditionArray = isset( $overrideSettingGroupArray[$overrideSettingKey]['Match'] ) ?
-                $overrideSettingGroupArray[$overrideSettingKey]['Match'] :
-                null;
-            $overrideMatchFile = $overrideSettingGroupArray[$overrideSettingKey]['MatchFile'];
-
-            $overrideMatchFilePath = false;
             // Find the matching file in the available resources
             $triedFiles = array();
-            $resourceInUse = false;
-            foreach ( $resourceArray as $resource )
-            {
-                if ( file_exists( $resource . "/" . $overrideMatchFile ) )
-                {
-                    $overrideMatchFilePath = $resource . "/" . $overrideMatchFile;
-                    $resourceInUse = $resource;
-                }
-                else
-                    $triedFiles[] = $resource . '/' . $overrideMatchFile;
-            }
+            $fileInfo = eZTemplateDesignResource::fileMatch( $bases, 'override/templates', $overrideMatchFile, $triedFiles );
 
+            $resourceInUse = is_array( $fileInfo ) ? $fileInfo['resource'] : false;
+            $overrideMatchFilePath = is_array( $fileInfo ) ? $fileInfo['path'] : false;
+
+            // if the override template is not found
+            // then we probably shouldn't use it
+            // there should be added a check around the following code
+            // if ( $overrideMatchFilePath )
+            // {
             $customMatchArray = array();
-            $customMatchArray['conditions'] = $overrideMatchConditionArray;
+            $customMatchArray['conditions'] = isset( $overrideSetting['Match'] ) ? $overrideSetting['Match'] : null;
             $customMatchArray['match_file'] = $overrideMatchFilePath;
             $customMatchArray['override_name'] = $overrideName;
+
             $matchFileArray[$overrideSource]['custom_match'][] = $customMatchArray;
-            if( $resourceInUse && !isset($matchFileArray[$overrideSource]['base_dir']))
+            // }
+
+            // if overriding a non-existing template
+            // then we use the override template as main template
+            // this code should probably be removed
+            // because we should not allow an override if the main template is missing
+            if ( $resourceInUse && !isset( $matchFileArray[$overrideSource]['base_dir'] ) )
             {
-                $matchFileArray[$overrideSource]['base_dir'] = $resource;
+                $matchFileArray[$overrideSource]['base_dir'] = $resourceInUse;
                 $matchFileArray[$overrideSource]['template'] = $overrideSource;
             }
+
             if ( ! $overrideMatchFilePath )
             {
-                eZDebug::writeError( "Custom match file: path '$overrideMatchFile' not found in any resource. Check template settings in settings/override.ini",
+                eZDebug::writeError( "Custom match file: path '$overrideMatchFile' not found in any resource. Check the template settings in settings/override.ini",
                                      "eZTemplateDesignResource::overrideArray" );
                 eZDebug::writeError( implode( ', ', $triedFiles ),
                                      "eZTemplateDesignResource::overrideArray, tried files" );
@@ -993,18 +943,6 @@
     }
 
     /*!
-     \return the unique instance of the standard resource.
-    */
-    static function standardInstance()
-    {
-        if ( !isset( $GLOBALS['eZTemplateStandardResourceInstance'] ) )
-        {
-            $GLOBALS['eZTemplateStandardResourceInstance'] = new eZTemplateDesignResource( 'standard', true );
-        }
-        return $GLOBALS['eZTemplateStandardResourceInstance'];
-    }
-
-    /*!
      Sets the siteaccess which are to be used for loading the override settings.
     */
     function setOverrideAccess( $siteAccess )
@@ -1013,7 +951,6 @@
     }
 
     public $Keys;
-    public $OnlyStandard;
     public $OverrideSiteAccess = false;
 }
 
Index: kernel/common/ezurloperator.php
===================================================================
--- kernel/common/ezurloperator.php	(revision 20875)
+++ kernel/common/ezurloperator.php	(working copy)
@@ -325,153 +325,38 @@
                 {
                     $path = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
 
-                    $matches = eZTemplateDesignResource::fileMatchingRules( false, $path );
-
-                    $designResource = eZTemplateDesignResource::instance();
-                    $matchKeys = $designResource->keys();
-                    $matchedKeys = array();
-
-                    //include_once( 'kernel/common/ezoverride.php' );
-                    $match = eZOverride::selectFile( $matches, $matchKeys, $matchedKeys, "#^(.+)/(.+)(\.[a-zA-Z0-9]+)$#" );
-                    if ( $match === null )
-                    {
-                        $tpl->warning( $operatorName, "Design element $path does not exist in any design" );
-                        $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
-                        $path = "design/$siteDesign/$path";
-                    }
-                    else
-                    {
-                        $path = $match["file"];
-                    }
-
-                    $path = $this->Sys->wwwDir() . '/' . $path;
-                    $path = htmlspecialchars( $path );
-
+                    $path = $this->eZDesign( $tpl, $path, $operatorName );
                     $path = $this->applyQuotes( $path, $parameters[1] );
 
                     return array( eZTemplateNodeTool::createStringElement( $path ) );
                 }
 
-                $code = ( '%tmp1% = eZTemplateDesignResource::instance();' . "\n" .
-                          '//include_once( \'kernel/common/ezoverride.php\' );' . "\n" .
-                          '%tmp2% = array();' . "\n" .
-                          '%tmp3% = eZOverride::selectFile( eZTemplateDesignResource::fileMatchingRules( false, %1% ), %tmp1%->keys(), %tmp2%, "#^(.+)/(.+)(\.[a-zA-Z0-9]+)$#" );' . "\n" .
-                          'if ( %tmp3% === null )' . "\n" .
-                          '{' . "\n" .
-                          '    %tmp3% = array();' . "\n" .
-                          '    $tpl->warning( "' . $operatorName . '", "Design element %1% does not exist in any design" );' . "\n" .
-                          '    %tmp4% = eZTemplateDesignResource::designSetting( "site" );' . "\n" .
-                          '    %1% = "design/%tmp4%/%1%";' . "\n" .
-                          '}' . "\n" .
-                          'else' . "\n" .
-                          '{' . "\n" .
-                          '    %1% = %tmp3%["file"];' . "\n" .
-                          '}' . "\n" .
-                          '%1% = %2% . "/" . %1%;' . "\n" .
-                          '%1% = htmlspecialchars( %1% );' . "\n"
-                        );
+                $code = ( '%1% = eZURLOperator::eZDesign( $tpl, %1%, "' . $operatorName . '" );' . "\n" );
 
                 $values[] = $parameters[0];
-                $values[] = array( eZTemplateNodeTool::createStringElement( $this->Sys->wwwDir() ) );
-                $tmpCount += 4;
                 ++$paramCount;
             } break;
 
             case $this->ImageName:
             {
+                $skipSlash = count( $parameters ) > 2 ? eZTemplateNodeTool::elementStaticValue( $parameters[2] ) == true : false;
+
                 if ( eZTemplateNodeTool::isStaticElement( $parameters[0] ) )
                 {
                     $path = eZTemplateNodeTool::elementStaticValue( $parameters[0] );
-                    $skipSlash = false;
-                    if ( count( $parameters ) > 2 )
-                    {
-                        $skipSlash = eZTemplateNodeTool::elementStaticValue( $parameters[2] );
-                    }
 
-                    $bases = eZTemplateDesignResource::allDesignBases();
-                    $no_slash_prefix = false;
-                    if ( $skipSlash == true && strlen( $this->Sys->wwwDir() ) == 0 )
-                        $no_slash_prefix = true;
-
-                    $imageFound = false;
-                    foreach ( $bases as $base )
-                    {
-                        $possiblePath = $base . '/images/' . $path;
-                        if ( file_exists( $possiblePath ) )
-                        {
-                            if ( $no_slash_prefix == true )
-                                $path = $possiblePath;
-                            else
-                                $path = $this->Sys->wwwDir() . '/' . $possiblePath;
-                            $imageFound = true;
-                            break;
-                        }
-                    }
-
-                    if ( !$imageFound )
-                    {
-                        $tpl->warning( $operatorName, "Image '$path' does not exist in any design" );
-                        $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
-                        $path = "design/$siteDesign/images/$path";
-                    }
-
-                    $path = htmlspecialchars( $path );
+                    $path = eZURLOperator::eZImage( $tpl, $path, $operatorName, $skipSlash );
                     $path = $this->applyQuotes( $path, $parameters[1] );
 
                     return array( eZTemplateNodeTool::createStringElement( $path ) );
                 }
-                else
-                {
-                    $values = array();
-                    $values[] = $parameters[0];
 
-                    $no_slash_prefix = false;
-                    if ( count ( $parameters ) > 2 )
-                    {
-                        if ( eZTemplateNodeTool::elementStaticValue( $parameters[2] ) == true && strlen( $wwwDir ) )
-                        {
-                            $no_slash_prefix = true;
-                        }
-                    }
+                $code = ( '%1% = eZURLOperator::eZImage( $tpl, %1%, "' . $operatorName . '", %2% );' . "\n" );
 
-                    $ini = eZINI::instance();
-                    $values[] = array( eZTemplateNodeTool::createStringElement( $this->Sys->wwwDir() ) );
-                    $values[] = array( eZTemplateNodeTool::createArrayElement( eZTemplateDesignResource::allDesignBases() ) );
+                $values[] = $parameters[0];
+                $values[] = array( eZTemplateNodeTool::createBooleanElement( $skipSlash ) );
 
-                    $code =
-                    '%tmp2% = false;'                                                                   . "\n" .
-                    'foreach ( %3% as %tmp1% )'                                                         . "\n" .
-                    '{'                                                                                 . "\n" .
-                    '    %tmp3% = %tmp1% . "/images/" . %1%;'                                           . "\n" .
-                    '    if ( file_exists( %tmp3% ) )'                                                  . "\n" .
-                    '    {'                                                                             . "\n" ;
-                    if ( $no_slash_prefix == true )
-                        $code .= '        %1% = %tmp3%;';
-                    else
-                        $code .= '        %1% = %2% . "/" . %tmp3%;';
-
-                    $code .= "\n" .
-                    '         %tmp2% = true;'                                                           . "\n" .
-                    '         break;'                                                                   . "\n" .
-                    '    }'                                                                             . "\n" .
-                    '}'                                                                                 . "\n" .
-                    'if ( !%tmp2% )'                                                                    . "\n" .
-                    '{'                                                                                 . "\n" .
-                    '    $tpl->warning( "' . $operatorName .
-                                                   '", "Image %1% does not exist in any design" );'     . "\n" .
-                    '    %tmp3% = eZTemplateDesignResource::designSetting( "site" );'                   . "\n" .
-                    '    %1% = "design/%tmp3%/images/%1%";'                                             . "\n" .
-                    '}'                                                                                 . "\n" .
-                    '%output% = htmlspecialchars( %1% );'                                               . "\n" ;
-
-                    $quote = $this->applyQuotes( '', $parameters[1], true );
-                    if ( $quote )
-                    {
-                        $values[] = array( eZTemplateNodeTool::createStringElement( $quote ) );
-                        $code .= '%output% = %4% . %output% . %4%;'                                     . "\n";
-                    }
-                    return array( eZTemplateNodeTool::createCodePieceElement( $code, $values, false, 3 ) );
-                }
+                ++$paramCount;
             } break;
 
             case $this->ExtName:
@@ -828,39 +713,18 @@
 
             case $this->ImageName:
             {
-                $ini = eZINI::instance();
-                $no_slash_prefix = false;
-                if ( count( $operatorParameters ) == 2 )
+                if ( count( $operatorParameters ) == 2 &&
+                     $tpl->elementValue( $operatorParameters[1], $rootNamespace, $currentNamespace ) == true &&
+                     strlen( $this->Sys->wwwDir() ) == 0 )
                 {
-                    if ( $operatorParameters[1] == true && strlen( $this->Sys->wwwDir() ) == 0 )
-                        $no_slash_prefix = true;
+                    $skipSlash = true;
                 }
-
-                $bases = eZTemplateDesignResource::allDesignBases();
-
-                $imageFound = false;
-                foreach ( $bases as $base )
+                else
                 {
-                    $possiblePath = $base . '/images/' . $operatorValue;
-                    if ( file_exists( $possiblePath ) )
-                    {
-                        if ( $no_slash_prefix == true )
-                            $operatorValue = $possiblePath;
-                        else
-                            $operatorValue = $this->Sys->wwwDir() . '/' . $possiblePath;
-                        $imageFound = true;
-                        break;
-                    }
+                    $skipSlash = false;
                 }
 
-                if ( !$imageFound )
-                {
-                    $tpl->warning( $operatorName, "Image '$operatorValue' does not exist in any design" );
-                    $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
-                    $operatorValue = "design/$siteDesign/images/$operatorValue";
-                }
-
-                $operatorValue = htmlspecialchars( $operatorValue );
+                $operatorValue = $this->eZImage( $tpl, $operatorValue, $operatorName, $skipSlash );
             } break;
 
             case $this->ExtName:
@@ -876,26 +740,7 @@
 
             case $this->DesignName:
             {
-                $matches = eZTemplateDesignResource::fileMatchingRules( false, $operatorValue );
-
-                $designResource = eZTemplateDesignResource::instance();
-                $matchKeys = $designResource->keys();
-                $matchedKeys = array();
-
-                //include_once( 'kernel/common/ezoverride.php' );
-                $match = eZOverride::selectFile( $matches, $matchKeys, $matchedKeys, "#^(.+)/(.+)(\.[a-zA-Z0-9]+)$#" );
-                if ( $match === null )
-                {
-                    $tpl->warning( $operatorName, "Design element $operatorValue does not exist in any design" );
-                    $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
-                    $file = "design/$siteDesign/$operatorValue";
-                }
-                else
-                    $file = $match["file"];
-
-                $operatorValue = $this->Sys->wwwDir() . "/$file";
-                $operatorValue = htmlspecialchars( $operatorValue );
-
+                $operatorValue = $this->eZDesign( $tpl, $operatorValue, $operatorName );
             } break;
         }
         $quote = "\"";
@@ -916,6 +761,68 @@
             $operatorValue = $quote . $operatorValue . $quote;
     }
 
+    /*!
+
+    */
+    static function eZDesign( $tpl, $operatorValue, $operatorName )
+    {
+        $sys = eZSys::instance();
+
+        $bases = eZTemplateDesignResource::allDesignBases();
+        $triedFiles = array();
+        $fileInfo = eZTemplateDesignResource::fileMatch( $bases, false, $operatorValue, $triedFiles);
+
+        if ( !$fileInfo )
+        {
+            $tpl->warning( $operatorName, "Design element $operatorValue does not exist in any design" );
+            $tpl->warning( $operatorName, "Tried files: " . implode( ', ', $triedFiles ) );
+            $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
+            $filePath = "design/$siteDesign/$operatorValue";
+        }
+        else
+        {
+            $filePath = $fileInfo['path'];
+        }
+
+        $operatorValue = $sys->wwwDir() . '/' . $filePath;
+        $operatorValue = htmlspecialchars( $operatorValue );
+
+        return $operatorValue;
+    }
+
+    /*!
+
+    */
+    static function eZImage( $tpl, $operatorValue, $operatorName, $skipSlash = false )
+    {
+        $sys = eZSys::instance();
+        if ( $skipSlash && strlen( $sys->wwwDir() ) != 0 )
+        {
+            $skipSlash = false;
+        }
+
+        $bases = eZTemplateDesignResource::allDesignBases();
+        $triedFiles = array();
+        $fileInfo = eZTemplateDesignResource::fileMatch( $bases, 'images', $operatorValue, $triedFiles );
+
+        if ( !$fileInfo )
+        {
+            $tpl->warning( $operatorName, "Image '$operatorValue' does not exist in any design" );
+            $tpl->warning( $operatorName, "Tried files: " . implode( ', ', $triedFiles ) );
+            $siteDesign = eZTemplateDesignResource::designSetting( 'site' );
+            $imgPath = "design/$siteDesign/images/$operatorValue";
+        }
+        else
+        {
+            $imgPath = $fileInfo['path'];
+        }
+
+        $operatorValue = $skipSlash ? $imgPath : $sys->wwwDir() . '/' . $imgPath;
+        $operatorValue = htmlspecialchars( $operatorValue );
+
+        return $operatorValue;
+    }
+
     public $Operators;
     public $URLName, $URLRootName, $DesignName, $ImageName;
     public $Sys;
Index: kernel/common/template.php
===================================================================
--- kernel/common/template.php	(revision 20875)
+++ kernel/common/template.php	(working copy)
@@ -61,7 +61,6 @@
     $tpl->autoload();
 
     $tpl->registerResource( eZTemplateDesignResource::instance() );
-    $tpl->registerResource( eZTemplateDesignResource::standardInstance() );
 
     if ( $name === false )
     {
