Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // mansoor sayed
- class Nebula
- {
- var $LEFT_DELIMITER = '{-';
- var $RIGHT_DELIMITER = '-}';
- var $ERROR_REPORT = true;
- var $CACHE_DIR = 'cache/'; // relitive to php file location
- var $PLUGIN_DIR = 'plugin/';
- var $CACHE = true;
- var $TEMPLATE_DIR = 'template/';
- //private var $_loadPlugins = true;
- /* END OF CONFIG. DO NOT TOUCH ANY THING BELOW THIS POINT */
- //var
- var $CONTLIST = array();
- var $PLUGINS = array();
- var $CONTROLS = array();
- protected static $erroflag = false;
- protected static $pagerro = array();
- static $content;
- /* Class constructor */
- public function Nebula()
- {
- $this->display("starting Nebula templating engine!<br>");
- $this->_load_plugins();
- }
- public function start()
- {
- $content = $this->_replace($this->PLUGINS, $this->_merge($this->CONTLIST));
- if($this->ERROR_REPORT == true)
- {
- foreach(Nebula::$pagerro as $erro)
- {
- $content = $erro . $content;
- }
- }
- Nebula::$content = $content;
- echo $content;
- }
- /* if ($this->$erroflag == false)
- {
- $this->_merge($this->CONTLIST);
- }
- */
- public function display($content) // imports any content imported from the php file
- { // will not import a file!!
- $this->CONTLIST[] = $content;
- }
- public function display_file($file) // import a file as resource
- {
- if(file_exists($file))
- $this->CONTLIST[] = file_get_contents($file);
- else
- $this->_reperro("Error! The file '" .$file ."' was not found!");
- }
- protected function _merge($parts)
- {
- $content = '';
- for($i=0; $i < count($parts);$i++)
- {
- $content .= $parts[$i];
- }
- return $content;
- }
- protected function _load_plugins() // also, as of now, prepares the variable array list
- {
- if ($handle = opendir($this->PLUGIN_DIR))
- {
- while (false !== ($file = readdir($handle)))
- {
- if(preg_match('`^plugin.`',$file))
- {
- $prt = explode('.',$file);
- $this->PLUGINS[$prt[1]] = $file;
- }
- }
- closedir($handle);
- }
- }
- public function set_controls($controls)
- {
- /* foreach($controls as $cont => $val)
- {
- $$cont = 'value';
- $$cont = $val;
- $this->CONTROLS[] = $$cont;
- } */
- $this->CONTROLS[] = $controls;
- }
- protected function _makeCache($content)
- {
- }
- protected function _reperro($erro)
- {
- Nebula::$erroflag = true;
- Nebula::$pagerro[] = $erro . "<br/>";
- }
- protected function _replace($arrayVar,$content)
- { // this will take an array of "list" of conditions (boolean) to be tested in the template
- $controls = array();
- foreach($this->CONTROLS[0] as $cont => $val) // making vars out of the supplied vars in index.php with corresponding vals
- {
- $$cont = 'value';
- $$cont = $val;
- }
- $result2 = array();
- $results = array();
- //$oldy = "IF (.*?) ([\>|\<|=]) (\d|'true'|'false') DO '(.*?)' ELSE '(.*?)'";
- $subpat = "\s*IF\s*([A-Za-z0-9_]+)\s*(\>|\<|\=)\s*(\d|'true'|'false'|[A-Za-z0-9_]+)\s*DO\s*'(.*?)'\s*ELSE\s*'(.*?)'\s*";
- $pattern = '`' .$this->LEFT_DELIMITER ."\s*" .$subpat ."\s*" .$this->RIGHT_DELIMITER .'`i';
- $replace = '<?php include "' .$this->PLUGIN_DIR .$fileName .'"; ?>';
- $count = preg_match_all($pattern, $content, $results);
- foreach($this->PLUGINS as $vari => $fileName)
- {
- $pattern2 = '`' .$this->LEFT_DELIMITER ."\s*" .$vari ."\s*" .$this->RIGHT_DELIMITER .'`i';
- $replace2 = '"; include "' .$this->PLUGIN_DIR .$fileName .'"; echo "';
- for($i = 0; $i < $count; $i++)
- {
- $results[4][$i] = preg_replace($pattern2, $replace2, $results[4][$i]);
- }
- for($i = 0; $i < $count; $i++)
- {
- $results[5][$i] = preg_replace($pattern2, $replace2, $results[5][$i]);
- }
- }
- for($i = 0; $i<$count;$i++)
- {
- switch($results[2][$i])
- {
- case '<':
- $op = '<';
- break;
- case '>':
- $op = '>';
- break;
- case '=':
- $op = '==';
- break;
- default:
- $op = '=='; // for idk erro avoidence
- $this->_reperro("Invalid Operator!");
- break;
- }
- if(!isset($this->CONTROLS[0][$results[1][$i]]))
- {
- continue;
- }
- $content = str_replace($results[0][$i], '<?php
- if($' .$results[1][$i] .$op .$results[3][$i] .')
- {
- echo "' .$results[4][$i] .'";'
- .'}
- else
- {
- echo "' .$results[5][$i] .'";'
- .'}?>' ,$content);
- }
- $fakeray = array();
- $fakecont = $content;
- $fakecounter = 0;
- $jumble = "{-WO87987W9879L8S7885HI89T!!S89RINGY78 WI75NGY TAKEY 89UPY S9PACY878-}";
- $pattarn = '`' .$this->LEFT_DELIMITER ."\s*([A-Za-z0-9_]+)\s*" .$this->RIGHT_DELIMITER .'`i';
- $fake_pat = '`' .$this->LEFT_DELIMITER ."(.*)'(.*?)\{-\s*([A-Za-z0-9_]+)\s*-\}(.*?)'(.*)" .$this->RIGHT_DELIMITER .'`i';
- preg_match_all($fake_pat,$fakecont,$fakeray);
- $fakecont = preg_replace($fake_pat,$jumble,$fakecont,-1,$fakecounter);
- preg_match_all($pattarn,$fakecont,$result2);
- foreach($result2[1] as $num => $holder)
- {
- if(!isset($this->PLUGINS[$holder]))
- continue;
- $replacer = '<?php include "' .$this->PLUGIN_DIR .$this->PLUGINS[$holder] .'"; ?>';
- $fakecont = preg_replace($pattarn, $replacer, $fakecont);
- }
- for($i = 0;$i < $fakecounter;$i++)
- {
- $bobdole = $fakeray[0][$i]; // fix reverse problem!!
- $fakecont = preg_replace('`' .$jumble .'`',$bobdole,$fakecont,1);
- }
- $content = $fakecont;
- return $content;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment