Version: current

     1 <?php
     2 /**
     3  * This is a helper class for the SS installer.
     4  *
     5  * It does all the specific checking for MySQLDatabase
     6  * to ensure that the configuration is setup correctly.
     7  *
     8  * @package sapphire
     9  * @subpackage dev
    10  */
    11 class MySQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper {
    12
    13     /**
    14      * Ensure that the database function for connectivity is available.
    15      * If it is, we assume the PHP module for this database has been setup correctly.
    16      *
    17      * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
    18      * @return boolean
    19      */
    20     public function requireDatabaseFunctions($databaseConfig) {
    21         return (function_exists('mysql_connect')) ? true : false;
    22     }
    23
    24     /**
    25      * Ensure that the database server exists.
    26      * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
    27      * @return array Result - e.g. array('success' => true, 'error' => 'details of error')
    28      */
    29     public function requireDatabaseServer($databaseConfig) {
    30         $success = false;
    31         $error = '';
    32         $conn = @mysql_connect($databaseConfig['server'], null, null);
    33         if($conn || mysql_errno() < 2000) {
    34             $success = true;
    35         } else {
    36             $success = false;
    37             $error = mysql_error();
    38         }
    39         return array(
    40             'success' => $success,
    41             'error' => $error
    42         );
    43     }
    44
    45     /**
    46      * Ensure a database connection is possible using credentials provided.
    47      * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
    48      * @return array Result - e.g. array('success' => true, 'error' => 'details of error')
    49      */
    50     public function requireDatabaseConnection($databaseConfig) {
    51         $success = false;
    52         $error = '';
    53         $conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
    54         if($conn) {
    55             $success = true;
    56         } else {
    57             $success = false;
    58             $error = mysql_error();
    59         }
    60         return array(
    61             'success' => $success,
    62             'error' => $error
    63         );
    64     }
    65
    66     /**
    67      * Ensure that the database connection is able to use an existing database,
    68      * or be able to create one if it doesn't exist.
    69      *
    70      * @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
    71      * @return array Result - e.g. array('success' => true, 'alreadyExists' => 'true')
    72      */
    73     public function requireDatabaseOrCreatePermissions($databaseConfig) {
    74         $success = false;
    75         $alreadyExists = false;
    76         $conn = @mysql_connect($databaseConfig['server'], $databaseConfig['username'], $databaseConfig['password']);
    77         if(@mysql_select_db($databaseConfig['database'], $conn)) {
    78             $success = true;
    79             $alreadyExists = true;
    80         } else {
    81             if(@mysql_query("CREATE DATABASE testing123", $conn)) {
    82                 mysql_query("DROP DATABASE testing123", $conn);
    83                 $success = true;
    84                 $alreadyExists = false;
    85             }
    86         }
    87         return array(
    88             'success' => $success,
    89             'alreadyExists' => $alreadyExists
    90         );
    91     }
    92
    93 }
    94

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -