Skip to content

Commit

Permalink
Add compatibility layer
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Sep 22, 2023
1 parent 5bd0630 commit 44b6cd8
Show file tree
Hide file tree
Showing 245 changed files with 42,561 additions and 54 deletions.
20 changes: 20 additions & 0 deletions CRM/Core/Form/Renderer.php
Expand Up @@ -217,6 +217,26 @@ public static function updateAttributes(&$element, $required, $error) {
$element->updateAttributes($attributes);
}


/**
* Process an template sourced in a string with Smarty
*
* Smarty has no core function to render a template given as a string.
* So we use the smarty eval plugin function to do this.
*
* @param string The template source
* @access private
* @return void
*/
function _tplFetch($tplSource) {
if (!function_exists('smarty_function_eval')) {
$smarty = $this->_tpl;
$smarty->assign('var', $tplSource);
return $smarty->fetch("string:$tplSource");
}
return smarty_function_eval(array('var' => $tplSource), $this->_tpl);
}

/**
* Convert IDs to values and format for display.
*
Expand Down
45 changes: 10 additions & 35 deletions CRM/Core/Smarty.php
Expand Up @@ -22,14 +22,10 @@

use Civi\Core\Event\SmartyErrorEvent;

if (!class_exists('Smarty')) {
require_once 'Smarty/Smarty.class.php';
}

/**
*
*/
class CRM_Core_Smarty extends Smarty {
class CRM_Core_Smarty extends CRM_Core_SmartyCompatibility {
const
// use print.tpl and bypass the CMS. Civi prints a valid html file
PRINT_PAGE = 1,
Expand Down Expand Up @@ -132,6 +128,14 @@ private function initialize() {

$this->assign('config', $config);
$this->assign('session', $session);
$this->assign('debugging', [
'smartyDebug' => CRM_Utils_Request::retrieveValue('smartyDebug', NULL, FALSE, FALSE, $_GET),
'sessionReset' => CRM_Utils_Request::retrieveValue('sessionReset', NULL, FALSE, FALSE, $_GET),
'sessionDebug' => CRM_Utils_Request::retrieveValue('sessionDebug', NULL, FALSE, FALSE, $_GET),
'directoryCleanup' => CRM_Utils_Request::retrieveValue('directoryCleanup', NULL, FALSE, FALSE, $_GET),
'cacheCleanup' => CRM_Utils_Request::retrieveValue('cacheCleanup', NULL, FALSE, FALSE, $_GET),
'configReset' => CRM_Utils_Request::retrieveValue('configReset', NULL, FALSE, FALSE, $_GET),
]);

$tsLocale = CRM_Core_I18n::getLocale();
$this->assign('tsLocale', $tsLocale);
Expand Down Expand Up @@ -183,36 +187,7 @@ public static function &singleton() {
return self::$_singleton;
}

/**
* Executes & returns or displays the template results
*
* @param string $resource_name
* @param string $cache_id
* @param string $compile_id
* @param bool $display
*
* @return bool|mixed|string
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
public function fetch($resource_name, $cache_id = NULL, $compile_id = NULL, $display = FALSE) {
if (preg_match('/^(\s+)?string:/', $resource_name)) {
$old_security = $this->security;
$this->security = TRUE;
}
try {
$output = parent::fetch($resource_name, $cache_id, $compile_id, $display);
}
finally {
if (isset($old_security)) {
$this->security = $old_security;
}
}
return $output;
}

/**
/**
* Handle smarty error in one off string.
*
* @param int $errorNumber
Expand Down
6 changes: 3 additions & 3 deletions CRM/Core/Smarty/plugins/function.help.php
Expand Up @@ -27,12 +27,12 @@
* the help html to be inserted
*/
function smarty_function_help($params, &$smarty) {
if (!isset($params['id']) || !isset($smarty->_tpl_vars['config'])) {
if (!isset($params['id']) || !$smarty->getTemplateVars('config')) {
return NULL;
}

if (empty($params['file']) && isset($smarty->_tpl_vars['tplFile'])) {
$params['file'] = $smarty->_tpl_vars['tplFile'];
if (empty($params['file']) && $smarty->getTemplateVars('tplFile')) {
$params['file'] = $smarty->getTemplateVars('tplFile');
}
elseif (empty($params['file'])) {
return NULL;
Expand Down
6 changes: 4 additions & 2 deletions CRM/Core/Smarty/plugins/modifier.smarty.php
Expand Up @@ -27,6 +27,8 @@
* the new modified html string
*/
function smarty_modifier_smarty($string, $class) {

return $string;
if ($class === 'nodefaults') {
return $string;
}
return CRM_Core_Smarty::escape($string);
}
2 changes: 1 addition & 1 deletion CRM/Core/Smarty/plugins/prefilter.htxtFilter.php
Expand Up @@ -9,7 +9,7 @@
* @param $smarty
* @return string
*/
function smarty_prefilter_htxtFilter($tpl_source, &$smarty) {
function smarty_prefilter_htxtFilter($tpl_source) {
if (strpos($tpl_source, '{htxt') === FALSE) {
return $tpl_source;
}
Expand Down
3 changes: 1 addition & 2 deletions CRM/Core/Smarty/plugins/prefilter.resetExtScope.php
Expand Up @@ -4,10 +4,9 @@
* Wrap every Smarty template in a {crmScope} tag that sets the
* variable "extensionKey" to blank.
* @param $tpl_source
* @param $smarty
* @return string
*/
function smarty_prefilter_resetExtScope($tpl_source, &$smarty) {
function smarty_prefilter_resetExtScope($tpl_source) {
return '{crmScope extensionKey=""}'
. $tpl_source
. '{/crmScope}';
Expand Down
4 changes: 2 additions & 2 deletions CRM/Core/Smarty/resources/String.php
Expand Up @@ -19,7 +19,7 @@
*
* @return bool
*/
function civicrm_smarty_resource_string_get_template($tpl_name, &$tpl_source, &$smarty_obj) {
function civicrm_smarty_resource_string_get_template($tpl_name, &$tpl_source) {
$tpl_source = $tpl_name;
return TRUE;
}
Expand All @@ -31,7 +31,7 @@ function civicrm_smarty_resource_string_get_template($tpl_name, &$tpl_source, &$
*
* @return bool
*/
function civicrm_smarty_resource_string_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) {
function civicrm_smarty_resource_string_get_timestamp($tpl_name, &$tpl_timestamp) {
$tpl_timestamp = time();
return TRUE;
}
Expand Down
212 changes: 212 additions & 0 deletions CRM/Core/SmartyCompatibility.php
@@ -0,0 +1,212 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

/**
* Fix for bug CRM-392. Not sure if this is the best fix or it will impact
* other similar PEAR packages. doubt it
*/
if (!class_exists('Smarty')) {
if (defined('CIVICRM_SMARTY3')) {
global $civicrm_root;
require_once $civicrm_root . '/ext/smarty3/vendor/autoload.php';
}
else {
require_once 'Smarty/Smarty.class.php';
}
}

/**
*
*/
class CRM_Core_SmartyCompatibility extends Smarty {
public function loadFilter($type, $name) {
if (method_exists(get_parent_class(), 'load_filter')) {
parent::load_filter($type, $name);
return;
}
parent::loadFilter($type, $name);
}

/**
* @deprecated
*
* @param string $type
* @param string $name
*
* @throws \SmartyException
*/
public function load_filter($type, $name) {
if (method_exists(get_parent_class(), 'load_filter')) {
parent::load_filter($type, $name);
return;
}
parent::loadFilter($type, $name);
}

/**
* Registers modifier to be used in templates
*
* @deprecated
*
* @param string $modifier name of template modifier
* @param string $modifier_impl name of PHP function to register
*/
public function register_modifier($modifier, $modifier_impl) {
if (method_exists(get_parent_class(), 'register_modifier')) {
parent::register_modifier($modifier, $modifier_impl);
return;
}
parent::registerPlugin('modifier', $modifier, $modifier_impl);
}

public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null) {
if (method_exists(get_parent_class(), 'registerPlugin')) {
parent::registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null);
return;
}
if ($type === 'modifier') {
parent::register_modifier($name, $callback);
}
}

/**
* Registers a resource to fetch a template
*
* @param string $type name of resource
* @param array $functions array of functions to handle resource
*/
public function register_resource($type, $functions) {
if (method_exists(get_parent_class(), 'register_resource')) {
parent::register_resource($type, $functions);
return;
}
parent::registerResource($type, $functions);
}

/**
* Registers custom function to be used in templates
*
* @param string $function the name of the template function
* @param string $function_impl the name of the PHP function to register
*/
function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null) {
if (method_exists(get_parent_class(), 'register_function')) {
parent::register_function($function, $function_impl, $cacheable=true, $cache_attrs=null);
return;
}
parent::registerPlugin('function', $function, $function, $cacheable, $cache_attrs);
}

/**
* Returns an array containing template variables
*
* @param string $name
* @param string $type
* @return array
*/
public function &get_template_vars($name=null) {
if (method_exists(get_parent_class(), 'get_template_vars')) {
return parent::get_template_vars($name);
}
$var = parent::getTemplateVars($name);
return $var;
}

/**
* Returns a single or all template variables
*
* @api Smarty::getTemplateVars()
* @link http://www.smarty.net/docs/en/api.get.template.vars.tpl
*
* @param string $varName variable name or null
* @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
* @param bool $searchParents include parent templates?
*
* @return mixed variable value or or array of variables
*/
public function getTemplateVars($varName = null, Smarty_Internal_Data $_ptr = null, $searchParents = true) {
if (method_exists(get_parent_class(), 'getTemplateVars')) {
return parent::getTemplateVars($varName. $_ptr, $searchParents);
}
return parent::get_template_vars($varName);
}

/**
* Generally Civi mis-uses this for perceived php4 conformance, avoid.
*
* @deprecated
* @param string $tpl_var
* @param mixed $value
*
* @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template|void
*/
public function assign_by_ref($tpl_var, &$value) {
if (method_exists(get_parent_class(), 'assign_by_ref')) {
parent::assign_by_ref($tpl_var, $value);
return;
}
return parent::assignByRef($tpl_var, $value);
}

/**
* Generally Civi mis-uses this for perceived php4 conformance, avoid.
*
* @deprecated
* @param string $tpl_var
*
* @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template|void
*/
public function clear_assign($tpl_var) {
if (method_exists(get_parent_class(), 'clear_assign')) {
parent::clear_assign($tpl_var);
return;
}
return parent::clearAssign($tpl_var);
}

/**
* Checks whether requested template exists.
*
* @param string $tpl_file
*
* @return bool
* @throws \SmartyException
*/
public function template_exists($tpl_file) {
if (method_exists(get_parent_class(), 'template_exists')) {
return parent::template_exists($tpl_file);
}
return parent::templateExists($tpl_file);
}

/**
* Check if a template resource exists
*
* @param string $resource_name template name
*
* @return bool status
* @throws \SmartyException
*/
public function templateExists($resource_name) {
if (method_exists(get_parent_class(), 'templateExists')) {
return parent::templateExists($resource_name);
}
return parent::template_exists($resource_name);
}


}

0 comments on commit 44b6cd8

Please sign in to comment.