예제 #1
0
function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
    $result = false;
    $contents = @file_get_contents($url);
    // Check if we need to go somewhere else
    if (isset($contents) && is_string($contents)) {
        preg_match_all('/<[\\s]*meta[\\s]*http-equiv="?REFRESH"?' . '[\\s]*content="?[0-9]*;[\\s]*URL[\\s]*=[\\s]*([^>"]*)"?' . '[\\s]*[\\/]?[\\s]*>/si', $contents, $match);
        if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1) {
            if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections) {
                return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
            }
            $result = false;
        } else {
            $result = $contents;
        }
    }
    return $contents;
}
function getUrlData($url,$option) {
    $result = false;   
    $contents  = getUrlContents($url);
	
	
    if (isset($contents) && is_string($contents))    {
        $title = null;
        $metaTags = null;    
		$links = null;   
       
	    preg_match('/<title>([^>]*)<\/title>/si', $contents, $match ); 
		if (isset($match) && is_array($match) && count($match) > 0) { $title = strip_tags($match[1]); 
		}
		
        preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);			  
		if (isset($match) && is_array($match) && count($match) == 3) {
            $originals = $match[0];
            $names = $match[1];
            $values = $match[2];
            if (count($originals) == count($names) && count($names) == count($values)) {
                $metaTags = array();              
                for ($i=0, $limiti=count($names); $i < $limiti; $i++) {                  
				$metaTags[$names[$i]] = array (
                        'html' => htmlentities($originals[$i]),
                        'value' => $values[$i]
                    );
                }
            }
        } 
		
					  		
        $result = array (
            'title' => $title,
            'metaTags' => $metaTags,		
        );
    
	
# Page Title
if($result['title'] != "") { $title = $result['title']; }
elseif($result['metaTags']['title']['value'] != "") { $title = $result['metaTags']['title']['value']; }
elseif($result['metaTags']['Title']['value'] != "") { $title = $result['metaTags']['Title']['value']; }
elseif($result['metaTags']['TITLE']['value'] != "") { $title = $result['metaTags']['TITLE']['value']; }
elseif($result['metaTags']['DC.Title']['value'] != "") { $title = $result['metaTags']['DC.Title']['value']; }
elseif($result['metaTags']['DC.title']['value'] != "") { $title = $result['metaTags']['DC.title']['value']; }
elseif($result['metaTags']['DC.TITLE']['value'] != "") { $title = $result['metaTags']['DC.TITLE']['value']; }
elseif($result['metaTags']['Page-topic']['value'] != "") { $title = $result['metaTags']['Page-topic']['value']; }
elseif($result['metaTags']['page-topic']['value'] != "") { $title = $result['metaTags']['page-topic']['value']; }
elseif($result['metaTags']['PAGE-TOPIC']['value'] != "") { $title = $result['metaTags']['PAGE-TOPIC']['value']; }
if( $title =="" ) { $title2=$site; } else { $title2=$title; }

# Keywords
if($result['metaTags']['DC.keywords']['value'] != "") { $keywords = $result['metaTags']['DC.keywords']['value']; }
elseif($result['metaTags']['keywords']['value'] != "") { $keywords = $result['metaTags']['keywords']['value']; }
elseif($result['metaTags']['Keywords']['value'] != "") { $keywords = $result['metaTags']['Keywords']['value']; }
elseif($result['metaTags']['KEYWORDS']['value'] != "") { $keywords = $result['metaTags']['KEYWORDS']['value']; }
elseif($result['metaTags']['DC.KEYWORDS']['value'] != "") { $keywords = $result['metaTags']['DC.KEYWORDS']['value']; }
elseif($result['metaTags']['DC.Keywords']['value'] != "") { $keywords = $result['metaTags']['DC.Keywords']['value']; }
elseif($result['metaTags']['DC.keywords']['value'] != "") { $keywords = $result['metaTags']['DC.keywords']['value']; }

# Description
if($result['metaTags']['description']['value'] != "") { $description = $result['metaTags']['description']['value']; }
elseif($result['metaTags']['DC.description']['value'] != "") { $description = $result['metaTags']['DC.description']['value']; }
elseif($result['metaTags']['DC.Description']['value'] != "") { $description = $result['metaTags']['DC.Description']['value']; }
elseif($result['metaTags']['DESCRIPTION']['value'] != "") { $description = $result['metaTags']['DESCRIPTION']['value']; }
elseif($result['metaTags']['DC.DESCRIPTION']['value'] != "") { $description = $result['metaTags']['DC.DESCRIPTION']['value']; }
elseif($result['metaTags']['Description']['value'] != "") { $description = $result['metaTags']['Description']['value']; }

# Generator
if($result['metaTags']['generator']['value'] != "") { $generator = $result['metaTags']['generator']['value']; }
elseif($result['metaTags']['Generator']['value'] != "") { $generator = $result['metaTags']['Generator']['value']; }
elseif($result['metaTags']['GENERATOR']['value'] != "") { $generator = $result['metaTags']['GENERATOR']['value']; }
elseif($result['metaTags']['DC.generator']['value'] != "") { $generator = $result['metaTags']['DC.generator']['value']; }
elseif($result['metaTags']['DC.Generator']['value'] != "") { $generator = $result['metaTags']['DC.Generator']['value']; }
elseif($result['metaTags']['DC.GENERATOR']['value'] != "") { $generator = $result['metaTags']['DC.GENERATOR']['value']; }
	
}
	
