//Check whether ADMIN has submitted comment to mark as ham for Defensio
 if (isset($_GET['view']) && $_GET['view'] == 'comments' && isset($_GET['action']) and $_GET['action'] == 'defensionotspam') {
     defensio_submit_nonspam_comment($defensio_conf);
 }
 if (isset($_GET['view']) && $_GET['view'] == 'comments' && isset($_GET['action']) and $_GET['action'] == 'defensioprocessall') {
     defensio_process_unprocessed($defensio_conf);
 }
 if (isset($_GET['view']) && $_GET['view'] == 'comments' && isset($_GET['action']) and $_GET['action'] == 'defensiogetresults') {
     $defensio = new Defensio($defensio_conf['key']);
     if (array_shift($defensio->getUser()) == 200) {
         $comment_id = (int) $_GET['cid'];
         // get the comment info in question
         $query = "SELECT `signature` FROM `{$pixelpost_db_prefix}comments` WHERE `id` = '" . $comment_id . "'";
         $defensio_result = mysql_query($query) or die(mysql_error());
         $row = mysql_fetch_array($defensio_result);
         $get_result = $defensio->getDocument($row[0]);
         // we always try to get the results here.
         defensio_process_comment_pixelpost($get_result, false);
     } else {
         die("The API key is invalid!!! Bye Bye.");
     }
 }
 //Check whether ADMIN has submitted a comment to resend to Defensio
 if (isset($_GET['view']) && $_GET['view'] == 'comments' && isset($_GET['action']) and $_GET['action'] == 'defensiorecheck') {
     // build $comment array used for testing.
     $comment_id = (int) $_GET['cid'];
     // get the comment info in question
     $query = "SELECT * FROM `{$pixelpost_db_prefix}comments` WHERE `id` = '" . $comment_id . "'";
     $defensio_result = mysql_query($query) or die(mysql_error());
     $row = mysql_fetch_array($defensio_result);
     $document = array('client' => 'Pixelpost Defensio Addon | ' . $addon_version . ' | Schonhose | schonhose@pixelpost.org', 'content' => $row['message'], 'platform' => 'pixelpost', 'type' => 'comment', 'async' => 'true', 'async-callback' => $defensio_conf['blog'] . 'addons/_defensio2.0/lib/callback.php?id=' . md5($defensio_conf['key']), 'author-email' => $row['email'], 'author-ip' => $row['ip'], 'author-logged-in' => 'false', 'author-name' => $row['name'], 'parent-document-date' => defensio_get_datetime_post($row['parent_id']), 'parent-document-permalink' => $defensio_conf['blog'] . "index.php?showimage=" . $row['parent_id'], 'referrer' => $_SERVER['HTTP_REFERER'], 'author-url' => $row['url']);
/**
 * defensio_process_unprocessed()
 * 
 * @param mixed $defensio_conf
 * @return
 */
function defensio_process_unprocessed($defensio_conf)
{
    global $pixelpost_db_prefix, $defensio;
    //There are three possibilities: it can have failed, it is pending or somehow the status is null
    //For each step there is a different approach.
    $defensio = new Defensio($defensio_conf['key']);
    $seconds = 1209600;
    //86399 is 24 hours, but in this case we approximately 14 days.
    // 1) first get all the comments that have failed or have status null for the last 2 weeks and process them again.
    $query = "SELECT *\n        FROM `{$pixelpost_db_prefix}comments`\n        WHERE (\n            (\n                `status` = 'fail'\n                OR `status` IS NULL\n            )\n            AND (\n                UNIX_TIMESTAMP( `datetime` )\n                BETWEEN UNIX_TIMESTAMP( DATE_ADD( CURDATE( ) , INTERVAL - " . $seconds . "\n                SECOND ) )\n                AND UNIX_TIMESTAMP( DATE_ADD( CURDATE( ) , INTERVAL +86400\n                SECOND ) )\n            )\n        )";
    $defensio_result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($defensio_result)) {
        $document = array('client' => 'Pixelpost Defensio Addon | ' . $addon_version . ' | Schonhose | schonhose@pixelpost.org', 'content' => $row['message'], 'platform' => 'pixelpost', 'type' => 'comment', 'async' => 'true', 'async-callback' => $defensio_conf['blog'] . 'addons/_defensio2.0/lib/callback.php?id=' . md5($defensio_conf['key']), 'author-email' => $row['email'], 'author-ip' => $row['ip'], 'author-logged-in' => 'false', 'author-name' => $row['name'], 'parent-document-date' => defensio_get_datetime_post($row['parent_id']), 'parent-document-permalink' => $defensio_conf['blog'] . "index.php?showimage=" . $row['parent_id'], 'referrer' => $_SERVER['HTTP_REFERER'], 'author-url' => $row['url']);
        /**
         * Only continue with Defensio if the API key is valid
         */
        if (array_shift($defensio->getUser()) == 200) {
            $post_result = $defensio->postDocument($document);
            // we always do a NEW request here.
            defensio_process_comment_pixelpost($post_result, true, $row['id']);
        } else {
            die("The API key is invalid!!! Bye Bye.");
        }
    }
    // 2) get the pending comments. But those are a bit tricky: depending on the date we either have to GET
    // results or process them again.
    $query = "SELECT * FROM `{$pixelpost_db_prefix}comments` WHERE `status` = 'pending'";
    $defensio_result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($defensio_result)) {
        $document = array('client' => 'Pixelpost Defensio Addon | ' . $addon_version . ' | Schonhose | schonhose@pixelpost.org', 'content' => $row['message'], 'platform' => 'pixelpost', 'type' => 'comment', 'async' => 'true', 'async-callback' => $defensio_conf['blog'] . 'addons/_defensio2.0/lib/callback.php?id=' . md5($defensio_conf['key']), 'author-email' => $row['email'], 'author-ip' => $row['ip'], 'author-logged-in' => 'false', 'author-name' => $row['name'], 'parent-document-date' => defensio_get_datetime_post($row['parent_id']), 'parent-document-permalink' => $defensio_conf['blog'] . "index.php?showimage=" . $row['parent_id'], 'referrer' => $_SERVER['HTTP_REFERER'], 'author-url' => $row['url']);
        /**
         * Only continue with Defensio if the API key is valid
         */
        if (array_shift($defensio->getUser()) == 200) {
            // here is the magic to decide if we need to GET or process
            // if the difference is less than thirty days we can still get it from Defensio
            // if it is more, then reprocess the comment.
            $no_days = floor((time() - strtotime($row['datetime'])) / 86400);
            if ($no_days < 30) {
                $get_result = $defensio->getDocument($row['signature']);
                // we always try to get the results here.
                defensio_process_comment_pixelpost($get_result, false);
            } else {
                $post_result = $defensio->postDocument($document);
                // we always do a NEW request here.
                defensio_process_comment_pixelpost($post_result, true, $row['id']);
            }
        } else {
            die("The API key is invalid!!! Bye Bye.");
        }
    }
    $defensio_comments_processed_at = mktime();
    mysql_query("UPDATE " . $pixelpost_db_prefix . "defensio SET defensio_comments_processed_at='" . $defensio_comments_processed_at . "'");
}