oxutilsview.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsView extends oxSuperCfg
00007 {
00013     private static $_instance = null;
00014 
00020     protected static $_oSmarty = null;
00021 
00027     protected $_aTemplateDir = array();
00028 
00034     protected $_blIsTplBlocks = null;
00035 
00041     protected $_aActiveModuleInfo = null;
00042 
00050     public static function getInstance()
00051     {
00052         return oxRegistry::get("oxUtilsView");
00053     }
00054 
00064     public function getSmarty( $blReload = false )
00065     {
00066         if ( !self::$_oSmarty || $blReload ) {
00067             self::$_oSmarty = new Smarty();
00068             $this->_fillCommonSmartyProperties( self::$_oSmarty );
00069             $this->_smartyCompileCheck( self::$_oSmarty );
00070         }
00071 
00072         return self::$_oSmarty;
00073     }
00074 
00084     public function getTemplateOutput( $sTemplate, $oObject )
00085     {
00086         $oSmarty = $this->getSmarty();
00087         $iDebug  = $this->getConfig()->getConfigParam( 'iDebug' );
00088 
00089         // assign
00090         $aViewData = $oObject->getViewData();
00091         if ( is_array( $aViewData ) ) {
00092             foreach ( array_keys( $aViewData ) as $sViewName ) {
00093                 // show debug information
00094                 if ( $iDebug == 4 ) {
00095                     echo( "TemplateData[$sViewName] : \n");
00096                     var_export( $aViewData[$sViewName] );
00097                 }
00098                 $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
00099             }
00100         }
00101 
00102         return $oSmarty->fetch( $sTemplate );
00103     }
00104 
00113     public function passAllErrorsToView( &$aView, $aErrors )
00114     {
00115         if ( count( $aErrors ) > 0 ) {
00116             foreach ( $aErrors as $sLocation => $aEx2 ) {
00117                 foreach ( $aEx2 as $sKey => $oEr ) {
00118                     $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
00119                 }
00120             }
00121         }
00122     }
00123 
00137     public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "", $sActiveController = "" )
00138     {
00139         if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00140             // check if the current request wants do display exceptions on its own
00141             $sDestination = oxConfig::getParameter( 'CustomError' );
00142             if ( $sCustomDestination != '' ) {
00143                 $sDestination = $sCustomDestination;
00144             }
00145         } else {
00146             //default
00147             $sDestination = 'default';
00148         }
00149 
00150         //starting session if not yet started as all exception
00151         //messages are stored in session
00152         $oSession = $this->getSession();
00153         if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
00154             $oSession->setForceNewSession();
00155             $oSession->start();
00156         }
00157 
00158         $aEx = oxSession::getVar( 'Errors' );
00159         if ( $oEr instanceof oxException ) {
00160              $oEx = oxNew( 'oxExceptionToDisplay' );
00161              $oEx->setMessage( $oEr->getMessage() );
00162              $oEx->setExceptionType( get_class( $oEr ) );
00163 
00164              if ( $oEr instanceof oxSystemComponentException ) {
00165                 $oEx->setMessageArgs( $oEr->getComponent() );
00166              }
00167 
00168              $oEx->setValues( $oEr->getValues() );
00169              $oEx->setStackTrace( $oEr->getTraceAsString() );
00170              $oEx->setDebug( $blFull );
00171              $oEr = $oEx;
00172         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00173             // assuming that a string was given
00174             $sTmp = $oEr;
00175             $oEr = oxNew( 'oxDisplayError' );
00176             $oEr->setMessage( $sTmp );
00177         } elseif ( $oEr instanceof oxIDisplayError ) {
00178             // take the object
00179         } else {
00180             $oEr = null;
00181         }
00182 
00183         if ( $oEr ) {
00184             $aEx[$sDestination][] = serialize( $oEr );
00185             oxRegistry::getSession()->setVariable( 'Errors', $aEx );
00186 
00187             if ( $sActiveController == '' ) {
00188                 $sActiveController = oxRegistry::getConfig()->getRequestParameter( 'actcontrol' );
00189             }
00190             if ( $sActiveController ) {
00191                 $aControllerErrors[$sDestination] = $sActiveController;
00192                 oxRegistry::getSession()->setVariable( 'ErrorController', $aControllerErrors );
00193             }
00194         }
00195     }
00196 
00209     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00210     {
00211         if ( oxRegistry::getConfig()->isDemoShop() ) {
00212             return $sDesc;
00213         }
00214 
00215         startProfile("parseThroughSmarty");
00216 
00217         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00218             stopProfile("parseThroughSmarty");
00219             return $sDesc;
00220         }
00221 
00222         $iLang = oxRegistry::getLang()->getTplLanguage();
00223 
00224         // now parse it through smarty
00225         $oSmarty = clone $this->getSmarty();
00226 
00227         // save old tpl data
00228         $sTplVars = $oSmarty->_tpl_vars;
00229         $blForceRecompile = $oSmarty->force_compile;
00230 
00231         $oSmarty->force_compile = $blRecompile;
00232 
00233         if ( !$oActView ) {
00234             $oActView = oxNew( 'oxubase' );
00235             $oActView->addGlobalParams();
00236         }
00237 
00238         $aViewData = $oActView->getViewData();
00239         foreach ( array_keys( $aViewData ) as $sName ) {
00240             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00241         }
00242 
00243         if ( is_array( $sDesc ) ) {
00244             foreach ( $sDesc as $sName => $aData ) {
00245                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00246                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00247             }
00248         } else {
00249             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00250             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00251         }
00252 
00253         // restore tpl vars for continuing smarty processing if it is in one
00254         $oSmarty->_tpl_vars = $sTplVars;
00255         $oSmarty->force_compile = $blForceRecompile;
00256 
00257         stopProfile("parseThroughSmarty");
00258 
00259         return $sRes;
00260     }
00261 
00269     public function setTemplateDir( $sTplDir )
00270     {
00271         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00272             $this->_aTemplateDir[] = $sTplDir;
00273         }
00274     }
00275 
00281     public function getTemplateDirs()
00282     {
00283         $myConfig = oxRegistry::getConfig();
00284 
00285         //T2010-01-13
00286         //#1531
00287         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00288 
00289         if ( !$this->isAdmin() ) {
00290             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00291         }
00292 
00293         return $this->_aTemplateDir;
00294     }
00295 
00301     public function getTemplateCompileId()
00302     {
00303         $sShopId = $this->getConfig()->getShopId();
00304         $aDirs   = $this->getTemplateDirs();
00305         $sDir    = reset($aDirs);
00306         return md5( $sDir.'__'.$sShopId );
00307     }
00308 
00314     public function getSmartyDir()
00315     {
00316         $myConfig = $this->getConfig();
00317 
00318         //check for the Smarty dir
00319         $sCompileDir = $myConfig->getConfigParam( 'sCompileDir' );
00320         $sSmartyDir = $sCompileDir . "/smarty/";
00321         if ( !is_dir( $sSmartyDir ) ) {
00322             @mkdir($sSmartyDir);
00323         }
00324 
00325         if ( !is_writable($sSmartyDir) ) {
00326             $sSmartyDir = $sCompileDir;
00327         }
00328 
00329         return $sSmartyDir;
00330     }
00331 
00339     protected function _fillCommonSmartyProperties( $oSmarty )
00340     {
00341         $myConfig = $this->getConfig();
00342         $oSmarty->left_delimiter  = '[{';
00343         $oSmarty->right_delimiter = '}]';
00344 
00345         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00346                                                   'ox_get_timestamp',
00347                                                   'ox_get_secure',
00348                                                   'ox_get_trusted' ) );
00349 
00350         $sSmartyDir = $this->getSmartyDir();
00351 
00352         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00353         $oSmarty->caching      = false;
00354         $oSmarty->compile_dir  = $sSmartyDir;
00355         $oSmarty->cache_dir    = $sSmartyDir;
00356         $oSmarty->template_dir = $this->getTemplateDirs();
00357         $oSmarty->compile_id   = $this->getTemplateCompileId();
00358 
00359         $oSmarty->default_template_handler_func = array(oxRegistry::get("oxUtilsView"),'_smartyDefaultTemplateHandler');
00360 
00361         include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
00362         $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00363 
00364         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00365         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00366             $oSmarty->debugging = true;
00367         }
00368 
00369         if ($iDebug == 8 && !$myConfig->isAdmin()) {
00370             include_once getShopBasePath().'core/smarty/plugins/prefilter.oxtpldebug.php';
00371             $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
00372         }
00373 
00374         //demoshop security
00375         if ( !$myConfig->isDemoShop() ) {
00376             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00377             $oSmarty->security     = false;
00378         } else {
00379             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00380             $oSmarty->security     = true;
00381             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00382             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00383             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00384             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00385             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00386             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00387             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00388             $oSmarty->secure_dir = $oSmarty->template_dir;
00389         }
00390     }
00391 
00399     protected function _smartyCompileCheck( $oSmarty )
00400     {
00401         $myConfig = $this->getConfig();
00402         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00403 
00404     }
00405 
00417     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
00418     {
00419         $myConfig = oxRegistry::getConfig();
00420         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00421             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00422             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00423             $sResourceTimestamp = filemtime($sResourceName);
00424             return is_file($sResourceName) && is_readable($sResourceName);
00425         }
00426         return false;
00427     }
00428 
00440     protected function _getTemplateBlock($sModule, $sFile)
00441     {
00442         $aModuleInfo = $this->_getActiveModuleInfo();
00443         $sModulePath = $aModuleInfo[$sModule];
00444         // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
00445         if ( substr($sFile, -4) != '.tpl' ) {
00446             $sFile = $sFile . ".tpl";
00447         }
00448         // for < 4.6 modules, since 4.7/5.0 insert in oxtplblocks the full file name and path
00449         if ( basename($sFile) == $sFile ) {
00450             $sFile = "out/blocks/$sFile";
00451         }
00452         $sFileName = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModulePath/$sFile";
00453         if ( file_exists($sFileName) && is_readable($sFileName) ) {
00454             return file_get_contents($sFileName);
00455         } else {
00456             throw oxNew( "oxException", "Template block file ($sFileName) not found for '$sModule' module." );
00457         }
00458     }
00459 
00469     public function getTemplateBlocks($sFile)
00470     {
00471         $oConfig = $this->getConfig();
00472 
00473         $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00474         $sFile = str_replace(array('\\', '//'), '/', $sFile);
00475         if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
00476             $sFile = $m[1];
00477         }
00478 
00479         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00480         $sFileParam = $oDb->quote($sFile);
00481         $sShpIdParam = $oDb->quote($oConfig->getShopId());
00482         $aRet = array();
00483         $aIds = array();
00484 
00485         if ( $this->_blIsTplBlocks === null ) {
00486             $this->_blIsTplBlocks = false;
00487             $aIds = $this->_getActiveModuleInfo();
00488             if (count($aIds)) {
00489                 $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) ";
00490                 $rs = $oDb->getOne( $sSql );
00491                 if ( $rs ) {
00492                     $this->_blIsTplBlocks = true;
00493                 }
00494             }
00495         }
00496 
00497         if ( $this->_blIsTplBlocks ) {
00498             $aIds = $this->_getActiveModuleInfo();
00499             if (count($aIds)) {
00500                 $sSql = "select * from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxtemplate=$sFileParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) order by oxpos asc";
00501                 $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
00502                 $rs = $oDb->select( $sSql );
00503 
00504                 if ($rs != false && $rs->recordCount() > 0) {
00505                     while (!$rs->EOF) {
00506                         try {
00507                             if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00508                                 $aRet[$rs->fields['OXBLOCKNAME']] = array();
00509                             }
00510                             $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00511                         } catch (oxException $oE) {
00512                             $oE->debugOut();
00513                         }
00514                         $rs->moveNext();
00515                     }
00516                 }
00517             }
00518         }
00519 
00520         return $aRet;
00521     }
00522 
00528     protected function _getActiveModuleInfo()
00529     {
00530         if ($this->_aActiveModuleInfo === null) {
00531             $oModulelist = oxNew('oxmodulelist');
00532             $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00533         }
00534         return $this->_aActiveModuleInfo;
00535     }
00536 
00537 }