Esempio n. 1
0
 public static function databaseAvailable()
 {
     self::$mDatabaseAvailableMessage = "AVAILABLE";
     $lMySQLConnection = null;
     $UNKNOWN_DATABASE = "Unknown database";
     $ACCESS_DENIED = "ccess denied for user";
     $USERNAME = self::$mMySQLDatabaseUsername;
     $PASSWORD = self::$mMySQLDatabasePassword;
     $SAMURAI_WTF_PASSWORD = "******";
     $HOSTNAME = self::$mMySQLDatabaseHost;
     $INCORRECT_DATABASE_CONFIGURATION_MESSAGE = "Error connecting to MySQL database on host '" . $HOSTNAME . "' with username '" . $USERNAME . "' and password '" . $PASSWORD . "'. First, try to reset the database (ResetDB button on menu). Next, check that the database service is running and that the database username, password, database name, and database location are configured correctly. Note: File /mutillidae/classes/MySQLHandler.php contains the database configuration.";
     $INCORRECT_DATABASE_CONFIGURATION_MESSAGE_SAMURAI = "Error connecting to MySQL database on host '" . $HOSTNAME . "' with username '" . $USERNAME . "' and password '" . $PASSWORD . "'. Note: In addition to the configured password '" . $PASSWORD . "', the password 'samurai' was tried as well. First, try to reset the database (ResetDB button on menu). Next, check that the database service is running and that the database username, password, database name, and database location are configured correctly. Note: File /mutillidae/classes/MySQLHandler.php contains the database configuration.";
     $UNKNOWN_DATABASE_MESSAGE = "Unable to select default database " . self::$mMySQLDatabaseName . ". It appears that the database to which Mutillidae is configured to connect has not been created. First, try to reset the database (ResetDB button on menu). Next, check that the database service is running and that the database username, password, database name, and database location are configured correctly. Note: File /mutillidae/classes/MySQLHandler.php contains the database configuration.";
     try {
         $lMySQLConnection = new mysqli($HOSTNAME, $USERNAME, $PASSWORD);
         if (strlen($lMySQLConnection->connect_error) > 0) {
             /* If error is "Access denied for user", it could just be an incorrect password. On samurai
              * the password is "samurai". Try that password. 
              */
             try {
                 $lMySQLConnection = new mysqli($HOSTNAME, $USERNAME, $SAMURAI_WTF_PASSWORD);
                 if (strlen($lMySQLConnection->connect_error) > 0) {
                     self::$mDatabaseAvailableMessage = $INCORRECT_DATABASE_CONFIGURATION_MESSAGE_SAMURAI . " Connection error: " . $lMySQLConnection->connect_error;
                     throw new Exception(self::$mDatabaseAvailableMessage);
                 }
                 // end if
             } catch (Exception $e) {
                 self::$mDatabaseAvailableMessage = $INCORRECT_DATABASE_CONFIGURATION_MESSAGE . " Connection error: " . $lMySQLConnection->connect_error;
                 throw new Exception(self::$mDatabaseAvailableMessage);
             }
         }
         // end if there was an error right away
         if (!$lMySQLConnection->select_db(self::$mMySQLDatabaseName)) {
             self::$mDatabaseAvailableMessage = $UNKNOWN_DATABASE_MESSAGE . " Connection error: " . $lMySQLConnection->connect_error;
             throw new Exception(self::$mDatabaseAvailableMessage);
         }
         //end if
         $lResult = $lMySQLConnection->query("SELECT 'test connection';");
         if (!$lResult) {
             self::$mDatabaseAvailableMessage = "Failed to execute test query on MySQL database but we appear to be connected " . $lMySQLConnection->error . "<br /><br />First, try to reset the database (ResetDB button on menu)<br /><br />Check if the database configuration is correct. If the system made it this far, the username and password are probably correct. Perhaps the database name is wrong.<br /><br />";
             throw new Exception(self::$mDatabaseAvailableMessage);
         }
         // end if
         $lResult = $lMySQLConnection->query("SELECT cid FROM blogs_table;");
         if (!$lResult) {
             self::$mDatabaseAvailableMessage = "Failed to execute test query on blogs_table in the MySQL database but we appear to be connected " . $lMySQLConnection->error . "<br /><br />First, try to reset the database (ResetDB button on menu)<br /><br />The blogs table should exist in the " . self::$mMySQLDatabaseName . " database if the database configuration is correct. If the system made it this far, the username and password are probably correct. Perhaps the database name is wrong.<br /><br />";
             throw new Exception(self::$mDatabaseAvailableMessage);
         }
         // end if
         $lMySQLConnection->close();
     } catch (Exception $e) {
         self::$mDatabaseAvailableMessage = "Failed to connect to MySQL database. " . $e->getMessage();
         throw new Exception(self::$mDatabaseAvailableMessage);
     }
     // end try
     return TRUE;
 }