/////////////////////////////////////////////////////////////////////
///                                                                //
// backupDB() - MySQL database backup utility                      //
//                                                                 //
// Requires:                                                       //
//   PHP 4.1.0 (or higher) with zlib support                       //
//   MySQL 3.22 (or higher)                                        //
//                                                                 //
// backupDB() by James Heinrich <info@silisoftware.com>            //
// available at http://www.silisoftware.com                        //
//                                                                 //
// This code is released under the GNU GPL:                        //
// http://www.gnu.org/copyleft/gpl.html                            //
//                                                                 //
// If you do use this code somewhere, send me an email and tell    //
// me how/where you used it.                                       //
//                                                                 //
/////////////////////////////////////////////////////////////////////
//                                                                 //
// Usage:                                                          //
//  * For interactive use, just follow the prompts.                //
//                                                                 //
//  * For cronjob use, call the script like this:                  //
//   (back up all databases & tables)                              //
//     backupDB.php?StartBackup=complete&nohtml=1                  //
//   (back up all tables in one database)                          //
//     backupDB.php?onlyDB=dbname&StartBackup=complete&nohtml=1    //
//   (back up one (or more) specific table(s))                     //
//     backupDB.php?StartBackup=complete                           //
//      &SelectedTables[dbname]=tablename                          //
//      &SelectedTables[otherdb]=othertable&nohtml=1               //
//   (using lynx browser that supports only one parameter)         //
//     backupDB.php?lynxauth=host.user.pass.db.standard.mailto     //
//                                                                 //
//  * You can substitute "StartBackup=standard" instead of         //
//   "StartBackup=complete" for inserts without field names        //
//                                                                 //
//                                                                 //
// Notes:                                                          //
//   * The syntax "CREATE TABLE `dbname`.`tablename`" requires     //
//     MySQL v3.22 or later                                        //
//   * The directory this script is used in (or where the output   //
//     files are written to) must be writable                      //
//                                                                ///
/////////////////////////////////////////////////////////////////////
///                                                                //
// Version History:                                                //
//                                                                 //
// 1.2.5a:  [June-19-2008]                                         //
//          * database/table name included in error reports        //
//          * bugfix: v1.2.5 default config shipped with admin     //
//            email hardcoded                                      //
//                                                                 //
// 1.2.5:   [June-15-2008]                                         //
//          * added support for PHP-bzip2                          //
//          * bugfix: better support for MySQL v4+ when not using  //
//            mysqldump (thanks barlowØsentex*net)                 //
//                                                                 //
// 1.2.4:   [September-13-2006]                                    //
//          * Bugfix: Set max email backup file size to 25% of     //
//            memory_limit (was 40% which was too much)            //
//            (thanks greybrow)                                    //
//                                                                 //
// 1.2.3:   [September-11-2006]                                    //
//          * Bugfix: Missing comma between key names              //
//            (thanks greybrow)                                    //
//                                                                 //
// 1.2.2:   [August-30-2006]                                       //
//          * Bugfix: Added missing key column sizes               //
//            (thanks greybrow)                                    //
//          * Bugfix: Added missing FULLTEXT index type keyword    //
//          * Bugfix: non-mysqldump bzip2 output disabled          //
//                                                                 //
// 1.2.1:   [August-03-2006]                                       //
//          Added OUTPUT_COMPRESSION_TYPE which now defaults to    //
//            bzip2  (thanks ahØartemis*dk)                        //
//          Changed GZIP_COMPRESSION_LEVEL to                      //
//            OUTPUT_COMPRESSION_LEVEL                             //
//                                                                 //
// 1.2.0:   [July-30-2006]                                         //
//          Added mysqldump code for local processing whereever    //
//            possible. This is an order of magnitude faster       //
//            (compare 35 seconds to 8.5 minutes in one test)      //
//            but has no progress output                           //
//          Seperated code into backupDB.functions.php and         //
//            backupDB.config.php                                  //
//          Added config GZIP_COMPRESSION_LEVEL                    //
//                                                                 //
// 1.1.33:  [February-17-2006]                                     //
//          Added MYSQL_RECONNECT_INTERVAL to prevent timeouts     //
//                                                                 //
// 1.1.32:  [December-9-2005]                                      //
//          * Bugfix: "0x" blobs broken for empty data             //
//            (thanks markoØvizio*biz)                             //
//                                                                 //
// 1.1.31:  [October-27-2005]                                      //
//          * Bugfix: "TYPE=___" fix for MySQL v4.x                //
//            (thanks bigpinsØlibertysurf*fr)                      //
//                                                                 //
// 1.1.30:  [August-25-2005]                                       //
//          Binary fields are now exported in hex string format    //
//            by default (configurable: $HexBLOBs)                 //
//          Numeric field values no longer quoted                  //
//          Presence of destination directory now checked at start //
//                                                                 //
// 1.1.29:  [August-19-2005]                                       //
//          Added $dbNameInCreate                                  //
//            (thanks geneØcapoferri*com)                          //
//          Added $CreateIfNotExists                               //
//          Added $ReplaceInto                                     //
//          * Bugfix: now ignore ISAM and other tables that report //
//            "The handler for the table doesn't support repair"   //
//            (thanks geneØcapoferri*com)                          //
//                                                                 //
// 1.1.28:  [June-18-2005]                                         //
//          Better support for servers that don't correctly set    //
//            $_SERVER['DOCUMENT_ROOT']                            //
//            (thanks brandolsetØgmail*com)                        //
//                                                                 //
// 1.1.27a: [March-08-2005]                                        //
//          Improved DHTML support                                 //
//          * Bugfix: previous backup download link was incorrect  //
//            (thanks bazil749Øhotmail*com)                        //
//                                                                 //
// 1.1.27:  [March-08-2005]                                        //
//          Added more intuitive form-based wizard, with easy      //
//            option for emailing backup as attachment             //
//          * Bugfix: EmailAttachment() wasn't wrapping base64     //
//            data at 76 characters, resulting in corrupt emails   //
//                                                                 //
// 1.1.26:  [March-07-2005]                                        //
//          Added new parameter "mailto" which if set (to any      //
//            value) will email the backup file to the admin email //
//            address. Note: this will only work for files that    //
//            are smaller than 40% of the available PHP memory     //
//            (thanks bazil749Øhotmail*com)                        //
//          Added optional 6th parameter for lynxauth: mailto      //
//          * Bugfix: NULL values are now exported as such         //
//            (thanks mapxØcentrum*cz)                             //
//                                                                 //
// 1.1.25:  [August-30-2004]                                       //
//          Changed default backup type to "standard" for lynxauth //
//          Added optional 5th parameter for lynxauth: backuptype  //
//                                                                 //
// 1.1.24:  [August-26-2004]                                       //
//          Added backticks around field names in INSERTs          //
//                                                                 //
// 1.1.23:  [August-11-2004]                                       //
//          Fixed missing 'KEY' for non-primary/unique indexes     //
//          Changed $LineTerminator to LINE_TERMINATOR             //
//          Added backticks around database/table name in CREATE   //
//                                                                 //
// 1.1.22:  [July-20-2004]                                         //
//          Better table error email reporting                     //
//          Allow HTML output (nohtml=0) with lynxauth             //
//          Split to backupDB.php and backupDB.txt                 //
//                                                                 //
// 1.1.21:  [June-09-2004]                                         //
//          No longer dies when table errors found & fixed         //
//                                                                 //
// 1.1.20:  [June-06-2004]                                         //
//          Added "lynxauth" parameter that allows you to pass all //
//            parameters in one value because the lynx browser     //
//            only supports one GETstring parameter/value pair.    //
//            The format is:                                       //
//            backupDB.php?lynxauth=host.user.pass.db              //
//                                                                 //
// 1.1.19:  [June-04-2004]                                         //
//          Now uses $_REQUEST rather than $_POST for hostname,    //
//            username and password to allow crontabbing of more   //
//            than one backup job when no master login available   //
//                                                                 //
// 1.1.18:  [February-22-2004]                                     //
//          * Bugfix: FULLTEXT indices were not being detected     //
//            (comparison was done on 'Comment' rather than        //
//            'Index_type' column)                                 //
//            (thanks fdellache1Ønoos*fr)                          //
//                                                                 //
// 1.1.17:  [February-05-2004]                                     //
//          For some database where the restricted user cannot     //
//            list any database (even those they have access to)   //
//            the backup was failing. DB_NAME (if defined) is now  //
//            taken as-is and no attempt to list available         //
//            databases is now made.                               //
//          Added configuration option STATS_INTERVAL              //
//                                                                 //
// 1.1.16:  [January-22-2004]                                      //
//          * Bugfix: Added TYPE and AUTO_INCREMENT values to      //
//            CREATE TABLE output                                  //
//            (thanks edwardkooØtime*net*my)                       //
//          * Bugfix: Added missing backtick characters around key //
//            field names                                          //
//          Changed default BACKTICKCHAR back to `                 //
//                                                                 //
// 1.1.15:  [January-21-2004]                                      //
//          * Added option to prompt for username/password instead //
//            of hardcoding it into the file                       //
//            (thanks edwardkooØtime*net*my)                       //
//          * Added CloseWindowOnFinish option for greater         //
//            compatability with Windows Task Manager              //
//            (thanks edwardkooØtime*net*my)                       //
//                                                                 //
// 1.1.14:  [January-07-2004]                                      //
//          * Added GETstring "onlyDB" parameter for all-tables in //
//            one database cronjob backup                          //
//          * Added usage documentation for calling for cronjob    //
//                                                                 //
// 1.1.13:  [September-08-2003]                                    //
//          * Tables of type HEAP are now excluded from backing up //
//            by default. New config option: $NeverBackupDBtypes   //
//            Thanks to bosshossØultimatedressage*com              //
//          * Bugfix: Selected-tables-only method did not set POST //
//            as method for form submit, so if you had a lot of    //
//            tables selected it exceeded the GETstring length     //
//            limits and would not submit.                         //
//          * New config option: $SuppressHTMLoutput, which can be //
//            passed as parameter 'nohtml', will prevent all HTML  //
//            output, mostly useful for running as a cron job.     //
//            Thanks to bosshossØultimatedressage*com              //
//          * ADMIN_EMAIL is now required                          //
//                                                                 //
// 1.1.12:  [May-04-2003]                                          //
//          * Bugfix: Selected-tables-only backup was broken with  //
//            register_globals == off                              //
//                                                                 //
// 1.1.11:  [January-28-2003]                                      //
//          Changed frequency of %-complete status info from 10%   //
//            of current table to every 1000 records               //
//                                                                 //
// 1.1.10:  [December-12-2002]                                     //
//          Added automatic detection of gzip functions, and run   //
//            in uncompressed mode if gzip functions not available //
//            WARNING! This can create very large files if you     //
//            have a large database (hundreds of MB possibly)      //
//                                                                 //
// 1.1.9:   [December-02-2002]                                     //
//          * Bugfix: Download-previous-backup link was pointing   //
//            to an absolute filename instead of a relative        //
//            filename meaning the file couldn't be downloaded via //
//            the provided link (except under Windows)             //
//          * Bugfix: REPAIR TABLE report was only showing table   //
//            errors, not results of the REPAIR                    //
//          Added FileSizeNiceDisplay() for consistant formatting  //
//            of file sizes                                        //
//          Added optional timestamping of backup filename         //
//                                                                 //
// 1.1.8:   [November-28-2002]                                     //
//          * Bugfix: FormattedTimeRemaining() had wrong numbers   //
//            when displaying "days"                               //
//          Replaced mysql_db_query() (which is deprecated) with   //
//            mysql_select_db() and mysql_query()                  //
//          Replaced all list()/each() with foreach()              //
//                                                                 //
// 1.1.7:   [November-26-2002]                                     //
//          * Bugfix: Complete rewrite of structure backup because //
//            FULLTEXT indexes weren't being described correctly   //
//          Include table structure backup in total time taken     //
//                                                                 //
// 1.1.6:   [November-13-2002]                                     //
//          Changed FixDBFields() to mysql_escape_string()         //
//            This should fix corrupted binary dumps               //
//          Changed default QUOTECHAR from double-quote to single  //
//          Changed default BACKTICKCHAR to nothing                //
//                                                                 //
// 1.1.5:   [November-06-2002]                                     //
//          * Bugfix: numerous undefined variable errors           //
//          Moved table structures to beginning of output file     //
//          Added backupDB() version info to head of output file   //
//          Added backticks for table names for insert statements  //
//          Added configurable BACKTICKCHAR and QUOTECHAR          //
//          Added optional "length" parameter to all gzwrite()'s   //
//          Added configurable UNIX/Windows/Mac - $LineTerminator  //
//          Changed $tablespercolumn to TABLES_PER_COL             //
//          Changed gzwrite() calls to only occur every            //
//            BUFFER_SIZE bytes, rather than a percentage-complete //
//            amount of each table, resulting in 20% faster speed  //
//                                                                 //
// 1.1.4:   [November-05-2002]                                     //
//          Support for register_globals = off                     //
//          Absolute pathnames for backup files                    //
//                                                                 //
// 1.1.3:   [February-28-2002]                                     //
//          * Bugfix: table name in selected-tables-only backup    //
//            had too many quotes                                  //
//          * Bugfix: seconds remaining was incorrect              //
//          Added version number display                           //
//          Added comment above data dump showing which data is    //
//            being dumped                                         //
//                                                                 //
// 1.1.2:   [January-23-2002]                                      //
//          Added configuration variable $tablespercolumn          //
//          Added automatic COLSPAN for database name based on     //
//            $tablespercolumn                                     //
//                                                                 //
// 1.1.1:   [January-18-2002]                                      //
//          * Bugfix: REPAIR TABLE syntax was incorrect            //
//          * Bugfix: rename() wasn't overwriting files under      //
//            Windows                                              //
//          Show database name as well as table name when checking //
//            tables                                               //
//                                                                 //
// 1.1.0:   [January-17-2002]                                      //
//          First public release                                   //
//          Support for multiple tables at once                    //
//          Auto-detection of which tables on a server are         //
//            accessible                                           //
//          Option to disable DHTML for Netscape                   //
//                                                                 //
// 1.0.x:   [2001]                                                 //
//          Never publicly released                                //
//                                                                ///
/////////////////////////////////////////////////////////////////////
///                                                                //
// Future Plans:                                                   //
//   Features:                                                     //
//     * Support for PHP Safe Mode with 30-second time limit       //
//     * Support non-MySQL databases                               //
//     * Support for non-compressed backups                        //
//     * Support for Netscape-style DHTML                          //
//                                                                 //
//   Known Bugs:                                                   //
//     * none (email info@silisoftware.com if you find one)        //
//                                                                ///
/////////////////////////////////////////////////////////////////////