Wednesday, September 02, 2009

Importing Blogs From Any Wordpress Site URL Using PHP

set_time_limit(0);
error_reporting(0);
@ini_set("memory_limit", "1024M");
@ini_set("allow_url_include", "on");
@ini_set("allow_url_fopen", "on");
@ini_set("allow_call_time_pass_reference", "on");
require_once('wp-config.php');

$type = 'wordpress';
$UserName = 'Wordpress Admin Username';
$Password = 'Wordpress Admin Password';
$BlogAddress = 'http://blogname.wordpress.com OR http://www.example.com/blogs';

$UserId = $_SESSION["ses_ID"];
$lastorder = 0;

?>
//For Wordpress.com Blogs
$destinationpath = "imported_blogs/";
$mainfilename = "blog.html";

if(!file_exists($destinationpath.$UserId)) {
mkdir($destinationpath.$UserId, 0777);
chmod($destinationpath.$UserId, 0777);
}
if(!file_exists($destinationpath.$UserId."/".$type)) {
mkdir($destinationpath.$UserId."/".$type, 0777);
chmod($destinationpath.$UserId."/".$type, 0777);
}
$destinationpath2 = $destinationpath.$UserId."/".$type."/";

$counter = 0;
if($lastorder > 0)
$orderno = $lastorder+1;
else
$orderno = 1;

