[home] [login]





<?php


// page_engine.php

// this is passed:
// $page_modifier
// $page_modifier_value
// $query_string
// $open_html
// $close_html



if ($directory['included_style'] == "alphabetical") {
    $order_by = "alphabetize";
}
if ($directory['included_style'] == "numeric") {
    $order_by = "numeric_order";
}
if ($directory['included_style'] == "chronological") {
    $order_by = "datetime";
}
if ($directory['direction'] == '1') {
    $the_direction = "DESC";
}
else {
    $the_direction = "ASC";
}

if ($query_string == 'order=reverse') {
    $order = 'reverse';
    if ($directory['direction'] == '0') {
        $directory['direction'] = '1';
        $the_direction = "DESC";
        
    }
    else {
        $directory['direction'] == '0';
        $the_direction = "ASC";
    }
    unset($query_string);
}


if($homepage != true) {
    $querydid = "did = '" . $directory['did'] . "'";
}
else {
    $publicdid = array('2','10','12','16','490','666','671','695','763');
//    $querydid = "(did='2' or did='10' or did='12' or did='16' or did='490' or did='666' or did='671' or did='695' or did='763'";
    if($user['uid'] > 2) {
        // not a guest, so get private pages they have on their home page
        $subquery = "select did from subscription where uid='" . $user['uid'] . "' and home_page > 0";
        $subres = mysqli_query($GLOBALS['dblink'],$subquery);
        while($sub = mysqli_fetch_assoc($subres)) {
            if(in_array($sub['did'],$publicdid)) {
                continue;
            }
            $publicdid[] = $sub['did'];
        }
    }
    $querydid = '(';
    foreach($publicdid as $did) {
        $querydid .= 'did=\'' . $did . '\' or ';
    }
    $querydid = substr($querydid,0,-4) . ')';
    $the_direction = "DESC";
    $order_by = "datetime";
    $directory['window'] = 25;
    $directory['post_foot'] = 1;
    $directory['post_comment'] = 1;
    $directory['included_style'] = 'chronological';
    $directory['direction'] = 1;
    
    
}




