<?php
/*
Plugin Name: Make Static
Version: 1.0
Plugin URI: http://www.theonering.net/staff/corvar/software/wordpress.html#makestatic
Description: Make a static copy of a dynamic page
Author: William "Corvar" Thomas
Author URI: http://www.theonering.net/

Installation: The two variables (baseurl and basedir) will need to be set for your scenario.
Only supports the default permalink format of yyyy/mm/dd/post-name

Usage:
Create a custom field for a post with a key of "static" and a value of "on"
When the post is saved, it will create a static version.

Rewrite Rules: To seemlessly use this plugin, additional
RewriteRules/Conds will be needed.  These should come between the
RewriteBase and first RewriteRule supplied by WordPress's permalink.
They should be of the form: 
RewriteCond /usr/share/%{REQUEST_URI}.html -f
RewriteRule ^archives/(.*)   /wordpress/archives/$1 [L]
In that example, REQUEST_URI would be of the form
'/wordpress/archives/2004/9/21/the-name-of-post', wordpress is
installed in '/usr/share/wordpress' 

Requires: curl
*/

function ms_mkdir_p($target) {
  if(
file_exists($target) || is_dir($target))
    return 
0;
  if(
substr($target01) != '/')
    
$target "/$target";
  if(
eregi('\.\.'$target) || eregi("[^a-zA-Z0-9\._\-\/]"$target))
    return 
0;
  if(
mkdir($target)){
    return 
1;
  }
  if(
ms_mkdir_p(substr($target0, (strrpos($target'/')))) == 1){
    if(
ms_mkdir_p($target) == 1)
      return 
1;
    else
      return 
0;
  }
  else{
    return 
0;
  } 
}

function 
make_static($id) {
  global 
$wpdb$tableposts$tablepostmeta;
  
$baseurl 'http://fqdn.com/wordpress/archives';
  
$basedir '/usr/share/wordpress/archives';
  
  if ( 
$metarow $wpdb->get_row("SELECT post_id,meta_key,meta_value 
                                        FROM $tablepostmeta 
                                        WHERE post_id = $id
                                "
) ) {
    
    
// Change from flat structure to hierarchical:
    
$post_meta_cache = array();
    
$mpid $metarow->post_id;
    
$mkey $metarow->meta_key;
    
$mval $metarow->meta_value;

    
// Force subkeys to be array type:
    
if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
      
$post_meta_cache[$mpid] = array();
    if (!isset(
$post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
      
$post_meta_cache[$mpid]["$mkey"] = array();
    
    
// Add a value to the current pid/key:
    
$post_meta_cache[$mpid][$mkey][] = $mval;
  }
  
  
  
$posttmp $wpdb->get_row("SELECT post_date,post_name FROM $tableposts WHERE ID=$id;");
  
$jj mysql2date('d'$posttmp->post_date);
  
$mm mysql2date('m'$posttmp->post_date);
  
$aa mysql2date('Y'$posttmp->post_date);
  
$name $posttmp->post_name;

  if(
eregi('\.\.'$name) || eregi("[^a-zA-Z0-9\._\-]"$name)) return 0;

  
  if(
is_file($basedir.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name.'.html')) {
    
unlink($basedir.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name.'.html');
  }
  
  if(
is_file($basedir.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name.'.html.gz')) {
    
unlink($basedir.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name.'.html.gz');
  }
  
  if(
$post_meta_cache[$id]['static'][0] == || 
     
$post_meta_cache[$id]['static'][0] == '1' ||
     
$post_meta_cache[$id]['static'][0] == 'On' ||
     
$post_meta_cache[$id]['static'][0] == 'on' ||
     
$post_meta_cache[$id]['static'][0] == 'true') {

    
sleep(1);
    
ms_mkdir_p($basedir.'/'.$aa.'/'.$mm.'/'.$jj);
       
    
$ch curl_init($baseurl.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name);
    
unlink('/tmp/'.$name.'.html');
    
$fp fopen('/tmp/'.$name.'.html'"w");

    
curl_setopt($chCURLOPT_FILE$fp);
    
curl_setopt($chCURLOPT_HEADER0);

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

    
copy('/tmp/'.$name.'.html',$basedir.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name.'.html');
    
system('gzip /tmp/'.$name.'.html');
    
copy('/tmp/'.$name.'.html.gz',$basedir.'/'.$aa.'/'.$mm.'/'.$jj.'/'.$name.'.html.gz');
    
unlink('/tmp/'.$name.'.html');
    
unlink('/tmp/'.$name.'.html.gz');

  }

  return 
$id;
}
#add_action('edit_post', 'make_static',6);
add_action('publish_post''make_static',6);

?>