if($option=="title") { return $title; }
if($option=="keywords") { return $keywords; }
if($option=="description") { return $description; }
if($option=="generator") { return $generator; }
}
function import()
{
    // Check if info was actually submitted
    if (empty($GLOBALS['tumblr_email']) || empty($GLOBALS['tumblr_password'])) {
        echo "Tumblr email and password required.<br /><br />\n";
        return;
    }
    if (empty($GLOBALS['feed_url'])) {
        echo "Blogger Feed URL required.<br /><br />\n";
        return;
    }
    echo "Importing.  This could take a few minutes.<br /><br />Please stand by...<br /><br />\n";
    try {
        // Download the posts
        echo "Downloading feed.<br />\n";
        $url = $GLOBALS['feed_url'];
        // Convert Blogger URL to Blogger Posts Feed URL.
        if ($GLOBALS['autocorrect'] && 1 == preg_match("#^http://[^/\\.]+\\.blogspot\\.com/\$#", $url)) {
            $url .= 'feeds/posts/default';
        }
        // Blogger doesn't return all posts by default,
        // so we need to specifically ask for them all.
        if (strpos($url, 'max-results=') === FALSE) {
            $url .= strpos($url, '?') === FALSE ? '?' : '&';
            $url .= 'max-results=' . pow(2, 30);
        }
        $xml = getUrlContents($url);
        if (empty($xml)) {
            throw new Exception("Unable to download feed contents; check that the URL of your feed is correct and publicly accessible.  You should be able to see your posts here: {$url}");
        }
        // Parse it
        $blog = getBlogFromAtomFeed($xml, $url);
        echo 'Found: ' . dispValidate($blog['title']) . "<br />\n";
        // Create a new post for each old post
        $n = count($blog['entries']);
        for ($i = count($blog['entries']) - 1; $i >= 0; $i--) {
            $entry = $blog['entries'][$i];
            echo 'Importing: ' . dispValidate($entry['title']) . "<br />\n";
            createTextPost($entry, 0);
        }
        echo "<br />\nDone.  Imported {$n} " . ($n == 1 ? 'post' : 'posts') . ".<br />\n";
    } catch (HtmlSafeException $e) {
        echo "Importing failed: " . $e->getMessage() . "<br /><br />\n";
        echo "Having trouble?  Your question may already be answered <a href=\"http://plpatterns.com/post/37782942/moving-from-blogger-to-tumblr#disqus_thread\" target=\"_blank\">here</a>.<br /><br />\n";
    } catch (Exception $e) {
        echo "Importing failed: " . dispValidate($e->getMessage(), FALSE) . "<br /><br />\n";
        echo "Having trouble?  Your question may already be answered <a href=\"http://plpatterns.com/post/37782942/moving-from-blogger-to-tumblr#disqus_thread\" target=\"_blank\">here</a>.<br /><br />\n";
    }
}