OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxutilsview.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsView extends oxSuperCfg
7 {
8 
14  protected static $_oSmarty = null;
15 
21  protected $_aTemplateDir = array();
22 
28  protected $_blIsTplBlocks = null;
29 
35  protected $_aActiveModuleInfo = null;
36 
46  public function getSmarty($blReload = false)
47  {
48  if (!self::$_oSmarty || $blReload) {
49  self::$_oSmarty = new Smarty();
50  $this->_fillCommonSmartyProperties(self::$_oSmarty);
51  $this->_smartyCompileCheck(self::$_oSmarty);
52  }
53 
54  return self::$_oSmarty;
55  }
56 
66  public function getTemplateOutput($sTemplate, $oObject)
67  {
68  $oSmarty = $this->getSmarty();
69  $iDebug = $this->getConfig()->getConfigParam('iDebug');
70 
71  // assign
72  $aViewData = $oObject->getViewData();
73  if (is_array($aViewData)) {
74  foreach (array_keys($aViewData) as $sViewName) {
75  // show debug information
76  if ($iDebug == 4) {
77  echo("TemplateData[$sViewName] : \n");
78  var_export($aViewData[$sViewName]);
79  }
80  $oSmarty->assign_by_ref($sViewName, $aViewData[$sViewName]);
81  }
82  }
83 
84  return $oSmarty->fetch($sTemplate);
85  }
86 
93  public function passAllErrorsToView(&$aView, $aErrors)
94  {
95  if (count($aErrors) > 0) {
96  foreach ($aErrors as $sLocation => $aEx2) {
97  foreach ($aEx2 as $sKey => $oEr) {
98  $aView['Errors'][$sLocation][$sKey] = unserialize($oEr);
99  }
100  }
101  }
102  }
103 
115  public function addErrorToDisplay($oEr, $blFull = false, $blCustomDestination = false, $sCustomDestination = "", $sActiveController = "")
116  {
117  if ($blCustomDestination && (oxRegistry::getConfig()->getRequestParameter('CustomError') || $sCustomDestination != '')) {
118  // check if the current request wants do display exceptions on its own
119  $sDestination = oxRegistry::getConfig()->getRequestParameter('CustomError');
120  if ($sCustomDestination != '') {
121  $sDestination = $sCustomDestination;
122  }
123  } else {
124  //default
125  $sDestination = 'default';
126  }
127 
128  //starting session if not yet started as all exception
129  //messages are stored in session
130  $oSession = $this->getSession();
131  if (!$oSession->getId() && !$oSession->isHeaderSent()) {
132  $oSession->setForceNewSession();
133  $oSession->start();
134  }
135 
136  $aEx = oxRegistry::getSession()->getVariable('Errors');
137  if ($oEr instanceof oxException) {
138  $oEx = oxNew('oxExceptionToDisplay');
139  $oEx->setMessage($oEr->getMessage());
140  $oEx->setExceptionType(get_class($oEr));
141 
142  if ($oEr instanceof oxSystemComponentException) {
143  $oEx->setMessageArgs($oEr->getComponent());
144  }
145 
146  $oEx->setValues($oEr->getValues());
147  $oEx->setStackTrace($oEr->getTraceAsString());
148  $oEx->setDebug($blFull);
149  $oEr = $oEx;
150  } elseif ($oEr && !($oEr instanceof oxIDisplayError)) {
151  // assuming that a string was given
152  $sTmp = $oEr;
153  $oEr = oxNew('oxDisplayError');
154  $oEr->setMessage($sTmp);
155  } elseif ($oEr instanceof oxIDisplayError) {
156  // take the object
157  } else {
158  $oEr = null;
159  }
160 
161  if ($oEr) {
162  $aEx[$sDestination][] = serialize($oEr);
163  oxRegistry::getSession()->setVariable('Errors', $aEx);
164 
165  if ($sActiveController == '') {
166  $sActiveController = oxRegistry::getConfig()->getRequestParameter('actcontrol');
167  }
168  if ($sActiveController) {
169  $aControllerErrors[$sDestination] = $sActiveController;
170  oxRegistry::getSession()->setVariable('ErrorController', $aControllerErrors);
171  }
172  }
173  }
174 
187  public function parseThroughSmarty($sDesc, $sOxid = null, $oActView = null, $blRecompile = false)
188  {
189  if (oxRegistry::getConfig()->isDemoShop()) {
190  return $sDesc;
191  }
192 
193  startProfile("parseThroughSmarty");
194 
195  if (!is_array($sDesc) && strpos($sDesc, "[{") === false) {
196  stopProfile("parseThroughSmarty");
197 
198  return $sDesc;
199  }
200 
201  $iLang = oxRegistry::getLang()->getTplLanguage();
202 
203  // now parse it through smarty
204  $oSmarty = clone $this->getSmarty();
205 
206  // save old tpl data
207  $sTplVars = $oSmarty->_tpl_vars;
208  $blForceRecompile = $oSmarty->force_compile;
209 
210  $oSmarty->force_compile = $blRecompile;
211 
212  if (!$oActView) {
213  $oActView = oxNew('oxubase');
214  $oActView->addGlobalParams();
215  }
216 
217  $aViewData = $oActView->getViewData();
218  foreach (array_keys($aViewData) as $sName) {
219  $oSmarty->assign_by_ref($sName, $aViewData[$sName]);
220  }
221 
222  if (is_array($sDesc)) {
223  foreach ($sDesc as $sName => $aData) {
224  $oSmarty->oxidcache = new oxField($aData[1], oxField::T_RAW);
225  $sRes[$sName] = $oSmarty->fetch("ox:" . $aData[0] . $iLang);
226  }
227  } else {
228  $oSmarty->oxidcache = new oxField($sDesc, oxField::T_RAW);
229  $sRes = $oSmarty->fetch("ox:{$sOxid}{$iLang}");
230  }
231 
232  // restore tpl vars for continuing smarty processing if it is in one
233  $oSmarty->_tpl_vars = $sTplVars;
234  $oSmarty->force_compile = $blForceRecompile;
235 
236  stopProfile("parseThroughSmarty");
237 
238  return $sRes;
239  }
240 
246  public function setTemplateDir($sTplDir)
247  {
248  if ($sTplDir && !in_array($sTplDir, $this->_aTemplateDir)) {
249  $this->_aTemplateDir[] = $sTplDir;
250  }
251  }
252 
258  public function getTemplateDirs()
259  {
261 
262  //T2010-01-13
263  //#1531
264  $this->setTemplateDir($myConfig->getTemplateDir($this->isAdmin()));
265 
266  if (!$this->isAdmin()) {
267  $this->setTemplateDir($myConfig->getOutDir(true) . $myConfig->getConfigParam('sTheme') . "/tpl/");
268  }
269 
270  return $this->_aTemplateDir;
271  }
272 
278  public function getTemplateCompileId()
279  {
280  $sShopId = $this->getConfig()->getShopId();
281  $aDirs = $this->getTemplateDirs();
282  $sDir = reset($aDirs);
283 
284  return md5($sDir . '__' . $sShopId);
285  }
286 
292  public function getSmartyDir()
293  {
294  $myConfig = $this->getConfig();
295 
296  //check for the Smarty dir
297  $sCompileDir = $myConfig->getConfigParam('sCompileDir');
298  $sSmartyDir = $sCompileDir . "/smarty/";
299  if (!is_dir($sSmartyDir)) {
300  @mkdir($sSmartyDir);
301  }
302 
303  if (!is_writable($sSmartyDir)) {
304  $sSmartyDir = $sCompileDir;
305  }
306 
307  return $sSmartyDir;
308  }
309 
315  protected function _fillCommonSmartyProperties($oSmarty)
316  {
317  $myConfig = $this->getConfig();
318  $oSmarty->left_delimiter = '[{';
319  $oSmarty->right_delimiter = '}]';
320 
321  $oSmarty->register_resource(
322  'ox', array('ox_get_template',
323  'ox_get_timestamp',
324  'ox_get_secure',
325  'ox_get_trusted')
326  );
327 
328  $sSmartyDir = $this->getSmartyDir();
329 
330  $oSmarty->caching = false;
331  $oSmarty->compile_dir = $sSmartyDir;
332  $oSmarty->cache_dir = $sSmartyDir;
333  $oSmarty->template_dir = $this->getTemplateDirs();
334  $oSmarty->compile_id = $this->getTemplateCompileId();
335 
336  $oSmarty->default_template_handler_func = array(oxRegistry::get("oxUtilsView"), '_smartyDefaultTemplateHandler');
337 
338  include_once dirname(__FILE__) . '/smarty/plugins/prefilter.oxblock.php';
339  $oSmarty->register_prefilter('smarty_prefilter_oxblock');
340 
341  $iDebug = $myConfig->getConfigParam('iDebug');
342  if ($iDebug == 1 || $iDebug == 3 || $iDebug == 4) {
343  $oSmarty->debugging = true;
344  }
345 
346  if ($iDebug == 8 && !$myConfig->isAdmin()) {
347  include_once getShopBasePath() . 'core/smarty/plugins/prefilter.oxtpldebug.php';
348  $oSmarty->register_prefilter('smarty_prefilter_oxtpldebug');
349  }
350 
351  //demo shop security
352  if (!$myConfig->isDemoShop()) {
353  $oSmarty->php_handling = (int) $myConfig->getConfigParam('iSmartyPhpHandling');
354  $oSmarty->security = false;
355  } else {
356  $oSmarty->php_handling = SMARTY_PHP_REMOVE;
357  $oSmarty->security = true;
358  $oSmarty->security_settings['IF_FUNCS'][] = 'XML_ELEMENT_NODE';
359  $oSmarty->security_settings['IF_FUNCS'][] = 'is_int';
360  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'round';
361  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'floor';
362  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'trim';
363  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'implode';
364  $oSmarty->security_settings['MODIFIER_FUNCS'][] = 'is_array';
365  $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
366  $oSmarty->secure_dir = $oSmarty->template_dir;
367  }
368  }
369 
375  protected function _smartyCompileCheck($oSmarty)
376  {
377  $myConfig = $this->getConfig();
378  $oSmarty->compile_check = $myConfig->getConfigParam('blCheckTemplates');
379 
380  }
381 
393  public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
394  {
396  if ($sResourceType == 'file' && !is_readable($sResourceName)) {
397  $sResourceName = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
398  $sResourceContent = $oSmarty->_read_file($sResourceName);
399  $sResourceTimestamp = filemtime($sResourceName);
400 
401  return is_file($sResourceName) && is_readable($sResourceName);
402  }
403 
404  return false;
405  }
406 
418  protected function _getTemplateBlock($sModule, $sFile)
419  {
420  $aModuleInfo = $this->_getActiveModuleInfo();
421  $sModulePath = $aModuleInfo[$sModule];
422  // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
423  if (substr($sFile, -4) != '.tpl') {
424  $sFile = $sFile . ".tpl";
425  }
426  // for < 4.6 modules, since 4.7/5.0 insert in oxtplblocks the full file name and path
427  if (basename($sFile) == $sFile) {
428  $sFile = "out/blocks/$sFile";
429  }
430  $sFileName = $this->getConfig()->getConfigParam('sShopDir') . "/modules/$sModulePath/$sFile";
431  if (file_exists($sFileName) && is_readable($sFileName)) {
432  return file_get_contents($sFileName);
433  } else {
435  $oException = oxNew("oxException", "Template block file ($sFileName) not found for '$sModule' module.");
436  throw $oException;
437  }
438  }
439 
449  public function getTemplateBlocks($sFile)
450  {
451  $oConfig = $this->getConfig();
452 
453  $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
454  $sFile = str_replace(array('\\', '//'), '/', $sFile);
455  if (preg_match('@/' . preg_quote($sTplDir, '@') . '/(.*)$@', $sFile, $m)) {
456  $sFile = $m[1];
457  }
458 
460  $sFileParam = $oDb->quote($sFile);
461  $sShpIdParam = $oDb->quote($oConfig->getShopId());
462  $aRet = array();
463  $aIds = array();
464 
465  if ($this->_blIsTplBlocks === null) {
466  $this->_blIsTplBlocks = false;
467  $aIds = $this->_getActiveModuleInfo();
468  if (count($aIds)) {
469  $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds))) . " ) ";
470  $rs = $oDb->getOne($sSql);
471  if ($rs) {
472  $this->_blIsTplBlocks = true;
473  }
474  }
475  }
476 
477  if ($this->_blIsTplBlocks) {
478  $aIds = $this->_getActiveModuleInfo();
479  if (count($aIds)) {
480  $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";
481  $oDb->setFetchMode(oxDb::FETCH_MODE_ASSOC);
482  $rs = $oDb->select($sSql);
483 
484  if ($rs != false && $rs->recordCount() > 0) {
485  while (!$rs->EOF) {
486  try {
487  if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
488  $aRet[$rs->fields['OXBLOCKNAME']] = array();
489  }
490  $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
491  } catch (oxException $oE) {
492  $oE->debugOut();
493  }
494  $rs->moveNext();
495  }
496  }
497  }
498  }
499 
500  return $aRet;
501  }
502 
508  protected function _getActiveModuleInfo()
509  {
510  if ($this->_aActiveModuleInfo === null) {
511  $oModulelist = oxNew('oxmodulelist');
512  $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
513  }
514 
516  }
517 }