Guest User

Untitled

a guest
Jul 7th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. <?php
  2. /*
  3. * Project: Nebula Templating Engine
  4. * By: Mansoor Sayed
  5. *
  6. *
  7. */
  8. if (!defined('NEBULA_DIR'))
  9. define('NEBULA_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
  10. if(!defined('PHP'))
  11. define('PHP', ".php");
  12.  
  13. class Nebula
  14. {
  15. var $LEFT_DELIMITER = '{-'; //Opening delimiter
  16. var $RIGHT_DELIMITER = '-}'; //Opening delimiter
  17. var $ERROR_REPORT = true;
  18. var $CACHE = true; //enable/disable caching
  19. var $CACHE_DIR = 'Nebula_cache/';// relitive to the php file calling this class's location
  20. var $PLUGIN_DIR = 'Nebula_plugin/';
  21. var $TEMPLATE_DIR = 'template/';
  22. var $PLUGIN_SYNTX = 'plugin.';//Nebula_plugin_name()
  23. var $PLUGIN_FUNC_STNX = 'Nebula_plugin_'; //sintax for the main function of the plugin
  24. var $CACHE_FILE_SNTX = 'Nebula_cache.';//Nebula_cache_pagename()
  25.  
  26. /* END OF CONFIG */
  27.  
  28. var $CONTLIST = array();
  29. var $PLUGINS = array();
  30. var $CONTROLS = array();
  31. var $CURRPAGE = ''; // for the use of any plugin
  32. protected static $erroflag = false;
  33. protected static $pagerro = array();
  34. var $CACHEINCLUDE = array();
  35.  
  36. function start(){
  37. if(count($this->CONTLIST) == 0) //Checking to see if there is any content.
  38. $this->_reperro("Error! There is no content to display!");
  39. $curr_file = $this->CACHE_DIR .$this->CACHE_FILE_SNTX .$this->CURRPAGE .PHP;
  40. if($this->CACHE === true && file_exists($curr_file) == true)
  41. include_once($curr_file);
  42. else{
  43. $this->_load_plugins();
  44. $content = $this->_replace($this->_merge(array_reverse($this->CONTLIST)));
  45. if ($this->ERROR_REPORT == true)
  46. foreach(Nebula::$pagerro as $erro)
  47. $content = $erro . $content;
  48. $fh = fopen($curr_file, 'w');
  49. fwrite($fh, $content);
  50. fclose($fh);
  51. include_once($curr_file);
  52. if($this->CACHE === false)
  53. unlink($curr_file);
  54. }
  55. }
  56. function display($content){
  57. // will not import a file!!
  58. $this->CONTLIST[] = $content;
  59. }
  60. function display_file($file){// import a file as page content
  61. if (file_exists($file))
  62. {
  63. $this->CONTLIST[] = file_get_contents($file);
  64. }
  65. else
  66. $this->_reperro("Error! The file '" .$file ."' was not found!");
  67. }
  68. function _merge($parts){
  69. $content = '';
  70. foreach ($parts as $peep)
  71. $content .= $peep;
  72. return $content;
  73. }
  74. function _load_plugins(){// also, as of now, prepares the variable array list
  75. if ($handle = opendir($this->PLUGIN_DIR)){
  76. while (false !== ($file = readdir($handle))){
  77. if(false !== strstr($file, $this->PLUGIN_SYNTX)){
  78. $prt = explode('.', $file);
  79. $this->PLUGINS[$prt[1]] = $file;
  80. }
  81. }
  82. closedir($handle);
  83. }
  84. }
  85. function set_controls($controls){
  86. $this->CONTROLS[] = $controls;
  87. }
  88. function cache($pram = true){
  89. $this->CACHE = $pram;
  90. }
  91. function set_currpage($iden){
  92. $this->CURRPAGE = $iden;
  93. }
  94. function clearCache(){
  95. if ($handle = opendir($this->CACHE_DIR)){
  96. while (false !== ($file = readdir($handle))){
  97. if(false !== strstr($file, $this->CACHE_FILE_SNTX)){
  98. if(!unlink($this->CACHE_DIR .$file))
  99. $this->_reperro("Error! Cannot delete cache files \"" .$this->CACHE_DIR .$file ."\"<br/>");
  100. }
  101. }
  102. closedir($handle);
  103. }
  104. else
  105. $this->_reperro("Error! Can't open Plugin directory! \"" .$this->CACHE_DIR ."\" Do you have permission? Does it exist?");
  106. }
  107. function clearPageCache($page){
  108. unlink($this->CACHE_DIR .$this->CACHE_FILE_SNTX .$page .PHP);
  109. }
  110. function _reperro($erro){
  111. Nebula::$erroflag = true;
  112. Nebula::$pagerro[] = $erro . "<br/>";
  113. }
  114. function _replace($content) {// this will take an array of "list" of conditions (boolean) to be tested in the template
  115. $controls = array();
  116. $results = array();
  117. $subpat = "\s*IF\s*([A-Za-z0-9_]+)\s*(\>|\<|\=)\s*(\d|'true'|'false'|[A-Za-z0-9_]+)\s*DO\s*'(.*?)'\s*ELSE\s*'(.*?)'\s*";
  118. $pattern = '`' .$this->LEFT_DELIMITER ."\s*" .$subpat ."\s*" .$this->RIGHT_DELIMITER .'`i';
  119. $count = preg_match_all($pattern, $content, $results);
  120. foreach ($this->PLUGINS as $iden => $fileName){
  121. $pattern2 = '`' .$this->LEFT_DELIMITER .'\s*' .$iden .'\s*' .$this->RIGHT_DELIMITER .'`i';
  122. $replace2 = "' . " .$this->PLUGIN_SYNTX .$iden ."() . '";
  123. for ($i = 0; $i < $count; $i++){
  124. $results[4][$i] = preg_replace($pattern2, $replace2, $results[4][$i]);
  125. $results[5][$i] = preg_replace($pattern2, $replace2, $results[5][$i]);
  126. }
  127. }
  128. for ($i = 0; $i<$count; $i++){ // Making sure that there are no php syntax errors at runtime
  129. switch ($results[2][$i])
  130. {
  131. case '<':
  132. $op = '<';
  133. break;
  134. case '>':
  135. $op = '>';
  136. break;
  137. case '=':
  138. $op = '==';
  139. break;
  140. default:$op = '==';// for idk erro avoidence
  141. $this->_reperro("Invalid Operator!");
  142. break;
  143. }
  144. if (!isset($this->CONTROLS[0][$results[1][$i]]))
  145. continue;
  146. $content = str_replace($results[0][$i], '<?php
  147. if($' .$results[1][$i] .$op .$results[3][$i] .')
  148. {
  149. echo \'' .$results[4][$i] .'\';' .'}
  150. else
  151. {
  152. echo \'' .$results[5][$i] .'\';'.'
  153. }?>', $content); // Creating the test statement for compiling
  154. }
  155. $fakeray = array();
  156. $fakecounter = 0;
  157. $ifUsedCount = 0;
  158. $jumble = $this->LEFT_DELIMITER .'Place_Holder.' .rand() .$this->RIGHT_DELIMITER;
  159. $pattarn = '`' .$this->LEFT_DELIMITER ."\s*([A-Za-z0-9_]+)\s*" .$this->RIGHT_DELIMITER .'`i';
  160. $fake_pat = '`' .$this->LEFT_DELIMITER ."(.*)'(.*?)\{-\s*([A-Za-z0-9_]+)\s*-\}(.*?)'(.*)" .$this->RIGHT_DELIMITER .'`i';
  161. // For returning statements inside the two delimeters that do not make sense without changing it
  162. preg_match_all($fake_pat, $content, $fakeray);
  163. $content = preg_replace($fake_pat, $jumble, $content, -1, $fakecounter);
  164. $includeprt = '<?php ';
  165. foreach ($this->PLUGINS as $iden => $arrayVar){
  166. $bob = explode('.', $arrayVar);
  167. $pattern = '`' .$this->LEFT_DELIMITER .$bob[1] .$this->RIGHT_DELIMITER .'`i';
  168. $replace = '<?php echo ' .$this->PLUGIN_FUNC_STNX .$iden .'(); ?>';
  169. $content = preg_replace($pattern, $replace, $content,-1,$ifUsedCount);
  170. if($ifUsedCount !=0) //this is used to include only the plugins that are used. for optomization...
  171. $includeprt .= 'include_once(\'' .$this->PLUGIN_DIR .$arrayVar .'\'); ';
  172. }
  173. $includeprt .= ' ?>';
  174. for ($i = 0; $i < $fakecounter; $i++)
  175. $content = preg_replace('`' .$jumble .'`', $fakeray[0][$i], $content);
  176. return $includeprt .$content;
  177. }
  178. }
  179.  
  180. ?>
Add Comment
Please, Sign In to add comment