Last active
December 20, 2015 13:29
-
-
Save lknight/6139615 to your computer and use it in GitHub Desktop.
Plugin for wordpress to make amazon_rds database accessible in wordpress. It provides special object $wp_rds_db.
Supports MYSQL flats, including MYSQL_CLIENT_SSL. Support ssl to AMAZON RDS servers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Plugin Name: DW RDS_DB object | |
Description: provides special object $wp_rds_db access external database - in this example - AMAZON RDS DataBase. | |
Plugin URI: http://pvp.alvegia.ru | |
Author: Pavel G. Roytberg | |
Author URI: http://onga.ru | |
Version: 0.9 no more passwords in plain texts in php code | |
USAGE: just declare $global $wp_rds_db and use it with all the wordpress class wpdb has to offer. | |
USAGE#2: You also can use custom_ssl_wpdb::getInstance(); instead of global. | |
EXAMPLE1: $wp_rds_db = custom_ssl_wpdb::getInstance(); | |
EXAMPLE2: GLOBAL $wp_rds_db; | |
*/ | |
//IT RUNS EVERY LOADED PAGE. SO PROVIDE THIS INSTANCE OF WPDB CLASS TO THE WHOLE WORDPRESS. | |
// It costs you near 65 ms per RDS connection and 140ms if connection is SSLed. Choice is yours. | |
//define all the params | |
apply_filters('debug', 'RDS_DB plugin start'); | |
////thanks to Otto42 for idea | |
/** | |
* Copy of the db_connect function with definable newlink and client_flags params | |
*/ | |
/** | |
* Connect to and select database | |
* | |
* @since 3.0.0 | |
*/ | |
class custom_ssl_wpdb extends wpdb { | |
protected static $instance = null; | |
function db_connect() { | |
$this->is_mysql = true; | |
$new_link = defined( 'MYSQL_NEW_LINK2' ) ? MYSQL_NEW_LINK2 : true; | |
$client_flags = defined( 'MYSQL_CLIENT_FLAGS2' ) ? MYSQL_CLIENT_FLAGS2 : 0; | |
if ( WP_DEBUG ) { | |
$this->dbh = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); | |
} else { | |
$this->dbh = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpassword, $new_link, $client_flags ); | |
} | |
error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR ); | |
if ( !$this->dbh ) { | |
wp_load_translations_early(); | |
$this->bail( sprintf( __( " | |
<h1>Error establishing a database connection</h1> | |
<p>This either means that the username and password information in your <code>wp-config.php</code> file is incorrect or we can't contact the database server at <code>%s</code>. This could mean your host's database server is down.</p> | |
<ul> | |
<li>Are you sure you have the correct username and password?</li> | |
<li>Are you sure that you have typed the correct hostname?</li> | |
<li>Are you sure that the database server is running?</li> | |
</ul> | |
<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p> | |
" ), htmlspecialchars( $this->dbhost, ENT_QUOTES ) ), 'db_connect_fail' ); | |
return; | |
} | |
$this->set_charset( $this->dbh ); | |
$this->ready = true; | |
$this->select( $this->dbname, $this->dbh ); | |
} | |
public static function getInstance() | |
{ | |
if (!self::$instance) { | |
self::$instance = new custom_ssl_wpdb(DB_RDS_USER, DB_RDS_PASSWORD, DB_RDS_NAME, DB_RDS_HOST); | |
} | |
return self::$instance; | |
} | |
} | |
add_action('admin_menu', 'dw_rdsdb_plugin_settings'); | |
function dw_rdsdb_plugin_settings() { | |
add_menu_page('RDS DB Connection Settings', 'RDS_DB Settings', 'administrator', 'rds_db_settings', 'rds_db_display_settings'); | |
} | |
//apply_filters('debug', 'This is a checkpoint'); | |
function rds_db_display_settings() { | |
$DB_RDS_NAME = (get_option('DB_RDS_NAME_s') != '') ? get_option('DB_RDS_NAME_s') : 'DB_NAME'; | |
$DB_RDS_USER = (get_option('DB_RDS_USER_s') != '') ? get_option('DB_RDS_USER_s') : 'USER'; | |
$DB_RDS_PASSWORD = (get_option('DB_RDS_PASSWORD_s') != '') ? get_option('DB_RDS_PASSWORD_s') : 'enter password'; | |
$DB_RDS_HOST = (get_option('DB_RDS_HOST_s') != '') ? get_option('DB_RDS_HOST_s') : 'instancename.secret-salt.zone-name.rds.amazonaws.com' ; | |
$DB_RDS_SSL = (get_option('DB_RDS_SSL_s') == 'enabled') ? 'checked' : '' ; | |
$html = '</pre> | |
<div class="wrap"><form action="options.php" method="post" name="options"> | |
<h2>Select Your Settings</h2> | |
' . wp_nonce_field('update-options') . ' | |
<table class="form-table" width="100%" cellpadding="10"> | |
<tbody> | |
<tr><td scope="row" align="left"> <label>DataBase Name</label><input type="text" name="DB_RDS_NAME_s" value="' . $DB_RDS_NAME . '" /></td></tr> | |
<tr><td scope="row" align="left"> <label>DataBase UserName</label><input type="text" name="DB_RDS_USER_s" value="' . $DB_RDS_USER . '" /></td></tr> | |
<tr><td scope="row" align="left"> <label>DataBase Password</label><input type="password" name="DB_RDS_PASSWORD_s" value="' . $DB_RDS_PASSWORD . '" /></td></tr> | |
<tr><td scope="row" align="left"> <label>HostName</label><input type="text" name="DB_RDS_HOST_s" value="' . $DB_RDS_HOST . '" /></td></tr> | |
<tr><td scope="row" align="left"> <label>MYSQL SSL Enable</label><input type="checkbox" name="DB_RDS_SSL_s" value="enabled" '.$DB_RDS_SSL.'/></td></tr> | |
</tbody> | |
</table> | |
<input type="hidden" name="action" value="update" /> | |
<input type="hidden" name="page_options" value="DB_RDS_NAME_s,DB_RDS_USER_s,DB_RDS_PASSWORD_s,DB_RDS_HOST_s,DB_RDS_SSL_s" /> | |
<input type="submit" name="Submit" value="Update" /></form></div> | |
<pre> | |
'; | |
global $wp_rds_db; | |
$wp_rds_db->get_results("select 22"); | |
if ($wp_rds_db->num_rows<1){ | |
$html=$html."<h3><strong>WP-RDS::CURRENT STATE: </strong>: no connection to database or not enough rights</h3>"; | |
} else { | |
$html=$html. "<h3><strong>WP-RDS::CURRENT STATE: </strong>connection successfull</h3>"; | |
} | |
echo $html; | |
} | |
global $wp_rds_db; | |
// error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR ); | |
if (get_option('DB_RDS_SSL_s')){ | |
define('MYSQL_CLIENT_FLAGS2', MYSQL_CLIENT_SSL); | |
apply_filters('debug', 'RDS_DB SSL activated'); | |
} | |
//DB_CLIENT_FLAGS | |
$wp_rds_db = new custom_ssl_wpdb( get_option('DB_RDS_USER_s'), get_option('DB_RDS_PASSWORD_s'), get_option('DB_RDS_NAME_s'), get_option('DB_RDS_HOST_s')); | |
//$wp_rds_db = new custom_ssl_wpdb( DB_RDS_USER, DB_RDS_PASSWORD, DB_RDS_NAME, DB_RDS_HOST); | |
apply_filters('debug', 'RDS_DB activated'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment