function createOverrideCache()
 {
     if (isset($GLOBALS['eZSiteBasics'])) {
         if (isset($GLOBALS['eZSiteBasics']['no-cache-adviced']) and $GLOBALS['eZSiteBasics']['no-cache-adviced']) {
             return false;
         }
     }
     global $eZTemplateOverrideCacheNoPermission;
     if ($eZTemplateOverrideCacheNoPermission == "nocache") {
         return false;
     }
     $ini = eZINI::instance('site.ini');
     $useOverrideCache = true;
     if ($ini->hasVariable('OverrideSettings', 'Cache')) {
         $useOverrideCache = $ini->variable('OverrideSettings', 'Cache') == 'enabled';
     }
     $standardBase = eZTemplateDesignResource::designSetting('standard');
     $siteBase = eZTemplateDesignResource::designSetting('site');
     $overrideKeys = $this->overrideKeys();
     $overrideKey = md5(implode(',', $overrideKeys) . $siteBase . $standardBase);
     $cacheDir = eZSys::cacheDirectory();
     $overrideCacheFile = $cacheDir . '/override/override_' . $overrideKey . '.php';
     // Build matching cache only of it does not already exists,
     // or override file has been updated
     if (!$useOverrideCache or !file_exists($overrideCacheFile)) {
         $matchFileArray = eZTemplateDesignResource::overrideArray($this->OverrideSiteAccess);
         // Generate PHP compiled cache file.
         $phpCache = new eZPHPCreator("{$cacheDir}/override", "override_{$overrideKey}.php");
         $phpCode = "\$GLOBALS['eZOverrideTemplateCacheMap'] = array (\n";
         $numMatchFiles = count($matchFileArray);
         $countMatchFiles = 0;
         //            $phpCode .= "switch ( \$matchFile )\n{\n    ";
         foreach (array_keys($matchFileArray) as $matchKey) {
             $countMatchFiles++;
             $phpCode .= '\'' . md5($matchKey) . '\' => ';
             if (isset($matchFileArray[$matchKey]['custom_match'])) {
                 $baseDir = isset($matchFileArray[$matchKey]['base_dir']) ? $matchFileArray[$matchKey]['base_dir'] : '';
                 $defaultMatchFile = $baseDir . $matchKey;
                 // Custom override matching
                 //                    $phpCode .= "    case  \"$matchKey\":\n    {\n";
                 $matchConditionArray = array();
                 foreach ($matchFileArray[$matchKey]['custom_match'] as $customMatch) {
                     $matchCondition = "";
                     $condCount = 0;
                     if (is_array($customMatch['conditions'])) {
                         foreach (array_keys($customMatch['conditions']) as $conditionKey) {
                             if ($condCount > 0) {
                                 $matchCondition .= " and ";
                             }
                             // Have a special substring match for subtree matching
                             $matchCondition .= "( isset( \$matchKeys[\\'{$conditionKey}\\'] ) and ";
                             if ($conditionKey == 'url_alias') {
                                 $matchCondition .= "( strpos( \$matchKeys[\\'url_alias\\'],  \\'" . $customMatch['conditions']['url_alias'] . "\\' ) === 0 ) )";
                             } else {
                                 $matchCondition .= "( is_array( \$matchKeys[\\'{$conditionKey}\\'] ) ? " . "in_array( \\'" . $customMatch['conditions'][$conditionKey] . "\\', \$matchKeys[\\'{$conditionKey}\\'] ) : " . "\$matchKeys[\\'{$conditionKey}\\'] == \\'" . $customMatch['conditions'][$conditionKey] . "\\') )";
                             }
                             $condCount++;
                         }
                     }
                     // Only create custom match if conditions are defined
                     if ($matchCondition != "") {
                         //                            $phpCode .= "        if ( $matchCondition )\n        {\n";
                         //                            $phpCode .= "            return '" . $customMatch['match_file'] . "';\n        }\n";
                         if ($condCount > 1) {
                             $matchConditionArray[] = array('condition' => '(' . $matchCondition . ')', 'matchFile' => $customMatch['match_file']);
                         } else {
                             $matchConditionArray[] = array('condition' => $matchCondition, 'matchFile' => $customMatch['match_file']);
                         }
                     } else {
                         // No override conditions defined. Override default match file
                         $defaultMatchFile = $customMatch['match_file'];
                     }
                 }
                 $phpCode .= "array ( 'eval' => 1, 'code' => ";
                 $phpCode .= "'";
                 foreach (array_keys($matchConditionArray) as $key) {
                     $phpCode .= '(' . $matchConditionArray[$key]['condition'] . ' ? ' . "\\'" . $matchConditionArray[$key]['matchFile'] . "\\'" . ' : ';
                 }
                 $phpCode .= "\\'" . $defaultMatchFile . "\\'";
                 for ($condCount = 0; $condCount < count($matchConditionArray); $condCount++) {
                     $phpCode .= ')';
                 }
                 $phpCode .= "' )";
             } else {
                 $phpCode .= "'" . $matchFileArray[$matchKey]['base_dir'] . $matchKey . "'";
             }
             if ($countMatchFiles < $numMatchFiles) {
                 $phpCode .= ",\n";
             } else {
                 $phpCode .= ");\n";
             }
         }
         $phpCache->addCodePiece($phpCode);
         if ($useOverrideCache and $phpCache->store()) {
         } else {
             if ($useOverrideCache) {
                 eZDebug::writeError("Could not write template override cache file, check permissions in {$cacheDir}/override/.\nRunning eZ Publish without this cache will have a performance impact.", __METHOD__);
             }
             $eZTemplateOverrideCacheNoPermission = 'nocache';
             $overrideCacheFile = false;
         }
     }
     return $overrideCacheFile;
 }
 function storeCacheFile($filepath, $transformationData, $extraCode, $type, $charsetName)
 {
     $file = basename($filepath);
     $dir = dirname($filepath);
     $php = new eZPHPCreator($dir, $file);
     $php->addComment("Cached transformation data");
     $php->addComment("Type: {$type}");
     $php->addComment("Charset: {$charsetName}");
     $php->addComment("Cached transformation data");
     $php->addCodePiece('$data = ' . eZCharTransform::varExport($transformationData) . ";\n");
     $php->addCodePiece("\$text = strtr( \$text, \$data['table'] );\n");
     if ($extraCode) {
         $php->addCodePiece($extraCode);
     }
     return $php->store(true);
 }
