| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Fix for bug CRM-392. Not sure if this is the best fix or it will impact |
| 38 | * other similar PEAR packages. doubt it |
| 39 | */ |
| 40 | if (!class_exists('Smarty')) { |
| 41 | require_once 'Smarty/Smarty.class.php'; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | */ |
| 47 | class CRM_Core_Smarty extends Smarty { |
| 48 | const |
| 49 | // use print.tpl and bypass the CMS. Civi prints a valid html file |
| 50 | PRINT_PAGE = 1, |
| 51 | // this and all the below bypasses the CMS html surrounding it and assumes we will embed this within other pages |
| 52 | PRINT_SNIPPET = 2, |
| 53 | // sends the generated html to the chosen pdf engine |
| 54 | PRINT_PDF = 3, |
| 55 | // this options also skips the enclosing form html and does not |
| 56 | // generate any of the hidden fields, most notably qfKey |
| 57 | // this is typically used in ajax scripts to embed form snippets based on user choices |
| 58 | PRINT_NOFORM = 4, |
| 59 | // this prints a complete form and also generates a qfKey, can we replace this with |
| 60 | // snippet = 2?? Does the constant _NOFFORM do anything? |
| 61 | PRINT_QFKEY = 5, |
| 62 | // Note: added in v 4.3 with the value '6' |
| 63 | // Value changed in 4.5 to 'json' for better readability |
| 64 | // @see CRM_Core_Page_AJAX::returnJsonResponse |
| 65 | PRINT_JSON = 'json'; |
| 66 | |
| 67 | /** |
| 68 | * We only need one instance of this object. So we use the singleton |
| 69 | * pattern and cache the instance in this variable |
| 70 | * |
| 71 | * @var object |
| 72 | */ |
| 73 | static private $_singleton = NULL; |
| 74 | |
| 75 | /** |
| 76 | * @var array (string $name => mixed $value) a list of variables ot save temporarily |
| 77 | */ |
| 78 | private $backupFrames = array(); |
| 79 | |
| 80 | /** |
| 81 | * Class constructor. |
| 82 | * |
| 83 | * @return CRM_Core_Smarty |
| 84 | */ |
| 85 | private function __construct() { |
| 86 | parent::__construct(); |
| 87 | } |
| 88 | |
| 89 | private function initialize() { |
| 90 | $config = CRM_Core_Config::singleton(); |
| 91 | |
| 92 | if (isset($config->customTemplateDir) && $config->customTemplateDir) { |
| 93 | $this->template_dir = array_merge(array($config->customTemplateDir), |
| 94 | $config->templateDir |
| 95 | ); |
| 96 | } |
| 97 | else { |
| 98 | $this->template_dir = $config->templateDir; |
| 99 | } |
| 100 | $this->compile_dir = CRM_Utils_File::addTrailingSlash(CRM_Utils_File::addTrailingSlash($config->templateCompileDir) . $this->getLocale()); |
| 101 | CRM_Utils_File::createDir($this->compile_dir); |
| 102 | CRM_Utils_File::restrictAccess($this->compile_dir); |
| 103 | |
| 104 | // check and ensure it is writable |
| 105 | // else we sometime suppress errors quietly and this results |
| 106 | // in blank emails etc |
| 107 | if (!is_writable($this->compile_dir)) { |
| 108 | echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting"; |
| 109 | exit(); |
| 110 | } |
| 111 | |
| 112 | //Check for safe mode CRM-2207 |
| 113 | if (ini_get('safe_mode')) { |
| 114 | $this->use_sub_dirs = FALSE; |
| 115 | } |
| 116 | else { |
| 117 | $this->use_sub_dirs = TRUE; |
| 118 | } |
| 119 | |
| 120 | $customPluginsDir = NULL; |
| 121 | if (isset($config->customPHPPathDir)) { |
| 122 | $customPluginsDir |
| 123 | = $config->customPHPPathDir . DIRECTORY_SEPARATOR . |
| 124 | 'CRM' . DIRECTORY_SEPARATOR . |
| 125 | 'Core' . DIRECTORY_SEPARATOR . |
| 126 | 'Smarty' . DIRECTORY_SEPARATOR . |
| 127 | 'plugins' . DIRECTORY_SEPARATOR; |
| 128 | if (!file_exists($customPluginsDir)) { |
| 129 | $customPluginsDir = NULL; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | $smartyDir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'packages' . DIRECTORY_SEPARATOR . 'Smarty' . DIRECTORY_SEPARATOR; |
| 134 | $pluginsDir = __DIR__ . DIRECTORY_SEPARATOR . 'Smarty' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR; |
| 135 | |
| 136 | if ($customPluginsDir) { |
| 137 | $this->plugins_dir = array($customPluginsDir, $smartyDir . 'plugins', $pluginsDir); |
| 138 | } |
| 139 | else { |
| 140 | $this->plugins_dir = array($smartyDir . 'plugins', $pluginsDir); |
| 141 | } |
| 142 | |
| 143 | // add the session and the config here |
| 144 | $session = CRM_Core_Session::singleton(); |
| 145 | |
| 146 | $this->assign_by_ref('config', $config); |
| 147 | $this->assign_by_ref('session', $session); |
| 148 | |
| 149 | global $tsLocale; |
| 150 | $this->assign('tsLocale', $tsLocale); |
| 151 | |
| 152 | // CRM-7163 hack: we don’t display langSwitch on upgrades anyway |
| 153 | if (!CRM_Core_Config::isUpgradeMode()) { |
| 154 | $this->assign('langSwitch', CRM_Core_I18n::languages(TRUE)); |
| 155 | } |
| 156 | |
| 157 | $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL')); |
| 158 | $this->load_filter('pre', 'resetExtScope'); |
| 159 | |
| 160 | $this->assign('crmPermissions', new CRM_Core_Smarty_Permissions()); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Static instance provider. |
| 165 | * |
| 166 | * Method providing static instance of SmartTemplate, as |
| 167 | * in Singleton pattern. |
| 168 | */ |
| 169 | public static function &singleton() { |
| 170 | if (!isset(self::$_singleton)) { |
| 171 | self::$_singleton = new CRM_Core_Smarty(); |
| 172 | self::$_singleton->initialize(); |
| 173 | |
| 174 | self::registerStringResource(); |
| 175 | } |
| 176 | return self::$_singleton; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Executes & returns or displays the template results |
| 181 | * |
| 182 | * @param string $resource_name |
| 183 | * @param string $cache_id |
| 184 | * @param string $compile_id |
| 185 | * @param bool $display |
| 186 | * |
| 187 | * @return bool|mixed|string |
| 188 | */ |
| 189 | public function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) { |
| 190 | if (preg_match('/^(\s+)?string:/', $resource_name)) { |
| 191 | $old_security = $this->security; |
| 192 | $this->security = TRUE; |
| 193 | } |
| 194 | $output = parent::fetch($resource_name, $cache_id, $compile_id, $display); |
| 195 | if (isset($old_security)) { |
| 196 | $this->security = $old_security; |
| 197 | } |
| 198 | return $output; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Fetch a template (while using certain variables) |
| 203 | * |
| 204 | * @param string $resource_name |
| 205 | * @param array $vars |
| 206 | * (string $name => mixed $value) variables to export to Smarty. |
| 207 | * @throws Exception |
| 208 | * @return bool|mixed|string |
| 209 | */ |
| 210 | public function fetchWith($resource_name, $vars) { |
| 211 | $this->pushScope($vars); |
| 212 | try { |
| 213 | $result = $this->fetch($resource_name); |
| 214 | } |
| 215 | catch (Exception $e) { |
| 216 | // simulate try { ... } finally { ... } |
| 217 | $this->popScope(); |
| 218 | throw $e; |
| 219 | } |
| 220 | $this->popScope(); |
| 221 | return $result; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @param string $name |
| 226 | * @param $value |
| 227 | */ |
| 228 | public function appendValue($name, $value) { |
| 229 | $currentValue = $this->get_template_vars($name); |
| 230 | if (!$currentValue) { |
| 231 | $this->assign($name, $value); |
| 232 | } |
| 233 | else { |
| 234 | if (strpos($currentValue, $value) === FALSE) { |
| 235 | $this->assign($name, $currentValue . $value); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | public function clearTemplateVars() { |
| 241 | foreach (array_keys($this->_tpl_vars) as $key) { |
| 242 | if ($key == 'config' || $key == 'session') { |
| 243 | continue; |
| 244 | } |
| 245 | unset($this->_tpl_vars[$key]); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | public static function registerStringResource() { |
| 250 | require_once 'CRM/Core/Smarty/resources/String.php'; |
| 251 | civicrm_smarty_register_string_resource(); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @param $path |
| 256 | */ |
| 257 | public function addTemplateDir($path) { |
| 258 | if (is_array($this->template_dir)) { |
| 259 | array_unshift($this->template_dir, $path); |
| 260 | } |
| 261 | else { |
| 262 | $this->template_dir = array($path, $this->template_dir); |
| 263 | } |
| 264 | |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Temporarily assign a list of variables. |
| 269 | * |
| 270 | * @code |
| 271 | * $smarty->pushScope(array( |
| 272 | * 'first_name' => 'Alice', |
| 273 | * 'last_name' => 'roberts', |
| 274 | * )); |
| 275 | * $html = $smarty->fetch('view-contact.tpl'); |
| 276 | * $smarty->popScope(); |
| 277 | * @endcode |
| 278 | * |
| 279 | * @param array $vars |
| 280 | * (string $name => mixed $value). |
| 281 | * @return CRM_Core_Smarty |
| 282 | * @see popScope |
| 283 | */ |
| 284 | public function pushScope($vars) { |
| 285 | $oldVars = $this->get_template_vars(); |
| 286 | $backupFrame = array(); |
| 287 | foreach ($vars as $key => $value) { |
| 288 | $backupFrame[$key] = isset($oldVars[$key]) ? $oldVars[$key] : NULL; |
| 289 | } |
| 290 | $this->backupFrames[] = $backupFrame; |
| 291 | |
| 292 | $this->assignAll($vars); |
| 293 | |
| 294 | return $this; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Remove any values that were previously pushed. |
| 299 | * |
| 300 | * @return CRM_Core_Smarty |
| 301 | * @see pushScope |
| 302 | */ |
| 303 | public function popScope() { |
| 304 | $this->assignAll(array_pop($this->backupFrames)); |
| 305 | return $this; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @param array $vars |
| 310 | * (string $name => mixed $value). |
| 311 | * @return CRM_Core_Smarty |
| 312 | */ |
| 313 | public function assignAll($vars) { |
| 314 | foreach ($vars as $key => $value) { |
| 315 | $this->assign($key, $value); |
| 316 | } |
| 317 | return $this; |
| 318 | } |
| 319 | |
| 320 | private function getLocale() { |
| 321 | global $tsLocale; |
| 322 | if (!empty($tsLocale)) { |
| 323 | return $tsLocale; |
| 324 | } |
| 325 | |
| 326 | $config = CRM_Core_Config::singleton(); |
| 327 | if (!empty($config->lcMessages)) { |
| 328 | return $config->lcMessages; |
| 329 | } |
| 330 | |
| 331 | return 'en_US'; |
| 332 | } |
| 333 | |
| 334 | } |