Guest User

Untitled

a guest
Jun 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. <?php
  2. // mansoor sayed
  3. class Nebula
  4. {
  5. var $LEFT_DELIMITER = '{-';
  6. var $RIGHT_DELIMITER = '-}';
  7. var $ERROR_REPORT = true;
  8. var $CACHE_DIR = 'cache/'; // relitive to php file location
  9. var $PLUGIN_DIR = 'plugin/';
  10. var $CACHE = true;
  11. var $TEMPLATE_DIR = 'template/';
  12.  
  13. //private var $_loadPlugins = true;
  14.  
  15. /* END OF CONFIG. DO NOT TOUCH ANY THING BELOW THIS POINT */
  16.  
  17. //var
  18. var $CONTLIST = array();
  19. var $PLUGINS = array();
  20. var $CONTROLS = array();
  21. protected static $erroflag = false;
  22. protected static $pagerro = array();
  23. static $content;
  24.  
  25.  
  26.  
  27. /* Class constructor */
  28. public function Nebula()
  29. {
  30. $this->display("starting Nebula templating engine!<br>");
  31. $this->_load_plugins();
  32. }
  33. public function start()
  34. {
  35.  
  36.  
  37. $content = $this->_replace($this->PLUGINS, $this->_merge($this->CONTLIST));
  38. if($this->ERROR_REPORT == true)
  39. {
  40. foreach(Nebula::$pagerro as $erro)
  41. {
  42. $content = $erro . $content;
  43. }
  44. }
  45. Nebula::$content = $content;
  46. echo $content;
  47. }
  48.  
  49. /* if ($this->$erroflag == false)
  50. {
  51. $this->_merge($this->CONTLIST);
  52.  
  53. }
  54. */
  55.  
  56. public function display($content) // imports any content imported from the php file
  57. { // will not import a file!!
  58. $this->CONTLIST[] = $content;
  59. }
  60.  
  61.  
  62. public function display_file($file) // import a file as resource
  63. {
  64. if(file_exists($file))
  65. $this->CONTLIST[] = file_get_contents($file);
  66. else
  67. $this->_reperro("Error! The file '" .$file ."' was not found!");
  68. }
  69.  
  70.  
  71. protected function _merge($parts)
  72. {
  73. $content = '';
  74. for($i=0; $i < count($parts);$i++)
  75. {
  76. $content .= $parts[$i];
  77. }
  78. return $content;
  79. }
  80.  
  81. protected function _load_plugins() // also, as of now, prepares the variable array list
  82. {
  83. if ($handle = opendir($this->PLUGIN_DIR))
  84. {
  85. while (false !== ($file = readdir($handle)))
  86. {
  87. if(preg_match('`^plugin.`',$file))
  88. {
  89. $prt = explode('.',$file);
  90. $this->PLUGINS[$prt[1]] = $file;
  91. }
  92. }
  93. closedir($handle);
  94. }
  95. }
  96. public function set_controls($controls)
  97. {
  98. /* foreach($controls as $cont => $val)
  99. {
  100. $$cont = 'value';
  101. $$cont = $val;
  102. $this->CONTROLS[] = $$cont;
  103. } */
  104. $this->CONTROLS[] = $controls;
  105. }
  106. protected function _makeCache($content)
  107. {
  108.  
  109. }
  110. protected function _reperro($erro)
  111. {
  112. Nebula::$erroflag = true;
  113. Nebula::$pagerro[] = $erro . "<br/>";
  114. }
  115.  
  116.  
  117.  
  118. protected function _replace($arrayVar,$content)
  119. { // this will take an array of "list" of conditions (boolean) to be tested in the template
  120. $controls = array();
  121. foreach($this->CONTROLS[0] as $cont => $val) // making vars out of the supplied vars in index.php with corresponding vals
  122. {
  123. $$cont = 'value';
  124. $$cont = $val;
  125. }
  126. $result2 = array();
  127. $results = array();
  128. //$oldy = "IF (.*?) ([\>|\<|=]) (\d|'true'|'false') DO '(.*?)' ELSE '(.*?)'";
  129. $subpat = "\s*IF\s*([A-Za-z0-9_]+)\s*(\>|\<|\=)\s*(\d|'true'|'false'|[A-Za-z0-9_]+)\s*DO\s*'(.*?)'\s*ELSE\s*'(.*?)'\s*";
  130. $pattern = '`' .$this->LEFT_DELIMITER ."\s*" .$subpat ."\s*" .$this->RIGHT_DELIMITER .'`i';
  131. $replace = '<?php include "' .$this->PLUGIN_DIR .$fileName .'"; ?>';
  132. $count = preg_match_all($pattern, $content, $results);
  133. foreach($this->PLUGINS as $vari => $fileName)
  134. {
  135. $pattern2 = '`' .$this->LEFT_DELIMITER ."\s*" .$vari ."\s*" .$this->RIGHT_DELIMITER .'`i';
  136. $replace2 = '"; include "' .$this->PLUGIN_DIR .$fileName .'"; echo "';
  137. for($i = 0; $i < $count; $i++)
  138. {
  139. $results[4][$i] = preg_replace($pattern2, $replace2, $results[4][$i]);
  140. }
  141. for($i = 0; $i < $count; $i++)
  142. {
  143. $results[5][$i] = preg_replace($pattern2, $replace2, $results[5][$i]);
  144. }
  145. }
  146.  
  147. for($i = 0; $i<$count;$i++)
  148. {
  149. switch($results[2][$i])
  150. {
  151. case '<':
  152. $op = '<';
  153. break;
  154. case '>':
  155. $op = '>';
  156. break;
  157. case '=':
  158. $op = '==';
  159. break;
  160. default:
  161. $op = '=='; // for idk erro avoidence
  162. $this->_reperro("Invalid Operator!");
  163. break;
  164. }
  165. if(!isset($this->CONTROLS[0][$results[1][$i]]))
  166. {
  167. continue;
  168. }
  169. $content = str_replace($results[0][$i], '<?php
  170. if($' .$results[1][$i] .$op .$results[3][$i] .')
  171. {
  172. echo "' .$results[4][$i] .'";'
  173. .'}
  174. else
  175. {
  176. echo "' .$results[5][$i] .'";'
  177. .'}?>' ,$content);
  178. }
  179.  
  180. $fakeray = array();
  181. $fakecont = $content;
  182. $fakecounter = 0;
  183. $jumble = "{-WO87987W9879L8S7885HI89T!!S89RINGY78 WI75NGY TAKEY 89UPY S9PACY878-}";
  184. $pattarn = '`' .$this->LEFT_DELIMITER ."\s*([A-Za-z0-9_]+)\s*" .$this->RIGHT_DELIMITER .'`i';
  185. $fake_pat = '`' .$this->LEFT_DELIMITER ."(.*)'(.*?)\{-\s*([A-Za-z0-9_]+)\s*-\}(.*?)'(.*)" .$this->RIGHT_DELIMITER .'`i';
  186. preg_match_all($fake_pat,$fakecont,$fakeray);
  187. $fakecont = preg_replace($fake_pat,$jumble,$fakecont,-1,$fakecounter);
  188. preg_match_all($pattarn,$fakecont,$result2);
  189. foreach($result2[1] as $num => $holder)
  190. {
  191. if(!isset($this->PLUGINS[$holder]))
  192. continue;
  193. $replacer = '<?php include "' .$this->PLUGIN_DIR .$this->PLUGINS[$holder] .'"; ?>';
  194. $fakecont = preg_replace($pattarn, $replacer, $fakecont);
  195.  
  196. }
  197.  
  198. for($i = 0;$i < $fakecounter;$i++)
  199. {
  200. $bobdole = $fakeray[0][$i]; // fix reverse problem!!
  201. $fakecont = preg_replace('`' .$jumble .'`',$bobdole,$fakecont,1);
  202. }
  203. $content = $fakecont;
  204. return $content;
  205.  
  206. }
  207. }
  208.  
  209.  
  210. ?>
Add Comment
Please, Sign In to add comment