Exemplo n.º 3
0
 static function createCommonCompileTemplate()
 {
     $php = new eZPHPCreator(eZTemplateCompiler::compilationDirectory(), 'common.php');
     if ($php->exists()) {
         return;
     }
     $php->addComment("This file contains functions which are common to all compiled templates.\n\n" . 'NOTE: This file is autogenerated and should not be modified, any changes will be lost!');
     $php->addSpace();
     $php->addDefine('EZ_TEMPLATE_COMPILER_COMMON_CODE', true);
     $php->addSpace();
     $namespaceStack = array();
     $php->addCodePiece("if ( !isset( \$namespaceStack ) )\n");
     $php->addVariable('namespaceStack', $namespaceStack, eZPHPCreator::VARIABLE_ASSIGNMENT, array('spacing' => 4));
     $php->addSpace();
     $lbracket = '{';
     $rbracket = '}';
     $initText = "if ( !function_exists( 'compiledfetchvariable' ) )\n{$lbracket}\n    function compiledFetchVariable( \$vars, \$namespace, \$name )\n    {$lbracket}\n        \$exists = ( array_key_exists( \$namespace, \$vars ) and\n                    array_key_exists( \$name, \$vars[\$namespace] ) );\n        if ( \$exists )\n        {$lbracket}\n            return \$vars[\$namespace][\$name];\n        {$rbracket}\n        return null;\n    {$rbracket}\n{$rbracket}\nif ( !function_exists( 'compiledfetchtext' ) )\n{$lbracket}\n    function compiledFetchText( \$tpl, \$rootNamespace, \$currentNamespace, \$namespace, \$var )\n    {$lbracket}\n        \$text = '';\n        \$tpl->appendElement( \$text, \$var, \$rootNamespace, \$currentNamespace );\n        return \$text;\n    {$rbracket}\n{$rbracket}\nif ( !function_exists( 'compiledAcquireResource' ) )\n{$lbracket}\n    function compiledAcquireResource( \$phpScript, \$key, &\$originalText,\n                                      \$tpl, \$rootNamespace, \$currentNamespace )\n    {\n        include( '" . eZTemplateCompiler::TemplatePrefix() . "' . \$phpScript );\n        if ( isset( \$text ) )\n        {\n            \$originalText .= \$text;\n            return true;\n        }\n        return false;\n    }\n{$rbracket}\nif ( !function_exists( 'compiledfetchattribute' ) )\n{$lbracket}\n    function compiledFetchAttribute( \$value, \$attributeValue )\n    {$lbracket}\n        if ( is_object( \$value ) )\n        {$lbracket}\n            if ( method_exists( \$value, \"attribute\" ) and\n                 method_exists( \$value, \"hasattribute\" ) )\n            {$lbracket}\n                if ( \$value->hasAttribute( \$attributeValue ) )\n                {$lbracket}\n                    return \$value->attribute( \$attributeValue );\n                {$rbracket}\n            {$rbracket}\n        {$rbracket}\n        else if ( is_array( \$value ) )\n        {$lbracket}\n            if ( isset( \$value[\$attributeValue] ) )\n            {$lbracket}\n                return \$value[\$attributeValue];\n            {$rbracket}\n        {$rbracket}\n        return null;\n    {$rbracket}\n{$rbracket}\n";
     $php->addCodePiece($initText);
     $php->store(true);
 }
