A tumblelog CMS built on AJAX, PHP and MySQL.

themes.class.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. //echo htmlspecialchars($this->output);
  70. #$this->output = preg_replace_callback("/{if ([^}]+)}/",create_function('$arr','return "\";if(".stripslashes($arr[1])."){echo\"";'),$this->output);
  71. // $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);
  72. // echo htmlspecialchars($this->output);
  73. $this->output = preg_replace_callback("/{if ([^}]+)}/", function ($arr) {
  74. return "\"; if (" . stripslashes(preg_replace(
  75. array("/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"),
  76. array("\$this->vars['\$1']", "\\\$this->vars['$1']['$2']"),
  77. $arr[1]
  78. )) . ") { echo \"";
  79. }, $this->output);
  80. $this->output = preg_replace("/{else}/", "\";}else{echo\"", $this->output);
  81. // $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);
  82. $this->output = preg_replace_callback(
  83. "/{elseif ([^}]+)}/",
  84. function ($arr) {
  85. return "\";}elseif(".stripslashes(preg_replace(
  86. ["/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"],
  87. ["\$this->vars['\$1']", "\\\$this->vars['\$1']['\$2']"],
  88. $arr[1]
  89. ))."){echo\"";
  90. },
  91. $this->output
  92. );
  93. $this->output = preg_replace("/{\/if}/","\";} echo \"",$this->output);
  94. //finding FOREACHs or BLOCKs sentences and converting to php code
  95. // $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);
  96. $this->output = preg_replace_callback(
  97. "/{block ([^}]+) as ([^}]+)=>[\$]([^}]+)}/",
  98. function ($arr) {
  99. $var1 = preg_quote($arr[1], '/');
  100. $var2 = preg_quote($arr[2], '/');
  101. $var3 = preg_quote($arr[3], '/');
  102. return "\";foreach(\$this->vars['$var1'] as \$var2=>\$this->vars['$var3']){echo\""; // Removed unnecessary stripslashes
  103. },
  104. $this->output
  105. );
  106. // $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);
  107. $this->output = preg_replace_callback(
  108. "/{block ([^}]+) as ([^}]+)}/",
  109. function ($arr) {
  110. return "\";foreach(".stripslashes(preg_replace(
  111. ["/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"],
  112. ["\$this->vars['\$1']", "\\\$this->vars['\$1']['\$2']"],
  113. $arr[1]
  114. ))." as ".stripslashes(preg_replace(
  115. ["/\\$([a-zA-Z0-9_]+)/s", "/\\\$this->vars\['([a-zA-Z0-9_]+)'\]\.([a-zA-Z0-9_]+)/s"],
  116. ["\$this->vars['\$1']", "\\\$this->vars['\$1']['\$2']"],
  117. $arr[2]
  118. ))."){echo\"";
  119. },
  120. $this->output
  121. );
  122. $this->output = preg_replace("/{\/block}/", "\";} echo \"", $this->output);
  123. //Converting the $this->vars[\'variable\'] format to {$this->vars['variable']}
  124. //$this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']['$2']}",$this->output);
  125. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/", "{\$this->vars['$1']}", $this->output);
  126. //Convertin the {__(\'word\')} format to {__('word')}
  127. $this->output = preg_replace("/[\$]this->vars\[\\\'([^ \.\\\]+)\\\'\]/","{\$this->vars['$1']}",$this->output);
  128. }
  129. //sustituye las variables en el output del template
  130. public function registrar_vars()
  131. {
  132. foreach ($this->vars as $k=>$v) {
  133. //pre($this->vars);
  134. if (is_array($v)) {
  135. //Si es un arreglo, se intenta procesar un nivel mas adentro para sustituir en el theme por lo que tenga: nombredearreglo.dato
  136. foreach ($v as $_k=>$_v) {
  137. if (!is_array($_v)) {
  138. $this->output = str_replace('{'.$k.'.'.$_k.'}', $_v, $this->output);
  139. }
  140. }
  141. } else {
  142. // sustituimos directamente las variables {$variable}
  143. $this->output = str_replace('{'.$k.'}', $v, $this->output);
  144. }
  145. }
  146. //replacing {$key} format by $this->vars['key']
  147. //replacing {$array.key} format by $this->vars['array']['key']
  148. $patrones = array(
  149. '/{\$([^ \.}]+)}/s',
  150. '/{\$([^ \.}]+)\.([^ \.}]+)}/s'
  151. );
  152. $reemplazos = array(
  153. "{\$this->vars['$1']}",
  154. "{\$this->vars['$1']['$2']}"
  155. );
  156. $this->output = preg_replace($patrones, $reemplazos, $this->output);
  157. }
  158. public function quotemeta($str)
  159. {
  160. $chars = array(/*'.',*/ "\\", /*'+',*/ /*'*',*/ /*'?',*/ /*'[',*/ /*'^',*/ /*']',*/ /*'(',*/ /* '$' Por el momento no validar este*/);
  161. foreach ($chars as $char) {
  162. $this->output = str_replace($char, "\\$char", $this->output);
  163. }
  164. return $this->output;
  165. }
  166. //Utiliza gettext
  167. public function __()
  168. {
  169. $patron = "/{__\((?:'|\")([^\)]+?)(?:'|\")\)}/s";
  170. preg_match_all($patron, $this->output, $out);
  171. foreach ($out[1] as $k=>$v) {
  172. $this->output = preg_replace("/{__\((?:'|\")$v(?:'|\")\)}/", __($v), $this->output);
  173. }
  174. }
  175. }