Fix some deprecated create_function for theme templates

dev
richard 2024-05-08 06:07:15 +02:00
parent 13cb905610
commit 31ec02e169
1 changed files with 191 additions and 128 deletions

View File

@ -1,152 +1,215 @@
<?php <?php
/* /*
Class name: Themes Class name: Themes
Class autor: Victor De la Rocha http//mis-algoritmos.com/themes-class Class autor: Victor De la Rocha http//mis-algoritmos.com/themes-class
Email: vyk2rr [at] gmail [dot] com Email: vyk2rr [at] gmail [dot] com
*/ */
class themes class themes
{ {
public $registry; public $registry;
public $path; public $path;
public $l10n; public $l10n;
public $output; public $output;
public $vars=array(); //variable para apilar las variables que se asignan a la plantilla public $vars=array(); //variable para apilar las variables que se asignan a la plantilla
public function __construct() public function __construct()
{ {
}
public function set($name, $value)
{
$this->vars[$name] = $value;
return true;
}
public function remove($name)
{
unset($this->vars[$name]);
return true;
}
//obtiene el contenido del tema ya con todos los valores sutituidos en las variables.
public function fetch($file)
{
$this->exec($file);
return $this->output;
}
//muestra el contenido del tema ya con todos los valroes sustituidos en las variables.
public function display($file)
{
$this->exec($file);
echo $this->output;
}
//corre el proceso de sustitucion de valores en las variables del theme y retorna todo en la variable $this->output para ser devuelto por: fetch o display
public function exec($file)
{
$this->file = $file;
$output = file_get_contents($file);
$this->output = $output;
$this->registrar_vars();
$this->__();
$this->eval_control_structures();
//evaluate All as PHP code
ob_start();
eval($this->output);
$this->output = stripslashes(ob_get_clean());
}
public function eval_control_structures()
{
//finding {header *}
preg_match_all("/{header ([^}]+?)}/s", $this->output, $out);
$headers = '';
foreach ($out[1] as $o) {
$headers .= "header('$o');\n";
} }
public function set($name, $value) $this->output = preg_replace("/{header [^}]+?}/s", "", $this->output);
{
$this->vars[$name] = $value;
return true;
}
public function remove($name) //fake scape
{ $this->output = $this->quotemeta($this->output);
unset($this->vars[$name]); $this->output = str_replace('\"', '\\"', trim($this->output));
return true;
}
//obtiene el contenido del tema ya con todos los valores sutituidos en las variables. $this->output = "echo \"".str_replace('"', '\"', trim($this->output))."\";";
public function fetch($file)
{
$this->exec($file);
return $this->output;
}
//muestra el contenido del tema ya con todos los valroes sustituidos en las variables. #$this->output = preg_replace("/\n+/s","\n",$this->output);
public function display($file) $this->output = $headers.trim($this->output);
{
$this->exec($file);
echo $this->output;
}
//corre el proceso de sustitucion de valores en las variables del theme y retorna todo en la variable $this->output para ser devuelto por: fetch o display //finding IFs sentences and converting to php code
public function exec($file) //echo htmlspecialchars($this->output);
{ #$this->output = preg_replace_callback("/{if ([^}]+)}/",create_function('$arr','return "\";if(".stripslashes($arr[1])."){echo\"";'),$this->output);
$this->file = $file; // $this->output = preg_replace_callback("/{if ([^}]+)}/", create_function('$arr', 'return "\";if(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))."){echo\"";'), $this->output);
$output = file_get_contents($file); // echo htmlspecialchars($this->output);
$this->output = $output; $this->output = preg_replace_callback("/{if ([^}]+)}/", function ($arr) {
$this->registrar_vars(); return "\"; if (" . stripslashes(preg_replace(
$this->__(); array("/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"),
$this->eval_control_structures(); array("\$this->vars['\$1']", "\\\$this->vars['$1']['$2']"),
$arr[1]
)) . ") { echo \"";
}, $this->output);
//evaluate All as PHP code
ob_start();
eval($this->output);
$this->output = stripslashes(ob_get_clean());
}
public function eval_control_structures()
{
//finding {header *}
preg_match_all("/{header ([^}]+?)}/s", $this->output, $out);
$headers = '';
foreach ($out[1] as $o) {
$headers .= "header('$o');\n";
}
$this->output = preg_replace("/{header [^}]+?}/s", "", $this->output);
//fake scape
$this->output = $this->quotemeta($this->output);
$this->output = str_replace('\"', '\\"', trim($this->output));
$this->output = "echo \"".str_replace('"', '\"', trim($this->output))."\";";
#$this->output = preg_replace("/\n+/s","\n",$this->output);
$this->output = $headers.trim($this->output);
//finding IFs sentences and converting to php code
#$this->output = preg_replace_callback("/{if ([^}]+)}/",create_function('$arr','return "\";if(".stripslashes($arr[1])."){echo\"";'),$this->output);
$this->output = preg_replace_callback("/{if ([^}]+)}/", create_function('$arr', 'return "\";if(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))."){echo\"";'), $this->output);
$this->output = preg_replace("/{else}/", "\";}else{echo\"", $this->output); $this->output = preg_replace("/{else}/", "\";}else{echo\"", $this->output);
$this->output = preg_replace_callback("/{elseif ([^}]+)}/", create_function('$arr', 'return "\";}elseif(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9]+)\\\'\]\.([a-zA-Z0-9]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))."){echo\"";'), $this->output); // $this->output = preg_replace_callback("/{elseif ([^}]+)}/", create_function('$arr', 'return "\";}elseif(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9]+)\\\'\]\.([a-zA-Z0-9]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))."){echo\"";'), $this->output);
$this->output = preg_replace("/{\/if}/", "\";} echo \"", $this->output);
//finding FOREACHs or BLOCKs sentences and converting to php code
$this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)=>[\$]([^}]+)}/", create_function('$arr', 'return "\";foreach(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))." as ".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[2]))."=>\$this->vars[\'".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[3]))."\']){echo\"";'), $this->output);
$this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)}/", create_function('$arr', 'return "\";foreach(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))." as ".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[2]))."){echo\"";'), $this->output);
$this->output = preg_replace("/{\/block}/", "\";} echo \"", $this->output);
//Converting the $this->vars[\'variable\'] format to {$this->vars['variable']} $this->output = preg_replace_callback(
//$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']['$2']}",$this->output); "/{elseif ([^}]+)}/",
$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/", "{\$this->vars['$1']}", $this->output); function ($arr) {
return "\";}elseif(".stripslashes(preg_replace(
["/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"],
["\$this->vars['\$1']", "\\\$this->vars['\$1']['\$2']"],
$arr[1]
))."){echo\"";
},
$this->output
);
//Convertin the {__(\'word\')} format to {__('word')}
#$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
}
//sustituye las variables en el output del template
public function registrar_vars()
{ $this->output = preg_replace("/{\/if}/","\";} echo \"",$this->output);
foreach ($this->vars as $k=>$v) {
//pre($this->vars); //finding FOREACHs or BLOCKs sentences and converting to php code
if (is_array($v)) { // $this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)=>[\$]([^}]+)}/", create_function('$arr', 'return "\";foreach(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))." as ".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[2]))."=>\$this->vars[\'".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[3]))."\']){echo\"";'), $this->output);
//Si es un arreglo, se intenta procesar un nivel mas adentro para sustituir en el theme por lo que tenga: nombredearreglo.dato
foreach ($v as $_k=>$_v) { $this->output = preg_replace_callback(
if (!is_array($_v)) { "/{block ([^}]+) as ([^}]+)=>[\$]([^}]+)}/",
$this->output = str_replace('{'.$k.'.'.$_k.'}', $_v, $this->output); function ($arr) {
$var1 = preg_quote($arr[1], '/');
$var2 = preg_quote($arr[2], '/');
$var3 = preg_quote($arr[3], '/');
return "\";foreach(\$this->vars['$var1'] as \$var2=>\$this->vars['$var3']){echo\""; // Removed unnecessary stripslashes
},
$this->output
);
// $this->output = preg_replace_callback("/{block ([^}]+) as ([^}]+)}/", create_function('$arr', 'return "\";foreach(".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[1]))." as ".stripslashes(preg_replace(array("/\\\\$([a-zA-Z0-9_]+)/s","/\\\\\\$this->vars\[\\\'([a-zA-Z0-9_]+)\\\'\]\.([a-zA-Z0-9_]+)/s"),array("\$this->vars[\'\$1\']","\\\\\\$this->vars[\\\'$1\\\'][\\\'$2\\\']"),$arr[2]))."){echo\"";'), $this->output);
$this->output = preg_replace_callback(
"/{block ([^}]+) as ([^}]+)}/",
function ($arr) {
return "\";foreach(".stripslashes(preg_replace(
["/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"],
["\$this->vars['\$1']", "\\\$this->vars['\$1']['\$2']"],
$arr[1]
))." as ".stripslashes(preg_replace(
["/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"],
["\$this->vars['\$1']", "\\\$this->vars['\$1']['\$2']"],
$arr[2]
))."){echo\"";
},
$this->output
);
$this->output = preg_replace("/{\/block}/", "\";} echo \"", $this->output);
//Converting the $this->vars[\'variable\'] format to {$this->vars['variable']}
//$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']['$2']}",$this->output);
$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/", "{\$this->vars['$1']}", $this->output);
//Convertin the {__(\'word\')} format to {__('word')}
$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
} }
}
} else {
// sustituimos directamente las variables {$variable}
$this->output = str_replace('{'.$k.'}', $v, $this->output);
}
}
//replacing {$key} format by $this->vars['key'] //sustituye las variables en el output del template
//replacing {$array.key} format by $this->vars['array']['key'] public function registrar_vars()
$patrones = array( {
'/{\$([^ \.}]+)}/s', foreach ($this->vars as $k=>$v) {
'/{\$([^ \.}]+)\.([^ \.}]+)}/s' //pre($this->vars);
); if (is_array($v)) {
$reemplazos = array( //Si es un arreglo, se intenta procesar un nivel mas adentro para sustituir en el theme por lo que tenga: nombredearreglo.dato
"{\$this->vars['$1']}", foreach ($v as $_k=>$_v) {
"{\$this->vars['$1']['$2']}" if (!is_array($_v)) {
); $this->output = str_replace('{'.$k.'.'.$_k.'}', $_v, $this->output);
$this->output = preg_replace($patrones, $reemplazos, $this->output); }
} }
} else {
// sustituimos directamente las variables {$variable}
$this->output = str_replace('{'.$k.'}', $v, $this->output);
}
}
public function quotemeta($str) //replacing {$key} format by $this->vars['key']
{ //replacing {$array.key} format by $this->vars['array']['key']
$chars = array(/*'.',*/ "\\", /*'+',*/ /*'*',*/ /*'?',*/ /*'[',*/ /*'^',*/ /*']',*/ /*'(',*/ /* '$' Por el momento no validar este*/); $patrones = array(
foreach ($chars as $char) { '/{\$([^ \.}]+)}/s',
$this->output = str_replace($char, "\\$char", $this->output); '/{\$([^ \.}]+)\.([^ \.}]+)}/s'
} );
return $this->output; $reemplazos = array(
} "{\$this->vars['$1']}",
"{\$this->vars['$1']['$2']}"
);
$this->output = preg_replace($patrones, $reemplazos, $this->output);
}
//Utiliza gettext public function quotemeta($str)
public function __() {
{ $chars = array(/*'.',*/ "\\", /*'+',*/ /*'*',*/ /*'?',*/ /*'[',*/ /*'^',*/ /*']',*/ /*'(',*/ /* '$' Por el momento no validar este*/);
$patron = "/{__\((?:'|\")([^\)]+?)(?:'|\")\)}/s"; foreach ($chars as $char) {
preg_match_all($patron, $this->output, $out); $this->output = str_replace($char, "\\$char", $this->output);
}
return $this->output;
}
foreach ($out[1] as $k=>$v) { //Utiliza gettext
$this->output = preg_replace("/{__\((?:'|\")$v(?:'|\")\)}/", __($v), $this->output); public function __()
} {
} $patron = "/{__\((?:'|\")([^\)]+?)(?:'|\")\)}/s";
} preg_match_all($patron, $this->output, $out);
foreach ($out[1] as $k=>$v) {
$this->output = preg_replace("/{__\((?:'|\")$v(?:'|\")\)}/", __($v), $this->output);
}
}
}