Location: PHPKode > projects > Write2Left > Base/Import.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Import.php
| Description:
|   Importer for other blogging systems
| Created Jul-09-2003
+------------------------------------------------------
*/

/* Class: Import
 * Description:
 *   Driver for our import page
 */
class Import
{
	var $skin   = '';
	var $menu   = true;
	var $result = '';
	var $error  = '';
	
	function run()
	{
		global $W2L, $userinfo, $output, $db, $func;
		
		// Do skin related stuff
		require( "./Skin/Import.php" );
		$this->skin = new Skin_Import();
		$output->page_title = "Import";
		$output->loc_add( "Import" );
		$output->add( $this->skin->body_top() );
		
		// Handle any methods
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'doimport' )
				$this->do_import();
		}

		if( $this->result != '' )
			$output->add( "<div class='message'>$this->result</div><br />" );
		if( $this->error != '' )
			$output->add( "<div class='error'>$this->error</div><br />" );
		
		$id = $func->get_logs_sql_id();
		$logs = '';
		while( $log = $db->fetch_array( $id ) )
		{
			$logs .= "<option value='$log[log_id]'>$log[name]</option>";
		}
		
		$output->add( $this->skin->import_form( $logs ) );
		
		$output->add( $this->skin->body_bottom() );
	}
	
	function do_import()
	{
		global $W2L, $db, $output;

		$filename = 'import.' . $_FILES['importfile']['name'];

		// Move the file to where it needs to go
		if ( !@move_uploaded_file( $_FILES['importfile']['tmp_name'], './Uploads/'. $filename ) )
		{
			// Oops!
			$this->error = "Could not move uploaded file. Are your directory permissions properly set?";
			return;
		}
		else
		{
			// Make sure we can read the damn thing
			@chmod( './Uploads/' . $filename, 0777 );
		}

		// Open and read the file
		$import_file_fp = fopen( './Uploads/' . $filename, 'rt' );
		$import_file = fread( $import_file_fp, filesize ( './Uploads/' . $filename ) );
		fclose( $import_file_fp );
		
		unlink( './Uploads/' . $filename );

		switch( $W2L->input['type'] )
		{
			case 'mt2':
				$this->import_mt( $import_file, $W2L->input['log_id'] );
				return;
			default:
				$this->error = "Invalid type selected";
				return;
		}
	}

	function import_mt( $import_file, $log_id )
	{
		global $db;
		
		// Boom!
		$posts = explode( "--------\n", $import_file );
		array_pop( $posts );
		
		$fields = array( 'AUTHOR:',
						 'TITLE:',
						 'DATE:',
						 'PRIMARY CATEGORY:',
						 'CATEGORY:',
						 'STATUS:',
						 'ALLOW COMMENTS:',
						 'ALLOW PINGS:',
						 'CONVERT BREAKS:',
						 'NO ENTRY:',
						 'BODY:',
						 'EXTENDED BODY:',
						 'EXCERPT:',
						 'COMMENT:',
						 'PING:' );
		
		// Get a conversion table for author name to id 
		$authors = array();	 
		$db->query( "SELECT * FROM w2l_users" );
		while( $user = $db->fetch_array() )
			$authors[$user['name']] = $user['user_id'];
			
		// Get a conversion table for category name to id 
		$categories = array();	 
		$db->query( "SELECT * FROM w2l_categories WHERE log_id = $log_id" );
		while( $cat = $db->fetch_array() )
			$categories[$cat['name']] = $cat['category_id'];

		foreach( $posts as $post )
		{                    					
			$post_ex = explode( "-----\n", $post );
			
			$cur_post = array();
			$cur_comments = array();

			foreach( $post_ex as $section )
			{	
				if( substr( $section, 0, 5 ) == 'BODY:' )
					$cur_post['body'] = addslashes( substr( $section, 6 ) );
				else if( substr( $section, 0, 14 ) == 'EXTENDED BODY:' )
					$cur_post['extended'] = addslashes( substr( $section, 15 ) );
				else if( substr( $section, 0, 8 ) == 'EXCERPT:' )
					continue; // do nothing right now
				else if( substr( $section, 0, 8 ) == 'COMMENT:' )
				{
					$comment = explode( "\n", substr( $section, 9 ) );
					
					$new_comment = array();
					$new_comment['body'] = '';
					
					foreach( $comment as $item )
					{
						if( substr( $item, 0, 7 )  == 'AUTHOR:' )
						{
							$new_comment['author_name'] = addslashes( substr( $item, 8 ) );
							if( substr( $item, 8 ) == '' )
								$new_comment['author_id'] = 0;
							else if( array_key_exists( substr( $item, 8 ), $authors ) )
								$new_comment['author_id'] = $authors[substr( $item, 8 )];
							else
								$new_comment['author_id'] = 0;
						}
						else if( substr( $item, 0, 6 )  == 'EMAIL:' )
							$new_comment['author_email'] = addslashes( substr( $item, 7 ) );
						else if( substr( $item, 0, 4 )  == 'URL:' )
							$new_comment['author_url'] = addslashes( substr( $item, 5 ) );
						else if( substr( $item, 0, 3 )  == 'IP:' )
							$new_comment['ip_address'] = substr( $item, 4, 16 );
						else if( substr( $item, 0, 5 )  == 'DATE:' )
							$new_comment['time'] = strtotime( substr( $item, 6 ) );
						else
							$new_comment['body'] .= addslashes( $item );
					}
					
					$cur_comments[] = $new_comment;
				}
				else if( substr( $section, 0, 8 ) == 'PING:' )
					continue; // do nothing right now
				else
				{
					$items = explode( "\n", $section );
					foreach( $items as $item )
					{
						if( substr( $item, 0, 7 )  == 'AUTHOR:' )
						{
							if( substr( $item, 8 ) == '' )
								$cur_post['author_id'] = 0;
							else if( array_key_exists( substr( $item, 8 ), $authors ) )
								$cur_post['author_id'] = $authors[substr( $item, 8 )];
							else
								$cur_post['author_id'] = 0;
						}
						if( substr( $item, 0, 6 )  == 'TITLE:' )
							$cur_post['title'] = addslashes( substr( $item, 7 ) );
						if( substr( $item, 0, 5 )  == 'DATE:' )
							$cur_post['time'] = strtotime( substr( $item, 6 ) );
						if( substr( $item, 0, 17 ) == 'PRIMARY CATEGORY:' )
						{
							if( substr( $item, 18 ) == '' )
								$cur_post['category_id'] = 0;
							else if( array_key_exists( substr( $item, 18 ), $categories ) )
								$cur_post['category_id'] = $categories[substr( $item, 18 )];
							else
								$cur_post['category_id'] = 0;
						}
						if( substr( $item, 0, 7 )  == 'STATUS:' )
							$cur_post['state'] = strtolower( substr( $item, 8 ) );
						if( substr( $item, 0, 15 ) == 'ALLOW COMMENTS:' )
							$cur_post['allow_comments'] = substr( $item, 16 );
						if( substr( $item, 0, 12 ) == 'ALLOW PINGS:' )
							$cur_post['allow_pings'] = substr( $item, 13 );
					}
				}
			}
			
			$db->query( "INSERT INTO w2l_posts 
								VALUES (
									NULL,
									$log_id,
									$cur_post[author_id],
									'$cur_post[title]',
									'$cur_post[body]', 
									'$cur_post[extended]', 
									$cur_post[category_id], 
									$cur_post[time], 
									'$cur_post[state]', 
									$cur_post[allow_comments], 
									$cur_post[allow_pings],
									".count( $cur_comments ).", 
									'nobody', 
									".time().")", 1 );
									
			$new_post_id = $db->insert_id();
			
			foreach( $cur_comments as $comment )
			{
				$db->query( "INSERT INTO w2l_comments 
									VALUES (
										NULL,
										$new_post_id,
										$log_id,
										'$comment[body]',
										$comment[author_id],
										'$comment[author_name]', 
										'$comment[author_email]', 
										'$comment[author_url]', 
										'$comment[ip_address]', 
										$comment[time] )", 1 );
			}
		}

		$this->result = 'File Imported Successfully!';
	}	
}

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