Merge pull request #3342 from systopia/CRM-14741
[civicrm-core.git] / CRM / Core / Smarty.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * @static
73 */
74 static private $_singleton = NULL;
75
76 /**
77 * class constructor
78 *
79 * @return CRM_Core_Smarty
80 * @access private
81 */
82 private function __construct() {
83 parent::__construct();
84 }
85
86 private function initialize( ) {
87 $config = CRM_Core_Config::singleton();
88
89 if (isset($config->customTemplateDir) && $config->customTemplateDir) {
90 $this->template_dir = array_merge(array($config->customTemplateDir),
91 $config->templateDir
92 );
93 }
94 else {
95 $this->template_dir = $config->templateDir;
96 }
97 $this->compile_dir = $config->templateCompileDir;
98
99 // check and ensure it is writable
100 // else we sometime suppress errors quietly and this results
101 // in blank emails etc
102 if (!is_writable($this->compile_dir)) {
103 echo "CiviCRM does not have permission to write temp files in {$this->compile_dir}, Exiting";
104 exit();
105 }
106
107 //Check for safe mode CRM-2207
108 if (ini_get('safe_mode')) {
109 $this->use_sub_dirs = FALSE;
110 }
111 else {
112 $this->use_sub_dirs = TRUE;
113 }
114
115 $customPluginsDir = NULL;
116 if (isset($config->customPHPPathDir)) {
117 $customPluginsDir =
118 $config->customPHPPathDir . DIRECTORY_SEPARATOR .
119 'CRM' . DIRECTORY_SEPARATOR .
120 'Core' . DIRECTORY_SEPARATOR .
121 'Smarty' . DIRECTORY_SEPARATOR .
122 'plugins' . DIRECTORY_SEPARATOR;
123 if (!file_exists($customPluginsDir)) {
124 $customPluginsDir = NULL;
125 }
126 }
127
128 if ($customPluginsDir) {
129 $this->plugins_dir = array($customPluginsDir, $config->smartyDir . 'plugins', $config->pluginsDir);
130 }
131 else {
132 $this->plugins_dir = array($config->smartyDir . 'plugins', $config->pluginsDir);
133 }
134
135 // add the session and the config here
136 $session = CRM_Core_Session::singleton();
137
138 $this->assign_by_ref('config', $config);
139 $this->assign_by_ref('session', $session);
140
141 // check default editor and assign to template
142 $defaultWysiwygEditor = $session->get('defaultWysiwygEditor');
143 if (!$defaultWysiwygEditor && !CRM_Core_Config::isUpgradeMode()) {
144 $defaultWysiwygEditor = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
145 'editor_id'
146 );
147 // For logged-in users, store it in session to reduce db calls
148 if ($session->get('userID')) {
149 $session->set('defaultWysiwygEditor', $defaultWysiwygEditor);
150 }
151 }
152
153 $this->assign('defaultWysiwygEditor', $defaultWysiwygEditor);
154
155 global $tsLocale;
156 $this->assign('tsLocale', $tsLocale);
157
158 // CRM-7163 hack: we don’t display langSwitch on upgrades anyway
159 if (!CRM_Core_Config::isUpgradeMode()) {
160 $this->assign('langSwitch', CRM_Core_I18n::languages(TRUE));
161 }
162
163 $this->register_function('crmURL', array('CRM_Utils_System', 'crmURL'));
164 $this->load_filter('pre', 'resetExtScope');
165
166 $this->assign('crmPermissions', new CRM_Core_Smarty_Permissions());
167 }
168
169 /**
170 * Static instance provider.
171 *
172 * Method providing static instance of SmartTemplate, as
173 * in Singleton pattern.
174 */
175 static function &singleton() {
176 if (!isset(self::$_singleton)) {
177 self::$_singleton = new CRM_Core_Smarty( );
178 self::$_singleton->initialize( );
179
180 self::registerStringResource();
181 }
182 return self::$_singleton;
183 }
184
185 /**
186 * executes & returns or displays the template results
187 *
188 * @param string $resource_name
189 * @param string $cache_id
190 * @param string $compile_id
191 * @param boolean $display
192 *
193 * @return bool|mixed|string
194 */
195 function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
196 if (preg_match( '/^(\s+)?string:/', $resource_name)) {
197 $old_security = $this->security;
198 $this->security = TRUE;
199 }
200 $output = parent::fetch($resource_name, $cache_id, $compile_id, $display);
201 if (isset($old_security)) {
202 $this->security = $old_security;
203 }
204 return $output;
205 }
206
207 /**
208 * @param $name
209 * @param $value
210 */
211 function appendValue($name, $value) {
212 $currentValue = $this->get_template_vars($name);
213 if (!$currentValue) {
214 $this->assign($name, $value);
215 }
216 else {
217 if (strpos($currentValue, $value) === FALSE) {
218 $this->assign($name, $currentValue . $value);
219 }
220 }
221 }
222
223 function clearTemplateVars() {
224 foreach (array_keys($this->_tpl_vars) as $key) {
225 if ($key == 'config' || $key == 'session') {
226 continue;
227 }
228 unset($this->_tpl_vars[$key]);
229 }
230 }
231
232 static function registerStringResource() {
233 require_once 'CRM/Core/Smarty/resources/String.php';
234 civicrm_smarty_register_string_resource();
235 }
236
237 /**
238 * @param $path
239 */
240 function addTemplateDir($path) {
241 if ( is_array( $this->template_dir ) ) {
242 array_unshift( $this->template_dir, $path );
243 } else {
244 $this->template_dir = array( $path, $this->template_dir );
245 }
246
247 }
248 }
249