OXID eShop CE  4.10.4
 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  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['MODIFIER_FUNCS'][] = 'getimagesize';
366  $oSmarty->security_settings['ALLOW_CONSTANTS'] = true;
367  $oSmarty->secure_dir = $oSmarty->template_dir;
368  }
369  }
370 
376  protected function _smartyCompileCheck($oSmarty)
377  {
378  $myConfig = $this->getConfig();
379  $oSmarty->compile_check = $myConfig->getConfigParam('blCheckTemplates');
380 
381  }
382 
394  public function _smartyDefaultTemplateHandler($sResourceType, $sResourceName, &$sResourceContent, &$sResourceTimestamp, $oSmarty)
395  {
397  if ($sResourceType == 'file' && !is_readable($sResourceName)) {
398  $sResourceName = $myConfig->getTemplatePath($sResourceName, $myConfig->isAdmin());
399  $sResourceContent = $oSmarty->_read_file($sResourceName);
400  $sResourceTimestamp = filemtime($sResourceName);
401 
402  return is_file($sResourceName) && is_readable($sResourceName);
403  }
404 
405  return false;
406  }
407 
419  protected function _getTemplateBlock($sModule, $sFile)
420  {
421  $aModuleInfo = $this->_getActiveModuleInfo();
422  $sModulePath = $aModuleInfo[$sModule];
423  // for 4.5 modules, since 4.6 insert in oxtplblocks the full file name
424  if (substr($sFile, -4) != '.tpl') {
425  $sFile = $sFile . ".tpl";
426  }
427  // for < 4.6 modules, since 4.7/5.0 insert in oxtplblocks the full file name and path
428  if (basename($sFile) == $sFile) {
429  $sFile = "out/blocks/$sFile";
430  }
431  $sFileName = $this->getConfig()->getConfigParam('sShopDir') . "/modules/$sModulePath/$sFile";
432  if (file_exists($sFileName) && is_readable($sFileName)) {
433  return file_get_contents($sFileName);
434  } else {
436  $oException = oxNew("oxException", "Template block file ($sFileName) not found for '$sModule' module.");
437  throw $oException;
438  }
439  }
440 
450  public function getTemplateBlocks($sFile)
451  {
452  $oConfig = $this->getConfig();
453 
454  $sTplDir = trim($oConfig->getConfigParam('_sTemplateDir'), '/\\');
455  $sFile = str_replace(array('\\', '//'), '/', $sFile);
456  if (preg_match('@/' . preg_quote($sTplDir, '@') . '/(.*)$@', $sFile, $m)) {
457  $sFile = $m[1];
458  }
459 
461  $sFileParam = $oDb->quote($sFile);
462  $sShpIdParam = $oDb->quote($oConfig->getShopId());
463  $aRet = array();
464  $aIds = array();
465 
466  if ($this->_blIsTplBlocks === null) {
467  $this->_blIsTplBlocks = false;
468  $aIds = $this->_getActiveModuleInfo();
469  if (count($aIds)) {
470  $sSql = "select COUNT(*) from oxtplblocks where oxactive=1 and oxshopid=$sShpIdParam and oxmodule in ( " . implode(", ", oxDb::getInstance()->quoteArray(array_keys($aIds))) . " ) ";
471  $rs = $oDb->getOne($sSql);
472  if ($rs) {
473  $this->_blIsTplBlocks = true;
474  }
475  }
476  }
477 
478  if ($this->_blIsTplBlocks) {
479  $aIds = $this->_getActiveModuleInfo();
480  if (count($aIds)) {
481  $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";
482  $oDb->setFetchMode(oxDb::FETCH_MODE_ASSOC);
483  $rs = $oDb->select($sSql);
484 
485  if ($rs != false && $rs->recordCount() > 0) {
486  while (!$rs->EOF) {
487  try {
488  if (!is_array($aRet[$rs->fields['OXBLOCKNAME']])) {
489  $aRet[$rs->fields['OXBLOCKNAME']] = array();
490  }
491  $aRet[$rs->fields['OXBLOCKNAME']][] = $this->_getTemplateBlock($rs->fields['OXMODULE'], $rs->fields['OXFILE']);
492  } catch (oxException $oE) {
493  $oE->debugOut();
494  }
495  $rs->moveNext();
496  }
497  }
498  }
499  }
500 
501  return $aRet;
502  }
503 
509  protected function _getActiveModuleInfo()
510  {
511  if ($this->_aActiveModuleInfo === null) {
512  $oModulelist = oxNew('oxmodulelist');
513  $this->_aActiveModuleInfo = $oModulelist->getActiveModuleInfo();
514  }
515 
517  }
518 }