OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxutilsview.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsView extends oxSuperCfg
7 {
13  private static $_instance = null;
14 
20  protected static $_oSmarty = null;
21 
27  protected $_aTemplateDir = array();
28 
34  protected $_blIsTplBlocks = null;
35 
41  protected $_aActiveModuleInfo = null;
42 
50  public static function getInstance()
51  {
52  return oxRegistry::get("oxUtilsView");
53  }
54 
64  public function getSmarty( $blReload = false )
65  {
66  if ( !self::$_oSmarty || $blReload ) {
67  self::$_oSmarty = new Smarty();
68  $this->_fillCommonSmartyProperties( self::$_oSmarty );
69  $this->_smartyCompileCheck( self::$_oSmarty );
70  }
71 
72  return self::$_oSmarty;
73  }
74 
84  public function getTemplateOutput( $sTemplate, $oObject )
85  {
86  $oSmarty = $this->getSmarty();
87  $iDebug = $this->getConfig()->getConfigParam( 'iDebug' );
88 
89  // assign
90  $aViewData = $oObject->getViewData();
91  if ( is_array( $aViewData ) ) {
92  foreach ( array_keys( $aViewData ) as $sViewName ) {
93  // show debug information
94  if ( $iDebug == 4 ) {
95  echo( "TemplateData[$sViewName] : \n");
96  var_export( $aViewData[$sViewName] );
97  }
98  $oSmarty->assign_by_ref( $sViewName, $aViewData[$sViewName] );
99  }
100  }
101 
102  return $oSmarty->fetch( $sTemplate );
103  }
104 
113  public function passAllErrorsToView( &$aView, $aErrors )
114  {
115  if ( count( $aErrors ) > 0 ) {
116  foreach ( $aErrors as $sLocation => $aEx2 ) {
117  foreach ( $aEx2 as $sKey => $oEr ) {
118  $aView['Errors'][$sLocation][$sKey] = unserialize( $oEr );
119  }
120  }
121  }
122  }
123 
137  public function addErrorToDisplay( $oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "", $sActiveController = "" )
138  {
139  if ( $blCustomDestination && ( oxConfig::getParameter( 'CustomError' ) || $sCustomDestination!= '' ) ) {
140  // check if the current request wants do display exceptions on its own
141  $sDestination = oxConfig::getParameter( 'CustomError' );
142  if ( $sCustomDestination != '' ) {
143  $sDestination = $sCustomDestination;
144  }
145  } else {
146  //default
147  $sDestination = 'default';
148  }
149 
150  //starting session if not yet started as all exception
151  //messages are stored in session
152  $oSession = $this->getSession();
153  if ( !$oSession->getId() && !$oSession->isHeaderSent() ) {
154  $oSession->setForceNewSession();
155  $oSession->start();
156  }
157 
158  $aEx = oxSession::getVar( 'Errors' );
159  if ( $oEr instanceof oxException ) {
160  $oEx = oxNew( 'oxExceptionToDisplay' );
161  $oEx->setMessage( $oEr->getMessage() );
162  $oEx->setExceptionType( get_class( $oEr ) );
163 
164  if ( $oEr instanceof oxSystemComponentException ) {
165  $oEx->setMessageArgs( $oEr->getComponent() );
166  }
167 
168  $oEx->setValues( $oEr->getValues() );
169  $oEx->setStackTrace( $oEr->getTraceAsString() );
170  $oEx->setDebug( $blFull );
171  $oEr = $oEx;
172  } elseif ( $oEr && ! ( $oEr instanceof oxIDisplayError ) ) {
173  // assuming that a string was given
174  $sTmp = $oEr;
175  $oEr = oxNew( 'oxDisplayError' );
176  $oEr->setMessage( $sTmp );
177  } elseif ( $oEr instanceof oxIDisplayError ) {
178  // take the object
179  } else {
180  $oEr = null;
181  }
182 
183  if ( $oEr ) {
184  $aEx[$sDestination][] = serialize( $oEr );
185  oxRegistry::getSession()->setVariable( 'Errors', $aEx );
186 
187  if ( $sActiveController == '' ) {
188  $sActiveController = oxRegistry::getConfig()->getRequestParameter( 'actcontrol' );
189  }
190  if ( $sActiveController ) {
191  $aControllerErrors[$sDestination] = $sActiveController;
192  oxRegistry::getSession()->setVariable( 'ErrorController', $aControllerErrors );
193  }
194  }
195  }
196 
209  public function parseThroughSmarty( $sDesc, $sOxid = null, $oActView = null, $blRecompile = false )
210  {
211  if ( oxRegistry::getConfig()->isDemoShop() ) {
212  return $sDesc;
213  }
214 
215  startProfile("parseThroughSmarty");
216 
217  if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
218  stopProfile("parseThroughSmarty");
219  return $sDesc;
220  }
221 
222  $iLang = oxRegistry::getLang()->getTplLanguage();
223 
224  // now parse it through smarty
225  $oSmarty = clone $this->getSmarty();
226 
227  // save old tpl data
228  $sTplVars = $oSmarty->_tpl_vars;
229  $blForceRecompile = $oSmarty->force_compile;
230 
231  $oSmarty->force_compile = $blRecompile;
232 
233  if ( !$oActView ) {
234  $oActView = oxNew( 'oxubase' );
235  $oActView->addGlobalParams();
236  }
237 
238  $aViewData = $oActView->getViewData();
239  foreach ( array_keys( $aViewData ) as $sName ) {
240  $oSmarty->assign_by_ref( $sName, $aViewData[$sName] );
241  }
242 
243  if ( is_array( $sDesc ) ) {
244  foreach ( $sDesc as $sName => $aData ) {
245  $oSmarty->oxidcache = new oxField( $aData[1], oxField::T_RAW );
246  $sRes[$sName] = $oSmarty->fetch( "ox:".$aData[0].$iLang );
247  }
248  } else {
249  $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
250  $sRes = $oSmarty->fetch( "ox:{$sOxid}{$iLang}" );
251  }
252 
253  // restore tpl vars for continuing smarty processing if it is in one
254  $oSmarty->_tpl_vars = $sTplVars;
255  $oSmarty->force_compile = $blForceRecompile;
256 
257  stopProfile("parseThroughSmarty");
258 
259  return $sRes;
260  }
261 
269  public function setTemplateDir( $sTplDir )
270  {
271  if ( $sTplDir && !in_array( $sTplDir, $this->_aTemplateDir ) ) {
272  $this->_aTemplateDir[] = $sTplDir;
273  }
274  }
275 
281  public function getTemplateDirs()
282  {
284 
285  //T2010-01-13
286  //#1531
287  $this->setTemplateDir( $myConfig->getTemplateDir( $this->isAdmin() ) );
288 
289  if ( !$this->isAdmin() ) {
290  $this->setTemplateDir( $myConfig->getOutDir( true ) . $myConfig->getConfigParam( 'sTheme' ) . "/tpl/" );
291  }
292 
293  return $this->_aTemplateDir;
294  }
295 
301  public function getTemplateCompileId()
302  {
303  $sShopId = $this->getConfig()->getShopId();
304  $aDirs = $this->getTemplateDirs();
305  $sDir = reset($aDirs);
306  return md5( $sDir.'__'.$sShopId );
307  }
308 
314  public function getSmartyDir()
315  {
316  $myConfig = $this->getConfig();
317 
318  //check for the Smarty dir
319  $sCompileDir = $myConfig->getConfigParam( 'sCompileDir' );
320  $sSmartyDir = $sCompileDir . "/smarty/";
321  if ( !is_dir( $sSmartyDir ) ) {
322  @mkdir($sSmartyDir);
323  }
324 
325  if ( !is_writable($sSmartyDir) ) {
326  $sSmartyDir = $sCompileDir;
327  }
328 
329  return $sSmartyDir;
330  }
331 
339  protected function _fillCommonSmartyProperties( $oSmarty )
340  {
341  $myConfig = $this->getConfig();
342  $oSmarty->left_delimiter = '[{';
343  $oSmarty->right_delimiter = '}]';
344 
345  $oSmarty->register_resource( 'ox', array( 'ox_get_template',
346  'ox_get_timestamp',
347  'ox_get_secure',
348  'ox_get_trusted' ) );
349 
350  $sSmartyDir = $this->getSmartyDir();
351 
352  // $myConfig->blTemplateCaching; // DODGER #655 : permanently switched off as it doesnt work good enough
353  $oSmarty->caching = false;
354  $oSmarty->compile_dir = $sSmartyDir;
355  $oSmarty->cache_dir = $sSmartyDir;
356  $oSmarty->template_dir = $this->getTemplateDirs();
357  $oSmarty->compile_id = $this->getTemplateCompileId();
358 
359  $oSmarty->default_template_handler_func = array(oxRegistry::get("oxUtilsView"),'_smartyDefaultTemplateHandler');
360 
361  include_once dirname(__FILE__).'/smarty/plugins/prefilter.oxblock.php';
362  $oSmarty->register_prefilter('smarty_prefilter_oxblock');
363 
364  $iDebug = $myConfig->getConfigParam( 'iDebug' );
365  if ( $iDebug == 1 || $iDebug == 3 || $iDebug == 4 ) {
366  $oSmarty->debugging = true;
367  }
368 
369  if ($iDebug == 8 && !$myConfig->isAdmin()) {
370  include_once getShopBasePath().'core/smarty/plugins/prefilter.oxtpldebug.php';
371  $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
372  }
373 
374  //demoshop security
375  if ( !$myConfig->isDemoShop() ) {
376  $oSmarty->php_handling = (int) $myConfig->getConfigParam( 'iSmartyPhpHandling' );
377  $oSmarty->security = false;
378  } else {
379  $oSmarty->php_handling = SMARTY_PHP_REMOVE;
380  $oSmarty->security = true;
381  $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
382  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
383  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
384  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
385  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
386  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
387  $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
388  $oSmarty->secure_dir = $oSmarty->template_dir;
389  }
390  }
391 
399  protected function _smartyCompileCheck( $oSmarty )
400  {
401  $myConfig = $this->getConfig();
402  $oSmarty->compile_check = $myConfig->getConfigParam( 'blCheckTemplates' );
403 
404  }
405 
417  public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
418  {
420  if ( $sResourceType == 'file' && !is_readable($sResourceName) ) {
421  $sResourceName = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
422  $sResourceContent = $oSmarty->_read_file($sResourceName);
423  $sResourceTimestamp = filemtime($sResourceName);
424  return is_file($sResourceName) && is_readable($sResourceName);
425  }
426  return false;
427  }
428 
440  protected function _getTemplateBlock($sModule, $sFile)
441  {
442  $aModuleInfo = $this->_getActiveModuleInfo();
443  $sModulePath = $aModuleInfo[$sModule];
444  // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
445  if ( substr($sFile, -4) != '.tpl' ) {
446  $sFile = $sFile . ".tpl";
447  }
448  // for < 4.6 modules, since 4.7/5.0 insert in oxtplblocks the full file name and path
449  if ( basename($sFile) == $sFile ) {
450  $sFile = "out/blocks/$sFile";
451  }
452  $sFileName = $this->getConfig()->getConfigParam( 'sShopDir' )."/modules/$sModulePath/$sFile";
453  if ( file_exists($sFileName) && is_readable($sFileName) ) {
454  return file_get_contents($sFileName);
455  } else {
456  throw oxNew( "oxException", "Template block file ($sFileName) not found for '$sModule' module." );
457  }
458  }
459 
469  public function getTemplateBlocks($sFile)
470  {
471  $oConfig = $this->getConfig();
472 
473  $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
474  $sFile = str_replace(array('\\', '//'), '/', $sFile);
475  if (preg_match('@/'.preg_quote($sTplDir, '@').'/(.*)$@', $sFile, $m)) {
476  $sFile = $m[1];
477  }
478 
480  $sFileParam = $oDb->quote($sFile);
481  $sShpIdParam = $oDb->quote($oConfig->getShopId());
482  $aRet = array();
483  $aIds = array();
484 
485  if ( $this->_blIsTplBlocks === null ) {
486  $this->_blIsTplBlocks = false;
487  $aIds = $this->_getActiveModuleInfo();
488  if (count($aIds)) {
489  $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds)) ) . " ) ";
490  $rs = $oDb->getOne( $sSql );
491  if ( $rs ) {
492  $this->_blIsTplBlocks = true;
493  }
494  }
495  }
496 
497  if ( $this->_blIsTplBlocks ) {
498  $aIds = $this->_getActiveModuleInfo();
499  if (count($aIds)) {
500  $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";
501  $oDb->setFetchMode( oxDb::FETCH_MODE_ASSOC );
502  $rs = $oDb->select( $sSql );
503 
504  if ($rs != false && $rs->recordCount() > 0) {
505  while (!$rs->EOF) {
506  try {
507  if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
508  $aRet[$rs->fields['OXBLOCKNAME']] = array();
509  }
510  $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
511  } catch (oxException $oE) {
512  $oE->debugOut();
513  }
514  $rs->moveNext();
515  }
516  }
517  }
518  }
519 
520  return $aRet;
521  }
522 
528  protected function _getActiveModuleInfo()
529  {
530  if ($this->_aActiveModuleInfo === null) {
531  $oModulelist = oxNew('oxmodulelist');
532  $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
533  }
535  }
536 
537 }