if ($page_modifier == "" and $query_string == "") {

    // just spit out the regular page view
    if ($directory['window'] == '0') {
        // infinite window
        $postquery = "select did, bid, name, uid, datetime, real_datetime, post, num_comments, num_references, alphabetize, numeric_order from blog where " . $querydid . " order by $order_by $the_direction";
    }
    else {
        if ($directory['included_style'] == "alphabetical" or $directory['included_style'] == "numeric") {
            $postquery = "select did, bid, name, uid, datetime, real_datetime, post, num_comments, num_references, alphabetize, numeric_order from blog where " . $querydid . " order by $order_by $the_direction, datetime $the_direction limit " . $directory['window'];
        }
        else {
            $postquery = "select did, bid, name, uid, datetime, real_datetime, post, num_comments, num_references, alphabetize, numeric_order from blog where " . $querydid . " order by $order_by $the_direction limit " . $directory['window'];
        }
    }


}
elseif ($query_string != "") {

    // is it a search or is it a specific post or new?
    $search_test = explode("search=", $query_string);
    $order_test = explode("order=", $query_string);
    if ($search_test[1] != "") {
        $search = $search_test[1];
        $original_search_string = $search;
        $search = str_replace("+", " ", "$search");
        $string_length = strlen($search);
        for($i=0;$i<$string_length;$i++) {
                
            $char = substr($search, $i, 1);
            if ($char != " ") {
                $convert_char = strtoupper($char);
                if ($convert_char == $char) {
                    $convert_char = strtolower($char);
                }
                $ci_phrase = $ci_phrase . "[{$char}{$convert_char}]";
            }
            else {
                $ci_phrase = $ci_phrase . " ";
            }    
                
        }
        $this_is_a_search = "true";
        $postquery = "select did, bid, name, uid, datetime, real_datetime, post, num_comments, num_references from blog where " . $querydid . " and post rlike '.*${ci_phrase}.*' order by $order_by $the_direction LIMIT 50";
            
    }
    else {
        $specific_post = $query_string;
        if ($specific_post == "new" and $user['uid'] != '2') {
            $new_posts = "true";
            $postquery = "select blog.did, blog.bid, blog.name, blog.uid, blog.datetime, blog.real_datetime, blog.post, blog.num_comments, blog.num_references from blog where blog.did = '" . $directory['did'] . "' and blog.real_datetime > '" . $subscription['last_seen'] . "' order by  " . $order_by . " " . $the_direction;
        }
        elseif ($specific_post == "preview") {
            $preview = "true";
            $postquery = "select preview.prid, preview.name, preview.uid, preview.datetime, preview.post from preview where preview.did = '" . $directory['did'] . "' and (preview.uid = '" . $user['uid'] . "' or " . $user['uid'] . " = '" . $directory['uid'] . "' or " . $user['group_level'] . " >= '900')";
        }
        else {
            $postquery = "select did, bid, name, uid, datetime, real_datetime, post, num_comments, num_references from blog where " . $querydid . " and bid = '" . $specific_post . "'";
        }
    }
}
else {

    // there is a $page_modifier
    if ($directory['direction'] == '1') {
        if ($page_modifier == "pageback" or $page_modifier == "next") {
            // we want the next $window many posts with a smaller datetime, numeric_order, or alphabetize
            if ($directory['included_style'] == "chronological") {
                if ($directory['window'] != 0) {
                    $lastdatetime = mysqli_query($GLOBALS['dblink'],"select datetime, real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastdate = mysqli_fetch_assoc($lastdatetime);
                    $postquery = "select did, bid, name, uid, datetime, real_datetime, post, num_comments, num_references from blog where " . $querydid . " and datetime < '" . $lastdate['datetime'] . "' order by datetime DESC limit " . $directory['window'];
                }
            }
            elseif ($directory['included_style'] == "alphabetical") {
                if ($directory['window'] != 0) {
                    $lastalpha = mysqli_query($GLOBALS['dblink'],"select alphabetize,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastalphab = mysqli_fetch_assoc($lastalpha);
                    
                    // we have to grab every post where alphabetize is greater than $lastalphab[alphabetize] so we can make sure we
                    // don't repeat any bids. Otherwise there is no way to keep things straight in case there are similar blog.alphabetize
                    // or more likely similarly null blog.alphabetize
                    $everyresult = mysqli_query($GLOBALS['dblink'],"select bid,alphabetize,datetime,real_datetime from blog where did='" . $directory['did'] . "' and alphabetize >= '" . $lastalphab['alphabetize'] . "' order by alphabetize DESC, datetime DESC");
                    $everycount = mysqli_num_rows($everyresult);
                    if ($everycount > 0) {
                        while($every = mysqli_fetch_assoc($everyresult)) {
                            if ($every['alphabetize'] < $lastalphab['alphabetize']) {
                                continue;
                            }
                            if ($every['alphabetize'] == $lastalphab['alphabetize']) {
                                if ($every['datetime'] < $lastalphab['datetime']) {
                                    continue;
                                }
                            }
                            $exclude = $exclude . " and bid != '" . $every['bid'] . "'";
                        }
                    }
                    
                    
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references, alphabetize from blog where " . $querydid . " and alphabetize <= '" . $lastalphab['alphabetize'] . "' " . $exclude . " order by alphabetize DESC, datetime DESC limit " . $directory['window'];
                }
            }
            elseif ($directory['included_style'] == "numeric") {
                if ($directory['window'] != 0) {
                    $lastalpha = mysqli_query($GLOBALS['dblink'],"select numeric_order,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastalphab = mysqli_fetch_assoc($lastalpha);
                    
                    // we have to grab every post where numeric_order is greater than $lastalphab[numeric_order] so we can make sure we
                    // don't repeat any bids. Otherwise there is no way to keep things straight in case there are similar blog.numeric_order
                    // or more likely similarly null blog.numeric_order
                    $everyresult = mysqli_query($GLOBALS['dblink'],"select bid,numeric_order,datetime,real_datetime from blog where did='" . $directory['did'] . "' and numeric_order >= '" . $lastalphab['numeric_order'] . "' order by numeric_order DESC, datetime DESC");
                    $everycount = mysqli_num_rows($everyresult);
                    if ($everycount > 0) {
                        while($every = mysqli_fetch_assoc($everyresult)) {
                            if ($every['numeric_order'] < $lastalphab['numeric_order']) {
                                continue;
                            }
                            if ($every['numeric_order'] == $lastalphab['numeric_order']) {
                                if ($every['datetime'] < $lastalphab['datetime']) {
                                    continue;
                                }
                            }
                            $exclude = $exclude . " and bid != '" . $every['bid'] . "'";
                        }
                    }
                    
                    
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references, numeric_order from blog where " . $querydid . " and numeric_order <= '" . $lastalphab['numeric_order'] . "' " . $exclude . " order by numeric_order DESC, datetime DESC limit " . $directory['window'];
                }
            }        
        }
        elseif ($page_modifier == "pageforward" or $page_modifier == "previous") {
            // we want the next $window many posts with a larger datetime, numeric_order, or alphabetize
            if ($directory['included_style'] == "chronological") {
                if ($directory['window'] != 0) {
                    $lastdatetime = mysqli_query($GLOBALS['dblink'],"select datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastdate = mysqli_fetch_assoc($lastdatetime);
                    // now we need the other end of the datetime range to make the select ($lastdate is one end of the range)
                    $lq = "select datetime,real_datetime from blog where " . $querydid . " and datetime > '" . $lastdate['datetime'] . "' order by datetime limit " . $directory['window'];
                    $lowerquery = mysqli_query($GLOBALS['dblink'],$lq);
                    while($firstdateresult = mysqli_fetch_assoc($lowerquery)) {
                        $firstdate = $firstdateresult['datetime'];

                    }
                    // we should exit this while loop with the the lower datetime boundary in $firstdate
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references from blog where " . $querydid . " and datetime <= '" . $firstdate . "' and datetime > '" . $lastdate['datetime'] . "' order by datetime DESC limit " . $directory['window'];
                }
            }
            elseif ($directory['included_style'] == "alphabetical") {
                if ($directory['window'] != 0) {
                    while ($bids_gotten < $directory['window']) {
                        $last_query = "select bid,alphabetize,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'";
                        $last_result = mysqli_query($GLOBALS['dblink'],$last_query);
                        $last = mysqli_fetch_assoc($last_result);
                        
                        // are there other bids with same alphabetize (in the right direction)?
                        $other_same_check = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where did='" . $directory['did'] . "' and alphabetize='" . $last['alphabetize'] . "' and datetime > '" . $last['datetime'] . "'");
                        $other_same_count = mysqli_fetch_row($other_same_check);
                        
                        if ($other_same_count[0] > 0) {
                            // yes there are other bids with the same alphabetize
                            // Are there at least as many as window?
                            if ($other_same_count[0] >= $directory['window']) {
                                // so get that many ordered by datetime
                                $entire_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and alphabetize = '" . $last[alphabetize]. "' order by datetime");
                                while($entire = mysqli_fetch_assoc($entire_result) and $bids_gotten < $directory['window']) {
                                    if ($entire['bid'] == $last['bid'] or $got_there == "true") {
                                        $got_there = "true";
                                    }
                                    else {
                                        continue;
                                    }
                                    if ($entire['bid'] == $last['bid']) {
                                        continue;
                                    }
                                    $bids_to_get[] = $entire['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $entire['bid'];
                                }
                                
                            }
                            else {
                                // yes there are other bids with the same alphabetize
                                // but not enough to fill the page
                                
                                // so we have to put the ones that are here into $bids_to_get[]
                                // and make note of how many we get by incrementing $bids_gotten
                                // and then send ourselves back through the big while loop again with
                                // $page_modifier_value now set to the last $bid from the loop we're
                                // about to take (real clear, no?)
                                
                                $partial_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did']. "' and bid != '" . $last['bid'] . "' and alphabetize = '" . $last['alphabetize'] . "' and datetime > '" . $last['datetime'] . "' order by datetime");
                                while($partial = mysqli_fetch_assoc($partial_result)) {
                                    $bids_to_get[] = $partial['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $partial['bid'];
                                }
                                
                            }
                        }
                        else {
                            // no matches for this alphabetize
                            // so get the next alphabetize from this page (just the next one)
                            // add it to $bids_to_get[]
                            // increment $bids_gotten
                            // and send yourself back through the big while with $page_modifier_value = the bid you just got
                            // so we can check that one for duplicates
                            
                            $get_previous_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and alphabetize > '" . $last['alphabetize'] . "' order by alphabetize, datetime limit 1");
                            $get_previous = mysqli_fetch_assoc($get_previous_result);
                            $bids_gotten = $bids_gotten + 1;
                            $bids_to_get[] = $get_previous['bid'];
                            $page_modifier_value = $get_previous['bid'];
                        }
                    }
                    
                    
                    
                    // we have to build the query using $bids_to_get[]
                        
                    $postquery = "select * from blog where";
                    $to_get_count = count($bids_to_get);
                    for ($i=0;$i<$to_get_count;$i++) {
                        if ($i == 0) {
                            $postquery = $postquery . " bid = '$bids_to_get[$i]'";
                        }
                        else {
                            $postquery = $postquery . " or bid = '$bids_to_get[$i]'";
                        }
                    }
                    $postquery = $postquery . " order by alphabetize DESC, datetime DESC";
                
                }
            }
            elseif ($directory['included_style'] == "numeric") {
                if ($directory['window'] != 0) {
                    while ($bids_gotten < $directory['window']) {
                        $last_query = "select bid,numeric_order,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'";
                        $last_result = mysqli_query($GLOBALS['dblink'],$last_query);
                        $last = mysqli_fetch_assoc($last_result);
                        
                        // are there other bids with same numeric_order (in the right direction)?
                        $other_same_check = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where did='" . $directory['did'] . "' and numeric_order='" . $last['numeric_order'] . "' and datetime > '" . $last['datetime'] . "'");
                        $other_same_count = mysqli_fetch_row($other_same_check);
                        
                        if ($other_same_count[0] > 0) {
                            // yes there are other bids with the same numeric_order
                            // Are there at least as many as window?
                            if ($other_same_count[0] >= $directory['window']) {
                                // so get that many ordered by datetime
                                $entire_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and numeric_order = '" . $last['numeric_order'] . "' order by datetime");
                                while($entire = mysqli_fetch_assoc($entire_result) and $bids_gotten < $directory['window']) {
                                    if ($entire['bid'] == $last['bid'] or $got_there == "true") {
                                        $got_there = "true";
                                    }
                                    else {
                                        continue;
                                    }
                                    if ($entire['bid'] == $last['bid']) {
                                        continue;
                                    }
                                    $bids_to_get[] = $entire['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $entire['bid'];
                                }
                                
                            }
                            else {
                                // yes there are other bids with the same numeric_order
                                // but not enough to fill the page
                                
                                // so we have to put the ones that are here into $bids_to_get[]
                                // and make note of how many we get by incrementing $bids_gotten
                                // and then send ourselves back through the big while loop again with
                                // $page_modifier_value now set to the last $bid from the loop we're
                                // about to take (real clear, no?)
                                
                                $partial_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and bid != '" . $last['bid'] . "' and numeric_order = '" . $last['numeric_order'] . "' and datetime > '" . $last['datetime'] . "' order by datetime");
                                while($partial = mysqli_fetch_assoc($partial_result)) {
                                    $bids_to_get[] = $partial['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $partial['bid'];
                                }
                                
                            }
                        }
                        else {
                            // no matches for this numeric_order
                            // so get the next numeric_order from this page (just the next one)
                            // add it to $bids_to_get[]
                            // increment $bids_gotten
                            // and send yourself back through the big while with $page_modifier_value = the bid you just got
                            // so we can check that one for duplicates
                            
                            $get_previous_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and numeric_order > '" . $last['numeric_order'] . "' order by numeric_order, datetime limit 1");
                            $get_previous = mysqli_fetch_assoc($get_previous_result);
                            $bids_gotten = $bids_gotten + 1;
                            $bids_to_get[] = $get_previous['bid'];
                            $page_modifier_value = $get_previous['bid'];
                        }
                    }
                    
                    
                    
                    // we have to build the query using $bids_to_get[]
                        
                    $postquery = "select * from blog where";
                    $to_get_count = count($bids_to_get);
                    for ($i=0;$i<$to_get_count;$i++) {
                        if ($i == 0) {
                            $postquery = $postquery . " bid = '" . $bids_to_get[$i] . "'";
                        }
                        else {
                            $postquery = $postquery . " or bid = '" . $bids_to_get[$i] . "'";
                        }
                    }
                    $postquery = $postquery . " order by numeric_order DESC, datetime DESC";
                }
            }    
        
        }
        
    }
    else {
        // direction = 0
        if ($page_modifier == "next") {
            // we want the next $window many posts with larger datetime, alpha, or numeric_order
            if ($directory['included_style'] == "chronological") {
                if ($directory['window'] != 0) {
                    $lastdatetime = mysqli_query($GLOBALS['dblink'],"select datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastdate = mysqli_fetch_assoc($lastdatetime);
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references from blog where " . $querydid . " and datetime > '" . $lastdate['datetime'] . "' order by datetime limit " . $directory['window'];
                }
            }
            elseif ($directory['included_style'] == "alphabetical") {
                if ($directory['window'] != 0) {
                    $lastalpha = mysqli_query($GLOBALS['dblink'],"select bid,alphabetize,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastalphab = mysqli_fetch_assoc($lastalpha);
                    
                    // we have to grab every post where alphabetize is greater than $lastalphab[alphabetize] so we can make sure we
                    // don't repeat any bids. Otherwise there is no way to keep things straight in case there are similar blog.alphabetize
                    // or more likely similarly null blog.alphabetize
                    $everyresult = mysqli_query($GLOBALS['dblink'],"select bid,alphabetize,datetime,real_datetime from blog where did='" . $directory['did'] . "' and alphabetize <= '" . $lastalphab['alphabetize'] . "' order by alphabetize, datetime");
                    $everycount = mysqli_num_rows($everyresult);
                    if ($everycount > 0) {
                        while($every = mysqli_fetch_assoc($everyresult)) {
                            if ($every['alphabetize'] > $lastalphab['alphabetize']) {
                                continue;
                            }
                            if ($every['alphabetize'] == $lastalphab['alphabetize']) {
                                if ($every['datetime'] > $lastalphab['datetime']) {
                                    continue;
                                }
                            }
                            $exclude = $exclude . " and bid != '$every[bid]'";
                        }
                    }
                    
                    
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references, alphabetize from blog where " . $querydid . " and alphabetize >= '" . $lastalphab['alphabetize'] . "' $exclude order by alphabetize, datetime limit " . $directory['window'];
                
                }
            }
            elseif ($directory['included_style'] == "numeric") {
                if ($directory['window'] != 0) {
                    $lastalpha = mysqli_query($GLOBALS['dblink'],"select bid,numeric_order,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastalphab = mysqli_fetch_assoc($lastalpha);
                    
                    // we have to grab every post where numeric_order is greater than $lastalphab[numeric_order] so we can make sure we
                    // don't repeat any bids. Otherwise there is no way to keep things straight in case there are similar blog.numeric_order
                    // or more likely similarly null blog.numeric_order
                    $everyresult = mysqli_query($GLOBALS['dblink'],"select bid,numeric_order,datetime,real_datetime from blog where did='" . $directory['did'] . "' and numeric_order <= '" . $lastalphab['numeric_order'] . "' order by numeric_order, datetime");
                    $everycount = mysqli_num_rows($everyresult);
                    if ($everycount > 0) {
                        while($every = mysqli_fetch_assoc($everyresult)) {
                            if ($every['numeric_order'] > $lastalphab['numeric_order']) {
                                continue;
                            }
                            if ($every['numeric_order'] == $lastalphab['numeric_order']) {
                                if ($every['datetime'] > $lastalphab['datetime']) {
                                    continue;
                                }
                            }
                            $exclude = $exclude . " and bid != '$every[bid]'";
                        }
                    }
                    
                    
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references, numeric_order from blog where " . $querydid . " and numeric_order >= '" . $lastalphab['numeric_order'] . "' $exclude order by numeric_order, datetime limit " . $directory['window'];
                
                }
            
            }    
        }
        elseif ($page_modifier == "previous") {
            // we want the next $window many posts with a smaller datetime, numeric_order, or alphabetize
            if ($directory['included_style'] == "chronological") {
                if ($directory['window'] != 0) {
                    $lastdatetime = mysqli_query($GLOBALS['dblink'],"select datetime,real_datetime from blog where bid='" . $page_modifier_value . "'");
                    $lastdate = mysqli_fetch_assoc($lastdatetime); // that tells us the upper bound
                    // now we need the lower bound, or else we'll just get the very top of the page
                    $lowerquery = mysqli_query($GLOBALS['dblink'],"select datetime,real_datetime from blog where did='" . $directory['did'] . "' and datetime < '" . $lastdate['datetime'] . "' order by datetime DESC limit " . $directory['window']);
                    while($firstdateresult = mysqli_fetch_assoc($lowerquery)) {
                        $firstdate = $firstdateresult['datetime'];
                    }
                    // we should exit this while loop with the the lower datetime boundary in $firstdate
                    $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references from blog where " . $querydid . " and datetime >= '" . $firstdate . "' and datetime < '" . $lastdate['datetime'] . "' order by datetime";
                }
            }
            elseif ($directory['included_style'] == "alphabetical") {
                if ($directory['window'] != 0) {
                    while ($bids_gotten < $directory['window']) {
                        $last_query = "select bid,alphabetize,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'";
                        $last_result = mysqli_query($GLOBALS['dblink'],$last_query);
                        $last = mysqli_fetch_assoc($last_result);
                        
                        // are there other bids with same alphabetize (in the right direction)?
                        $other_same_check = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where did='" . $directory['did'] . "' and alphabetize='" . $last['alphabetize'] . "' and datetime < '" . $last['datetime'] . "'");
                        $other_same_count = mysqli_fetch_row($other_same_check);
                        
                        if ($other_same_count[0] > 0) {
                            // yes there are other bids with the same alphabetize
                            // Are there at least as many as window?
                            if ($other_same_count[0] >= $directory['window']) {
                                // so get that many ordered by datetime
                                $entire_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and alphabetize = '" . $last['alphabetize'] . "' order by datetime DESC");
                                while($entire = mysqli_fetch_assoc($entire_result) and $bids_gotten < $directory['window']) {
                                    if ($entire['bid'] == $last['bid'] or $got_there == "true") {
                                        $got_there = "true";
                                    }
                                    else {
                                        continue;
                                    }
                                    if ($entire['bid'] == $last['bid']) {
                                        continue;
                                    }
                                    $bids_to_get[] = $entire['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $entire['bid'];
                                }
                                
                            }
                            else {
                                // yes there are other bids with the same alphabetize
                                // but not enough to fill the page
                                
                                // so we have to put the ones that are here into $bids_to_get[]
                                // and make note of how many we get by incrementing $bids_gotten
                                // and then send ourselves back through the big while loop again with
                                // $page_modifier_value now set to the last $bid from the loop we're
                                // about to take (real clear, no?)
                                
                                $partial_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and bid != '" . $last['bid'] . "' and alphabetize = '" . $last['alphabetize'] . "' and datetime < '" . $last['datetime'] . "' order by datetime DESC");
                                while($partial = mysqli_fetch_assoc($partial_result)) {
                                    $bids_to_get[] = $partial['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $partial['bid'];
                                }
                                
                            }
                        }
                        else {
                            // no matches for this alphabetize
                            // so get the next alphabetize from this page (just the next one)
                            // add it to $bids_to_get[]
                            // increment $bids_gotten
                            // and send yourself back through the big while with $page_modifier_value = the bid you just got
                            // so we can check that one for duplicates
                            
                            $get_previous_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and alphabetize < '" . $last['alphabetize'] . "' order by alphabetize DESC, datetime DESC limit 1");
                            $get_previous = mysqli_fetch_assoc($get_previous_result);
                            $bids_gotten = $bids_gotten + 1;
                            $bids_to_get[] = $get_previous['bid'];
                            $page_modifier_value = $get_previous['bid'];
                        }
                    }
                    
                    
                    
                    // we have to build the query using $bids_to_get[]
                        
                    $postquery = "select * from blog where";
                    $to_get_count = count($bids_to_get);
                    for ($i=0;$i<$to_get_count;$i++) {
                        if ($i == 0) {
                            $postquery = $postquery . " bid = '" . $bids_to_get[$i] . "'";
                        }
                        else {
                            $postquery = $postquery . " or bid = '" . $bids_to_get[$i] . "'";
                        }
                    }
                    $postquery = $postquery . " order by alphabetize, datetime";
                
                }
            }
            elseif ($directory['included_style'] == "numeric") {
                if ($directory['window'] != 0) {
                    while ($bids_gotten < $directory['window']) {
                        $last_query = "select bid,numeric_order,datetime,real_datetime from blog where bid='" . $page_modifier_value . "'";
                        $last_result = mysqli_query($GLOBALS['dblink'],$last_query);
                        $last = mysqli_fetch_assoc($last_result);
                        
                        // are there other bids with same numeric_order (in the right direction)?
                        $other_same_check = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where did='" . $directory['did'] . "' and numeric_order='" . $last['numeric_order'] . "' and datetime < '" . $last['datetime'] . "'");
                        $other_same_count = mysqli_fetch_row($other_same_check);
                        
                        if ($other_same_count[0] > 0) {
                            // yes there are other bids with the same alphabetize
                            // Are there at least as many as window?
                            if ($other_same_count[0] >= $directory['window']) {
                                // so get that many ordered by datetime
                                $entire_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and numeric_order = '" . $last['numeric_order'] . "' order by datetime DESC");
                                while($entire = mysqli_fetch_assoc($entire_result) and $bids_gotten < $directory['window']) {
                                    if ($entire['bid'] == $last['bid'] or $got_there == "true") {
                                        $got_there = "true";
                                    }
                                    else {
                                        continue;
                                    }
                                    if ($entire['bid'] == $last['bid']) {
                                        continue;
                                    }
                                    $bids_to_get[] = $entire['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $entire['bid'];
                                }
                                
                            }
                            else {
                                // yes there are other bids with the same numeric_order
                                // but not enough to fill the page
                                
                                // so we have to put the ones that are here into $bids_to_get[]
                                // and make note of how many we get by incrementing $bids_gotten
                                // and then send ourselves back through the big while loop again with
                                // $page_modifier_value now set to the last $bid from the loop we're
                                // about to take (real clear, no?)
                                
                                $partial_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and bid != '" . $last['bid'] . "' and numeric_order = '" . $last['numeric_order'] . "' and datetime < '" . $last['datetime'] . "' order by datetime DESC");
                                while($partial = mysqli_fetch_assoc($partial_result)) {
                                    $bids_to_get[] = $partial['bid'];
                                    $bids_gotten = $bids_gotten + 1;
                                    $page_modifier_value = $partial['bid'];
                                }
                                
                            }
                        }
                        else {
                            // no matches for this numeric_order
                            // so get the next numeric_order from this page (just the next one)
                            // add it to $bids_to_get[]
                            // increment $bids_gotten
                            // and send yourself back through the big while with $page_modifier_value = the bid you just got
                            // so we can check that one for duplicates
                            
                            $get_previous_result = mysqli_query($GLOBALS['dblink'],"select bid from blog where did='" . $directory['did'] . "' and numeric_order < '" . $last['numeric_order'] . "' order by numeric_order DESC, datetime DESC limit 1");
                            $get_previous = mysqli_fetch_assoc($get_previous_result);
                            $bids_gotten = $bids_gotten + 1;
                            $bids_to_get[] = $get_previous['bid'];
                            $page_modifier_value = $get_previous['bid'];
                        }
                    }
                    
                    
                    
                    // we have to build the query using $bids_to_get[]
                        
                    $postquery = "select * from blog where";
                    $to_get_count = count($bids_to_get);
                    for ($i=0;$i<$to_get_count;$i++) {
                        if ($i == 0) {
                            $postquery = $postquery . " bid = '" . $bids_to_get[$i]. "'";
                        }
                        else {
                            $postquery = $postquery . " or bid = '" . $bids_to_get[$i] . "'";
                        }
                    }
                    $postquery = $postquery . " order by numeric_order, datetime";
                }
            }    
        }
    }
    if ($page_modifier == "date") {

        if (substr($page_modifier_value, 0, 1) == "/") {
            $page_modifier_value = substr($page_modifier_value, 1);
        }

        $date_array = explode("/", $page_modifier_value);

        if (($date_array[0] > 1998) and ($date_array[0] < 2013)) {
            $date_year = $date_array[0];
    
        }
        else {
            $no_year = "true";
            $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references from blog where " . $querydid . " and bid = '" . $specific_post . "'";
        }
        
        if (($date_array[1] > 0) and ($date_array[1] < 13)) {
            $date_month = $date_array[1];
        }
        else {
        
        /*
            $no_month = "true";
            $date_month = 1;
        */
        
            // we are being destroyed by the googlebot requesting /date/ pages that do not exist -
            // like /tommoody/date/rachael/jim.php/index.php/dratfink/ (note: WTF?)
            // so at least temporarily I am going to reject all /date/ requests that don't have a valid numeric month
            
            echo'Archive requests must include a year and a month (and optionally a day.) Requests for entire years have been disabled.';
            echo'</body></html>';
            mysqli_close($GLOBALS['dblink']);
            die();
            
        
        
        
        
        
        }
        if (($date_array[2] > 0) and ($date_array[2] < 32)) {
            $date_day = $date_array[2];
        }
        else {
            $no_day = "true";
            $date_day = 1;
        }
        
        $pagestart = mktime (0, 0, 0, $date_month, $date_day, $date_year);
        $pagestart = $pagestart + $user['time_offset'];
    
        if ($no_month == "true") {
            // $date_year++;
            $pageend = mktime (23, 59, 59, 12, 31, $date_year);
            $pageend = $pageend - $user['time_offset'];
        }
        elseif ($no_day == "true") {
            if (($date_month == 4) or ($date_month == 6) or ($date_month == 9) or ($date_month == 11)) {
                $date_day = 30;
                $pageend = mktime (23, 59, 59, $date_month, $date_day, $date_year);
                $pageend = $pageend - $user['time_offset'];
            }
            elseif ($date_month == 2) {

                $date_day = 28;
                $pageend = mktime (23, 59, 59, $date_month, $date_day, $date_year);
                $pageend = $pageend - $user['time_offset'];
            }
            else {
                $date_day = 31;

                $pageend = mktime (23, 59, 59, $date_month, $date_day, $date_year);
                $pageend = $pageend - $user['time_offset'];
            }
        }
        elseif ($no_year != "true") {


            $pageend = mktime (23, 59, 59, $date_month, $date_day, $date_year);
            $pageend = $pageend - $user['time_offset'];
        }
    
        $postquery = "select did, bid, name, uid, datetime,real_datetime, post, num_comments, num_references from blog where " . $querydid . " and datetime >= '" . $pagestart . "' and datetime <= '" . $pageend . "' order by datetime " . $the_direction;            
        
    }
}

// main loop

// print"<br><br>pq: $postquery<br><br>";
    


$first_time = "true";
$entirepageurl = $bald_host . $directory['path'] . "/";
$result = mysqli_query($GLOBALS['dblink'],$postquery);
if ($result == "") {
    if ($page_modifier == "" and $query_string == "") {
        // print"There are no posts for page $directory['path']";
    }
    else {
        print"I could not find any posts.<br><br>Click <a href=\"$entirepageurl\">here</a> for the current view of this page.";
    }
    return;
}
if ($this_is_a_search == "true" or $new_posts == "true" or $preview == "true" or $directory['included'] == "slideshow.php") {
    $search_count = mysqli_num_rows($result);
    
    if ($search_count == 0) {
        if ($directory['window'] > 0) {
            print"<a href=\"$entirepageurl\">View current page</a><br><br>
            ";
        }
        else {
            print"<a href=\"$entirepageurl\">View entire page</a><br><br>
            ";
        }
        print"There are no matches for $original_search_string
        ";
    }
}

if ($user['uid'] != '2') {
    $max_last_seen = $subscription['last_seen'];
}

if ($this_is_a_search != 'true' and $specific_post == '') {
    $prevquery = "select bid,datetime from preview where did='" . $directory['did'] . "'";
    $previewres = mysqli_query($GLOBALS['dblink'],$prevquery);
    $previewcount = mysqli_num_rows($previewres);
}


while($blog = mysqli_fetch_assoc($result)) {
    
    if($homepage == true and $user['uid'] > 2) {
        // there are posts from multiple pages on this front page view, so we need to get 
        // $max_last_seen for each different page
        $mlsquery = "select last_seen from subscription where uid='" . $user['uid'] . "' and did='" . $blog['did'] . "' limit 1";
        $mlsres = mysqli_query($GLOBALS['dblink'],$mlsquery);
        $mls = mysqli_fetch_assoc($mlsres);
        $max_last_seen = $mls['last_seen'];
    }
    
    if ($first_time == "true") {
        $first_time = "false";
        // print pre links if necessary
        if ($new_posts == "true") {
            if ($search_count == 0) {
                print"There are no new posts for you on this page<br><br>
                <a href=\"$entirepageurl\">View entire page</a><br><br>
                ";
            }
        }
        if ($this_is_a_search == "true") {
            if ($directory['window'] > 0) {
                print"<a href=\"$entirepageurl\">View current page</a><br><br>
                ";
            }
            else {
                print"<a href=\"$entirepageurl\">View entire page</a><br><br>
                ";
            }
            
            if ($search_count == 1) {
                print"1 match for " . $original_search_string . ":<br><br>
                ";
            }
            elseif ($search_count < 50) {
                print"$search_count matchs for " . $original_search_string . ":<br><br>
                ";
            }
            else {
                print"Here are the maximum 50 results for " . $original_search_string . ":<br><br>
                ";
            }
        }
        
        /*
        ***************************
        
        if ($preview == "true") {
            print"<a href=\"$entirepageurl\">View current page</a><br><br>
            ";
            if ($search_count == 0) {
                if ($user[uid] == $directory[uid] or $user[group_level] >= '900') {
                    print"There are no preview posts on this page.";
                }
                else {
                    print"There are no preview posts of yours on this page.";
                }
            }
            else {
                print"Preview posts:<br><br>
                ";
            }
        }
        
        ******************************
        */
        
        if (($specific_post != "" and $specific_post != "new") or ($page_modifier != "")) {
            // are there posts forward of the first one?
            if ($directory['direction'] == 1) {
                // is there a higher datetime, higher numeric_order, or a later alphabetize (toward 'z')?
                if ($directory['included_style'] == "chronological") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and datetime > '" . $blog['datetime'] . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {

                        if ($directory['included'] == "slideshow.php") {
                            $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom

                        }
                        else {
                            if($homepage == true) {
                                $directory['path'] = '';
                                echo'<div class="postcontrol">';
                            }
                            $pforwardurl = $bald_host . $directory['path'] . "/pageforward/" . $blog['bid'] . "/";
                            if ($order == 'reverse') {
                                $pforwardurl = $pforwardurl . '?order=reverse';
                            }
                            $currenturl = $bald_host . $directory['path'] . "/";
                            print"<a href=\"$currenturl\">View current page</a><br>";
                            print"<a href=\"$pforwardurl\">...more recent posts</a><br><br>
                            ";
                            if($homepage == true) {
                                echo'</div>';
                            }
                        }
                    }
                }
                if ($directory['included_style'] == "alphabetical") {
                    $testquery = "select count(bid) from blog where did='" . $directory['did'] . "' and alphabetize = '" . $blog['alphabetize'] . "' and datetime > '" . $blog['datetime'] . "'";
                    $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        if ($directory['included'] == "slideshow.php") {
                            $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                        }
                        else {
                            $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                            if ($order == 'reverse') {
                                $pforwardurl = $pforwardurl . '?order=reverse';
                            }
                            print"<a href=\"$pforwardurl\">...previous</a><br><br>
                            ";
                        }
                    }
                    else {
                        $testquery = "select count(bid) from blog where " . $querydid . " and alphabetize > '" . $blog['alphabetize'] . "'";
                        $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                        $testcount = mysqli_fetch_row($test);
                        if ($testcount[0] > 0) {
                            if ($directory['included'] == "slideshow.php") {
                                $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                            }
                            else {
                                $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                                if ($order == 'reverse') {
                                    $pforwardurl = $pforwardurl . '?order=reverse';
                                }
                                print"<a href=\"$pforwardurl\">...previous</a><br><br>
                                ";
                            }
                        }
                    }
                }
                if ($directory['included_style'] == "numeric") {
                    $testquery = "select count(bid) from blog where did='" . $directory['did'] . "' and numeric_order = '" . $blog['numeric_order'] . "' and datetime > '" . $blog['datetime'] . "'";
                    $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        if ($directory['included'] == "slideshow.php") {
                            $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                        }
                        else {
                            $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                            if ($order == 'reverse') {
                                $pforwardurl = $pforwardurl . '?order=reverse';
                            }
                            print"<a href=\"$pforwardurl\">...previous</a><br><br>
                            ";
                        }
                    }
                    else {
                        $testquery = "select count(bid) from blog where " . $querydid . " and numeric_order > '" . $blog['numeric_order'] . "'";
                        $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                        $testcount = mysqli_fetch_row($test);
                        if ($testcount[0] > 0) {
                            if ($directory['included'] == "slideshow.php") {
                                $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                            }
                            else {
                                $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                                if ($order == 'reverse') {
                                    $pforwardurl = $pforwardurl . '?order=reverse';
                                }
                                print"<a href=\"$pforwardurl\">...previous</a><br><br>
                                ";
                            }
                        }
                    }
                }
            }
            else {
                if ($directory['included_style'] == "chronological") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and datetime < '" . $blog['datetime'] . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        if ($directory['included'] == "slideshow.php") {
                            $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                        }
                        else {
                            $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                            if ($order == 'reverse') {
                                $pforwardurl = $pforwardurl . '?order=reverse';
                            }
                            print"<a href=\"$pforwardurl\">...previous</a><br><br>
                            ";
                        }
                    }
                }
                if ($directory['included_style'] == "alphabetical") {
                    
                    $testquery = "select count(bid) from blog where did='" . $directory['did'] . "' and alphabetize = '" . $blog['alphabetize'] . "' and datetime < '" . $blog['datetime'] . "'";
                    $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        if ($directory['included'] == "slideshow.php") {
                            $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                        }
                        else {
                            $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                            if ($order == 'reverse') {
                                $pforwardurl = $pforwardurl . '?order=reverse';
                            }
                            print"<a href=\"$pforwardurl\">...previous</a><br><br>
                            ";
                        }
                    }
                    else {
                        $testquery = "select count(bid) from blog where " . $querydid . " and alphabetize < '" . $blog['alphabetize'] . "'";
                        $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                        $testcount = mysqli_fetch_row($test);
                        if ($testcount[0] > 0) {
                            if ($directory['included'] == "slideshow.php") {
                                $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                            }
                            else {
                                $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                                if ($order == 'reverse') {
                                    $pforwardurl = $pforwardurl . '?order=reverse';
                                }
                                print"<a href=\"$pforwardurl\">...previous</a><br><br>
                                ";
                            }
                        }
                    }
                }
                if ($directory['included_style'] == "numeric") {
                    $testquery = "select count(bid) from blog where did='" . $directory['did'] . "' and numeric_order = '" . $blog['numeric_order'] . "' and datetime < '" . $blog['datetime'] . "'";
                    $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        if ($directory['included'] == "slideshow.php") {
                            $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                        }
                        else {
                            $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                            if ($order == 'reverse') {
                                $pforwardurl = $pforwardurl . '?order=reverse';
                            }
                            print"<a href=\"$pforwardurl\">...previous</a><br><br>
                            ";
                        }
                    }
                    else {
                        $testquery = "select count(bid) from blog where " . $querydid . " and numeric_order < '" . $blog['numeric_order'] . "'";
                        $test = mysqli_query($GLOBALS['dblink'],"$testquery");
                        $testcount = mysqli_fetch_row($test);
                        if ($testcount[0] > 0) {
                            if ($directory['included'] == "slideshow.php") {
                                $slideshow_previous = "true"; // we need to know this for printing the slide show links at the bottom
                            }
                            else {
                                $pforwardurl = $bald_host . $directory['path'] . "/previous/" . $blog['bid'] . "/";
                                if ($order == 'reverse') {
                                    $pforwardurl = $pforwardurl . '?order=reverse';
                                }
                                print"<a href=\"$pforwardurl\">...previous</a><br><br>
                                ";
                            }
                        }
                    }
                }
            }
        }
    }
            
    if ($previewcount > 0) {
        
        
        if ($directory['included_style'] == 'chronological') {
            if ($directory['direction'] == '1') {
                // reverse chronological
                // is there a preview post with a more recent time than what we are about to print?
                if ($page_modifier == 'date') {
                    $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where datetime > '" . $blog['datetime'] . "' and datetime < '" . $pagestart . "' and did='" . $directory['did'] . "' order by datetime desc");
                }
                else {
                    $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where datetime > '" . $blog['datetime'] . "' and did='" . $directory['did'] . "' order by datetime desc");
                }
            }
            else {
                // chronological
                // is there a preview post with a less recent time than what we are about to print?
                if ($page_modifier == 'date') {
                    $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where datetime < '" . $blog['datetime'] . "' and datetime > '" . $pagestart . "' and did='" . $directory['did'] . "' order by datetime desc");
                }
                else {
                    $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where datetime < '" . $blog['datetime'] . "' and did='" . $directory['did'] . "' order by datetime");
                }
            }
        }
        elseif ($directory['included_style'] == 'alphabetical') {
            if ($directory['direction'] == '1') {
                // reverse alphabetical
                // is there a preview post with a more recent alphabetize by than what we are about to print?
                $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where alphabetize > '$blog[alphabetize]' and did='" . $directory['did'] . "' order by alphabetize desc");
            }
            else {
                // alphabetical
                // is there a preview post with a less recent alphabetize by than what we are about to print?
                $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where alphabetize < '$blog[alphabetize]' and did='" . $directory['did'] . "' order by alphabetize");
            }
        }
        elseif ($directory['included_style'] == 'numeric') {
            if ($directory['direction'] == '1') {
                // reverse alphabetical
                // is there a preview post with a more recent numeric order by than what we are about to print?
                $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where numeric_order > '" . $blog['numeric_order'] . "' and did='" . $directory['did'] . "' order by numeric_order desc");
            }
            else {
                // alphabetical
                // is there a preview post with a less recent alphabetize by than what we are about to print?
                $pres = mysqli_query($GLOBALS['dblink'],"select * from preview where numeric_order < '" . $blog['numeric_order'] . "' and did='" . $directory['did'] . "' order by numeric_order");
            }
        }
                
        $prescount = mysqli_num_rows($pres);
        if ($prescount > 0) {
            $previewdone[] = '';
            while($pblog = mysqli_fetch_assoc($pres)) {
                
                if (in_array($pblog['bid'], $previewdone)) {
                    continue;
                }
                if ($pblog['uid'] != $user['uid'] and $user['group_level'] < '1000' and $user['uid'] != $directory['uid']) {
                    continue;
                }
                $previewdone[] = $pblog['bid'];
                
                print"<span class=\"preview\">Preview<br><br>";
                $thispost = stripslashes($pblog['post']);
                $thisdatetime = format_datetime($pblog['datetime'], $user);
            
                if ($directory['date_header'] == 1) {
                    $usertimestamp = $pblog['datetime'] + $user['time_offset'];
                    // get month, day of month, and year for this timestamp
                    $theyear = date("Y", $usertimestamp);
                    $themonth = date("M", $usertimestamp);
                    $theday = date("l", $usertimestamp);
                    $thedayofmonth = date("d", $usertimestamp);
                    
                    if (($theyear == $lastyear) and ($themonth == $lastmonth) and ($thedayofmonth == $lastdayofmonth)) {
                        // do nothing
                    }
                    else {
                        print"<span class=\"date_header\">${theday}, $themonth ${thedayofmonth}, ${theyear}</span><br><br>";
                    }
                }
                
                $lastyear = $theyear;
                $lastmonth = $themonth;
                $lastdayofmonth = $thedayofmonth;
                
                // html entities hack
                $thispost = ereg_replace("&_", "&", $thispost);
                
                
                
        
                
                print"$open_html";
                
                
                print"$thispost<br>
                ";
                
                
                print"</span>";
                
                
                
                print"<span class=\"footer\">";
                
                if ($directory['post_foot'] == '1') {
                    $thisname = stripslashes($pblog['name']);
                    if ($blog['uid'] != '2') {
                        print"- $thisname $thisdatetime ";
                    }
                    else {
                        print"- $thisname (guest) $thisdatetime ";
                    }
                    
                }
                
                if ($pblog['uid'] == $user['uid'] or $directory['uid'] == $user['uid'] or $user['group_level'] >= 900) {
            
                    $edit_url = $this_host . "edit/" . $pblog['bid'] . "/?preview=true";
                    print"[<a href=\"$edit_url\">edit</a>] ";
                }
                
                print"</span>";
                
            
                print"$close_html";
                
                $started_foot = "false";
                // $last_bid = $pblog[bid];
                // $last_datetime = $pblog[datetime];
                // $last_alphabetize = $pblog[alphabetize];
                // $last_numeric_order = $pblog[numeric_order];

            
            }
        }

    }            
        
    $thispost = stripslashes($blog['post']);
    $thisdatetime = format_datetime($blog['datetime'], $user);


    if ($user['uid'] != '2' and ($subscription['tracking'] == '1' or $subscription['tracking'] == '3') and ($blog['real_datetime'] > $max_last_seen)) {
        // $max_last_seen = $blog[real_datetime];
        $newpost = 'true';
    }
    
    if ($directory['date_header'] == 1) {
        $usertimestamp = $blog['datetime'] + $user['time_offset'];
        // get month, day of month, and year for this timestamp
        $theyear = date("Y", $usertimestamp);
        $themonth = date("M", $usertimestamp);
        $theday = date("l", $usertimestamp);
        $thedayofmonth = date("d", $usertimestamp);
        
        if (($theyear == $lastyear) and ($themonth == $lastmonth) and ($thedayofmonth == $lastdayofmonth)) {
            // do nothing
        }
        else {
            print"<span class=\"date_header\">${theday}, $themonth ${thedayofmonth}, ${theyear}</span><br><br>";
        }
    }
    
    $lastyear = $theyear;
    $lastmonth = $themonth;
    $lastdayofmonth = $thedayofmonth;
    
    print"$open_html";
    
    if ($newpost == 'true') {
        print"<span class=\"new\">new</span><br>";
        $newpost = 'false';
    }
    
    // html entities hack
    $thispost = preg_replace("/&_/", "&", $thispost);
    
    if($homepage == true) {
        trim($thispost);
        do {
            if(strtolower(substr($thispost,-4)) == '<br>') {
                $thispost = substr($thispost,0,-4);
            }
        } while (strtolower(substr($thispost,-4)) == '<br>');
        
        $dirquery = "select path,title,page_name from directory where did='" . $blog['did'] . "' limit 1";
        $dirres = mysqli_query($GLOBALS['dblink'],$dirquery);
        $thisdir = mysqli_fetch_assoc($dirres);
        $perma = $bald_host . $thisdir['path'] . "/";
        $directory['path'] = $thisdir['path'];
        $path = $thisdir['path'];
    }
    
    if ($directory['post_comment'] > 0) {
        // can only break on <more> if comments are on for the page
        $moretest = explode('<more>', $thispost);
        if (count($moretest) > 1) {
            $moreurl = $bald_host . $directory['path'] . '/comment/' . $blog['bid'];
            $thispost = $moretest[0] . "<br><br><div class=\"more\"><a class=\"moreloader\" href=\"$moreurl\" data-bid=\"" . $blog['bid'] . "\">Read the rest of this post</a>...</div><br><br>";
        }
        $moretest = explode('<moreblockquote>', $thispost);
        if (count($moretest) > 1) {
            $moreurl = $bald_host . $directory['path'] . '/comment/' . $blog['bid'];
            $thispost = $moretest[0] . "<br><br><span class=\"more\"><a href=\"$moreurl\">Read the rest of this post</a>...</span><br><br></blockquote>";
        }
        if($homepage === true) {
            $moretest = explode('<fpmore>', $thispost);
            if (count($moretest) > 1) {
                $moreurl = $bald_host . $directory['path'] . '/comment/' . $blog['bid'];
                $thispost = $moretest[0] . "<br><br><div class=\"more\"><a class=\"moreloader\" href=\"$moreurl\" data-bid=\"" . $blog['bid'] . "\" data-did=\"" . $directory['did'] . "\">Read the rest of this post</a>...</div><br><br>";
            }
        }
        else {
            $thispost = str_replace('<fpmore>','',$thispost);
        }
    }
    
    if ($directory['included'] == 'simple_template.php') {
        if ($directory['comment_style'] == '') {
            if ($directory['uid'] == $user[uid]) {
                $edurl = $this_host . 'edit/' . $blog['bid'] . '/';
                print"<a href=\"$edurl\">$thispost</a>";
            }
            else {
                print"$thispost";
            }
        }
        else {
            $edurl = $bald_host . $directory['path'] . '/comment/' . $blog['bid'] . '/';
            print"<a href=\"$edurl\">$thispost</a>";
        }
    }
    else {
        if($homepage == true) {
            echo $thispost;
        }
        else {
        print"$thispost<br>
    ";
        }
    }
    
    if($homepage == true) {
        echo "<div class=\"postfooter\">";
    }
    else {
        print"<span class=\"footer\">";
    }
    
    
    $just_read[] = $blog['bid'];
    
    if($homepage == true) {
        
        if($thisdir['page_name'] != '') {
            $pname = $thisdir['page_name'];
        }
        else {
            $pname = $thisdir['title'];
        }
        echo'Posted to <a href="' . $perma . '">' . $pname . '</a> by ';
    }
    if ($directory['post_foot'] == '1') {
        if($homepage != true) {
            echo'-';
        }
        $thisname = stripslashes($blog['name']);
        if ($blog['uid'] != '2') {
            print" $thisname $thisdatetime ";
        }
        else {
            print" $thisname (guest) $thisdatetime ";
        }
        
    }
    if($homepage == true) {
        
        $perma = $bald_host . $thisdir['path'] . "/?" . $blog['bid'];
        print"[<a href=\"$perma\">link</a>] ";
        
    }
    else {
        if ($directory['permalink'] == '1') {
            $perma = $bald_host . $path . "/?" . $blog['bid'];
            print"[<a href=\"$perma\">link</a>] ";
        }
    }
    if ($blog['num_references'] > 0 and $specific_post != "preview") {
        $ref_url = $bald_host . $path . "/references/" . $blog['bid'] . "/";
        if ($blog['num_references'] == '1') {
            print"[<a href=\"$ref_url\">1 ref</a>] ";
        }
        else {
            print"[<a href=\"$ref_url\">$blog[num_references] refs</a>] ";
        }
    }
    if ($directory['post_comment'] == '1' and $specific_post != "preview" and $directory['included'] != 'simple_template.php') {
        
        if ($user['uid'] != '2') {
            $unreadres = mysqli_query($GLOBALS['dblink'],"select count(commid) from unread_comments where bid='" . $blog['bid'] . "' and uid='" . $user['uid'] . "'");
            $unreadcount = mysqli_fetch_row($unreadres);
        }
        $comm_url = $bald_host . $path . "/comment/" . $blog['bid'] . "/";
        if ($blog['num_comments'] == '0') {
            print"[<a href=\"$comm_url\">add a comment</a>] ";
        }
        elseif ($blog['num_comments'] == '1') {
            if ($unreadcount[0] == '1') {
                print"[<a href=\"$comm_url\"><font color=\"red\">1 comment</font></a>] ";
            }
            else {
                print"[<a href=\"$comm_url\">1 comment</a>] ";
            }
        }
        else {
            if ($unreadcount[0] > 0) {
                print"[<a href=\"$comm_url\"><font color=\"red\">$unreadcount[0]</font>/$blog[num_comments] comments</a>] ";
            }
            else {
                print"[<a href=\"$comm_url\">$blog[num_comments] comments</a>] ";
            }
        }
    }
    if (($blog['uid'] == $user['uid'] or $directory['uid'] == $user['uid'] or $user['group_level'] >= 900) and $directory['included'] != 'simple_template.php') {
        
        $edit_url = $this_host . "edit/" . $blog['bid'];
        print"[<a href=\"$edit_url\">edit</a>] ";
    }
    if($homepage == true) {
        echo'</div>
        ';
    }
    else {
        print"</span>";
    }
    

    print"$close_html";
    $started_foot = "false";
    $last_bid = $blog['bid'];
    $last_datetime = $blog['datetime'];
    $last_alphabetize = $blog['alphabetize'];
    $last_numeric_order = $blog['numeric_order'];


}

if($homepage == true and $user['uid'] > 2) {
    foreach($publicdid as $subdid) {
        $timenow = gmtime();
        $subquery = "update subscription set last_seen='" . $timenow . "' where did='" . $subdid . "' and uid='" . $user['uid'] . "'";
        echo'<!-- ' . $subquery . ' -->';
        $result = mysqli_query($GLOBALS['dblink'],$subquery);
    }
}

$max_last_seen = $blog['real_datetime'];
        
// do post links if necessary

if ($preview != "true" and $this_is_a_search != "true") {
    if($homepage == true) {
        $directory['path'] = '';
    }
    if ($directory['included'] == "slideshow.php") {

        print"<div class=\"center_block\">";
        
        $ssresult = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where did='" . $directory['did'] . "'");
        $sscount = mysqli_fetch_row($ssresult);
        $search_count = $sscount[0];
        
        if ($search_count > '1') {

            if ($slideshow_previous == "true") {
                $previousurl = $bald_host . $directory['path'] . "/previous/" . $last_bid;
                print"&lt;&lt;&nbsp;<a href=\"$previousurl\">previous</a>&nbsp;&nbsp;&nbsp;";
            }
            if ($directory['included_style'] == "chronological") {
                if ($directory['direction'] == '1') {
                    $bidquery = "select bid from blog where did='" . $directory['did'] . "' order by datetime desc";
                }
                else {
                    $bidquery = "select bid from blog where did='" . $directory['did'] . "' order by datetime";
                }
            }
            elseif ($directory['included_style'] == "alphabetical") {
                if ($directory['direction'] == '1') {
                    $bidquery = "select bid from blog where did='" . $directory['did'] . "' order by alphabetize desc";
                }
                else {
                    $bidquery = "select bid from blog where did='" . $directory['did'] . "' order by alphabetize";
                }
            
            }
            elseif ($directory['included_style'] == "numeric") {
                if ($directory['direction'] == '1') {
                    $bidquery = "select bid from blog where did='" . $directory['did'] . "' order by numeric_order desc";
                }
                else {
                    $bidquery = "select bid from blog where did='" . $directory['did'] . "' order by numeric_order";
                }
            }
            
            // get all the bids in their order
            $bidresult = mysqli_query($GLOBALS['dblink'],$bidquery);
        
            $currentnum = 1;
            while($bids = mysqli_fetch_assoc($bidresult)) {
                if ($bids['bid'] == $last_bid) {
                    print"<font color=\"red\">$currentnum</font> ";
                    $didthispage = "true";
                }
                else {
                    $specificurl = $bald_host . $directory['path'] . "/?" . $bids['bid'];
                    print"<a href=\"$specificurl\">$currentnum</a>&nbsp;";
                    if ($didthispage == "true") {
                        $slideshow_next = "true";
                    }
                }
                $currentnum++;
            }
            if ($slideshow_next == "true") {
                $nexturl = $bald_host . $directory['path'] . "/next/" . $last_bid;
                print"&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"$nexturl\">next</a>&nbsp;&gt;&gt;";
            }
        }
        if ($directory['thumbnail_url'] != "") {
            print"<br><br><a href=\"$directory[thumbnail_url]\">thumbnails</a>
            ";
        }
        print"</div>";
    }
    else {
        
        if ($directory['window'] != '0') {
            if ($directory['direction'] == '1') {
                // is there a lower datetime, higher numeric_order, or a later alphabetize (toward 'z')?
                if ($directory['included_style'] == "chronological") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and datetime < '" . $last_datetime . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        $pforwardurl = $bald_host . $directory['path'] . "/pageback/" . $last_bid . "/";
                        if ($order == 'reverse') {
                            $pforwardurl = $pforwardurl . '?order=reverse';
                        }
                        print"<div class=\"postcontrol\"><a href=\"$pforwardurl\">older posts...</a></div><br><br>
                        ";
                    }
                }
                if ($directory['included_style'] == "alphabetical") {
                    $query = "select count(bid) from blog where " . $querydid . " and bid != '$last_bid' and alphabetize <= '" . $last_alphabetize . "'";
    
                    $test = mysqli_query($GLOBALS['dblink'],"$query");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        $pforwardurl = $bald_host . $directory['path'] . "/next/" . $last_bid . "/";
                        if ($order == 'reverse') {
                            $pforwardurl = $pforwardurl . '?order=reverse';
                        }
                        print"<div class=\"postcontrol\"><a href=\"$pforwardurl\">next...</a></div><br><br>
                        ";
                    }
                }
                if ($directory['included_style'] == "numeric") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and bid != '" . $last_bid . "' and numeric_order <= '" . $last_numeric_order . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        $pforwardurl = $bald_host . $directory['path'] . "/next/" . $last_bid . "/";
                        if ($order == 'reverse') {
                            $pforwardurl = $pforwardurl . '?order=reverse';
                        }
                        print"<div class=\"postcontrol\"><a href=\"$pforwardurl\">next...</a></div><br><br>
                        ";
                    }
                }
            
            }
            else {
                if ($directory['included_style'] == "chronological") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and datetime > '" . $last_datetime . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        $pforwardurl = $bald_host . $directory['path'] . "/next/" . $last_bid . "/";
                        if ($order == 'reverse') {
                            $pforwardurl = $pforwardurl . '?order=reverse';
                        }
                        print"<div class=\"postcontrol\"><a href=\"$pforwardurl\">next...</a></div><br><br>
                        ";
                    }
                }
                if ($directory['included_style'] == "alphabetical") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and bid != '" . $last_bid . "' and alphabetize >= '" . $last_alphabetize . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        $pforwardurl = $bald_host . $directory['path'] . "/next/" . $last_bid . "/";
                        if ($order == 'reverse') {
                            $pforwardurl = $pforwardurl . '?order=reverse';
                        }
                        print"<div class=\"postcontrol\"><a href=\"$pforwardurl\">next</a></div><br><br>
                        ";
                    }
                }
                if ($directory['included_style'] == "numeric") {
                    $test = mysqli_query($GLOBALS['dblink'],"select count(bid) from blog where " . $querydid . " and bid != '" . $last_bid . "' and numeric_order >= '" . $last_numeric_order . "'");
                    $testcount = mysqli_fetch_row($test);
                    if ($testcount[0] > 0) {
                        $pforwardurl = $bald_host . $directory['path'] . "/next/" . $last_bid . "/";
                        if ($order == 'reverse') {
                            $pforwardurl = $pforwardurl . '?order=reverse';
                        }
                        print"<div class=\"postcontrol\"><a href=\"$pforwardurl\">next</a></div><br><br>
                        ";
                    }
                }
            }
        }
    }
}



?>