Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Project: Nebula Templating Engine
- * By: Mansoor Sayed
- *
- *
- */
- if (!defined('NEBULA_DIR'))
- define('NEBULA_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
- if(!defined('PHP'))
- define('PHP', ".php");
- class Nebula
- {
- var $LEFT_DELIMITER = '{-'; //Opening delimiter
- var $RIGHT_DELIMITER = '-}'; //Opening delimiter
- var $ERROR_REPORT = true;
- var $CACHE = true; //enable/disable caching
- var $CACHE_DIR = 'Nebula_cache/';// relitive to the php file calling this class's location
- var $PLUGIN_DIR = 'Nebula_plugin/';
- var $TEMPLATE_DIR = 'template/';
- var $PLUGIN_SYNTX = 'plugin.';//Nebula_plugin_name()
- var $PLUGIN_FUNC_STNX = 'Nebula_plugin_'; //sintax for the main function of the plugin
- var $CACHE_FILE_SNTX = 'Nebula_cache.';//Nebula_cache_pagename()
- /* END OF CONFIG */
- var $CONTLIST = array();
- var $PLUGINS = array();
- var $CONTROLS = array();
- var $CURRPAGE = ''; // for the use of any plugin
- protected static $erroflag = false;
- protected static $pagerro = array();
- var $CACHEINCLUDE = array();
- function start(){
- if(count($this->CONTLIST) == 0) //Checking to see if there is any content.
- $this->_reperro("Error! There is no content to display!");
- $curr_file = $this->CACHE_DIR .$this->CACHE_FILE_SNTX .$this->CURRPAGE .PHP;
- if($this->CACHE === true && file_exists($curr_file) == true)
- include_once($curr_file);
- else{
- $this->_load_plugins();
- $content = $this->_replace($this->_merge(array_reverse($this->CONTLIST)));
- if ($this->ERROR_REPORT == true)
- foreach(Nebula::$pagerro as $erro)
- $content = $erro . $content;
- $fh = fopen($curr_file, 'w');
- fwrite($fh, $content);
- fclose($fh);
- include_once($curr_file);
- if($this->CACHE === false)
- unlink($curr_file);
- }
- }
- function display($content){
- // will not import a file!!
- $this->CONTLIST[] = $content;
- }
- function display_file($file){// import a file as page content
- if (file_exists($file))
- {
- $this->CONTLIST[] = file_get_contents($file);
- }
- else
- $this->_reperro("Error! The file '" .$file ."' was not found!");
- }
- function _merge($parts){
- $content = '';
- foreach ($parts as $peep)
- $content .= $peep;
- return $content;
- }
- function _load_plugins(){// also, as of now, prepares the variable array list
- if ($handle = opendir($this->PLUGIN_DIR)){
- while (false !== ($file = readdir($handle))){
- if(false !== strstr($file, $this->PLUGIN_SYNTX)){
- $prt = explode('.', $file);
- $this->PLUGINS[$prt[1]] = $file;
- }
- }
- closedir($handle);
- }
- }
- function set_controls($controls){
- $this->CONTROLS[] = $controls;
- }
- function cache($pram = true){
- $this->CACHE = $pram;
- }
- function set_currpage($iden){
- $this->CURRPAGE = $iden;
- }
- function clearCache(){
- if ($handle = opendir($this->CACHE_DIR)){
- while (false !== ($file = readdir($handle))){
- if(false !== strstr($file, $this->CACHE_FILE_SNTX)){
- if(!unlink($this->CACHE_DIR .$file))
- $this->_reperro("Error! Cannot delete cache files \"" .$this->CACHE_DIR .$file ."\"<br/>");
- }
- }
- closedir($handle);
- }
- else
- $this->_reperro("Error! Can't open Plugin directory! \"" .$this->CACHE_DIR ."\" Do you have permission? Does it exist?");
- }
- function clearPageCache($page){
- unlink($this->CACHE_DIR .$this->CACHE_FILE_SNTX .$page .PHP);
- }
- function _reperro($erro){
- Nebula::$erroflag = true;
- Nebula::$pagerro[] = $erro . "<br/>";
- }
- function _replace($content) {// this will take an array of "list" of conditions (boolean) to be tested in the template
- $controls = array();
- $results = array();
- $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';
- $count = preg_match_all($pattern, $content, $results);
- foreach ($this->PLUGINS as $iden => $fileName){
- $pattern2 = '`' .$this->LEFT_DELIMITER .'\s*' .$iden .'\s*' .$this->RIGHT_DELIMITER .'`i';
- $replace2 = "' . " .$this->PLUGIN_SYNTX .$iden ."() . '";
- for ($i = 0; $i < $count; $i++){
- $results[4][$i] = preg_replace($pattern2, $replace2, $results[4][$i]);
- $results[5][$i] = preg_replace($pattern2, $replace2, $results[5][$i]);
- }
- }
- for ($i = 0; $i<$count; $i++){ // Making sure that there are no php syntax errors at runtime
- 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); // Creating the test statement for compiling
- }
- $fakeray = array();
- $fakecounter = 0;
- $ifUsedCount = 0;
- $jumble = $this->LEFT_DELIMITER .'Place_Holder.' .rand() .$this->RIGHT_DELIMITER;
- $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';
- // For returning statements inside the two delimeters that do not make sense without changing it
- preg_match_all($fake_pat, $content, $fakeray);
- $content = preg_replace($fake_pat, $jumble, $content, -1, $fakecounter);
- $includeprt = '<?php ';
- foreach ($this->PLUGINS as $iden => $arrayVar){
- $bob = explode('.', $arrayVar);
- $pattern = '`' .$this->LEFT_DELIMITER .$bob[1] .$this->RIGHT_DELIMITER .'`i';
- $replace = '<?php echo ' .$this->PLUGIN_FUNC_STNX .$iden .'(); ?>';
- $content = preg_replace($pattern, $replace, $content,-1,$ifUsedCount);
- if($ifUsedCount !=0) //this is used to include only the plugins that are used. for optomization...
- $includeprt .= 'include_once(\'' .$this->PLUGIN_DIR .$arrayVar .'\'); ';
- }
- $includeprt .= ' ?>';
- for ($i = 0; $i < $fakecounter; $i++)
- $content = preg_replace('`' .$jumble .'`', $fakeray[0][$i], $content);
- return $includeprt .$content;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment