| 1 |
<?php |
| 2 |
|
| 3 |
class MMB_Core extends MMB_Helper |
| 4 |
{ |
| 5 |
var $name; |
| 6 |
var $slug; |
| 7 |
var $settings; |
| 8 |
var $remote_client; |
| 9 |
var $comment_instance; |
| 10 |
var $plugin_instance; |
| 11 |
var $theme_instance; |
| 12 |
var $wp_instance; |
| 13 |
var $post_instance; |
| 14 |
var $stats_instance; |
| 15 |
var $user_instance; |
| 16 |
var $backup_instance; |
| 17 |
|
| 18 |
|
| 19 |
function __construct(){ |
| 20 |
global $mmb_plugin_dir; |
| 21 |
|
| 22 |
$this->name = 'Manage Multiple Blogs'; |
| 23 |
$this->slug = 'manage-multiple-blogs'; |
| 24 |
$this->settings = get_option($this->slug); |
| 25 |
if (!$this->settings) |
| 26 |
{ |
| 27 |
$this->settings = array( |
| 28 |
'blogs' => array(), |
| 29 |
'current_blog' => array('type' => null) |
| 30 |
); |
| 31 |
} |
| 32 |
add_action('rightnow_end', array($this, 'add_right_now_info')); |
| 33 |
add_action('wp_footer', array('MMB_Stats', 'set_hit_count')); |
| 34 |
register_activation_hook($mmb_plugin_dir.'/init.php', array($this, 'install')); |
| 35 |
add_action('init', array($this, 'automatic_login')); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Add an item into the Right Now Dashboard widget |
| 41 |
* to inform that the blog can be managed remotely |
| 42 |
* |
| 43 |
*/ |
| 44 |
function add_right_now_info() |
| 45 |
{ |
| 46 |
echo '<div class="mmb-slave-info"> |
| 47 |
<p>This site can be managed remotely.</p> |
| 48 |
</div>'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Gets an instance of the Comment class |
| 53 |
* |
| 54 |
*/ |
| 55 |
function get_comment_instance() |
| 56 |
{ |
| 57 |
if (!isset($this->comment_instance)) |
| 58 |
{ |
| 59 |
$this->comment_instance = new MMB_Comment(); |
| 60 |
} |
| 61 |
|
| 62 |
return $this->comment_instance; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Gets an instance of the Plugin class |
| 67 |
* |
| 68 |
*/ |
| 69 |
function get_plugin_instance() |
| 70 |
{ |
| 71 |
if (!isset($this->plugin_instance)) |
| 72 |
{ |
| 73 |
$this->plugin_instance = new MMB_Plugin(); |
| 74 |
} |
| 75 |
|
| 76 |
return $this->plugin_instance; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets an instance of the Theme class |
| 81 |
* |
| 82 |
*/ |
| 83 |
function get_theme_instance() |
| 84 |
{ |
| 85 |
if (!isset($this->theme_instance)) |
| 86 |
{ |
| 87 |
$this->theme_instance = new MMB_Theme(); |
| 88 |
} |
| 89 |
|
| 90 |
return $this->theme_instance; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Gets an instance of MMB_Post class |
| 96 |
* |
| 97 |
*/ |
| 98 |
function get_post_instance() |
| 99 |
{ |
| 100 |
if (!isset($this->post_instance)) |
| 101 |
{ |
| 102 |
$this->post_instance = new MMB_Post(); |
| 103 |
} |
| 104 |
|
| 105 |
return $this->post_instance; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Gets an instance of Blogroll class |
| 110 |
* |
| 111 |
*/ |
| 112 |
function get_blogroll_instance() |
| 113 |
{ |
| 114 |
if (!isset($this->blogroll_instance)) |
| 115 |
{ |
| 116 |
$this->blogroll_instance = new MMB_Blogroll(); |
| 117 |
} |
| 118 |
|
| 119 |
return $this->blogroll_instance; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Gets an instance of the WP class |
| 126 |
* |
| 127 |
*/ |
| 128 |
function get_wp_instance() |
| 129 |
{ |
| 130 |
if (!isset($this->wp_instance)) |
| 131 |
{ |
| 132 |
$this->wp_instance = new MMB_WP(); |
| 133 |
} |
| 134 |
|
| 135 |
return $this->wp_instance; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Gets an instance of User |
| 140 |
* |
| 141 |
*/ |
| 142 |
function get_user_instance() |
| 143 |
{ |
| 144 |
if (!isset($this->user_instance)) |
| 145 |
{ |
| 146 |
$this->user_instance = new MMB_User(); |
| 147 |
} |
| 148 |
|
| 149 |
return $this->user_instance; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Gets an instance of stats class |
| 154 |
* |
| 155 |
*/ |
| 156 |
function get_stats_instance() |
| 157 |
{ |
| 158 |
if (!isset($this->stats_instance)) |
| 159 |
{ |
| 160 |
$this->stats_instance = new MMB_Stats(); |
| 161 |
} |
| 162 |
return $this->stats_instance; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Gets an instance of stats class |
| 167 |
* |
| 168 |
*/ |
| 169 |
function get_backup_instance() |
| 170 |
{ |
| 171 |
if (!isset($this->backup_instance)) |
| 172 |
{ |
| 173 |
$this->backup_instance = new MMB_Backup(); |
| 174 |
} |
| 175 |
|
| 176 |
return $this->backup_instance; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Plugin install callback function |
| 181 |
* Check PHP version |
| 182 |
*/ |
| 183 |
function install() |
| 184 |
{ |
| 185 |
delete_option('_worker_nossl_key'); |
| 186 |
delete_option('_worker_public_key'); |
| 187 |
delete_option('_action_message_id'); |
| 188 |
if(PHP_VERSION < 5) |
| 189 |
exit("<p>Plugin could not be activated. Your PHP version must be 5.0 or higher.</p>"); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Saves the (modified) options into the database |
| 194 |
* |
| 195 |
*/ |
| 196 |
function _save_options() |
| 197 |
{ |
| 198 |
if (get_option($this->slug)) |
| 199 |
{ |
| 200 |
update_option($this->slug, $this->settings); |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
add_option($this->slug, $this->settings); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Deletes options for communication with master |
| 210 |
* |
| 211 |
*/ |
| 212 |
function uninstall(){ |
| 213 |
delete_option('_worker_nossl_key'); |
| 214 |
delete_option('_worker_public_key'); |
| 215 |
delete_option('_action_message_id'); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Constructs a url (for ajax purpose) |
| 220 |
* |
| 221 |
* @param mixed $base_page |
| 222 |
*/ |
| 223 |
function _construct_url($params = array(), $base_page = 'index.php') |
| 224 |
{ |
| 225 |
$url = "$base_page?_wpnonce=" . wp_create_nonce($this->slug); |
| 226 |
foreach ($params as $key => $value) |
| 227 |
{ |
| 228 |
$url .= "&$key=$value"; |
| 229 |
} |
| 230 |
|
| 231 |
return $url; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Worker update |
| 236 |
* |
| 237 |
*/ |
| 238 |
function update_worker_plugin($params) { |
| 239 |
|
| 240 |
extract($params); |
| 241 |
if($download_url){ |
| 242 |
|
| 243 |
include_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 244 |
include_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 245 |
include_once ABSPATH . 'wp-admin/includes/template.php'; |
| 246 |
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 247 |
|
| 248 |
ob_start(); |
| 249 |
@unlink(dirname(__FILE__)); |
| 250 |
$upgrader = new Plugin_Upgrader(); |
| 251 |
$result = $upgrader->run(array( |
| 252 |
'package' => $download_url, |
| 253 |
'destination' => WP_PLUGIN_DIR, |
| 254 |
'clear_destination' => true, |
| 255 |
'clear_working' => true, |
| 256 |
'hook_extra' => array( |
| 257 |
'plugin' => 'worker/init.php' |
| 258 |
))); |
| 259 |
ob_end_clean(); |
| 260 |
if(is_wp_error($result) || !$result){ |
| 261 |
return array('error' => 'ManageWP Worker could not been upgraded.'); |
| 262 |
} |
| 263 |
else{ |
| 264 |
return array('success' => 'ManageWP Worker plugin successfully upgraded.'); |
| 265 |
} |
| 266 |
} |
| 267 |
return array('error' => 'Bad download path for worker installation file.'); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Automatically logs in when called from Master |
| 272 |
* |
| 273 |
*/ |
| 274 |
function automatic_login(){ |
| 275 |
|
| 276 |
|
| 277 |
$where = ($_GET['mwp_goto']); |
| 278 |
if (!is_user_logged_in() && $_GET['auto_login']) { |
| 279 |
$signature = base64_decode($_GET['signature']); |
| 280 |
$message_id = trim($_GET['message_id']); |
| 281 |
$username = $_GET['username']; |
| 282 |
|
| 283 |
$auth = $this->_authenticate_message($where.$message_id, $signature, $message_id); |
| 284 |
if($auth === true){ |
| 285 |
$user = get_user_by('login', $username); |
| 286 |
$user_id = $user->ID; |
| 287 |
wp_set_current_user($user_id, $username); |
| 288 |
wp_set_auth_cookie( $user_id ); |
| 289 |
do_action('wp_login', $username); |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
wp_die($auth['error']); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if($_GET['auto_login']){ |
| 298 |
wp_redirect(get_bloginfo('url')."/wp-admin/".$where); |
| 299 |
exit(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
} |