PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/config/smarty.config.inc.php

https://gitlab.com/staging06/myproject
PHP | 279 lines | 202 code | 34 blank | 43 comment | 36 complexity | cb02d883e35efcd3b674abf732b587c9 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');
  27. require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
  28. global $smarty;
  29. $smarty = new SmartyCustom();
  30. $smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
  31. $smarty->setCacheDir(_PS_CACHE_DIR_.'smarty/cache');
  32. if (!Tools::getSafeModeStatus()) {
  33. $smarty->use_sub_dirs = true;
  34. }
  35. $smarty->setConfigDir(_PS_SMARTY_DIR_.'configs');
  36. $smarty->caching = false;
  37. if (Configuration::get('PS_SMARTY_CACHING_TYPE') == 'mysql') {
  38. include(_PS_CLASS_DIR_.'/SmartyCacheResourceMysql.php');
  39. $smarty->caching_type = 'mysql';
  40. }
  41. $smarty->force_compile = (Configuration::get('PS_SMARTY_FORCE_COMPILE') == _PS_SMARTY_FORCE_COMPILE_) ? true : false;
  42. $smarty->compile_check = (Configuration::get('PS_SMARTY_FORCE_COMPILE') >= _PS_SMARTY_CHECK_COMPILE_) ? true : false;
  43. $smarty->debug_tpl = _PS_ALL_THEMES_DIR_.'debug.tpl';
  44. /* Use this constant if you want to load smarty without all PrestaShop functions */
  45. if (defined('_PS_SMARTY_FAST_LOAD_') && _PS_SMARTY_FAST_LOAD_) {
  46. return;
  47. }
  48. if (defined('_PS_ADMIN_DIR_')) {
  49. require_once(dirname(__FILE__).'/smartyadmin.config.inc.php');
  50. } else {
  51. require_once(dirname(__FILE__).'/smartyfront.config.inc.php');
  52. }
  53. smartyRegisterFunction($smarty, 'modifier', 'truncate', 'smarty_modifier_truncate');
  54. smartyRegisterFunction($smarty, 'modifier', 'secureReferrer', array('Tools', 'secureReferrer'));
  55. smartyRegisterFunction($smarty, 'function', 't', 'smartyTruncate'); // unused
  56. smartyRegisterFunction($smarty, 'function', 'm', 'smartyMaxWords'); // unused
  57. smartyRegisterFunction($smarty, 'function', 'p', 'smartyShowObject'); // Debug only
  58. smartyRegisterFunction($smarty, 'function', 'd', 'smartyDieObject'); // Debug only
  59. smartyRegisterFunction($smarty, 'function', 'l', 'smartyTranslate', false);
  60. smartyRegisterFunction($smarty, 'function', 'hook', 'smartyHook');
  61. smartyRegisterFunction($smarty, 'function', 'toolsConvertPrice', 'toolsConvertPrice');
  62. smartyRegisterFunction($smarty, 'modifier', 'json_encode', array('Tools', 'jsonEncode'));
  63. smartyRegisterFunction($smarty, 'modifier', 'json_decode', array('Tools', 'jsonDecode'));
  64. smartyRegisterFunction($smarty, 'function', 'dateFormat', array('Tools', 'dateFormat'));
  65. smartyRegisterFunction($smarty, 'function', 'convertPrice', array('Product', 'convertPrice'));
  66. smartyRegisterFunction($smarty, 'function', 'convertPriceWithCurrency', array('Product', 'convertPriceWithCurrency'));
  67. smartyRegisterFunction($smarty, 'function', 'displayWtPrice', array('Product', 'displayWtPrice'));
  68. smartyRegisterFunction($smarty, 'function', 'displayWtPriceWithCurrency', array('Product', 'displayWtPriceWithCurrency'));
  69. smartyRegisterFunction($smarty, 'function', 'displayPrice', array('Tools', 'displayPriceSmarty'));
  70. smartyRegisterFunction($smarty, 'modifier', 'convertAndFormatPrice', array('Product', 'convertAndFormatPrice')); // used twice
  71. smartyRegisterFunction($smarty, 'function', 'getAdminToken', array('Tools', 'getAdminTokenLiteSmarty'));
  72. smartyRegisterFunction($smarty, 'function', 'displayAddressDetail', array('AddressFormat', 'generateAddressSmarty'));
  73. smartyRegisterFunction($smarty, 'function', 'getWidthSize', array('Image', 'getWidth'));
  74. smartyRegisterFunction($smarty, 'function', 'getHeightSize', array('Image', 'getHeight'));
  75. smartyRegisterFunction($smarty, 'function', 'addJsDef', array('Media', 'addJsDef'));
  76. smartyRegisterFunction($smarty, 'block', 'addJsDefL', array('Media', 'addJsDefL'));
  77. smartyRegisterFunction($smarty, 'modifier', 'boolval', array('Tools', 'boolval'));
  78. smartyRegisterFunction($smarty, 'modifier', 'cleanHtml', 'smartyCleanHtml');
  79. function smartyDieObject($params, &$smarty)
  80. {
  81. return Tools::d($params['var']);
  82. }
  83. function smartyShowObject($params, &$smarty)
  84. {
  85. return Tools::p($params['var']);
  86. }
  87. function smartyMaxWords($params, &$smarty)
  88. {
  89. Tools::displayAsDeprecated();
  90. $params['s'] = str_replace('...', ' ...', html_entity_decode($params['s'], ENT_QUOTES, 'UTF-8'));
  91. $words = explode(' ', $params['s']);
  92. foreach ($words as &$word) {
  93. if (Tools::strlen($word) > $params['n']) {
  94. $word = Tools::substr(trim(chunk_split($word, $params['n']-1, '- ')), 0, -1);
  95. }
  96. }
  97. return implode(' ', Tools::htmlentitiesUTF8($words));
  98. }
  99. function smartyTruncate($params, &$smarty)
  100. {
  101. Tools::displayAsDeprecated();
  102. $text = isset($params['strip']) ? strip_tags($params['text']) : $params['text'];
  103. $length = $params['length'];
  104. $sep = isset($params['sep']) ? $params['sep'] : '...';
  105. if (Tools::strlen($text) > $length + Tools::strlen($sep)) {
  106. $text = Tools::substr($text, 0, $length).$sep;
  107. }
  108. return (isset($params['encode']) ? Tools::htmlentitiesUTF8($text, ENT_NOQUOTES) : $text);
  109. }
  110. function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false, $charset = 'UTF-8')
  111. {
  112. if (!$length) {
  113. return '';
  114. }
  115. $string = trim($string);
  116. if (Tools::strlen($string) > $length) {
  117. $length -= min($length, Tools::strlen($etc));
  118. if (!$break_words && !$middle) {
  119. $string = preg_replace('/\s+?(\S+)?$/u', '', Tools::substr($string, 0, $length+1, $charset));
  120. }
  121. return !$middle ? Tools::substr($string, 0, $length, $charset).$etc : Tools::substr($string, 0, $length/2, $charset).$etc.Tools::substr($string, -$length/2, $length, $charset);
  122. } else {
  123. return $string;
  124. }
  125. }
  126. function smarty_modifier_htmlentitiesUTF8($string)
  127. {
  128. return Tools::htmlentitiesUTF8($string);
  129. }
  130. function smartyMinifyHTML($tpl_output, &$smarty)
  131. {
  132. $context = Context::getContext();
  133. if (isset($context->controller) && in_array($context->controller->php_self, array('pdf-invoice', 'pdf-order-return', 'pdf-order-slip'))) {
  134. return $tpl_output;
  135. }
  136. $tpl_output = Media::minifyHTML($tpl_output);
  137. return $tpl_output;
  138. }
  139. function smartyPackJSinHTML($tpl_output, &$smarty)
  140. {
  141. $context = Context::getContext();
  142. if (isset($context->controller) && in_array($context->controller->php_self, array('pdf-invoice', 'pdf-order-return', 'pdf-order-slip'))) {
  143. return $tpl_output;
  144. }
  145. $tpl_output = Media::packJSinHTML($tpl_output);
  146. return $tpl_output;
  147. }
  148. function smartyRegisterFunction($smarty, $type, $function, $params, $lazy = true)
  149. {
  150. if (!in_array($type, array('function', 'modifier', 'block'))) {
  151. return false;
  152. }
  153. // lazy is better if the function is not called on every page
  154. if ($lazy) {
  155. $lazy_register = SmartyLazyRegister::getInstance();
  156. $lazy_register->register($params);
  157. if (is_array($params)) {
  158. $params = $params[1];
  159. }
  160. // SmartyLazyRegister allows to only load external class when they are needed
  161. $smarty->registerPlugin($type, $function, array($lazy_register, $params));
  162. } else {
  163. $smarty->registerPlugin($type, $function, $params);
  164. }
  165. }
  166. function smartyHook($params, &$smarty)
  167. {
  168. if (!empty($params['h'])) {
  169. $id_module = null;
  170. $hook_params = $params;
  171. $hook_params['smarty'] = $smarty;
  172. if (!empty($params['mod'])) {
  173. $module = Module::getInstanceByName($params['mod']);
  174. if ($module && $module->id) {
  175. $id_module = $module->id;
  176. }
  177. unset($hook_params['mod']);
  178. }
  179. unset($hook_params['h']);
  180. return Hook::exec($params['h'], $hook_params, $id_module);
  181. }
  182. }
  183. function smartyCleanHtml($data)
  184. {
  185. // Prevent xss injection.
  186. if (Validate::isCleanHtml($data)) {
  187. return $data;
  188. }
  189. }
  190. function toolsConvertPrice($params, &$smarty)
  191. {
  192. return Tools::convertPrice($params['price'], Context::getContext()->currency);
  193. }
  194. /**
  195. * Used to delay loading of external classes with smarty->register_plugin
  196. */
  197. class SmartyLazyRegister
  198. {
  199. protected $registry = array();
  200. protected static $instance;
  201. /**
  202. * Register a function or method to be dynamically called later
  203. * @param string|array $params function name or array(object name, method name)
  204. */
  205. public function register($params)
  206. {
  207. if (is_array($params)) {
  208. $this->registry[$params[1]] = $params;
  209. } else {
  210. $this->registry[$params] = $params;
  211. }
  212. }
  213. /**
  214. * Dynamically call static function or method
  215. *
  216. * @param string $name function name
  217. * @param mixed $arguments function argument
  218. * @return mixed function return
  219. */
  220. public function __call($name, $arguments)
  221. {
  222. $item = $this->registry[$name];
  223. // case 1: call to static method - case 2 : call to static function
  224. if (is_array($item[1])) {
  225. return call_user_func_array($item[1].'::'.$item[0], array($arguments[0], &$arguments[1]));
  226. } else {
  227. $args = array();
  228. foreach ($arguments as $a => $argument) {
  229. if ($a == 0) {
  230. $args[] = $arguments[0];
  231. } else {
  232. $args[] = &$arguments[$a];
  233. }
  234. }
  235. return call_user_func_array($item, $args);
  236. }
  237. }
  238. public static function getInstance()
  239. {
  240. if (!self::$instance) {
  241. self::$instance = new SmartyLazyRegister();
  242. }
  243. return self::$instance;
  244. }
  245. }