oxutilsview.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsView extends oxSuperCfg
00007 {
00008 
00014     protected static $_oSmarty = null;
00015 
00021     protected $_aTemplateDir = array();
00022 
00028     protected $_blIsTplBlocks = null;
00029 
00035     protected $_aActiveModuleInfo = null;
00036 
00046     public function getSmarty($blReload = false)
00047     {
00048         if (!self::$_oSmarty || $blReload) {
00049             self::$_oSmarty = new Smarty();
00050             $this->_fillCommonSmartyProperties(self::$_oSmarty);
00051             $this->_smartyCompileCheck(self::$_oSmarty);
00052         }
00053 
00054         return self::$_oSmarty;
00055     }
00056 
00066     public function getTemplateOutput($sTemplate, $oObject)
00067     {
00068         $oSmarty = $this->getSmarty();
00069         $iDebug = $this->getConfig()->getConfigParam('iDebug');
00070 
00071         // assign
00072         $aViewData = $oObject->getViewData();
00073         if (is_array($aViewData)) {
00074             foreach (array_keys($aViewData) as $sViewName) {
00075                 // show debug information
00076                 if ($iDebug == 4) {
00077                     echo("TemplateData[$sViewName] : \n");
00078                     var_export($aViewData[$sViewName]);
00079                 }
00080                 $oSmarty->assign_by_ref($sViewName, $aViewData[$sViewName]);
00081             }
00082         }
00083 
00084         return $oSmarty->fetch($sTemplate);
00085     }
00086 
00093     public function passAllErrorsToView(&$aView, $aErrors)
00094     {
00095         if (count($aErrors) > 0) {
00096             foreach ($aErrors as $sLocation => $aEx2) {
00097                 foreach ($aEx2 as $sKey => $oEr) {
00098                     $aView['Errors'][$sLocation][$sKey] = unserialize($oEr);
00099                 }
00100             }
00101         }
00102     }
00103 
00115     public function addErrorToDisplay($oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "", $sActiveController = "")
00116     {
00117         if ($blCustomDestination && (oxRegistry::getConfig()->getRequestParameter('CustomError') || $sCustomDestination != '')) {
00118             // check if the current request wants do display exceptions on its own
00119             $sDestination = oxRegistry::getConfig()->getRequestParameter('CustomError');
00120             if ($sCustomDestination != '') {
00121                 $sDestination = $sCustomDestination;
00122             }
00123         } else {
00124             //default
00125             $sDestination = 'default';
00126         }
00127 
00128         //starting session if not yet started as all exception
00129         //messages are stored in session
00130         $oSession = $this->getSession();
00131         if (!$oSession->getId() && !$oSession->isHeaderSent()) {
00132             $oSession->setForceNewSession();
00133             $oSession->start();
00134         }
00135 
00136         $aEx = oxRegistry::getSession()->getVariable('Errors');
00137         if ($oEr instanceof oxException) {
00138             $oEx = oxNew('oxExceptionToDisplay');
00139             $oEx->setMessage($oEr->getMessage());
00140             $oEx->setExceptionType(get_class($oEr));
00141 
00142             if ($oEr instanceof oxSystemComponentException) {
00143                 $oEx->setMessageArgs($oEr->getComponent());
00144             }
00145 
00146             $oEx->setValues($oEr->getValues());
00147             $oEx->setStackTrace($oEr->getTraceAsString());
00148             $oEx->setDebug($blFull);
00149             $oEr = $oEx;
00150         } elseif ($oEr && !($oEr instanceof oxIDisplayError)) {
00151             // assuming that a string was given
00152             $sTmp = $oEr;
00153             $oEr = oxNew('oxDisplayError');
00154             $oEr->setMessage($sTmp);
00155         } elseif ($oEr instanceof oxIDisplayError) {
00156             // take the object
00157         } else {
00158             $oEr = null;
00159         }
00160 
00161         if ($oEr) {
00162             $aEx[$sDestination][] = serialize($oEr);
00163             oxRegistry::getSession()->setVariable('Errors', $aEx);
00164 
00165             if ($sActiveController == '') {
00166                 $sActiveController = oxRegistry::getConfig()->getRequestParameter('actcontrol');
00167             }
00168             if ($sActiveController) {
00169                 $aControllerErrors[$sDestination] = $sActiveController;
00170                 oxRegistry::getSession()->setVariable('ErrorController', $aControllerErrors);
00171             }
00172         }
00173     }
00174 
00187     public function parseThroughSmarty($sDesc, $sOxid = null, $oActView = null, $blRecompile = false)
00188     {
00189         if (oxRegistry::getConfig()->isDemoShop()) {
00190             return $sDesc;
00191         }
00192 
00193         startProfile("parseThroughSmarty");
00194 
00195         if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
00196             stopProfile("parseThroughSmarty");
00197 
00198             return $sDesc;
00199         }
00200 
00201         $iLang = oxRegistry::getLang()->getTplLanguage();
00202 
00203         // now parse it through smarty
00204         $oSmarty = clone $this->getSmarty();
00205 
00206         // save old tpl data
00207         $sTplVars = $oSmarty->_tpl_vars;
00208         $blForceRecompile = $oSmarty->force_compile;
00209 
00210         $oSmarty->force_compile = $blRecompile;
00211 
00212         if (!$oActView) {
00213             $oActView = oxNew('oxubase');
00214             $oActView->addGlobalParams();
00215         }
00216 
00217         $aViewData = $oActView->getViewData();
00218         foreach (array_keys($aViewData) as $sName) {
00219             $oSmarty->assign_by_ref($sName, $aViewData[$sName]);
00220         }
00221 
00222         if (is_array($sDesc)) {
00223             foreach ($sDesc as $sName => $aData) {
00224                 $oSmarty->oxidcache = new oxField($aData[1], oxField::T_RAW);
00225                 $sRes[$sName] = $oSmarty->fetch("ox:" . $aData[0] . $iLang);
00226             }
00227         } else {
00228             $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
00229             $sRes = $oSmarty->fetch("ox:{$sOxid}{$iLang}");
00230         }
00231 
00232         // restore tpl vars for continuing smarty processing if it is in one
00233         $oSmarty->_tpl_vars = $sTplVars;
00234         $oSmarty->force_compile = $blForceRecompile;
00235 
00236         stopProfile("parseThroughSmarty");
00237 
00238         return $sRes;
00239     }
00240 
00246     public function setTemplateDir($sTplDir)
00247     {
00248         if ($sTplDir && !in_array($sTplDir, $this->_aTemplateDir)) {
00249             $this->_aTemplateDir[] = $sTplDir;
00250         }
00251     }
00252 
00258     public function getTemplateDirs()
00259     {
00260         $myConfig = oxRegistry::getConfig();
00261 
00262         //T2010-01-13
00263         //#1531
00264         $this->setTemplateDir($myConfig->getTemplateDir($this->isAdmin()));
00265 
00266         if (!$this->isAdmin()) {
00267             $this->setTemplateDir($myConfig->getOutDir(true) . $myConfig->getConfigParam('sTheme') . "/tpl/");
00268         }
00269 
00270         return $this->_aTemplateDir;
00271     }
00272 
00278     public function getTemplateCompileId()
00279     {
00280         $sShopId = $this->getConfig()->getShopId();
00281         $aDirs = $this->getTemplateDirs();
00282         $sDir = reset($aDirs);
00283 
00284         return md5($sDir . '__' . $sShopId);
00285     }
00286 
00292     public function getSmartyDir()
00293     {
00294         $myConfig = $this->getConfig();
00295 
00296         //check for the Smarty dir
00297         $sCompileDir = $myConfig->getConfigParam('sCompileDir');
00298         $sSmartyDir = $sCompileDir . "/smarty/";
00299         if (!is_dir($sSmartyDir)) {
00300             @mkdir($sSmartyDir);
00301         }
00302 
00303         if (!is_writable($sSmartyDir)) {
00304             $sSmartyDir = $sCompileDir;
00305         }
00306 
00307         return $sSmartyDir;
00308     }
00309 
00315     protected function _fillCommonSmartyProperties($oSmarty)
00316     {
00317         $myConfig = $this->getConfig();
00318         $oSmarty->left_delimiter = '[{';
00319         $oSmarty->right_delimiter = '}]';
00320 
00321         $oSmarty->register_resource(
00322             'ox', array('ox_get_template',
00323                         'ox_get_timestamp',
00324                         'ox_get_secure',
00325                         'ox_get_trusted')
00326         );
00327 
00328         $sSmartyDir = $this->getSmartyDir();
00329 
00330         $oSmarty->caching = false;
00331         $oSmarty->compile_dir = $sSmartyDir;
00332         $oSmarty->cache_dir = $sSmartyDir;
00333         $oSmarty->template_dir = $this->getTemplateDirs();
00334         $oSmarty->compile_id = $this->getTemplateCompileId();
00335 
00336         $oSmarty->default_template_handler_func = array(oxRegistry::get("oxUtilsView"), '_smartyDefaultTemplateHandler');
00337 
00338         include_once dirname(__FILE__) . '/smarty/plugins/prefilter.oxblock.php';
00339         $oSmarty->register_prefilter('smarty_prefilter_oxblock');
00340 
00341         $iDebug = $myConfig->getConfigParam('iDebug');
00342         if ($iDebug == 1 || $iDebug == 3 || $iDebug == 4) {
00343             $oSmarty->debugging = true;
00344         }
00345 
00346         if ($iDebug == 8 && !$myConfig->isAdmin()) {
00347             include_once getShopBasePath() . 'core/smarty/plugins/prefilter.oxtpldebug.php';
00348             $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
00349         }
00350 
00351         //demo shop security
00352         if (!$myConfig->isDemoShop()) {
00353             $oSmarty->php_handling = (int) $myConfig->getConfigParam('iSmartyPhpHandling');
00354             $oSmarty->security = false;
00355         } else {
00356             $oSmarty->php_handling = SMARTY_PHP_REMOVE;
00357             $oSmarty->security = true;
00358             $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
00359             $oSmarty->security_settings['IF_FUNCS'][] = 'is_int';
00360             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
00361             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
00362             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
00363             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
00364             $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
00365             $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
00366             $oSmarty->secure_dir = $oSmarty->template_dir;
00367         }
00368     }
00369 
00375     protected function _smartyCompileCheck($oSmarty)
00376     {
00377         $myConfig = $this->getConfig();
00378         $oSmarty->compile_check = $myConfig->getConfigParam('blCheckTemplates');
00379 
00380     }
00381 
00393     public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
00394     {
00395         $myConfig = oxRegistry::getConfig();
00396         if ($sResourceType == 'file' && !is_readable($sResourceName)) {
00397             $sResourceName = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
00398             $sResourceContent = $oSmarty->_read_file($sResourceName);
00399             $sResourceTimestamp = filemtime($sResourceName);
00400 
00401             return is_file($sResourceName) && is_readable($sResourceName);
00402         }
00403 
00404         return false;
00405     }
00406 
00418     protected function _getTemplateBlock($sModule, $sFile)
00419     {
00420         $aModuleInfo = $this->_getActiveModuleInfo();
00421         $sModulePath = $aModuleInfo[$sModule];
00422         // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
00423         if (substr($sFile, -4) != '.tpl') {
00424             $sFile = $sFile . ".tpl";
00425         }
00426         // for < 4.6 modules, since 4.7/5.0 insert in oxtplblocks the full file name and path
00427         if (basename($sFile) == $sFile) {
00428             $sFile = "out/blocks/$sFile";
00429         }
00430         $sFileName = $this->getConfig()->getConfigParam('sShopDir') . "/modules/$sModulePath/$sFile";
00431         if (file_exists($sFileName) && is_readable($sFileName)) {
00432             return file_get_contents($sFileName);
00433         } else {
00435             $oException = oxNew("oxException", "Template block file ($sFileName) not found for '$sModule' module.");
00436             throw $oException;
00437         }
00438     }
00439 
00449     public function getTemplateBlocks($sFile)
00450     {
00451         $oConfig = $this->getConfig();
00452 
00453         $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
00454         $sFile = str_replace(array('\\', '//'), '/', $sFile);
00455         if (preg_match('@/' . preg_quote($sTplDir, '@') . '/(.*)$@', $sFile, $m)) {
00456             $sFile = $m[1];
00457         }
00458 
00459         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
00460         $sFileParam = $oDb->quote($sFile);
00461         $sShpIdParam = $oDb->quote($oConfig->getShopId());
00462         $aRet = array();
00463         $aIds = array();
00464 
00465         if ($this->_blIsTplBlocks === null) {
00466             $this->_blIsTplBlocks = false;
00467             $aIds = $this->_getActiveModuleInfo();
00468             if (count($aIds)) {
00469                 $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds))) . " ) ";
00470                 $rs = $oDb->getOne($sSql);
00471                 if ($rs) {
00472                     $this->_blIsTplBlocks = true;
00473                 }
00474             }
00475         }
00476 
00477         if ($this->_blIsTplBlocks) {
00478             $aIds = $this->_getActiveModuleInfo();
00479             if (count($aIds)) {
00480                 $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";
00481                 $oDb->setFetchMode(oxDb::FETCH_MODE_ASSOC);
00482                 $rs = $oDb->select($sSql);
00483 
00484                 if ($rs != false && $rs->recordCount() > 0) {
00485                     while (!$rs->EOF) {
00486                         try {
00487                             if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
00488                                 $aRet[$rs->fields['OXBLOCKNAME']] = array();
00489                             }
00490                             $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
00491                         } catch (oxException $oE) {
00492                             $oE->debugOut();
00493                         }
00494                         $rs->moveNext();
00495                     }
00496                 }
00497             }
00498         }
00499 
00500         return $aRet;
00501     }
00502 
00508     protected function _getActiveModuleInfo()
00509     {
00510         if ($this->_aActiveModuleInfo === null) {
00511             $oModulelist = oxNew('oxmodulelist');
00512             $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
00513         }
00514 
00515         return $this->_aActiveModuleInfo;
00516     }
00517 }