<?php
    
/**
    * Wrapper for file_get_contents that performs caching
    *
    * @param string $uri URI of the resource to fetch
    * @param boolean $cached If true, serves cached result and stores if not cached.  If false, deletes from cache and serves direct
    * @return string Contents of requested URI, served from cache if exists
    **/
    
function get($uri,$cached=true)
    {
        
$md5 '/tmp/'.md5($uri);
        if (
is_file($md5))
            if (
$cached)
                return 
file_get_contents($md5);
            else
                
unlink($md5);
        
$content file_get_contents($uri);
        if (
$cached)
            
file_put_contents($md5,$content);
            
        return 
$content;
    }
    
    
/**
    * Returns an array of BDMT tags found at a particular URL
    *
    * @param string $url URL of the page to fetch
    * @return array Array of arrays of tags, e.g. ['BlogComment'][0] => 'blah', ['BlogComment'][1] => 'foo'
    **/
    
function getContentFromBlog($url)
    {
        
$content get($url);
        if (
preg_match_all('/<!-- BDMT:(.*?) -->(.*?)<!-- \/BDMT:\\1 -->/s',$content,$bits,PREG_SET_ORDER))
        {
            foreach (
$bits as $bit)
            {
                
$post[$bit[1]][] = $bit[2];
            }
            return 
$post;
        }
        else
        {
            return array();
        }
    }
    
    if (
$_POST['go'])
    {
        
// Get the content from the main page
        
$blog getContentFromBlog($_POST['go']);
        
        
// If there are archive links on the page ...
        
if (count($blog['BlogArchiveName'])>0)
        {
            
// Go through each of the archive links
            
foreach ($blog['BlogArchiveName'] as $url)
            {
                
// The BlogArchiveName tag is the entire <A> tag.  Grab the URL from it
                
if (preg_match('/<a href="(.*?)"/',$url,$result))
                {
                    
$url $result[1];
                    
$month getContentFromBlog($url); // Get the content for the month for this tag
                    
                    // For each month, get all of the Permalink URL's to the invidual blog posts.
                    // This is because the comments for each post are visible on the individual
                    // posts, but not on the monthly view.
                    
foreach ($month['BlogItemPermalinkUrl'] as $itemURL)
                    {
                        if (
preg_match('/<a href="(.*?)"/',$itemURL,$result))
                        {
                            
// $result[1] is the URL to the individual post.  Get the content
                            // from the URL and store it for later pumping into Drupal.
                            // (this is the URL that has the comments)
                            
$posts[] = getContentFromBlog($result[1]);
                        }
                    }
                }
            }
                    
            
// Now that we have all of the posts, pump them into Drupal.
            
foreach ($posts as $post)
            {
                if (
preg_match('/<!-- BDMT:BlogItemDateTime -->(.*?)<!-- \/BDMT:BlogItemDateTime -->/',$post['BlogItemPermalinkUrl'][0],$bit))
                {
                    
$post['BlogItemDateTime'][0] = $bit[1];
                }
                
$form_state = array();
                
module_load_include('inc''node''node.pages'); 
                
$node = array('type' => 'story'); 
                
$form_state['values']['title'] = (empty($post['BlogItemTitle'][0])) ? $post['BlogDateHeaderDate'][0] : $post['BlogItemTitle'][0]; 
                
$form_state['values']['body'] = $post['BlogItemBody'][0];
                
$form_state['values']['format'] = 2// Full HTML
                
$form_state['values']['name'] = 'admin'
                
$form_state['values']['date'] = date('Y-m-d H:i:s',strtotime($post['BlogDateHeaderDate'][0].' '.$post['BlogItemDateTime'][0]));
                
$form_state['values']['op'] = t('Save'); 
                
drupal_execute('story_node_form'$form_state, (object)$node);
            }
        }
        else
        {
            echo 
"<h3>The URL at ".$_POST['go']." didn't seem to be tagged";
        }
    }
    else
    {
        echo 
'<form method="POST">URL to tagged blog: <input type="text" name="go" size="40" /><input type="submit" /></form>';
    }
?>