<?php

$username = isset($_REQUEST['username']) && !empty($_REQUEST['username']) ? $_REQUEST['username'] : '';

if(empty($username))
{
?>
<html>
	<head>
		<title>Tumblr2WordPress: Export Your Tumblr To WordPress</title>
		<style>
			body 
			{
				font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;
			}
			.sep
			{
				color: #CCCCCC;
			}
		</style>
	</head>
	<body>
		<h2>Tumblr2WordPress: Export Your Tumblr To WordPress</h2>
		<small>
			Version: 0.1 &nbsp;<span class="sep">|</span>&nbsp; 
			Author: <a href="http://haochen.me/">Hao Chen</a> &nbsp;<span class="sep">|</span>&nbsp;
			Email: detect [ta] hotmail [tod] com
		</small><br/><br/>
		<p>
			This tool will create a WordPress/WordPress.com compatible XML file from your Tumblr blog, which you can then save and import as a WordPress type.
		</p>
		<br/>
		Tumblr username:<br/><br/>
		<form method="POST" action="">
		<input type="text" id="username" name="username" size="40"/>
		<input type="submit" value="       Export       "/>
		<br/><br/>
		<input type="radio" name="type" value="wordpress.com" checked="checked">WordPress.com
		<input type="radio" name="type" value="wordpress">WordPress (self hosted)
		</form>
		<br/><br/>
	</body>
</html>
<?php
die();
}
$type = $_REQUEST["type"];
$i = 0;

$posts = array();
$feed = '';
$allTags = array();

do
{
	$url = 'http://'.$username.'.tumblr.com/api/read?start='. $i . '&num=50';
	$file = file_get_contents($url);
	$feed = new SimpleXMLElement($file);
	$posts = array_merge($posts, $feed->xpath('posts//post'));
	$i = (int)$feed->posts->attributes()->start + 50;
}while($i <= (int)$feed->posts["total"]);

function formatForWP($str)
{
	global $type;
	switch($type)
	{
		case "wordpress.com":
			$str = formatVideoForWP(formatImageForWP($str));
	}
	return $str;
}
function formatImageForWP($str)
{
	if(preg_match_all('/(<p>)?\s*(<img[^>]*\/?>)\s*(<\/p>)?/', $str, $matches))
	{
		for($i=0;$i<sizeof($matches[0]);$i++)
		{
			$str = str_replace($matches[0][$i], str_replace('/>',' alt=""/>', $matches[2][$i]), $str);
		}
	}
	return $str;
}

function formatVideoForWP($str)
{
	if(preg_match_all('/<object[\s\S]*src="([\S\s]*?)&amp;[\s\S]*"[\s\S]*<\/object>/', $str, $matches))
	{
		for($i=0;$i<sizeof($matches);$i++)
		{
			if((strpos($matches[1][$i], 'youtube.com') !== false))
			{
				$str = str_replace($matches[0][$i], '[youtube='.$matches[1][$i].']', $str);
			}
		}
	}
	return $str;
}

function removeWeirdChars($str)
{
	return trim(preg_replace('{(-)\1+}','$1',preg_replace('/[^a-zA-Z0-9-]/', '', str_replace(' ','-',strtolower(strip_tags($str))))),'-');
}

function getTags($post)
{
	echo "<category><![CDATA[Uncategorized]]></category>\n";
	echo "\t\t<category domain=\"category\" nicename=\"uncategorized\"><![CDATA[Uncategorized]]></category>\n";
	if($post->tag) 
	{ 
		foreach($post->tag as $tag)
		{
			echo "\t\t<category domain=\"tag\"><![CDATA[$tag]]></category>\n";
			echo "\t\t<category domain=\"tag\" nicename=\"" . removeWeirdChars($tag) . "\"><![CDATA[$tag]]></category>\n";
			addTag((String)$tag);
		}
	}
}

function addTag($tag)
{
	global $allTags;
	if(!in_array($tag, $allTags))
		$allTags[] = $tag;
}

function getAllTags()
{
	global $allTags;
	foreach($allTags as $tag)
	{
		echo "\t<wp:tag><wp:tag_slug>". removeWeirdChars($tag) . "</wp:tag_slug><wp:tag_name><![CDATA[$tag]]></wp:tag_name></wp:tag>\n";
	}
}

header('content-type: text/xml');
header("content-disposition: attachment; filename=tumblr_$username.xml");
?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; ?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
<!-- It contains information about your blog's posts, comments, and categories. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your blog. -->

<!-- To import this information into a WordPress blog follow these steps. -->
<!-- 1. Log into that blog as an administrator. -->
<!-- 2. Go to Manage: Import in the blog's admin panels. -->
<!-- 3. Choose "WordPress" from the list. -->
<!-- 4. Upload this file using the form provided on that page. -->
<!-- 5. You will first be asked to map the authors in this export file to users -->
<!--    on the blog.  For each author, you may choose to map to an -->
<!--    existing user on the blog or to create a new user -->
<!-- 6. WordPress will then import each of the posts, comments, and categories -->
<!--    contained in this file into your blog -->

<!-- generator="Tumblr2WordPress/0.1" created="<?php echo date("Y-m-d H:i") ?>"-->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:wp="http://wordpress.org/export/1.0/"
>

