]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/class-mysql.php
Improve support for non standard DB port
[Github/YOURLS.git] / includes / class-mysql.php
1 <?php
2
3 /**
4  * Pick the right DB class and return an instance
5  *
6  * @since 1.7
7  * @param string $extension Optional: user defined choice
8  * @return class $ydb DB class instance
9  */
10 function yourls_set_DB_driver( ) {
11
12         // Auto-pick the driver. Priority: user defined, then PDO, then mysqli, then mysql
13         if ( defined( 'YOURLS_DB_DRIVER' ) ) {
14                 $driver = strtolower( YOURLS_DB_DRIVER ); // accept 'MySQL', 'mySQL', etc
15         } elseif ( extension_loaded( 'pdo_mysql' ) ) {
16                 $driver = 'pdo';
17         } elseif ( extension_loaded( 'mysqli' ) ) {
18                 $driver = 'mysqli';
19         } elseif ( extension_loaded( 'mysql' ) ) {
20                 $driver = 'mysql';
21         } else {
22                 $driver = '';
23         }
24         
25         /**
26         mysql_connect( "$host:$port", $user, $pass,
27
28         new mysqli( $host, $user, $pass, $dbname, $port )
29         
30         $dsn = sprintf( 'mysql:host=%1$s;port=%2$d', $host, $port );
31         new PDO( $dsn, $user, $pass );
32         /**/
33
34         // Get the new driver
35         if ( in_array( $driver, array( 'mysql', 'mysqli', 'pdo' ) ) ) {
36                 require_once( YOURLS_INC . '/ezSQL/ez_sql_core.php' );
37                 require_once( YOURLS_INC . '/ezSQL/ez_sql_core_yourls.php' );
38                 require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '.php' );
39                 require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '_yourls.php' );
40         }
41         $class = 'ezSQL_' . $driver . '_yourls';
42
43         global $ydb;
44
45         if ( !class_exists( $class, false ) ) {
46                 $ydb = new stdClass();
47                 yourls_die(
48                         yourls__( 'YOURLS requires the mysql, mysqli or pdo_mysql PHP extension. No extension found. Check your server config, or contact your host.' ),
49                         yourls__( 'Fatal error' ),
50                         503
51                 );
52         }
53         
54         yourls_do_action( 'set_DB_driver', $driver );
55         
56         $port = 3306;
57         $host = YOURLS_DB_HOST;
58         if ( false !== strpos( YOURLS_DB_HOST, ':' ) ) {
59                 list( $host, $port ) = explode( ':', YOURLS_DB_HOST );
60                 if ( 'pdo' == 'driver' ) {
61                         $host = sprintf( '%1$s;port=%2$d', $host, $port );
62                 }
63         }
64         
65         $ydb = new $class( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, $host, $port );
66         $ydb->debug_log[] = "DB driver: $driver";
67 }
68