<?php
   /*
     * $Id$
     *
     * MAIA MAILGUARD LICENSE v.1.0
     *
     * Copyright 2004 by Robert LeBlanc <rjl@renaissoft.com>
     *                   David Morton <mortonda@dgrmm.net>
     * All rights reserved.
     *
     * PREAMBLE
     *
     * This License is designed for users of Maia Mailguard
     * ("the Software") who wish to support the Maia Mailguard project by
     * leaving "Maia Mailguard" branding information in the HTML output
     * of the pages generated by the Software, and providing links back
     * to the Maia Mailguard home page.  Users who wish to remove this
     * branding information should contact the copyright owner to obtain
     * a Rebranding License.
     *
     * DEFINITION OF TERMS
     *
     * The "Software" refers to Maia Mailguard, including all of the
     * associated PHP, Perl, and SQL scripts, documentation files, graphic
     * icons and logo images.
     *
     * GRANT OF LICENSE
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions
     * are met:
     *
     * 1. Redistributions of source code must retain the above copyright
     *    notice, this list of conditions and the following disclaimer.
     *
     * 2. Redistributions in binary form must reproduce the above copyright
     *    notice, this list of conditions and the following disclaimer in the
     *    documentation and/or other materials provided with the distribution.
     *
     * 3. The end-user documentation included with the redistribution, if
     *    any, must include the following acknowledgment:
     *
     *    "This product includes software developed by Robert LeBlanc
     *    <rjl@renaissoft.com>."
     *
     *    Alternately, this acknowledgment may appear in the software itself,
     *    if and wherever such third-party acknowledgments normally appear.
     *
     * 4. At least one of the following branding conventions must be used:
     *
     *    a. The Maia Mailguard logo appears in the page-top banner of
     *       all HTML output pages in an unmodified form, and links
     *       directly to the Maia Mailguard home page; or
     *
     *    b. The "Powered by Maia Mailguard" graphic appears in the HTML
     *       output of all gateway pages that lead to this software,
     *       linking directly to the Maia Mailguard home page; or
     *
     *    c. A separate Rebranding License is obtained from the copyright
     *       owner, exempting the Licensee from 4(a) and 4(b), subject to
     *       the additional conditions laid out in that license document.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     *
     */

  function pre_check_3() {
    if (table_exists("bayes_token")) {
      return array(false, "Already exists");
    } else {
      return array(true,"");
    }
  }
  
  function upgrade_3() {
    global $dbh;
    
    if (is_mysql()) {
        $sql_stmts = array(
                     "CREATE TABLE awl ( " .
                     "username varchar(100) NOT NULL default '',  " .
                     "email varchar(200) NOT NULL default '',  " .
                     "ip varchar(10) NOT NULL default '',  " .
                     "count int(11) default '0', " .
                     "totscore float default '0', " .
                     "PRIMARY KEY (username, email, ip) " . 
                     ") TYPE=InnoDB",
                     
                     "CREATE TABLE bayes_expire (  " .
                     "id int(11) NOT NULL default '0', " . 
                     "runtime int(11) NOT NULL default '0', " .
                     "KEY bayes_expire_idx1 (id) " .  
                     ") TYPE=InnoDB",
                                    
                     "CREATE TABLE bayes_global_vars ( " .
                     "variable varchar(30) NOT NULL default '', " .  
                     "value varchar(200) NOT NULL default '', " .  
                     "PRIMARY KEY (variable) " .  
                     ") TYPE=InnoDB",
                     
                     "CREATE TABLE bayes_seen ( " . 
                     "id int(11) NOT NULL default '0', " .
                     "msgid varchar(200) binary NOT NULL default '', " . 
                     "flag char(1) NOT NULL default '', " . 
                     "PRIMARY KEY (id, msgid) " .
                     ") TYPE=InnoDB",
    
                     "CREATE TABLE bayes_token ( " .
                     "id int(11) NOT NULL default '0', " .  
                     "token char(5) NOT NULL default '', " .  
                     "spam_count int(11) NOT NULL default '0', " .  
                     "ham_count int(11) NOT NULL default '0', " . 
                     "atime int(11) NOT NULL default '0', " .  
                     "PRIMARY KEY (id, token) " .  
                     ") TYPE=InnoDB",
                     
                     "CREATE TABLE bayes_vars ( " .
                     "id int(11) NOT NULL AUTO_INCREMENT, " .
                     "username varchar(200) NOT NULL default '', " . 
                     "spam_count int(11) NOT NULL default '0', " .  
                     "ham_count int(11) NOT NULL default '0', " .  
                     "token_count int(11) NOT NULL default '0', " .  
                     "last_expire int(11) NOT NULL default '0', " .  
                     "last_atime_delta int(11) NOT NULL default '0', " .  
                     "last_expire_reduce int(11) NOT NULL default '0', " . 
                     "oldest_token_age int(11) NOT NULL default '2147483647', " .  
                     "newest_token_age int(11) NOT NULL default '0', " .  
                     "PRIMARY KEY (id), " .  
                     "UNIQUE bayes_vars_idx1 (username) " .  
                     ") TYPE=InnoDB"
                     
                    );
    } else {
      $sql_stmts = array("CREATE TABLE awl (
                          username 	VARCHAR(100) NOT NULL DEFAULT '',
                          email 	VARCHAR(200) NOT NULL DEFAULT '',
                          ip 		VARCHAR(10) NOT NULL DEFAULT '',
                          count 	BIGINT DEFAULT '0',
                          totscore 	FLOAT DEFAULT '0'
                          )",
                          
                         "CREATE UNIQUE INDEX awl_pkey ON awl (username, email, ip)",

                         "CREATE TABLE bayes_expire (
                          id 		INTEGER NOT NULL DEFAULT '0',
                          runtime 	INTEGER NOT NULL DEFAULT '0'
                         )",
                         "CREATE INDEX bayes_expire_idx1 ON bayes_expire (id)",

                        "CREATE TABLE bayes_global_vars (
                          variable 	VARCHAR(30) NOT NULL DEFAULT '',
                          value 	VARCHAR(200) NOT NULL DEFAULT '',
                          PRIMARY KEY (variable)
                        )",

                        "CREATE TABLE bayes_seen (
                          id 		INTEGER NOT NULL DEFAULT '0',
                          msgid 	VARCHAR(200) NOT NULL DEFAULT '',
                          flag 		CHAR(1) NOT NULL DEFAULT '',
                          PRIMARY KEY (id, msgid)
                        )",

                        "CREATE TABLE bayes_token (
                          id 		INTEGER NOT NULL DEFAULT '0',
                          token 	CHAR(5) NOT NULL DEFAULT '',
                          spam_count 	INTEGER NOT NULL DEFAULT '0',
                          ham_count 	INTEGER NOT NULL DEFAULT '0',
                          atime 	INTEGER NOT NULL DEFAULT '0',
                          PRIMARY KEY (id, token)
                        )",

                        "CREATE TABLE bayes_vars (
                          id 			SERIAL NOT NULL,
                          username 		VARCHAR(200) NOT NULL DEFAULT '',
                          spam_count 		INTEGER NOT NULL DEFAULT '0',
                          ham_count 		INTEGER NOT NULL DEFAULT '0',
                          token_count 		INTEGER NOT NULL DEFAULT '0',
                          last_expire 		INTEGER NOT NULL DEFAULT '0',
                          last_atime_delta 	INTEGER NOT NULL DEFAULT '0',
                          last_expire_reduce 	INTEGER NOT NULL DEFAULT '0',
                          oldest_token_age 	INTEGER NOT NULL DEFAULT '2147483647',
                          newest_token_age 	INTEGER NOT NULL DEFAULT '0',
                          PRIMARY KEY  (id)
                        )",
                        "CREATE INDEX bayes_vars_idx1 ON bayes_vars (username)"
                      );
    }                
    foreach ($sql_stmts as $sql) {
       $result = $dbh->query($sql);
       if (PEAR::isError($result)) {
         return array(false, $result->getMessage()); 
       }
    }
        
    $sql = "INSERT INTO bayes_global_vars VALUES (?, ?)";

    $sth = $dbh->prepare($sql);
    
    $params = array(
                  array("Version", 3)
              );
    
    foreach ($params as $p) {
      $result = $dbh->execute($sth,$p);
      if (PEAR::isError($result)) {
         return array(false, $result->getMessage()); 
      }
    }
    
    return array(true,"");
  }
  
  function post_check_3() {
    if (! column_exists("bayes_vars", "ham_count")) {
      return array(false, "Error creating");
    } else {
      return array(true,"");
    }
    
  }  
?>