<channel>
	<title><?php echo $feed->tumblelog->attributes()->title ?></title>
	<link>http://<?php echo $feed->tumblelog->attributes()->name ?>.tumblr.com/</link>
	<description><?php echo $feed->tumblelog ?></description>
	<pubDate><?php echo date("r") ?></pubDate>
	<generator>http://haochen.me/tumblr/?v=0.1</generator>
	<language>en</language>
	<wp:wxr_version>1.0</wp:wxr_version>
	<wp:base_site_url>http://<?php echo $feed->tumblelog->attributes()->name ?>.tumblr.com/</wp:base_site_url>
	<wp:base_blog_url>http://<?php echo $feed->tumblelog->attributes()->name ?>.tumblr.com/</wp:base_blog_url>
	<wp:category>
		<wp:category_nicename>uncategorized</wp:category_nicename>
		<wp:category_parent></wp:category_parent>
		<wp:cat_name><![CDATA[Uncategorized]]></wp:cat_name>
	</wp:category>
<?php 
ob_start();
	foreach($posts as $post) 
	{ 
?>
	<item>
<?php
		switch($post->attributes()->type) 
		{
			case "regular":
?>
	 	<title><?php echo htmlspecialchars($post->{'regular-title'}) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description></description>
		<content:encoded><![CDATA[<?php echo formatForWP($post->{'regular-body'}) ?>]]></content:encoded>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars($post->{'regular-title'}) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
			case "photo":
?>
	 	<title><?php echo htmlspecialchars(strip_tags($post->{'photo-caption'})) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description></description>
		<content:encoded><![CDATA[<img src="<?php echo $post->{'photo-url'} ?>" alt=""/><br/><br/><?php echo formatForWP($post->{'photo-caption'}) ?>]]></content:encoded>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars($post->{'photo-caption'}) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
			case "quote":
?>
	 	<title><?php echo htmlspecialchars(strip_tags($post->{'quote-text'})) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description></description>
		<content:encoded><![CDATA[<?php echo $post->{'quote-text'} ?><br/><?php echo formatForWP($post->{'quote-source'}) ?>]]></content:encoded>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars(str_replace('&#8220;','',str_replace('&#8221;','',$post->{'quote-text'}))) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
			case "link":
?>
	 	<title><?php echo htmlspecialchars(strip_tags($post->{'link-text'})) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description><?php echo htmlspecialchars(strip_tags($post->{'link-description'})) ?></description>
		<content:encoded><![CDATA[<a href="<?php echo $post->{'link-url'} ?>"><?php echo $post->{'link-text'} ?></a><br/><?php echo formatForWP($post->{'link-description'}) ?>]]></content:encoded>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars($post->{'link-text'}) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
			case "conversation":
?>
	 	<title><?php echo htmlspecialchars(strip_tags($post->{'conversation-title'})) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description></description>
		<content:encoded><![CDATA[<?php foreach($post->{'conversation-line'} as $line) { ?><strong><?php echo $line->attributes()->label ?></strong> <?php echo $line ?><br/><?php } ?>]]></content:encoded>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars($post->{'conservation-title'}) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
			case "video":
?>
	 	<title><?php echo htmlspecialchars(strip_tags($post->{'video-caption'})) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description></description>
<?php if($type == 'wordpress.com' && strpos($post->{'video-source'}, 'youtube.com') !== false) { ?>
		<content:encoded><![CDATA[[youtube=<?php echo $post->{'video-source'} ?>]]]></content:encoded>
<?php }elseif($type == 'wordpress.com' && strpos($post->{'video-source'}, 'video.google.com') !== false) { ?>	
		<content:encoded><![CDATA[[googlevideo=<?php preg_match('/src="([\S\s]*?)"/', $post->{'video-player'}, $matches); echo $matches[1]; ?>]]]></content:encoded>
<?php }else{ ?>	
		<content:encoded><![CDATA[<?php echo $post->{'video-player'} ?>]]></content:encoded>
<?php } ?>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars($post->{'video-caption'}) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
			case "audio":
?>
	 	<title><?php echo htmlspecialchars(strip_tags($post->{'audio-caption'})) ?></title>
		<link><?php echo $post->attributes()->url ?></link>
		<pubDate><?php echo $post->attributes()->date ?> +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
		<?php getTags($post) ?>
		<guid isPermaLink="false"><?php echo $post->attributes()->url ?></guid>
		<description></description>
		<content:encoded><![CDATA[<a href="<?php preg_match('/audio_file=([\S\s]*?)(&|")/', $post->{'audio-player'}, $matches); echo $matches[1]; ?>"><?php echo $post->{'audio-caption'} ?></a>]]></content:encoded>
		<wp:post_id><?php echo $post->attributes()->id ?></wp:post_id>
		<wp:post_date><?php echo date('Y-m-d G:i:s', (double)$post->attributes()->{'unix-timestamp'}) ?></wp:post_date>
		<wp:post_date_gmt><?php echo str_replace(" GMT", "", $post->attributes()->{'date-gmt'}) ?></wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name><?php echo removeWeirdChars($post->{'audio-caption'}) ?></wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>post</wp:post_type>
		<wp:post_password></wp:post_password>
<?php
				break;
		}
?>
	</item>
<?php 
	}
	$out = ob_get_contents();
	ob_end_clean();
	getAllTags();
	echo $out;
?>
</channel>
</rss>
