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