<?
/*
################# AJAX Functions #################
These are functions that are used by my ajax objects.

They're in a separate file for a few reasons.  They
were added later in the codebase, so I didnt want to 
mess up anything by trying to integrate it.  Also when these 
functions return html, it is just plain html without 
my templates or navigation tables or anything like that.
##################################################
*/

// *** I may have to do some stuff to prevent spam, but for now I think I'm ok ***

if ($QUERY[0] == "ajax")
// *** Got some ajax stuff come'n! ***
{
switch (
$QUERY[1])
{
    case 
"scratch_post":
        
session_start();
        
        if (
$_SERVER["REQUEST_METHOD"] != "POST")
            die(
"Sorry, invalid request method");

        if (empty(
$QUERY[2]) || empty($QUERY[3]))
            die(
"<b>Sorry, fill in name and/or message<br><br></b>" get_scratch(20));
            
        
// *** CAPTCHA test! ***
        // *** We have to make sure that the pass is not blank - otherwise cookies that 
        // are not set correctly will "match" a blank password ***
        
$pass_text $QUERY[5];
        if (empty(
$scratch_pass) || empty($pass_text) || ($pass_text != $scratch_pass))
            die(
"<b>Sorry, you type exactly what you see in the black box.  Are you a bot?");
            
        if (
post_scratch($QUERY[2], $QUERY[4], $QUERY[3]))
        {
            
// *** Successful, so output the new scratchpad ***
            
die(get_scratch(20));
        } else {
            die(
"Scratch Aborted - Error!");
        }
        die(
"Unknown Error");
    case 
"scratch_update";
        
// *** Sometimes this returns quicker than the above one, so lets make it wait ***
        
die(get_scratch(1) . " [<a href='index.php?scratchpad'>more</a>]");
    case 
"stats":
        list(
$id) = mysql_fetch_row(mysql_query("SELECT id FROM `stats` ORDER BY id DESC LIMIT 1"));

        
$stats0_1 mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
            sum(hits_unique) from stats where id=%s;"
$id)));
            
        
$stats1_1 mysql_fetch_array(mysql_query(sprintf("select rss_views, max_online
        from stats where id=%s;"
$id)));
        
        
$stats0_2 mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
            sum(hits_unique) from stats where id=%s;"
$id 1)));
            
        
$stats1_2 mysql_fetch_array(mysql_query(sprintf("select rss_views, max_online
        from stats where id=%s;"
$id 1)));
        

        
$stats_7 mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
            sum(hits_unique), avg(hits_total), avg(hits_unique) 
            from stats where id > %s and id < %s;"
$id 7$id)));
            
        
$stats_30 mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
            sum(hits_unique), avg(hits_total), avg(hits_unique) 
            from stats where id > %s and id < %s;"
$id 30$id)));
            
        
$stats_60 mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
            sum(hits_unique), avg(hits_total), avg(hits_unique) 
            from stats where id > %s and id < %s;"
$id 60$id-30)));
            
        
$stats_180 mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
                    sum(hits_unique), avg(hits_total), avg(hits_unique) 
            from stats where id > %s and id < %s;"
$id 180$id)));
            
        
$stats_life mysql_fetch_array(mysql_query(sprintf("select sum(hits_total), 
                    sum(hits_unique), avg(hits_total), avg(hits_unique) 
            from stats;"
)));
            
        
$stats_num mysql_num_rows(mysql_query("select id from stats"));

        
$output .= sprintf("<b>Today:</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>RSS_Views: %s
        <br>Max Online Users: %s<br><br>"
$stats0_1[0], $stats0_1[1], $stats1_1[0], $stats1_1[1]);
        
        
$output .= sprintf("<b>Yesterday:</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>RSS_Views: %s
        <br>Max Online Users: %s<br><br>"
$stats0_2[0], $stats0_2[1], $stats1_2[0], $stats1_2[1]);
        
        
$output .= sprintf("<b>Past week:</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>Average Total: %s
        <br>Average Uniques: %s<br><br>"
$stats_7[0], $stats_7[1], round($stats_7[2]), round($stats_7[3]));
        
        
$output .= sprintf("<b>Past 30 days:</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>Average Total: %s
        <br>Average Uniques: %s<br><br>"
$stats_30[0], $stats_30[1], round($stats_30[2]), round($stats_30[3]));
        
        
$output .= sprintf("<b>Previous 30 days:</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>Average Total: %s
        <br>Average Uniques: %s<br><br>"
$stats_60[0], $stats_60[1], round($stats_60[2]), round($stats_60[3]));
        
        
$output .= sprintf("<b>Past 180 days:</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>Average Total: %s
        <br>Average Uniques: %s<br><br>"
$stats_180[0], $stats_180[1], round($stats_180[2]), round($stats_180[3]));
        
        
$output .= sprintf("<b>Lifetime (%s days):</b><br>Total Hits: %s<br>
        Total Uniques: %s<br>Average Total: %s
        <br>Average Uniques: %s<br><br>"
$id$stats_life[0], $stats_life[1], round($stats_life[2]), round($stats_life[3]));
        
        die(
$output);
        break;
    default:
        die(
"Sorry, Invalid Ajax Post");
}
}
?>