protected function create_database_mysql()
 {
     //database connection
     $connect_string;
     if (strlen($this->global_settings->db_host()) == 0 && strlen($this->global_settings->db_port()) == 0) {
         //if both host and port are empty use the unix socket
         $connect_string = "mysql:host={$this->global_settings->db_host()};unix_socket=/var/run/mysqld/mysqld.sock;";
     } elseif (strlen($this->global_settings->db_port()) == 0) {
         //leave out port if it is empty
         $connect_string = "mysql:host={$this->global_settings->db_host()};";
     } else {
         $connect_string = "mysql:host={$this->global_settings->db_host()};port={$this->global_settings->db_port()};";
     }
     //create the table, user and set the permissions only if the db_create_username was provided
     if ($this->global_settings->db_create_username()) {
         $this->write_progress("\tCreating database");
         try {
             $this->dbh = new PDO($connect_string, $this->global_settings->db_create_username(), $this->global_settings->db_create_password(), array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
             $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
             $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
         } catch (PDOException $error) {
             throw new Exception("error connecting to database for ccreate: " . $error->getMessage() . "\n" . $sql);
         }
         //select the mysql database
         try {
             $this->dbh->query("USE mysql;");
         } catch (PDOException $error) {
             throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
         }
         //create user and set the permissions
         try {
             $tmp_sql = "GRANT SELECT ON " . $this->global_settings->db_name() . ".* TO '" . $this->global_settings->db_username() . "'@'%' IDENTIFIED BY '" . $this->global_settings->db_password() . "'; ";
             $this->dbh->query($tmp_sql);
         } catch (PDOException $error) {
             throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
         }
         //set account to unlimited use
         try {
             if ($this->global_settings->db_host() == "localhost" || $this->global_settings->db_host() == "127.0.0.1") {
                 $tmp_sql = "GRANT USAGE ON * . * TO '" . $this->global_settings->db_username() . "'@'localhost' ";
                 $tmp_sql .= "IDENTIFIED BY '" . $this->global_settings->db_password() . "' ";
                 $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; ";
                 $this->dbh->query($tmp_sql);
                 $tmp_sql = "GRANT USAGE ON * . * TO '" . $this->global_settings->db_username() . "'@'127.0.0.1' ";
                 $tmp_sql .= "IDENTIFIED BY '" . $this->global_settings->db_password() . "' ";
                 $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; ";
                 $this->dbh->query($tmp_sql);
             } else {
                 $tmp_sql = "GRANT USAGE ON * . * TO '" . $this->global_settings->db_username() . "'@'" . $this->global_settings->db_host() . "' ";
                 $tmp_sql .= "IDENTIFIED BY '" . $this->global_settings->db_password() . "' ";
                 $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; ";
                 $this->dbh->query($tmp_sql);
             }
         } catch (PDOException $error) {
             throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
         }
         //create the database and set the create user with permissions
         try {
             $tmp_sql = "CREATE DATABASE IF NOT EXISTS " . $this->global_settings->db_name() . "; ";
             $this->dbh->query($tmp_sql);
         } catch (PDOException $error) {
             throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
         }
         //set user permissions
         try {
             $this->dbh->query("GRANT ALL PRIVILEGES ON " . $this->global_settings->db_name() . ".* TO '" . $this->global_settings->db_username() . "'@'%'; ");
         } catch (PDOException $error) {
             throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
         }
         //make the changes active
         try {
             $tmp_sql = "FLUSH PRIVILEGES; ";
             $this->dbh->query($tmp_sql);
         } catch (PDOException $error) {
             throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
         }
         $this->dbh = null;
     }
     //if (strlen($this->global_settings->db_create_username()) > 0)
     $this->write_progress("\tInstalling data to database");
     //select the database
     try {
         $this->dbh = new PDO($connect_string, $this->global_settings->db_username(), $this->global_settings->db_password(), array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
         $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $this->dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
     } catch (PDOException $error) {
         throw new Exception("error connecting to database: " . $error->getMessage() . "\n" . $sql);
     }
     try {
         $this->dbh->query("USE " . $this->global_settings->db_name() . ";");
     } catch (PDOException $error) {
         throw new Exception("error in database: " . $error->getMessage() . "\n" . $sql);
     }
     //add the database structure
     require_once "resources/classes/schema.php";
     $schema = new schema();
     $schema->db = $this->dbh;
     $schema->db_type = $this->global_settings->db_type();
     $schema->sql();
     $schema->exec();
     //add the defaults data into the database
     //get the contents of the sql file
     if (file_exists('/usr/share/examples/fusionpbx/resources/install/sql/mysql.sql')) {
         $filename = "/usr/share/examples/fusionpbx/resources/install/sql/mysql.sql";
     } else {
         $filename = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . '/resources/install/sql/mysql.sql';
     }
     $file_contents = file_get_contents($filename);
     //replace \r\n with \n then explode on \n
     $file_contents = str_replace("\r\n", "\n", $file_contents);
     //loop line by line through all the lines of sql code
     $string_array = explode("\n", $file_contents);
     $x = 0;
     foreach ($string_array as $sql) {
         if (strlen($sql) > 3) {
             try {
                 if ($this->debug) {
                     $this->write_debug($sql . "\n");
                 }
                 $this->dbh->query($sql);
             } catch (PDOException $error) {
                 //echo "error on line $x: " . $error->getMessage() . " sql: $sql<br/>";
                 //die();
             }
         }
         $x++;
     }
     unset($file_contents, $sql);
 }
Exemple #2
0
 //if (strlen($db_create_username) > 0)
 //select the database
 try {
     $db_tmp->query("USE " . $db_name . ";");
 } catch (PDOException $error) {
     if ($v_debug) {
         print "error: " . $error->getMessage() . "<br/>";
     }
 }
 //add the database structure
 require_once "resources/classes/schema.php";
 $schema = new schema();
 $schema->db = $db_tmp;
 $schema->db_type = $db_type;
 $schema->sql();
 $schema->exec();
 //add the defaults data into the database
 //get the contents of the sql file
 if (file_exists('/usr/share/examples/fusionpbx/resources/install/sql/mysql.sql')) {
     $filename = "/usr/share/examples/fusionpbx/resources/install/sql/mysql.sql";
 } else {
     $filename = $_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . '/resources/install/sql/mysql.sql';
 }
 $file_contents = file_get_contents($filename);
 //replace \r\n with \n then explode on \n
 $file_contents = str_replace("\r\n", "\n", $file_contents);
 //loop line by line through all the lines of sql code
 $string_array = explode("\n", $file_contents);
 $x = 0;
 foreach ($string_array as $sql) {
     if (strlen($sql) > 3) {