]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/class-mysql.php
Merge branch 'master' of https://github.com/YOURLS/YOURLS
[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         // Set the new driver
26         if ( in_array( $driver, array( 'mysql', 'mysqli', 'pdo' ) ) ) {
27                 require_once( YOURLS_INC . '/ezSQL/ez_sql_core.php' );
28                 require_once( YOURLS_INC . '/ezSQL/ez_sql_core_yourls.php' );
29                 require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '.php' );
30                 require_once( YOURLS_INC . '/ezSQL/ez_sql_' . $driver . '_yourls.php' );
31         }
32         $class = 'ezSQL_' . $driver . '_yourls';
33
34         global $ydb;
35
36         if ( !class_exists( $class, false ) ) {
37                 $ydb = new stdClass();
38                 yourls_die(
39                         yourls__( 'YOURLS requires the mysql, mysqli or pdo_mysql PHP extension. No extension found. Check your server config, or contact your host.' ),
40                         yourls__( 'Fatal error' ),
41                         503
42                 );
43         }
44         
45         yourls_do_action( 'set_DB_driver', $driver );
46                 
47         $ydb = new $class( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
48     $ydb->DB_driver = $driver;
49
50         yourls_debug_log( "DB driver: $driver" );
51 }
52
53 /**
54  * Connect to DB
55  *
56  * @since 1.0
57  */
58 function yourls_db_connect() {
59         global $ydb;
60
61         if (   !defined( 'YOURLS_DB_USER' )
62                 or !defined( 'YOURLS_DB_PASS' )
63                 or !defined( 'YOURLS_DB_NAME' )
64                 or !defined( 'YOURLS_DB_HOST' )
65         ) yourls_die ( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 ); 
66
67         // Are we standalone or in the WordPress environment?
68         if ( class_exists( 'wpdb', false ) ) {
69                 /* TODO: should we deprecate this? Follow WP dev in that area */
70                 $ydb =  new wpdb( YOURLS_DB_USER, YOURLS_DB_PASS, YOURLS_DB_NAME, YOURLS_DB_HOST );
71         } else {
72                 yourls_set_DB_driver();
73         }
74         
75         return $ydb;
76 }
77
78 /**
79  * Return true if DB server is responding
80  *
81  * This function is supposed to be called right after yourls_get_all_options() has fired. It is not designed (yet) to
82  * check for a responding server after several successful operation to check if the server has gone MIA
83  *
84  * @since 1.7.1
85  */
86 function yourls_is_db_alive() {
87     global $ydb;
88     
89     $alive = false;
90     switch( $ydb->DB_driver ) {
91         case 'pdo' :
92             $alive = isset( $ydb->dbh );
93             break;
94     
95         case 'mysql' :
96             $alive = ( isset( $ydb->dbh ) && false !== $ydb->dbh );
97             break;
98     
99         case 'mysqli' :
100             $alive = ( null == mysqli_connect_error() );
101             break;
102         
103         // Custom DB driver & class : delegate check
104         default:
105             $alive = yourls_apply_filter( 'is_db_alive_custom', false );
106     }
107     
108     return $alive;
109 }
110
111 /**
112  * Die with a DB error message
113  *
114  * @TODO in version 1.8 : use a new localized string, specific to the problem (ie: "DB is dead")
115  *
116  * @since 1.7.1
117  */
118 function yourls_db_dead() {
119     // Use any /user/db_error.php file
120     if( file_exists( YOURLS_USERDIR . '/db_error.php' ) ) {
121         include_once( YOURLS_USERDIR . '/db_error.php' );
122         die();
123     }
124
125     yourls_die( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );
126 }