Location: PHPKode > projects > Write2Left > Build/BuildManager.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: BuildManager.php
| Description:
|   Handles the building of pages from templates
| Created Dec-21-2002
| Recreated Mar-31-2003
+------------------------------------------------------
*/

/* Class: BuildManager
 * Description:
 *   The handy-dandy Build Manager!
 */
class BuildManager
{
    var $callbacks  = array();
    var $context    = array();
    var $tag_parser = array();
    
	//================
	// Builds the pages passed into it
	//================
	function build_page( $temp_id )
	{
		global $W2L, $db;
		
		if( count( $this->tag_parser ) == 0 )
			$this->load_handlers();
        
	    // Get the pages and build each one
		$template = $db->query_fetch( "SELECT * FROM w2l_templates WHERE temp_id='$temp_id'" );
		$log_settings = $db->query_fetch( "SELECT l.*, c.name as def_category_name 
										   FROM w2l_logs AS l
												LEFT JOIN w2l_categories AS c
													ON l.def_category_id = c.category_id
										   WHERE l.log_id=$template[log_id]" );

        // Do all the fun stuff
        $template = $this->pre_filter( $template );
        $template['data'] = $this->parse_wrap_tags( $template['data'], 
        											$template['filename'],
        											$log_settings );
        $template['data'] = $this->parse_single_tags( $template['data'], 
        											  $template['filename'], 
        											  $log_settings );
        $template['data'] = str_replace( '<W2NoParse>', '', $template['data'] );
        $template['data'] = str_replace( '</W2NoParse>', '', $template['data'] );
        $template = $this->post_filter( $template );

        // Open our file for writing
        $page_fp = @fopen( $log_settings['path'] .
                           $template['filename'], 'w' );
                   
        // Quickly written error. Must replace...
        if( $page_fp == FALSE )
            return 'Unable to open template file!';
            
        // Lock it while we work with it...
        while( flock( $page_fp, LOCK_EX + LOCK_NB ) == FALSE )
            usleep( 20 );
            
        // Write the data and close
        fwrite( $page_fp, $template['data'] );
        fclose( $page_fp );
	}

	//================
	// Caches the data for the particular page type and possible id
	//================
	function cache_data( $type, $log_id, $data_id = -1 )
	{
		global $db;
		static $cache, $settings;
		
		if( !isset( $settings ) )
			$settings = $db->query_fetch( "SELECT l.*, c.name as def_category_name 
													FROM w2l_logs AS l
														 LEFT JOIN w2l_categories AS c
															ON l.def_category_id = c.category_id
													WHERE l.log_id=$log_id" );
		
		require_once( './Build/Caches/'.$type.'.php' );
		return $cache->build( $settings, $data_id );
	}
	
	//================
	// Loads the parsers from the Tags and Plugins directory
	//================
	function load_handlers()
	{	
		// In case there are no handlers installed ( o_O? )
		$tag_parser = array();
		
		// Open up the directories
		$tag_dir  = opendir( './Build/Tags' );
		$plug_dir = opendir( './Plugins' );
		
		// Load the distro tag handlers
        while( ( $file = readdir( $tag_dir ) ) !== false ) 
        { 
            if( substr( $file, -4 ) != '.php' )
                continue;
			
            // Load the tag parser and register it's callbacks
            include_once( './Build/Tags/'.$file );
            $tag_parser[count( $tag_parser ) - 1]->register_callbacks( $this->callbacks );
        }
        
        // Now do the addon module tag handlers
        while( ( $file = readdir( $plug_dir ) ) !== false ) 
        { 
			if( is_dir( './Plugins/' . $file ) )
				continue;
            if( substr( $file, 0, 4 ) != 'tag_' )
                continue;
            if( substr( $file, -4 ) != '.php' )
                continue;

			include( './Plugins/' . $file );
			$tag_parser[count( $tag_parser ) - 1]->register_callbacks( $this->callbacks );
        }
        
        // Play nice with the filesystem
		closedir( $tag_dir );
		closedir( $plug_dir );  	
        
        $this->tag_parser =& $tag_parser;
	}
	
	//================
	// Parses out the wrapper tags from the string passed into it recursively
	//================
	function parse_wrap_tags( &$data, $filename, $log_settings )
	{
        while( preg_match( '#(?!\<W2NoParse\>)<W2([^>]*?)(\s+([^>]*?)|)>(.*?)</W2\1>(?!\<W2NoParse\>)#is', $data, $matches ) )
        {
            if( !array_key_exists( $matches[1], $this->callbacks ) )
            {
                $data = preg_replace( '#<W2([^>]*?)(\s+([^>]*?)|)>(.*?)</W2\1>#is',
                                           '\\4', 
                                           $data, 
                                           1 );
                continue;
            }
            
            $attribs_temp = array();
            $attribs = array();
            preg_match_all ( '#(\S*?)=["\'](.+?)["\']#i', $matches[3],  $attribs_temp );
            $i = 0;
            foreach( $attribs_temp[1] as $attrib )
            {
                $attribs["$attrib"] = $attribs_temp[2][$i];
                $i++;
            }

			$this->context[] = array( $matches[1], $attribs );
            
            $replace  = call_user_func( $this->callbacks[$matches[1]], 
                                        $matches[1],
                                        $this->context, 
                                        $attribs,
                                        $matches[4],
										$filename,
										$log_settings );
            
                             
            $replace = $this->parse_wrap_tags( $replace, 
            								   $filename, 
            								   $log_settings );
            $replace = $this->parse_single_tags( $replace, 
            									 $filename, 
            								     $log_settings );
        
            $data = preg_replace( '#<W2'.$matches[1].'([^>]*?)(\s+([^>]*?)|)>(.*?)</W2'.$matches[1].'>#is',
                                       $replace, 
                                       $data, 
                                       1 );
			array_pop( $this->context );
        }
        
        return $data;
	}

	//================
	// Parses all single tags in the input
	//================
	function parse_single_tags( &$data, $filename, $log_settings )
	{
        while( preg_match( '#(?!\<W2NoParse\>)<W2([^>]*?)(\s+([^>]*?)|)>(?!\</W2NoParse\>)#is', $data, $matches ) )
        {
            if( !array_key_exists( $matches[1], $this->callbacks ) )
            {
                $data = preg_replace( '#<W2([^>]*?)(\s+([^>]*?)|)>#is',
                                           '', 
                                           $data, 
                                           1 );
                continue;
            }
            
            $attribs_temp = array();
            $attribs = array();
            preg_match_all ( '#(\S*?)=["\'](.+?)["\']#i', $matches[2],  $attribs_temp );
            $i = 0;
            foreach( $attribs_temp[1] as $attrib )
            {
                $attribs["$attrib"] = $attribs_temp[2][$i];
                $i++;
            }
            
            $data = preg_replace( '#<W2([^>]*?)(\s+([^>]*?)|)>#is',
                                       call_user_func( $this->callbacks[$matches[1]],
                                                       $matches[1],
                                                       $this->context,
                                                       $attribs, 
                                                       '',
													   $filename,
													   $log_settings ), 
                                       $data, 
                                       1 );
        }
        
        return $data;
	}
	
	//================
	// Runs all prefilters on the provided data
	//================
	function pre_filter( &$template )
	{
		static $pre_parser;
		
		if( !isset( $pre_parser ) )
		{
			$pre_parser = array();
			$pre_dir = opendir( './Build/PreFilters' );
			
	        while( ( $file = readdir( $pre_dir ) ) !== false ) 
	        { 
	            if( substr( $file, -4 ) != '.php' )
	                continue;
	
	            require( './Build/PreFilters/'.$file );
	        }
		}
		
        foreach( $pre_parser as $parser )
        {
            $template['data'] = $parser->parse_page( $template['data'], $template['filename'] );
        }
        
        return $template;
	}
	
	//================
	// Runs all postfilters on the provided data
	//================
	function post_filter( &$template )
	{
		static $post_parser;
		
		if( !isset( $post_parser ) )
		{
			$post_parser = array();
			$post_dir = opendir( './Build/PostFilters' );
			
	        while( ( $file = readdir( $post_dir ) ) !== false ) 
	        { 
	            if( substr( $file, -4 ) != '.php' )
	                continue;
	
	            require_once( './Build/PostFilters/'.$file );
	        }
		}
		
        foreach( $post_parser as $parser )
        {
            $template['data'] = $parser->parse_page( $template['data'], $template['filename'] );
        }
         
        return $template;       
	}

	//================
	// Searches the context tree for a particular tag
	//================
	function context_search( $needle = "", $context = array() )
	{
		foreach( $context as $level )
		{
			if( $level[0] == $needle )
				return $level;
		}
		
		return false;
	}
}

$BM = new BuildManager();

?>
Return current item: Write2Left