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 
00136     public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "" )
00137     {
00138         if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
00139             // check if the current request wants do display exceptions on its own
00140             $sDestination = oxConfig::getParameter( 'CustomError' );
00141             if ( $sCustomDestination != '' ) {
00142                 $sDestination = $sCustomDestination;
00143             }
00144         } else {
00145             //default
00146             $sDestination = 'default';
00147         }
00148 
00149         //starting session if not yet started as all exception
00150         //messages are stored in session
00151         $oSession = $this->getSession();
00152         if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
00153             $oSession->setForceNewSession();
00154             $oSession->start();
00155         }
00156 
00157         $aEx = oxSession::getVar( 'Errors' );
00158         if ( $oEr instanceof oxException ) {
00159              $oEx = oxNew( 'oxExceptionToDisplay' );
00160              $oEx->setMessage( $oEr->getMessage() );
00161              $oEx->setExceptionType( get_class( $oEr ) );
00162 
00163              if ( $oEr instanceof oxSystemComponentException ) {
00164                 $oEx->setMessageArgs( $oEr->getComponent() );
00165              }
00166 
00167              $oEx->setValues( $oEr->getValues() );
00168              $oEx->setStackTrace( $oEr->getTraceAsString() );
00169              $oEx->setDebug( $blFull );
00170              $oEr = $oEx;
00171         } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
00172             // assuming that a string was given
00173             $sTmp = $oEr;
00174             $oEr = oxNew( 'oxDisplayError' );
00175             $oEr->setMessage( $sTmp );
00176         } elseif ( $oEr instanceof oxIDisplayError ) {
00177             // take the object
00178         } else {
00179             $oEr = null;
00180         }
00181 
00182         if ( $oEr ) {
00183             $aEx[$sDestination][] = serialize( $oEr );
00184             oxSession::setVar( 'Errors', $aEx );
00185 
00186             $sActiveController = oxConfig::getParameter( 'actcontrol' );
00187             if ( $sActiveController ) {
00188                 $aControllerErrors[$sDestination] = $sActiveController;
00189                 oxSession::setVar( 'ErrorController', $aControllerErrors );
00190             }
00191         }
00192     }
00193 
00206     public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
00207     {
00208         startProfile("parseThroughSmarty");
00209 
00210         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00211             stopProfile("parseThroughSmarty");
00212             return $sDesc;
00213         }
00214 
00215 
00216         $iLang = oxRegistry::getLang()->getTplLanguage();
00217 
00218         // now parse it through smarty
00219         $oSmarty = clone $this->getSmarty();
00220 
00221         // save old tpl data
00222         $sTplVars = $oSmarty->_tpl_vars;
00223         $blForceRecompile = $oSmarty->force_compile;
00224 
00225         $oSmarty->force_compile = $blRecompile;
00226 
00227         if ( !$oActView ) {
00228             $oActView = oxNew( 'oxubase' );
00229             $oActView->addGlobalParams();
00230         }
00231 
00232         $aViewData = $oActView->getViewData();
00233         foreach ( array_keys( $aViewData ) as $sName ) {
00234             $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
00235         }
00236 
00237         if ( is_array( $sDesc ) ) {
00238             foreach ( $sDesc as $sName => $aData ) {
00239                 $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
00240                 $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
00241             }
00242         } else {
00243             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00244             $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
00245         }
00246 
00247         // restore tpl vars for continuing smarty processing if it is in one
00248         $oSmarty->_tpl_vars = $sTplVars;
00249         $oSmarty->force_compile = $blForceRecompile;
00250 
00251         stopProfile("parseThroughSmarty");
00252 
00253         return $sRes;
00254     }
00255 
00263     public function setTemplateDir( $sTplDir )
00264     {
00265         if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
00266             $this->_aTemplateDir[] = $sTplDir;
00267         }
00268     }
00269 
00275     public function getTemplateDirs()
00276     {
00277         $myConfig = oxRegistry::getConfig();
00278 
00279         //T2010-01-13
00280         //#1531
00281         $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
00282 
00283         if ( !$this->isAdmin() ) {
00284             $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
00285         }
00286 
00287         return $this->_aTemplateDir;
00288     }
00289 
00295     public function getTemplateCompileId()
00296     {
00297         $sShopId = $this->getConfig()->getShopId();
00298         $aDirs   = $this->getTemplateDirs();
00299         $sDir    = reset($aDirs);
00300         return md5( $sDir.'__'.$sShopId );
00301     }
00302 
00308     public function getSmartyDir()
00309     {
00310         $myConfig = $this->getConfig();
00311 
00312         //check for the Smarty dir
00313         $sCompileDir = $myConfig->getConfigParam( 'sCompileDir' );
00314         $sSmartyDir = $sCompileDir . "/smarty/";
00315         if ( !is_dir( $sSmartyDir ) ) {
00316             @mkdir($sSmartyDir);
00317         }
00318 
00319         if ( !is_writable($sSmartyDir) ) {
00320             $sSmartyDir = $sCompileDir;
00321         }
00322 
00323         return $sSmartyDir;
00324     }
00325 
00333     protected function _fillCommonSmartyProperties( $oSmarty )
00334     {
00335         $myConfig = $this->getConfig();
00336         $oSmarty->left_delimiter  = '[{';
00337         $oSmarty->right_delimiter = '}]';
00338 
00339         $oSmarty->register_resource( 'ox', array( 'ox_get_template',
00340                                                   'ox_get_timestamp',
00341                                                   'ox_get_secure',
00342                                                   'ox_get_trusted' ) );
00343 
00344         $sSmartyDir = $this->getSmartyDir();
00345 
00346         // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
00347         $oSmarty->caching      = false;
00348         $oSmarty->compile_dir  = $sSmartyDir;
00349         $oSmarty->cache_dir    = $sSmartyDir;
00350         $oSmarty->template_dir = $this->getTemplateDirs();
00351         $oSmarty->compile_id   = $this->getTemplateCompileId();
00352 
00353         $oSmarty->default_template_handler_func = array(oxRegistry::get("oxUtilsView"),'_smartyDefaultTemplateHandler');
00354 
00355         include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
00356         $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00357 
00358         $iDebug = $myConfig->getConfigParam( 'iDebug' );
00359         if (  $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
00360             $oSmarty->debugging = true;
00361         }
00362 
00363         if ($iDebug == 8 && !$myConfig->isAdmin()) {
00364             include_once getShopBasePath().'core/smarty/plugins/prefilter.oxtpldebug.php';
00365             $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
00366         }
00367 
00368         //demoshop security
00369         if ( !$myConfig->isDemoShop() ) {
00370             $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
00371             $oSmarty->security     = false;
00372         } else {
00373             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00374             $oSmarty->security     = true;
00375             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00376             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00377             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00378             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00379             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00380             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00381             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00382             $oSmarty->secure_dir = $oSmarty->template_dir;
00383         }
00384     }
00385 
00393     protected function _smartyCompileCheck( $oSmarty )
00394     {
00395         $myConfig = $this->getConfig();
00396         $oSmarty->compile_check  = $myConfig->getConfigParam( 'blCheckTemplates' );
00397 
00398     }
00399 
00411     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
00412     {
00413         $myConfig = oxRegistry::getConfig();
00414         if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
00415             $sResourceName      = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00416             $sResourceContent   = $oSmarty->_read_file($sResourceName);
00417             $sResourceTimestamp = filemtime($sResourceName);
00418             return is_file($sResourceName) && is_readable($sResourceName);
00419         }
00420         return false;
00421     }
00422 
00434     protected function _getTemplateBlock($sModule, $sFile)
00435     {
00436         $aModuleInfo = $this->_getActiveModuleInfo();
00437         $sModulePath = $aModuleInfo[$sModule];
00438         // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
00439         if ( substr($sFile, -4) != '.tpl' ) {
00440             $sFile = $sFile . ".tpl";
00441         }
00442         // for < 4.6 modules, since 4.7/5.0 insert in oxtplblocks the full file name and path
00443         if ( basename($sFile) == $sFile ) {
00444             $sFile = "out/blocks/$sFile";
00445         }
00446         $sFileName = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModulePath/$sFile";
00447         if ( file_exists($sFileName) && is_readable($sFileName) ) {
00448             return file_get_contents($sFileName);
00449         } else {
00450             throw oxNew( "oxException", "Template block file ($sFileName) not found for '$sModule' module." );
00451         }
00452     }
00453 
00463     public function getTemplateBlocks($sFile)
00464     {
00465         $oConfig = $this->getConfig();
00466 
00467         $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00468         $sFile = str_replace(array('\\', '//'), '/', $sFile);
00469         if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
00470             $sFile = $m[1];
00471         }
00472 
00473         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00474         $sFileParam = $oDb->quote($sFile);
00475         $sShpIdParam = $oDb->quote($oConfig->getShopId());
00476         $aRet = array();
00477         $aIds = array();
00478 
00479         if ( $this->_blIsTplBlocks === null ) {
00480             $this->_blIsTplBlocks = false;
00481             $aIds = $this->_getActiveModuleInfo();
00482             if (count($aIds)) {
00483                 $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) ";
00484                 $rs = $oDb->getOne( $sSql );
00485                 if ( $rs ) {
00486                     $this->_blIsTplBlocks = true;
00487                 }
00488             }
00489         }
00490 
00491         if ( $this->_blIsTplBlocks ) {
00492             $aIds = $this->_getActiveModuleInfo();
00493             if (count($aIds)) {
00494                 $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";
00495                 $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
00496                 $rs = $oDb->select( $sSql );
00497 
00498                 if ($rs != false && $rs->recordCount() > 0) {
00499                     while (!$rs->EOF) {
00500                         try {
00501                             if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00502                                 $aRet[$rs->fields['OXBLOCKNAME']] = array();
00503                             }
00504                             $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00505                         } catch (oxException $oE) {
00506                             $oE->debugOut();
00507                         }
00508                         $rs->moveNext();
00509                     }
00510                 }
00511             }
00512         }
00513 
00514         return $aRet;
00515     }
00516 
00522     protected function _getActiveModuleInfo()
00523     {
00524         if ($this->_aActiveModuleInfo === null) {
00525             $oModulelist = oxNew('oxmodulelist');
00526             $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00527         }
00528         return $this->_aActiveModuleInfo;
00529     }
00530 
00531 }