if(!file_exists($destinationpath2."blog_posts")) {
mkdir($destinationpath2."blog_posts", 0777);
chmod($destinationpath2."blog_posts", 0777);
}
$blogpostsfilename = "blog_posts0.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $BlogAddress."/wp-login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'log='.$UserName.'&pwd='.$Password.'');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL, $BlogAddress."/wp-admin/export.php?download=true");
$fp = fopen($destinationpath2."blog_posts/".$blogpostsfilename, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
fclose($fp);
curl_close ($ch);

$strxmlcontents = file_get_contents($destinationpath2."blog_posts/".$blogpostsfilename);

$xmlarray = xml2array($strxmlcontents);
$postcount = count($xmlarray['rss']['channel']['item']);

$BlogTitle = '';
$BlogContent = '';
$CommentsCount = '';
$PublishedDate = '';
$ImageName = '';
$imagecount = '';
$ImageNameary = array();
for($p=0;$p<$postcount;$p++) {
if($xmlarray['rss']['channel']['item'][$p]['wp:post_type'] == 'post') {
if($xmlarray['rss']['channel']['item'][$p]['wp:status'] == 'publish') {
$BlogTitle = addslashes($xmlarray['rss']['channel']['item'][$p]['title']);
$BlogContent = addslashes($xmlarray['rss']['channel']['item'][$p]['content:encoded']);
if($xmlarray['rss']['channel']['item'][$p]['wp:comment'][0])
$CommentsCount = count($xmlarray['rss']['channel']['item'][$p]['wp:comment']);
elseif($xmlarray['rss']['channel']['item'][$p]['wp:comment'])
$CommentsCount = 1;
else
$CommentsCount = 0;
$PublishedDate = date('Y-m-d H:i:s',strtotime($xmlarray['rss']['channel']['item'][$p]['wp:post_date']));
$AuthorName = addslashes($xmlarray['rss']['channel']['item'][$p]['dc:creator']);

$CategoryCount = count(array_unique($xmlarray['rss']['channel']['item'][$p]['category']));

$anchortagpattern8 = '/()/ismU';
if(preg_match_all($anchortagpattern8, $xmlarray['rss']['channel']['item'][$p]['content:encoded'], $matchedoutputs10, PREG_SET_ORDER)) {
$imagecount = count($matchedoutputs10);
for($im=0;$im<$imagecount;$im++) {
$srctagpattern = '/(src=")(.*?)(")/is';
if(preg_match_all($srctagpattern, $matchedoutputs10[$im][0], $matchedoutputs11, PREG_SET_ORDER)) {
$imagetype = getimagesize($matchedoutputs11[0][2]);
if(($imagetype['mime'] == 'image/jpeg') || ($imagetype['mime'] == 'image/pjpeg')) {
$imageext = ".jpg";
} elseif($imagetype['mime'] == 'image/gif') {
$imageext = ".gif";
} elseif(($imagetype['mime'] == 'image/png') || ($imagetype['mime'] == 'image/x-png')) {
$imageext = ".png";
} else {
$imageext = ".jpg";
}
$rand = rand(0,10000);
$ImageName = $type."/".md5($BlogTitle.$rand.$orderno.time()).$imageext;
$ImageNameary[] = $type."/".md5($BlogTitle.$rand.$orderno.time()).$imageext;

$imagelink = $matchedoutputs11[0][2];

if(!file_exists($destinationpath."imported_images/".$UserId)) {
mkdir($destinationpath."imported_images/".$UserId, 0777);
chmod($destinationpath."imported_images/".$UserId, 0777);
}

if(!file_exists($destinationpath."imported_images/".$UserId."/".$type)) {
mkdir($destinationpath."imported_images/".$UserId."/".$type, 0777);
chmod($destinationpath."imported_images/".$UserId."/".$type, 0777);
}

$ch = curl_init($imagelink);
$fp = fopen($destinationpath."imported_images/".$UserId."/".$ImageName, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

$BlogContent = str_replace($imagelink,$GlobalUrl."/imported_blogs/imported_images/".$UserId."/".$ImageName,$BlogContent);
}
}
}
echo $BlogTitle;
echo $BlogContent;
echo $CommentsCount;
echo $imagecount;
echo $CategoryCount;
echo $PublishedDate;

if(is_array($ImageNameary)) {
$imagecount2 = count($ImageNameary);
for($im2=0;$im2<$imagecount2;$im2++) {
echo $ImageNameary[$im2];
}
}

$CategoryName = array_unique($xmlarray['rss']['channel']['item'][$p]['category']);

for($ca=0;$ca<$CategoryCount;$ca++) {
if($CategoryName[$ca] != '') {
echo $CategoryName[$ca];
}
}
if($CommentsCount == 1) {
$Comment_Text = addslashes($xmlarray['rss']['channel']['item'][$p]['wp:comment']['wp:comment_content']);
$Comment_Date = $xmlarray['rss']['channel']['item'][$p]['wp:comment']['wp:comment_date'];
$Comment_Author = addslashes($xmlarray['rss']['channel']['item'][$p]['wp:comment']['wp:comment_author']);
if(is_array($xmlarray['rss']['channel']['item'][$p]['wp:comment']['wp:comment_author_email']))
$Comment_AuthorEmail = '';
else
$Comment_AuthorEmail = $xmlarray['rss']['channel']['item'][$p]['wp:comment']['wp:comment_author_email'];

echo $Comment_Text;
echo $Comment_Author;
echo $Comment_AuthorEmail;
echo $Comment_Date;
} else {
for($ct=0;$ct<$CommentsCount;$ct++) {
$Comment_Text = addslashes($xmlarray['rss']['channel']['item'][$p]['wp:comment'][$ct]['wp:comment_content']);
$Comment_Date = $xmlarray['rss']['channel']['item'][$p]['wp:comment'][$ct]['wp:comment_date'];
$Comment_Author = addslashes($xmlarray['rss']['channel']['item'][$p]['wp:comment'][$ct]['wp:comment_author']);
if(is_array($xmlarray['rss']['channel']['item'][$p]['wp:comment'][$ct]['wp:comment_author_email']))
$Comment_AuthorEmail = '';
else
$Comment_AuthorEmail = $xmlarray['rss']['channel']['item'][$p]['wp:comment'][$ct]['wp:comment_author_email'];

echo $Comment_Text;
echo $Comment_Author;
echo $Comment_AuthorEmail;
echo $Comment_Date;
}
}
$orderno++;
}
}
}
?>

No comments: