<?php require('wp-config.php'); $single 1$siteurl get_settings('siteurl'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head profile="http://gmpg.org/xfn/1">
    <title><?php bloginfo('name'); ?><?php wp_title(); ?></title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo('charset'); ?>" />
    <meta name="generator" content="WordPress <?php bloginfo('version'); ?>" /> <!-- leave this for stats -->

    <style type="text/css" media="screen">
        @import url( <?php echo get_settings('siteurl'); ?>/wp-layout.css );
    </style>
    
    <link rel="stylesheet" type="text/css" media="print" href="<?php echo get_settings('siteurl'); ?>/print.css" />
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo('rss2_url'); ?>" />
    <link rel="alternate" type="text/xml" title="RSS .92" href="<?php bloginfo('rss_url'); ?>" />
    <link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="<?php bloginfo('atom_url'); ?>" />
    
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    <?php wp_get_archives('type=monthly&format=link'); ?>
    <?php //comments_popup_script(); // off by default ?>
    <?php wp_head(); ?>
</head>

<body>
<div id="rap">
<h1 id="header"><a href="<?php bloginfo('url'); ?>"><?php bloginfo('name'); ?></a></h1>


<div id="content">

<?php
$posts_per_page 
'-1';

$defaultorderby 'post_date';
$defaultorder 'DESC';

define('NL'"\n");

function 
show_year_select() {
    global 
$wpdb$tableposts$m;
    
$m substr($m,0,4);
    
$years $wpdb->get_col("SELECT DISTINCT YEAR(post_date) as year FROM $tableposts ORDER BY year ASC");
    
$output .= '<option value="">All Years</option>'.NL;
    foreach (
$years as $year) {
        
$output .= '<option value="'.$year.'"';
        if (
$year == $m) {
            
$output .= ' selected="selected"';
        }
        
$output .= '>'.$year.'</option>';
    }
    
$output  '<select name="m">'.NL.$output.'</select>'.NL;
    echo 
$output;
}

function 
show_author_select() {
    global 
$wpdb$tableusers$author;
    
$users $wpdb->get_results("SELECT * FROM $tableusers WHERE user_level > 0"ARRAY_A);
    
$output .= '<option value="">All Authors</option>'.NL;
    foreach (
$users as $user) {
        
$output .= '<option value="'.$user['ID'].'"';
        if (
$user['ID'] == $author) {
            
$output .= 'selected="selected"';
        }
        
$output .= '>'.$user['user_nickname'].'</option>'.NL;
    }
    
$output '<select name="author">'.NL.$output.'</select>'.NL;
    echo 
$output;
}

function 
show_orderby_select() {
    global 
$orderby;
    
$orderby explode(' '$orderby);
    
$orderby $orderby[0];
    if (
$orderby == 'date') {
       
$output .= '<option value="date" selected="selected">Date</option>'.NL;
    } else {
       
$output .= '<option value="date">Date</option>'.NL;
    }
    if (
$orderby == 'title') {
       
$output .= '<option value="title" selected="selected">Title</option>'.NL;
    } else {
       
$output .= '<option value="title">Title</option>'.NL;
    }
    if (
$orderby == 'category') {
       
$output .= '<option value="category" selected="selected">Category</option>'.NL;
    } else {
       
$output .= '<option value="category">Category</option>'.NL;
    }
    
$output '<select name="orderby" onchange="Choose(this)">'.NL.$output.'</select>'.NL;
    echo 
$output;
}

function 
show_order_select() {
    global 
$order;
    if (
$order == 'ASC') {
       
$output .= '<option value="ASC" selected="selected">Ascending</option>'.NL;
    } else {
       
$output .= '<option value="ASC">Ascending</option>'.NL;
    }
    if (
$order == 'DESC') {
       
$output .= '<option value="DESC" selected="selected">Descending</option>'.NL;
    } else {
       
$output .= '<option value="DESC">Descending</option>'.NL;
   }
   
$output '<select name="order" id="asc_desc">'.NL.$output.'</select>'.NL;
   echo 
$output;
}

function 
archive_header($before=''$after='') {
    global 
$post$orderby$month$previous$siteurl$blogfilename$archiveheadstart$archiveheadend$category_name;
    
$orderby explode(' '$orderby);
    
$orderby $orderby[0];
    if (
'date' == $orderby || empty($orderby)) {
        
$thismonth mysql2date('m'$post->post_date);
        
$thisyear mysql2date('Y'$post->post_date);
        
$thisdate $thisyear.$thismonth;
        if (
$thisdate != $previous) {
            
$thismonth mysql2date('m'$post->post_date);
            
$output .= '<strong><br/><br/><a href="'.$siteurl.'/'.$blogfilename.'?m='.$thisdate.'">'.$month[$thismonth].' '.$thisyear.'</a></strong>';
        }
        
$previous $thisdate;
    } elseif (
'title' == $orderby) {
        
preg_match('/[a-z0-9]{1}/i'$post->post_title$match);
        
$thisletter ucfirst($match[0]);
        if (
$thisletter != $previous) {
            
$output .= "<br/><br/>".$thisletter;
        }
        
$previous $thisletter;
    } elseif (
'category' == $orderby) {
        
$thiscategory $category_name;
        if (
$thiscategory != $previous) {
            
$output .= '<br/><br/><strong><a href="'.$siteurl.'/'.$blogfilename.'?cat='.$thiscategory.'">'.get_catname($thiscategory).'</a></strong>';
        }
        
$previous $thiscategory;
    }
    if (!empty(
$output)) {
        
$output $before.$output.$after.NL;
        echo 
$output;
    }
}

function 
archive_date($format='Y-m-d H:i:s') {
    global 
$post;
    echo 
mysql2date($format$post->post_date);
}

?>

<script language="javascript" type="text/javascript">
function Choose(whichSort) {
    if (whichSort.selectedIndex == 2) {
        document.getElementById('asc_desc').selectedIndex = 1;
    } else {
        document.getElementById('asc_desc').selectedIndex = 0;
    }
}
</script>

<?php
//Make sure categories get parsed out, they are deprecated in wp-blog-header.php
if ($_POST["orderby"] == 'category') {
    global 
$author$m;
    
$orderby 'category';
    if (
$_POST["order"] == ''$order "DESC";
    else 
$order $_POST["order"];
    
$year '' intval($_POST["m"]);
    
$m $year;
    
$author ''.intval($_POST["author"]);
    if (empty(
$author)) {
        
$whichauthor='';
    } else {
        
$author ''.urldecode($author).'';
        
$author addslashes_gpc($author);
        if (
stristr($author'-')) {
            
$eq '!=';
            
$andor 'AND';
            
$author explode('-'$author);
            
$author ''.intval($author[1]);
        } else {
            
$eq '=';
            
$andor 'OR';
        }
        
$author_array explode(' '$author);
        
$whichauthor .= ' AND (post_author '.$eq.' '.intval($author_array[0]);
        for (
$i 1$i < (count($author_array)); $i $i 1) {
            
$whichauthor .= ' '.$andor.' post_author '.$eq.' '.intval($author_array[$i]);
        }
        
$whichauthor .= ')';
    }

    
// Author stuff for nice URIs

    
if ('' != $author_name) {
        if (
stristr($author_name,'/')) {
            
$author_name explode('/',$author_name);
            if (
$author_name[count($author_name)-1]) {
            
$author_name $author_name[count($author_name)-1];#no trailing slash
            
} else {
            
$author_name $author_name[count($author_name)-2];#there was a trailling slash
            
}
        }
        
$author_name preg_replace('|[^a-z0-9-]|'''strtolower($author_name));
        
$author $wpdb->get_var("SELECT ID FROM $tableusers WHERE user_nicename='".$author_name."'");
        
$whichauthor .= ' AND (post_author = '.intval($author).')';
    }
    if (!empty(
$year)) $where .= ' AND YEAR(post_date)=' $year;
    if (!empty(
$whichauthor)) $where .= $whichauthor;
    
?>

    Sort By:
    <form action="<?php getenv('PHP_SELF'?>" method="post">
    <?php show_orderby_select() ?>
    <?php show_order_select() ?>
    <?php show_year_select() ?>
    <?php show_author_select() ?>
    <input type="submit" name="submit" value="sort" />
    </form>
    <?php
    $dogs 
$wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1 ORDER BY cat_name $order");
    foreach (
$dogs as $catt) {
        
$categories $wpdb->get_results("SELECT * FROM $tablepost2cat WHERE category_id = $catt->cat_ID");
        if (
$categories) {
            foreach (
$categories as $post2category) {
                
$posts $wpdb->get_results("SELECT * FROM $tableposts WHERE $post2category->post_id = ID".$where);
                
//$category_realname = $wpdb->get_row("SELECT cat_name FROM $tablecategories WHERE cat_ID = $post2category->category_id");
                //print_r($category_realname);
                
global $category_name;
                
$category_name $post2category->category_id;
                if (
$posts) {
                    foreach (
$posts as $post) {
                        
start_wp();
                        
archive_header('<h3>''</h3>');
                        
archive_date('m-d-Y h:iA'?>: <a href="<?php echo get_permalink($post->ID?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a><br />
                        <?php

                    
}
                }
            }
        }
    }
}
else {
    require_once (
'wp-blog-header.php');
    
// echo $request;
    
?>

    Sort By:
    <form action="<?php getenv('PHP_SELF'?>" method="post">
    <?php show_orderby_select() ?>
    <?php show_order_select() ?>
    <?php show_year_select() ?>
    <?php show_author_select() ?>
    <input type="submit" name="submit" value="sort" />
    </form>

    <?php if ($posts) { foreach ($posts as $post) { start_wp(); ?>
    <?php //print_r($post); ?>
    <?php archive_header('<h3>''</h3>'?>
    <?php archive_date('m-d-Y h:iA'?>: <a href="<?php echo get_permalink($post->ID?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a><br />
    <?php } }
    }
?>
</div>

</div>

<p class="credit"><!--<?php echo $wpdb->querycount?> queries.--> <?php timer_stop(1); ?> || <cite>Powered by <a href="http://wordpress.org" title="Powered by WordPress, state-of-the-art semantic personal publishing platform"><strong>WordPress</strong></a></cite></p>
</body>
</html>