Location: PHPKode > projects > Write2Left > Base/Export.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Export.php
| Description:
|   The log exporter
| Created Aug-4-2003
+------------------------------------------------------
*/

/* Class: Export
 * Description:
 *   Driver for our exporter
 */
class Export
{
	var $skin   = '';
	var $menu   = true;
	var $result = '';
	var $error  = '';
	
	function run()
	{
		global $W2L, $userinfo, $output, $db;
		
		// Do skin related stuff
		require( "./Skin/Export.php" );
		$this->skin = new Skin_Export();
		$output->page_title = "Export Log";
		
		$output->add( $this->skin->body_top() );
		
		// Handle methods now to deal with submitted posts
		if ( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'DoExport' )
			{
				switch( $W2L->input['type'] )
				{
					case 'mt':
						$this->export_mt();
						break;
				}
			}
		}
		
		// Show whatever result we get, if any
		if( $this->result != "" )
			$output->add( "<div class=\"message\">\n" . $this->result . "\n</div><br />\n" );
		
		// Show whatever error we get, if any
		if( $this->error != "" )
			$output->add( "<div class=\"error\">\n" . $this->error . "\n</div><br />\n" );
		
		$output->add( $this->skin->export_form() );
		
		$output->add( $this->skin->body_bottom() );
	}
	
	//================
	// Exports in Moveable Type format
	//================
	function export_mt()
	{
		global $db, $userinfo;
		
		// Get a conversion table for author id to name 
		$authors = array();	 
		$db->query( "SELECT * FROM w2l_users" );
		while( $user = $db->fetch_array() )
			$authors[$user['user_id']] = $user['name'];
			
		// Get a conversion table for category id to name 
		$categories = array();	 
		$db->query( "SELECT * FROM w2l_categories WHERE log_id = $userinfo->log_id" );
		while( $cat = $db->fetch_array() )
			$categories[$cat['category_id']] = $cat['name'];

		
		$posts = $db->query( "SELECT * FROM w2l_posts 
									  WHERE log_id = $userinfo->log_id" );
		
		$file = fopen( './Uploads/export.' . $userinfo->log_id . '.txt', 'wt' );
		
		$output = '';			  
		while( $post = $db->fetch_array( $posts ) )
		{
			$output .= "AUTHOR: {$authors[$post['author_id']]}\n";
			$output .= "TITLE: $post[title]\n";
			$output .= 'DATE: ' . date( 'm/d/Y h:i:s A', $post['time'] ) . "\n";
			$output .= "PRIMARY CATEGORY:" . ( $post['category_id'] == 0 ) ? '' : $categories[$post['category_id']] . "\n";
			$output .= 'STATUS: ' . ( $post['state'] == 'draft' ) ? 'draft' : 'publish' . "\n";
			$output .= "ALLOW COMMENTS: $post[allow_comments]\n";
			$output .= "ALLOW PINGS: $post[allow_pings]\n";
			$output .= "CONVERT BREAKS: 0\n";
			$output .= "-----\n";
			
			$output .= "BODY:\n$post[body]";
			$output .= "-----\n";
			
			if( trim( $post['extended'] ) != '' )
			{
				$output .= "EXTENDED BODY:\n$post[extended]";
				$output .= "-----\n";
			}
			
			$coms = $db->query( "SELECT * FROM w2l_comments 
										 WHERE log_id = $userinfo->log_id
										   AND post_id = $post[post_id]" );
			while( $com = $db->fetch_array( $coms ) )
			{
				$output .= "COMMENT:\n";
				
				$output .= "AUTHOR: $com[author_name]\n";
				$output .= "EMAIL: $com[author_email]\n";
				$output .= "URL: $com[author_url]\n";
				$output .= 'DATA:' . date( 'm/d/Y h:i:s A', $com['time'] ). "\n";
				$output .= "IP: $com[ip_address]\n";
				$output .= $com['body'];
				$output .= "-----\n";
			}
			
			$output .= "--------\n";
		}
		
		if( !fwrite( $file, $output ) )
		{
			$this->error = 'Writing to the file failed. Check directory permissions.';
			return;
		}
		
		$this->result = "Posts Exported!<br /><br /><a href='./Uploads/export.{$userinfo->log_id}.txt'>Download Export</a>";
	}
}

$driver = new Export();
?>
Return current item: Write2Left