[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/includes/runtime/ -> Viewer.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.1
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  
  11  vimport ('~/libraries/Smarty/libs/SmartyBC.class.php');
  12  
  13  class Vtiger_Viewer extends SmartyBC {
  14  
  15      const DEFAULTLAYOUT = 'vlayout';
  16      const DEFAULTTHEME  = 'softed';
  17      static $currentLayout;
  18      
  19      // Turn-it on to analyze the data pushed to templates for the request.
  20      protected static $debugViewer = false;
  21      
  22      /**
  23       * log message into the file if in debug mode.
  24       * @param type $message
  25       * @param type $delimiter 
  26       */
  27  	protected function log($message, $delimiter="\n") {
  28          static $file = null;
  29          if ($file == null) $file = dirname(__FILE__) . '/../../logs/viewer-debug.log';
  30          if (self::$debugViewer) {
  31              file_put_contents($file, $message.$delimiter, FILE_APPEND);
  32          }
  33      }
  34  
  35      /**
  36       * Constructor - Sets the templateDir and compileDir for the Smarty files
  37       * @param <String> - $media Layout/Media name
  38       */
  39  	function __construct($media='') {
  40          parent::__construct();
  41  
  42          $THISDIR = dirname(__FILE__);
  43  
  44          $templatesDir = '';
  45          $compileDir = '';
  46          if(!empty($media)) {
  47              self::$currentLayout = $media;
  48              $templatesDir = $THISDIR . '/../../layouts/'.$media;
  49              $compileDir = $THISDIR . '/../../test/templates_c/'.$media;
  50          }
  51          if(empty($templatesDir) || !file_exists($templatesDir)) {
  52              self::$currentLayout = self::getDefaultLayoutName();
  53              $templatesDir = $THISDIR . '/../../layouts/'.self::getDefaultLayoutName();
  54              $compileDir = $THISDIR . '/../../test/templates_c/'.self::getDefaultLayoutName();
  55          }
  56  
  57          if (!file_exists($compileDir)) {
  58              mkdir($compileDir, 0777, true);
  59          }
  60          $this->setTemplateDir(array($templatesDir));
  61          $this->setCompileDir($compileDir);        
  62  
  63          // FOR SECURITY
  64          // Escape all {$variable} to overcome XSS
  65          // We need to use {$variable nofilter} to overcome double escaping
  66          // TODO: Until we review the use disabled.
  67          //$this->registerFilter('variable', array($this, 'safeHtmlFilter'));
  68          
  69          // FOR DEBUGGING: We need to have this only once.
  70          static $debugViewerURI = false;
  71          if (self::$debugViewer && $debugViewerURI === false) {
  72              $debugViewerURI = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  73              if (!empty($_POST)) {
  74                  $debugViewerURI .= '?' . http_build_query($_POST);
  75              } else {
  76                  $debugViewerURI = $_SERVER['REQUEST_URI'];
  77              }
  78              
  79              $this->log("URI: $debugViewerURI, TYPE: " . $_SERVER['REQUEST_METHOD']);
  80          }
  81      }
  82      
  83  	function safeHtmlFilter($content, $smarty) {
  84          //return htmlspecialchars($content,ENT_QUOTES,UTF-8);
  85          // NOTE: to_html is being used as data-extraction depends on this
  86          // We shall improve this as it plays role across the product.
  87          return to_html($content);
  88      }
  89  
  90      /**
  91       * Function to get the current layout name
  92       * @return <String> - Current layout name if not empty, otherwise Default layout name
  93       */
  94  	public static function getLayoutName() {
  95          if(!empty(self::$currentLayout)) {
  96              return self::$currentLayout;
  97          }
  98          return self::getDefaultLayoutName();
  99      }
 100  
 101      /**
 102       * Function to return for default layout name
 103       * @return <String> - Default Layout Name
 104       */
 105  	public static function getDefaultLayoutName(){
 106          return self::DEFAULTLAYOUT;
 107      }
 108  
 109      /**
 110       * Function to get the module specific template path for a given template
 111       * @param <String> $templateName
 112       * @param <String> $moduleName
 113       * @return <String> - Module specific template path if exists, otherwise default template path for the given template name
 114       */
 115  	public function getTemplatePath($templateName, $moduleName='') {
 116          $moduleName = str_replace(':', '/', $moduleName);
 117          $completeFilePath = $this->getTemplateDir(0). DIRECTORY_SEPARATOR . "modules/$moduleName/$templateName";
 118          if(!empty($moduleName) && file_exists($completeFilePath)) {
 119              return "modules/$moduleName/$templateName";
 120          } else {
 121              // Fall back lookup on actual module, in case where parent module doesn't contain actual module within in (directory structure)
 122              if(strpos($moduleName, '/') > 0) {
 123                  $moduleHierarchyParts = explode('/', $moduleName);
 124                  $actualModuleName = $moduleHierarchyParts[count($moduleHierarchyParts)-1];
 125                  $baseModuleName = $moduleHierarchyParts[0];
 126                  $fallBackOrder = array (
 127                      "$actualModuleName",
 128                      "$baseModuleName/Vtiger"
 129                  );
 130  
 131                  foreach($fallBackOrder as $fallBackModuleName) {
 132                      $intermediateFallBackFileName = 'modules/'. $fallBackModuleName .'/'.$templateName;
 133                      $intermediateFallBackFilePath = $this->getTemplateDir(0). DIRECTORY_SEPARATOR . $intermediateFallBackFileName;
 134                      if(file_exists($intermediateFallBackFilePath)) {
 135                          return $intermediateFallBackFileName;
 136                      }
 137                  }
 138              }
 139              return "modules/Vtiger/$templateName";
 140          }
 141      }
 142      
 143      /**
 144       * Function to display/fetch the smarty file contents
 145       * @param <String> $templateName
 146       * @param <String> $moduleName
 147       * @param <Boolean> $fetch
 148       * @return html data
 149       */
 150  	public function view($templateName, $moduleName='', $fetch=false) {
 151          $templatePath = $this->getTemplatePath($templateName, $moduleName);
 152          $templateFound = $this->templateExists($templatePath);
 153          
 154          // Logging
 155          if (self::$debugViewer) {
 156              $templatePathToLog = $templatePath;
 157              $qualifiedModuleName = str_replace(':', '/', $moduleName);
 158              // In case we found a fallback template, log both lookup and target template resolved to.
 159              if (!empty($moduleName) && strpos($templatePath, "modules/$qualifiedModuleName/") !== 0) {
 160                  $templatePathToLog = "modules/$qualifiedModuleName/$templateName > $templatePath";
 161              }
 162              $this->log("VIEW: $templatePathToLog, FOUND: " . ($templateFound? "1" : "0"));
 163              foreach ($this->tpl_vars as $key => $smarty_variable) {
 164                  // Determine type of value being pased.
 165                  $valueType = 'literal';
 166                  if (is_object($smarty_variable->value)) $valueType = get_class($smarty_variable->value);
 167                  else if (is_array($smarty_variable->value)) $valueType = 'array';
 168                  $this->log(sprintf("DATA: %s, TYPE: %s", $key, $valueType));
 169              }
 170          }
 171          // END
 172          
 173          if ($templateFound) {
 174              if($fetch) {
 175                  return $this->fetch($templatePath);
 176              } else {
 177                  $this->display($templatePath);
 178              }
 179              return true;
 180          }
 181          
 182          return false;
 183      }
 184  
 185      /**
 186       * Static function to get the Instance of the Class Object
 187       * @param <String> $media Layout/Media
 188       * @return Vtiger_Viewer instance
 189       */
 190  	static function getInstance($media='') {
 191          $instance = new self($media);
 192          return $instance;
 193      }
 194  
 195  }
 196  
 197  function vtemplate_path($templateName, $moduleName='') {
 198      $viewerInstance = Vtiger_Viewer::getInstance();
 199      $args = func_get_args();
 200      return call_user_func_array(array($viewerInstance, 'getTemplatePath'), $args);
 201  }
 202  
 203  /**
 204   * Generated cache friendly resource URL linked with version of Vtiger
 205   */
 206  function vresource_url($url) {
 207      global $vtiger_current_version;
 208      if (stripos($url, '://') === false) {
 209          $url = $url .'?v='.$vtiger_current_version;
 210      }
 211      return $url;
 212  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1