<?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_9() {
    global $dbh;
    
    if (index_exists("bayes_token", "bayes_token_idx1")) {
      return array(false, "Index bayes_token_idx1 already exists");
    } else {
      return array(true,"");
    }
  }
  
  function upgrade_9() {
    global $dbh;
    
    if (is_mysql()) {
        $sql_stmts = array(
                       "ALTER TABLE bayes_token ADD INDEX bayes_token_idx1 (token)",
                       "ALTER TABLE bayes_token ADD INDEX bayes_token_idx2 (id, atime)",
                       "ALTER TABLE maia_stats MODIFY total_suspected_ham_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_ham_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_wl_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_bl_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_suspected_spam_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_fp_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_fn_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_spam_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_virus_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_bad_header_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_banned_file_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats MODIFY total_oversized_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_ham_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_spam_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_virus_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_fp_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_fn_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_banned_file_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_bad_header_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_wl_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_bl_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                       "ALTER TABLE maia_stats_history MODIFY total_oversized_size BIGINT UNSIGNED DEFAULT '0' NOT NULL",
                     );
    } else {
      $sql_stmts = array(
                     "ALTER TABLE bayes_token ADD COLUMN token_b BYTEA NOT NULL DEFAULT ''",
                     "UPDATE bayes_token SET token_b = (decode(replace(token, '\\', '\\\\'), 'escape'))",
                     "ALTER TABLE bayes_token DROP CONSTRAINT bayes_token_pkey",
                     "ALTER TABLE bayes_token DROP COLUMN token",
                     "ALTER TABLE bayes_token RENAME COLUMN token_b TO token",
                     "ALTER TABLE bayes_token ADD CONSTRAINT bayes_token_pkey PRIMARY KEY(id, token)",
                     "CREATE INDEX bayes_token_idx1 ON bayes_token (token)",
                     "ALTER TABLE bayes_expire SET WITHOUT OIDS",
                     "ALTER TABLE bayes_global_vars SET WITHOUT OIDS",
                     "ALTER TABLE bayes_seen SET WITHOUT OIDS",
                     "ALTER TABLE bayes_token SET WITHOUT OIDS",
                     "ALTER TABLE bayes_vars SET WITHOUT OIDS",
                     "ALTER TABLE maia_stats ALTER COLUMN total_suspected_ham_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_ham_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_wl_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_bl_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_suspected_spam_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_fp_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_fn_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_spam_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_virus_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_bad_header_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_banned_file_size TYPE BIGINT",
                     "ALTER TABLE maia_stats ALTER COLUMN total_oversized_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_ham_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_spam_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_virus_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_fp_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_fn_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_banned_file_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_bad_header_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_wl_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_bl_size TYPE BIGINT",
                     "ALTER TABLE maia_stats_history ALTER COLUMN total_oversized_size TYPE BIGINT",
                   );
    }                
    foreach ($sql_stmts as $sql) {
       $result = $dbh->query($sql);
       if (PEAR::isError($result)) {
         $str = $result->getMessage() . " = [" . $sql . "]" ;
         /* return array(false, $result->getMessage()); */
         return array(false, $str );
       }
    }
    
    return array(true,"");
  }
  
  function post_check_9() {
    global $dbh;
    
    if (! index_exists("bayes_token", "bayes_token_idx1")) {
      return array(false, "Error creating index bayes_token_idx1");
    } else {
      return array(true,"");
    }
    
  }
  
?>
