A tumblelog CMS built on AJAX, PHP and MySQL.

themes.class.php 6.3KB

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