Install_Mysql_Db.object
上传用户:xiao730204
上传日期:2007-01-04
资源大小:141k
文件大小:8k
- <?php
- /*
- NOTES:
- JEO - I've turned on debugging for installation support debugging
- */
- class Install_Mysql_Db extends BaseObject {
- var $mysql_db;
- var $debug;
- var $sql_base;
- Function Install_Mysql_Db( $db_config = '' ) {
- $this->BaseObject( 'Install_Mysql_Db' );
- $this->mysql_db = new Mysql_Db( $db_config );
- $this->debug = new Debug();
- $this->mysql_db->debug->On();
- $this->debug->On();
- $this->sql_base = './sql/table/mysql';
- }
- Function Create_Prometheus_Db( $db_name, $db_tables, $db_dir ) {
- $this->debug->Message( 'Creating ' . $db_name );
- if ( $this->Create_Db( $db_name ) ) {
- $this->debug->Message( 'Database creation successful' );
- } else {
- $this->debug->Message( 'Database creation UN-successful' );
- }
- $this->mysql_db->db->db_name = $db_name;
- //$this->mysql_db->Close();
- reset( $db_tables );
- for( $i = 0; $i < count( $db_tables ); $i++ ) {
- $this->debug->Message( 'Table : ' . $db_tables[ $i ] );
- $sql_file = $db_dir . '/' . $db_tables[ $i ] . '.sql';
- $pre_sql_file = $db_dir . '/pre_' . $db_tables[ $i ] . '.sql';
- $post_sql_file = $db_dir . '/post_' . $db_tables[ $i ] . '.sql';
- if ( file_exists( $pre_sql_file ) ) {
- $this->debug->Message( 'Running pre sql file : ' . $pre_sql_file );
- $pre_sql_result = $this->Run_Sql( join( '', file( $pre_sql_file ) ) );
- if ( $pre_sql_result == 1 ) {
- $this->debug->Message( 'Pre script completed ok.' );
- } else {
- $this->debug->Message( 'Pre script FAILED.' );
- }
- } else {
- $this->debug->Message( 'PRE Does not exist : ' . $pre_sql_file );
- }
- if ( file_exists( $sql_file ) ) {
- $this->debug->Message( 'Creating Table : ' . $sql_file );
- $create_result = $this->Create_Table( join( '', file( $sql_file ) ) );
- if ( $create_result == 1 ) {
- $this->debug->Message( 'Table create completed ok.' );
- } else {
- $this->debug->Message( 'Table create FAILED.' );
- }
- } else {
- $this->debug->Message( 'Table sql Does not exist : ' . $pre_sql_file );
- }
- if ( file_exists( $post_sql_file ) ) {
- $this->debug->Message( 'Running post sql file : ' . $post_sql_file );
- $post_sql_result = $this->Run_Sql( join( '', file( $post_sql_file ) ) );
- if ( $post_sql_result == 1 ) {
- $this->debug->Message( 'Post script completed ok.' );
- } else {
- $this->debug->Message( 'Post script FAILED.' );
- }
- } else {
- $this->debug->Message( 'POST Does not exist : ' . $pre_sql_file );
- }
- }
- $this->debug->Message( 'Completed Creating ' . $db_name );
- }
- Function Run_Sql( $sql_text ) {
- if ( $this->mysql_db->Open() != 1 ) {
- return 0;
- }
- $sql_t = $this->mysql_db->Prepare( $sql_text );
- return $sql_t->Exec();
- }
- Function Create_Table( $table_spec ) {
- if ( $this->mysql_db->Open() != 1 ) {
- return 0;
- }
- $create_table = $this->mysql_db->Prepare( $table_spec );
- return $create_table->Exec();
- }
- Function Create_Db( $db_name ) {
- if ( $this->mysql_db->Open() != 1 ) {
- return 0;
- }
- $create_db = $this->mysql_db->Prepare( 'CREATE DATABASE ' . $db_name );
- return $create_db->Exec();
- }
- Function Create_User( $from_host, $user_name, $password ) {
- if ( $this->mysql_db->Open() != 1 ) {
- return 0;
- }
- $this->mysql_db->db->db_name = 'mysql';
- $rm_user = $this->mysql_db->Prepare(
- "DELETE FROM user WHERE user = '$user_name' AND host = '$from_host' " );
- $rm_user->Exec();
- $create_user = $this->mysql_db->Prepare(
- "INSERT INTO
- user ( host, user, password )
- VALUES
- ( '$from_host', '$user_name', password( '$password' ) )"
- );
- return $create_user->Exec();
- }
- Function Grant_Db_Privs( $from_host, $user_name, $database ) {
- if ( $this->mysql_db->Open() != 1 ) {
- return 0;
- }
- $this->mysql_db->db->db_name = 'mysql';
- $rm_privs = $this->mysql_db->Prepare(
- "DELETE FROM db WHERE user = '$user_name' AND host = '$from_host' db = '$database'"
- );
- $rm_privs->Exec();
- $create_priv = $this->mysql_db->Prepare( "
- INSERT INTO db (
- host, db, user,
- Select_priv, Insert_priv, Update_priv, Delete_priv,
- Create_priv, Drop_priv )
- VALUES (
- '$from_host', '$database', '$user_name',
- 'Y', 'Y', 'Y', 'Y',
- 'Y', 'Y'
- )
- " );
- return $create_priv->Exec();
- }
- Function Reload_Db_Privs() {
- if ( $this->mysql_db->Open() != 1 ) {
- return 0;
- }
- $this->mysql_db->db->db_name = 'mysql';
- $reload_privs = $this->mysql_db->Prepare( 'FLUSH PRIVILEGES' );
- return $reload_privs->Exec();
- }
- /* Start userspace :: user usable exported functions */
- Function Create_AdminPrivileges_Db() {
- $this->Create_Prometheus_Db(
- 'prometheus_privileges',
- Array( 'admin_privileges_table' ),
- $this->sql_base . '/AdminPrivileges'
- );
- }
- Function Create_CustomerInformation_Db() {
- $this->Create_Prometheus_Db(
- 'prometheus_customers',
- Array(
- 'address_table', 'ccard_table', 'customer_notes',
- 'customer_services_table', 'personal_table', 'phone_num_table',
- 'service_table', 'services_completed_table'
- ),
- $this->sql_base . '/CustomerInformation'
- );
- }
- Function Create_Domains_Db() {
- $this->Create_Prometheus_Db(
- 'prometheus_domains',
- Array( 'domain_table' ),
- $this->sql_base . '/Domain'
- );
- }
- Function Create_MailServerSettings_Db() {
- $this->Create_Prometheus_Db(
- 'mail_settings',
- Array( 'server_settings_table', 'user_signature_table' ),
- $this->sql_base . '/MailServerSettings'
- );
- }
- Function Create_Settings_Db() {
- $this->Create_Prometheus_Db(
- 'prometheus_settings',
- Array( 'user_theme_table' ),
- $this->sql_base . '/PrometheusSettings'
- );
- }
- Function Create_Sessions_Db() {
- $this->Create_Prometheus_Db(
- 'prometheus_sessions',
- Array( 'sessions_data_table', 'sessions_table' ),
- $this->sql_base . '/Sessions'
- );
- }
- Function Create_User_Db() {
- $this->Create_Prometheus_Db(
- 'prometheus_users',
- Array( 'user_table', 'user_theme_table' ),
- $this->sql_base . '/User'
- );
- }
- Function Create_ProUser() {
- if ( $this->Create_User( 'localhost', 'prouser', 'prouser' ) ) {
- $this->debug->Message( 'Created prouser okay' );
- } else {
- $this->debug->Message( 'Prouser create failed' );
- }
- $dbs_to_grant = Array(
- 'prometheus_users',
- 'prometheus_domains',
- 'prometheus_privileges',
- 'prometheus_sessions',
- 'prometheus_settings',
- 'mail_settings',
- 'prometheus_customers',
- 'vpopmail'
- );
- for( $i = 0; $i < count( $dbs_to_grant ); $i++ ) {
- if ( $this->Grant_Db_Privs( 'localhost', 'prouser', $dbs_to_grant[ $i ] ) ) {
- $this->debug->Message( 'Granted prouser - ' . $dbs_to_grant[ $i ] );
- } else {
- $this->debug->Message( 'Failed to grant prouser - ' . $dbs_to_grant[ $i ] );
- }
- }
- if ( $this->Reload_Db_Privs() ) {
- $this->debug->Message( 'Reloaded the permissions database' );
- } else {
- $this->debug->Message( 'Failed to reload the permissions database' );
- }
- }
- }
- ?>