Exemplo n.º 4
0
 function storeCacheObject($filename, $permissionArray)
 {
     $dir = dirname($filename);
     $file = basename($filename);
     $php = new eZPHPCreator($dir, $file);
     $php->addVariable("umap", array());
     $php->addVariable("utf8map", array());
     $php->addVariable("cmap", array());
     $php->addVariable("utf8cmap", array());
     reset($this->UnicodeMap);
     while (($key = key($this->UnicodeMap)) !== null) {
         $item = $this->UnicodeMap[$key];
         $php->addVariable("umap[{$key}]", $item);
         next($this->UnicodeMap);
     }
     reset($this->UTF8Map);
     while (($key = key($this->UTF8Map)) !== null) {
         $item = $this->UTF8Map[$key];
         if ($item == 0) {
             $php->addCodePiece("\$utf8map[0] = chr(0);\n");
         } else {
             $val = str_replace(array("\\", "'"), array("\\\\", "\\'"), $item);
             $php->addVariable("utf8map[{$key}]", $val);
         }
         next($this->UTF8Map);
     }
     reset($this->CodeMap);
     while (($key = key($this->CodeMap)) !== null) {
         $item = $this->CodeMap[$key];
         $php->addVariable("cmap[{$key}]", $item);
         next($this->CodeMap);
     }
     reset($this->UTF8CodeMap);
     while (($key = key($this->UTF8CodeMap)) !== null) {
         $item = $this->UTF8CodeMap[$key];
         if ($item == 0) {
             $php->addVariable("utf8cmap[chr(0)]", 0);
         } else {
             $val = str_replace(array("\\", "'"), array("\\\\", "\\'"), $key);
             $php->addVariable("utf8cmap['{$val}']", $item);
         }
         next($this->UTF8CodeMap);
     }
     reset($this->ReadExtraMap);
     while (($key = key($this->ReadExtraMap)) !== null) {
         $item = $this->ReadExtraMap[$key];
         $php->addVariable("read_extra[{$key}]", $item);
         next($this->ReadExtraMap);
     }
     $php->addVariable("eZCodePageCacheCodeDate", self::CACHE_CODE_DATE);
     $php->addVariable("min_char", $this->MinCharValue);
     $php->addVariable("max_char", $this->MaxCharValue);
     $php->store(true);
     if (file_exists($filename)) {
         // Store the old umask and set a new one.
         $oldPermissionSetting = umask(0);
         // Change the permission setting.
         @chmod($filename, $permissionArray['file_permission']);
         // Restore the old umask.
         umask($oldPermissionSetting);
     }
 }