A tumblelog CMS built on AJAX, PHP and MySQL.

themes.class.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. Class name: Themes
  4. Class autor: Victor De la Rocha http//mis-algoritmos.com/themes-class
  5. Email: vyk2rr [at] gmail [dot] com
  6. */
  7. class themes
  8. {
  9. public $registry;
  10. public $path;
  11. public $l10n;
  12. public $output;
  13. public $vars=array(); //variable para apilar las variables que se asignan a la plantilla
  14. public function __construct()
  15. {
  16. }
  17. public function set($name, $value)
  18. {
  19. $this->vars[$name] = $value;
  20. return true;
  21. }
  22. public function remove($name)
  23. {
  24. unset($this->vars[$name]);
  25. return true;
  26. }
  27. //obtiene el contenido del tema ya con todos los valores sutituidos en las variables.
  28. public function fetch($file)
  29. {
  30. $this->exec($file);
  31. return $this->output;
  32. }
  33. //muestra el contenido del tema ya con todos los valroes sustituidos en las variables.
  34. public function display($file)
  35. {
  36. $this->exec($file);
  37. echo $this->output;
  38. }
  39. //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
  40. public function exec($file)
  41. {
  42. $this->file = $file;
  43. $output = file_get_contents($file);
  44. $this->output = $output;
  45. $this->registrar_vars();
  46. $this->__();
  47. $this->eval_control_structures();
  48. //evaluate All as PHP code
  49. ob_start();
  50. eval($this->output);
  51. $this->output = stripslashes(ob_get_clean());
  52. }
  53. public function eval_control_structures()
  54. {
  55. //finding {header *}
  56. preg_match_all("/{header ([^}]+?)}/s", $this->output, $out);
  57. $headers = '';
  58. foreach ($out[1] as $o) {
  59. $headers .= "header('$o');\n";
  60. }
  61. $this->output = preg_replace("/{header [^}]+?}/s", "", $this->output);
  62. //fake scape
  63. $this->output = $this->quotemeta($this->output);
  64. $this->output = str_replace('\"', '\\"', trim($this->output));
  65. $this->output = "echo \"".str_replace('"', '\"', trim($this->output))."\";";
  66. #$this->output = preg_replace("/\n+/s","\n",$this->output);
  67. $this->output = $headers.trim($this->output);
  68. //finding IFs sentences and converting to php code
  69. #$this->output = preg_replace_callback("/{if ([^}]+)}/",create_function('$arr','return "\";if(".stripslashes($arr[1])."){echo\"";'),$this->output);
  70. $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);
  71. $this->output = preg_replace("/{else}/", "\";}else{echo\"", $this->output);
  72. $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);
  73. $this->output = preg_replace("/{\/if}/", "\";} echo \"", $this->output);
  74. //finding FOREACHs or BLOCKs sentences and converting to php code
  75. $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);
  76. $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);
  77. $this->output = preg_replace("/{\/block}/", "\";} echo \"", $this->output);
  78. //Converting the $this->vars[\'variable\'] format to {$this->vars['variable']}
  79. //$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']['$2']}",$this->output);
  80. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/", "{\$this->vars['$1']}", $this->output);
  81. //Convertin the {__(\'word\')} format to {__('word')}
  82. #$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
  83. }
  84. //sustituye las variables en el output del template
  85. public function registrar_vars()
  86. {
  87. foreach ($this->vars as $k=>$v) {
  88. //pre($this->vars);
  89. if (is_array($v)) {
  90. //Si es un arreglo, se intenta procesar un nivel mas adentro para sustituir en el theme por lo que tenga: nombredearreglo.dato
  91. foreach ($v as $_k=>$_v) {
  92. if (!is_array($_v)) {
  93. $this->output = str_replace('{'.$k.'.'.$_k.'}', $_v, $this->output);
  94. }
  95. }
  96. } else {
  97. // sustituimos directamente las variables {$variable}
  98. $this->output = str_replace('{'.$k.'}', $v, $this->output);
  99. }
  100. }
  101. //replacing {$key} format by $this->vars['key']
  102. //replacing {$array.key} format by $this->vars['array']['key']
  103. $patrones = array(
  104. '/{\$([^ \.}]+)}/s',
  105. '/{\$([^ \.}]+)\.([^ \.}]+)}/s'
  106. );
  107. $reemplazos = array(
  108. "{\$this->vars['$1']}",
  109. "{\$this->vars['$1']['$2']}"
  110. );
  111. $this->output = preg_replace($patrones, $reemplazos, $this->output);
  112. }
  113. public function quotemeta($str)
  114. {
  115. $chars = array(/*'.',*/ "\\", /*'+',*/ /*'*',*/ /*'?',*/ /*'[',*/ /*'^',*/ /*']',*/ /*'(',*/ /* '$' Por el momento no validar este*/);
  116. foreach ($chars as $char) {
  117. $this->output = str_replace($char, "\\$char", $this->output);
  118. }
  119. return $this->output;
  120. }
  121. //Utiliza gettext
  122. public function __()
  123. {
  124. $patron = "/{__\((?:'|\")([^\)]+?)(?:'|\")\)}/s";
  125. preg_match_all($patron, $this->output, $out);
  126. foreach ($out[1] as $k=>$v) {
  127. $this->output = preg_replace("/{__\((?:'|\")$v(?:'|\")\)}/", __($v), $this->output);
  128. }
  129. }
  130. }