3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Fix for bug CRM-392. Not sure if this is the best fix or it will impact
20 * other similar PEAR packages. doubt it
22 if (!class_exists('Smarty')) {
23 require_once 'Smarty/Smarty.class.php';
29 class CRM_Core_Smarty
extends Smarty
{
31 // use print.tpl and bypass the CMS. Civi prints a valid html file
33 // this and all the below bypasses the CMS html surrounding it and assumes we will embed this within other pages
35 // sends the generated html to the chosen pdf engine
37 // this options also skips the enclosing form html and does not
38 // generate any of the hidden fields, most notably qfKey
39 // this is typically used in ajax scripts to embed form snippets based on user choices
41 // this prints a complete form and also generates a qfKey, can we replace this with
42 // snippet = 2?? Does the constant _NOFFORM do anything?
44 // Note: added in v 4.3 with the value '6'
45 // Value changed in 4.5 to 'json' for better readability
46 // @see CRM_Core_Page_AJAX::returnJsonResponse
50 * We only need one instance of this object. So we use the singleton
51 * pattern and cache the instance in this variable
55 static private $_singleton = NULL;
60 * A list of variables ot save temporarily in format (string $name => mixed $value).
64 private $backupFrames = [];
69 * @return CRM_Core_Smarty
71 public function __construct() {
72 parent
::__construct();
75 private function initialize() {
76 $config = CRM_Core_Config
::singleton();
78 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
79 $this->template_dir
= array_merge([$config->customTemplateDir
],
84 $this->template_dir
= $config->templateDir
;
86 $this->compile_dir
= CRM_Utils_File
::addTrailingSlash(CRM_Utils_File
::addTrailingSlash($config->templateCompileDir
) . $this->getLocale());
87 CRM_Utils_File
::createDir($this->compile_dir
);
88 CRM_Utils_File
::restrictAccess($this->compile_dir
);
90 // check and ensure it is writable
91 // else we sometime suppress errors quietly and this results
92 // in blank emails etc
93 if (!is_writable($this->compile_dir
)) {
94 echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting";
98 //Check for safe mode CRM-2207
99 if (ini_get('safe_mode')) {
100 $this->use_sub_dirs
= FALSE;
103 $this->use_sub_dirs
= TRUE;
106 $customPluginsDir = NULL;
107 if (isset($config->customPHPPathDir
)) {
109 = $config->customPHPPathDir
. DIRECTORY_SEPARATOR
.
110 'CRM' . DIRECTORY_SEPARATOR
.
111 'Core' . DIRECTORY_SEPARATOR
.
112 'Smarty' . DIRECTORY_SEPARATOR
.
113 'plugins' . DIRECTORY_SEPARATOR
;
114 if (!file_exists($customPluginsDir)) {
115 $customPluginsDir = NULL;
119 $pkgsDir = Civi
::paths()->getVariable('civicrm.packages', 'path');
120 $smartyDir = $pkgsDir . DIRECTORY_SEPARATOR
. 'Smarty' . DIRECTORY_SEPARATOR
;
121 $pluginsDir = __DIR__
. DIRECTORY_SEPARATOR
. 'Smarty' . DIRECTORY_SEPARATOR
. 'plugins' . DIRECTORY_SEPARATOR
;
123 if ($customPluginsDir) {
124 $this->plugins_dir
= [$customPluginsDir, $smartyDir . 'plugins', $pluginsDir];
127 $this->plugins_dir
= [$smartyDir . 'plugins', $pluginsDir];
130 $this->compile_check
= $this->isCheckSmartyIsCompiled();
132 // add the session and the config here
133 $session = CRM_Core_Session
::singleton();
135 $this->assign_by_ref('config', $config);
136 $this->assign_by_ref('session', $session);
138 $tsLocale = CRM_Core_I18n
::getLocale();
139 $this->assign('tsLocale', $tsLocale);
141 // CRM-7163 hack: we don’t display langSwitch on upgrades anyway
142 if (!CRM_Core_Config
::isUpgradeMode()) {
143 $this->assign('langSwitch', CRM_Core_I18n
::uiLanguages());
146 $this->register_function('crmURL', ['CRM_Utils_System', 'crmURL']);
147 $this->load_filter('pre', 'resetExtScope');
149 $this->assign('crmPermissions', new CRM_Core_Smarty_Permissions());
153 * Static instance provider.
155 * Method providing static instance of SmartTemplate, as
156 * in Singleton pattern.
158 public static function &singleton() {
159 if (!isset(self
::$_singleton)) {
160 self
::$_singleton = new CRM_Core_Smarty();
161 self
::$_singleton->initialize();
163 self
::registerStringResource();
165 return self
::$_singleton;
169 * Executes & returns or displays the template results
171 * @param string $resource_name
172 * @param string $cache_id
173 * @param string $compile_id
174 * @param bool $display
176 * @return bool|mixed|string
178 public function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
179 if (preg_match('/^(\s+)?string:/', $resource_name)) {
180 $old_security = $this->security
;
181 $this->security
= TRUE;
183 $output = parent
::fetch($resource_name, $cache_id, $compile_id, $display);
184 if (isset($old_security)) {
185 $this->security
= $old_security;
191 * Fetch a template (while using certain variables)
193 * @param string $resource_name
195 * (string $name => mixed $value) variables to export to Smarty.
197 * @return bool|mixed|string
199 public function fetchWith($resource_name, $vars) {
200 $this->pushScope($vars);
202 $result = $this->fetch($resource_name);
204 catch (Exception
$e) {
205 // simulate try { ... } finally { ... }
214 * @param string $name
217 public function appendValue($name, $value) {
218 $currentValue = $this->get_template_vars($name);
219 if (!$currentValue) {
220 $this->assign($name, $value);
223 if (strpos($currentValue, $value) === FALSE) {
224 $this->assign($name, $currentValue . $value);
229 public function clearTemplateVars() {
230 foreach (array_keys($this->_tpl_vars
) as $key) {
231 if ($key == 'config' ||
$key == 'session') {
234 unset($this->_tpl_vars
[$key]);
238 public static function registerStringResource() {
239 require_once 'CRM/Core/Smarty/resources/String.php';
240 civicrm_smarty_register_string_resource();
246 public function addTemplateDir($path) {
247 if (is_array($this->template_dir
)) {
248 array_unshift($this->template_dir
, $path);
251 $this->template_dir
= [$path, $this->template_dir
];
257 * Temporarily assign a list of variables.
260 * $smarty->pushScope(array(
261 * 'first_name' => 'Alice',
262 * 'last_name' => 'roberts',
264 * $html = $smarty->fetch('view-contact.tpl');
265 * $smarty->popScope();
269 * (string $name => mixed $value).
270 * @return CRM_Core_Smarty
273 public function pushScope($vars) {
274 $oldVars = $this->get_template_vars();
276 foreach ($vars as $key => $value) {
277 $backupFrame[$key] = $oldVars[$key] ??
NULL;
279 $this->backupFrames
[] = $backupFrame;
281 $this->assignAll($vars);
287 * Remove any values that were previously pushed.
289 * @return CRM_Core_Smarty
292 public function popScope() {
293 $this->assignAll(array_pop($this->backupFrames
));
299 * (string $name => mixed $value).
300 * @return CRM_Core_Smarty
302 public function assignAll($vars) {
303 foreach ($vars as $key => $value) {
304 $this->assign($key, $value);
310 * Get the locale for translation.
314 private function getLocale() {
315 $tsLocale = CRM_Core_I18n
::getLocale();
316 if (!empty($tsLocale)) {
320 $config = CRM_Core_Config
::singleton();
321 if (!empty($config->lcMessages
)) {
322 return $config->lcMessages
;
329 * Get the compile_check value.
333 private function isCheckSmartyIsCompiled() {
334 // check for define in civicrm.settings.php as FALSE, otherwise returns TRUE
335 return CRM_Utils_Constant
::value('CIVICRM_TEMPLATE_COMPILE_CHECK', TRUE);