Install_Mysql_Db.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:8k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. /*
  3. NOTES:
  4. JEO - I've turned on debugging for installation support debugging 
  5. */
  6. class Install_Mysql_Db extends BaseObject {
  7.    var $mysql_db;
  8.    var $debug;
  9.    var $sql_base;
  10.    Function Install_Mysql_Db( $db_config = '' ) {
  11.       $this->BaseObject( 'Install_Mysql_Db' );
  12.       $this->mysql_db      = new Mysql_Db( $db_config );
  13.       $this->debug         = new Debug();
  14.       $this->mysql_db->debug->On();
  15.       $this->debug->On();
  16.       $this->sql_base      = './sql/table/mysql';
  17.    }
  18.    Function Create_Prometheus_Db( $db_name, $db_tables, $db_dir ) {
  19.       $this->debug->Message( 'Creating ' . $db_name );
  20.       if ( $this->Create_Db( $db_name ) ) {
  21.          $this->debug->Message( 'Database creation successful' );
  22.       } else {
  23.          $this->debug->Message( 'Database creation UN-successful' );
  24.       }
  25.       $this->mysql_db->db->db_name = $db_name;
  26.       //$this->mysql_db->Close();
  27.       reset( $db_tables );
  28.       for( $i = 0; $i < count( $db_tables ); $i++ ) {
  29.          $this->debug->Message( 'Table : ' . $db_tables[ $i ] );
  30.          $sql_file         = $db_dir . '/' . $db_tables[ $i ] . '.sql';
  31.          $pre_sql_file     = $db_dir . '/pre_'  . $db_tables[ $i ] . '.sql';
  32.          $post_sql_file    = $db_dir . '/post_' . $db_tables[ $i ] . '.sql';
  33.          if ( file_exists( $pre_sql_file ) ) {
  34.             $this->debug->Message( 'Running pre sql file : ' . $pre_sql_file );
  35.             $pre_sql_result = $this->Run_Sql( join( '', file( $pre_sql_file ) ) );
  36.             if ( $pre_sql_result == 1 ) {
  37.                $this->debug->Message( 'Pre script completed ok.' );
  38.             } else {
  39.                $this->debug->Message( 'Pre script FAILED.' );
  40.             }
  41.          } else {
  42.             $this->debug->Message( 'PRE Does not exist : ' . $pre_sql_file );
  43.          }
  44.          if ( file_exists( $sql_file ) ) {
  45.             $this->debug->Message( 'Creating Table : ' . $sql_file );
  46.             $create_result = $this->Create_Table( join( '', file( $sql_file ) ) );
  47.             if ( $create_result == 1 ) {
  48.                $this->debug->Message( 'Table create completed ok.' );
  49.             } else {
  50.                $this->debug->Message( 'Table create FAILED.' );
  51.             }
  52.          } else {
  53.             $this->debug->Message( 'Table sql Does not exist : ' . $pre_sql_file );
  54.          }
  55.          if ( file_exists( $post_sql_file ) ) {
  56.             $this->debug->Message( 'Running post sql file : ' . $post_sql_file );
  57.             $post_sql_result = $this->Run_Sql( join( '', file( $post_sql_file ) ) );
  58.             if ( $post_sql_result == 1 ) {
  59.                $this->debug->Message( 'Post script completed ok.' );
  60.             } else {
  61.                $this->debug->Message( 'Post script FAILED.' );
  62.             }
  63.          } else {
  64.             $this->debug->Message( 'POST Does not exist : ' . $pre_sql_file );
  65.          }
  66.       }
  67.       $this->debug->Message( 'Completed Creating ' . $db_name );
  68.    }
  69.    Function Run_Sql( $sql_text ) {
  70.       if ( $this->mysql_db->Open() != 1 ) {
  71.          return 0;
  72.       }
  73.       $sql_t = $this->mysql_db->Prepare( $sql_text );
  74.       return $sql_t->Exec();
  75.    }
  76.    Function Create_Table( $table_spec ) {
  77.       if ( $this->mysql_db->Open() != 1 ) {
  78.          return 0;
  79.       }
  80.       $create_table = $this->mysql_db->Prepare( $table_spec );
  81.       return $create_table->Exec();
  82.    }
  83.    Function Create_Db( $db_name ) {
  84.       if ( $this->mysql_db->Open() != 1 ) {
  85.          return 0;
  86.       }
  87.       $create_db = $this->mysql_db->Prepare( 'CREATE DATABASE ' . $db_name );
  88.       return $create_db->Exec();
  89.    }
  90.    Function Create_User( $from_host, $user_name, $password ) {
  91.       if ( $this->mysql_db->Open() != 1 ) {
  92.          return 0;
  93.       }
  94.       $this->mysql_db->db->db_name = 'mysql';
  95.       $rm_user = $this->mysql_db->Prepare( 
  96. "DELETE FROM user WHERE user = '$user_name' AND host = '$from_host' " );
  97.       $rm_user->Exec();
  98.       $create_user = $this->mysql_db->Prepare(
  99. "INSERT INTO 
  100. user ( host, user, password ) 
  101. VALUES 
  102. ( '$from_host', '$user_name', password( '$password' ) )"
  103.       );
  104.       return $create_user->Exec();
  105.    }
  106.    Function Grant_Db_Privs( $from_host, $user_name, $database ) {
  107.       if ( $this->mysql_db->Open() != 1 ) {
  108.          return 0;
  109.       }
  110.       $this->mysql_db->db->db_name = 'mysql';
  111.       $rm_privs = $this->mysql_db->Prepare(
  112. "DELETE FROM db WHERE user = '$user_name' AND host = '$from_host' db = '$database'"
  113.       );
  114.       $rm_privs->Exec();
  115.       $create_priv = $this->mysql_db->Prepare( "
  116. INSERT INTO db (
  117.       host,          db,                           user,
  118.       Select_priv,   Insert_priv,   Update_priv,   Delete_priv,
  119.       Create_priv,   Drop_priv )
  120.       VALUES (
  121.       '$from_host',   '$database',           '$user_name',
  122.       'Y',           'Y',           'Y',           'Y',
  123.       'Y',           'Y' 
  124. )
  125. " );
  126.       return $create_priv->Exec();
  127.    }
  128.    Function Reload_Db_Privs() {
  129.       if ( $this->mysql_db->Open() != 1 ) {
  130.          return 0;
  131.       }
  132.       $this->mysql_db->db->db_name = 'mysql';
  133.       $reload_privs = $this->mysql_db->Prepare( 'FLUSH PRIVILEGES' );
  134.       return $reload_privs->Exec();
  135.    }
  136.    /* Start userspace :: user usable exported functions */
  137.    Function Create_AdminPrivileges_Db() {
  138.       $this->Create_Prometheus_Db(
  139.          'prometheus_privileges',
  140.          Array( 'admin_privileges_table' ),
  141.          $this->sql_base . '/AdminPrivileges'
  142.       );
  143.    }
  144.    Function Create_CustomerInformation_Db() {
  145.       $this->Create_Prometheus_Db(
  146.          'prometheus_customers',
  147.          Array( 
  148.          'address_table',           'ccard_table',          'customer_notes', 
  149.          'customer_services_table', 'personal_table',       'phone_num_table',
  150.          'service_table',           'services_completed_table'
  151.          ),
  152.          $this->sql_base . '/CustomerInformation'
  153.       );
  154.    }
  155.    Function Create_Domains_Db() {
  156.       $this->Create_Prometheus_Db(
  157.          'prometheus_domains',
  158.          Array( 'domain_table' ),
  159.          $this->sql_base . '/Domain'
  160.       );
  161.    }
  162.    Function Create_MailServerSettings_Db() {
  163.       $this->Create_Prometheus_Db(
  164.          'mail_settings',
  165.          Array( 'server_settings_table', 'user_signature_table' ),
  166.          $this->sql_base . '/MailServerSettings'
  167.       );
  168.    }
  169.    Function Create_Settings_Db() {
  170.       $this->Create_Prometheus_Db(
  171.          'prometheus_settings',
  172.          Array( 'user_theme_table' ),
  173.          $this->sql_base . '/PrometheusSettings'
  174.       );
  175.    }
  176.    Function Create_Sessions_Db() {
  177.       $this->Create_Prometheus_Db(
  178.          'prometheus_sessions',
  179.          Array( 'sessions_data_table', 'sessions_table' ),
  180.          $this->sql_base . '/Sessions'
  181.       );
  182.    }
  183.    Function Create_User_Db() {
  184.       $this->Create_Prometheus_Db(
  185.          'prometheus_users',
  186.          Array( 'user_table', 'user_theme_table' ),
  187.          $this->sql_base . '/User'
  188.       );
  189.    }
  190.    Function Create_ProUser() {
  191.       if ( $this->Create_User( 'localhost', 'prouser', 'prouser' ) ) {
  192.          $this->debug->Message( 'Created prouser okay' );
  193.       } else {
  194.          $this->debug->Message( 'Prouser create failed' );
  195.       }
  196.       $dbs_to_grant = Array(
  197.          'prometheus_users',
  198.          'prometheus_domains',
  199.          'prometheus_privileges',
  200.          'prometheus_sessions',
  201.          'prometheus_settings',
  202.          'mail_settings',
  203.          'prometheus_customers',
  204.          'vpopmail'
  205.       );
  206.       for( $i = 0; $i < count( $dbs_to_grant ); $i++ ) {
  207.          if ( $this->Grant_Db_Privs( 'localhost', 'prouser', $dbs_to_grant[ $i ] ) ) {
  208.             $this->debug->Message( 'Granted prouser - ' . $dbs_to_grant[ $i ] );
  209.          } else {
  210.             $this->debug->Message( 'Failed to grant prouser - ' . $dbs_to_grant[ $i ] );
  211.          }
  212.       }
  213.       if ( $this->Reload_Db_Privs() ) {
  214.          $this->debug->Message( 'Reloaded the permissions database' );
  215.       } else {
  216.          $this->debug->Message( 'Failed to reload the permissions database' );
  217.       }
  218.    }
  219. }
  220. ?>