| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP System Utility Helper |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
// phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.WP.AlternativeFunctions, WordPress.PHP.NoSilencedErrors -- Using cURL functions. |
| 11 |
|
| 12 |
/** |
| 13 |
* Class MainWP_System_Utility |
| 14 |
* |
| 15 |
* @package MainWP\Dashboard |
| 16 |
*/ |
| 17 |
class MainWP_System_Utility { |
| 18 |
|
| 19 |
/** |
| 20 |
* Method get_class_name() |
| 21 |
* |
| 22 |
* Get Class Name. |
| 23 |
* |
| 24 |
* @return object |
| 25 |
*/ |
| 26 |
public static function get_class_name() { |
| 27 |
return __CLASS__; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Method is_admin() |
| 32 |
* |
| 33 |
* Check if current user is an administrator. |
| 34 |
* |
| 35 |
* @return boolean True|False. |
| 36 |
*/ |
| 37 |
public static function is_admin() { |
| 38 |
|
| 39 |
/** |
| 40 |
* Current user global. |
| 41 |
* |
| 42 |
* @global string |
| 43 |
*/ |
| 44 |
global $current_user; |
| 45 |
if ( 0 === $current_user->ID ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
if ( 10 == $current_user->wp_user_level || ( isset( $current_user->user_level ) && 10 == $current_user->user_level ) || current_user_can( 'level_10' ) ) { |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Method get_primary_backup() |
| 58 |
* |
| 59 |
* Check if using Legacy Backup Solution. |
| 60 |
* |
| 61 |
* @return mixed False|$enable_legacy_backup. |
| 62 |
*/ |
| 63 |
public static function get_primary_backup() { |
| 64 |
$enable_legacy_backup = get_option( 'mainwp_enableLegacyBackupFeature' ); |
| 65 |
if ( ! $enable_legacy_backup ) { |
| 66 |
return get_option( 'mainwp_primaryBackup', false ); |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Method get_notification_email() |
| 73 |
* |
| 74 |
* Check if user wants to recieve MainWP Notification Emails. |
| 75 |
* |
| 76 |
* @param null $user User Email Address. |
| 77 |
* |
| 78 |
* @return mixed null|User Email Address. |
| 79 |
*/ |
| 80 |
public static function get_notification_email( $user = null ) { |
| 81 |
if ( null == $user ) { |
| 82 |
|
| 83 |
/** |
| 84 |
* Current user global. |
| 85 |
* |
| 86 |
* @global string |
| 87 |
*/ |
| 88 |
global $current_user; |
| 89 |
|
| 90 |
$user = $current_user; |
| 91 |
} |
| 92 |
|
| 93 |
if ( null == $user ) { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! ( $user instanceof \WP_User ) ) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
$userExt = MainWP_DB_Common::instance()->get_user_extension(); |
| 102 |
if ( '' != $userExt->user_email ) { |
| 103 |
return $userExt->user_email; |
| 104 |
} |
| 105 |
|
| 106 |
return $user->user_email; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Method get_base_dir() |
| 111 |
* |
| 112 |
* Get the base upload directory. |
| 113 |
* |
| 114 |
* @return string basedir/ |
| 115 |
*/ |
| 116 |
public static function get_base_dir() { |
| 117 |
$upload_dir = wp_upload_dir(); |
| 118 |
|
| 119 |
return $upload_dir['basedir'] . DIRECTORY_SEPARATOR; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Method get_icons_dir() |
| 124 |
* |
| 125 |
* Get MainWP icons directory, |
| 126 |
* if it doesn't exist create it. |
| 127 |
* |
| 128 |
* @return array $dir, $url |
| 129 |
*/ |
| 130 |
public static function get_icons_dir() { |
| 131 |
$hasWPFileSystem = self::get_wp_file_system(); |
| 132 |
|
| 133 |
/** |
| 134 |
* WordPress files system object. |
| 135 |
* |
| 136 |
* @global object |
| 137 |
*/ |
| 138 |
global $wp_filesystem; |
| 139 |
|
| 140 |
$dirs = self::get_mainwp_dir(); |
| 141 |
$dir = $dirs[0] . 'icons' . DIRECTORY_SEPARATOR; |
| 142 |
$url = $dirs[1] . 'icons/'; |
| 143 |
if ( ! $wp_filesystem->exists( $dir ) ) { |
| 144 |
$wp_filesystem->mkdir( $dir, 0777, true ); |
| 145 |
} |
| 146 |
if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) { |
| 147 |
$wp_filesystem->touch( $dir . 'index.php' ); |
| 148 |
} |
| 149 |
return array( $dir, $url ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Method get_mainwp_dir() |
| 154 |
* |
| 155 |
* Get the MainWP directory, |
| 156 |
* if it doesn't exist create it. |
| 157 |
* |
| 158 |
* @param string|null $subdir mainwp sub diectories. |
| 159 |
* @param bool $direct_access Return true if Direct access file system. Default: false. |
| 160 |
* |
| 161 |
* @return array $dir, $url |
| 162 |
*/ |
| 163 |
public static function get_mainwp_dir( $subdir = null, $direct_access = false ) { |
| 164 |
$hasWPFileSystem = self::get_wp_file_system(); |
| 165 |
|
| 166 |
/** |
| 167 |
* WordPress files system object. |
| 168 |
* |
| 169 |
* @global object |
| 170 |
*/ |
| 171 |
global $wp_filesystem; |
| 172 |
|
| 173 |
$upload_dir = wp_upload_dir(); |
| 174 |
$dir = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'mainwp' . DIRECTORY_SEPARATOR; |
| 175 |
$url = $upload_dir['baseurl'] . '/mainwp/'; |
| 176 |
if ( ! $wp_filesystem->exists( $dir ) ) { |
| 177 |
$wp_filesystem->mkdir( $dir, 0777, true ); |
| 178 |
} |
| 179 |
if ( ! $wp_filesystem->exists( $dir . 'index.php' ) ) { |
| 180 |
$wp_filesystem->touch( $dir . 'index.php' ); |
| 181 |
} |
| 182 |
|
| 183 |
if ( null != $subdir && ! stristr( $subdir, '..' ) ) { |
| 184 |
$newdir = $dir . $subdir . DIRECTORY_SEPARATOR; |
| 185 |
$url = $url . $subdir . '/'; |
| 186 |
|
| 187 |
if ( ! $wp_filesystem->exists( $newdir ) ) { |
| 188 |
$wp_filesystem->mkdir( $newdir, 0777, true ); |
| 189 |
} |
| 190 |
|
| 191 |
if ( $direct_access ) { |
| 192 |
if ( ! $wp_filesystem->exists( trailingslashit( $newdir ) . 'index.php' ) ) { |
| 193 |
$wp_filesystem->touch( trailingslashit( $newdir ) . 'index.php' ); |
| 194 |
} |
| 195 |
if ( $wp_filesystem->exists( trailingslashit( $newdir ) . '.htaccess' ) ) { |
| 196 |
$wp_filesystem->delete( trailingslashit( $newdir ) . '.htaccess' ); |
| 197 |
} |
| 198 |
} else { |
| 199 |
if ( ! $wp_filesystem->exists( trailingslashit( $newdir ) . '.htaccess' ) ) { |
| 200 |
$wp_filesystem->put_contents( trailingslashit( $newdir ) . '.htaccess', 'deny from all' ); |
| 201 |
} |
| 202 |
} |
| 203 |
return array( $newdir, $url ); |
| 204 |
} |
| 205 |
|
| 206 |
return array( $dir, $url ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Method get_mainwp_sub_dir() |
| 211 |
* |
| 212 |
* Get the MainWP directory, |
| 213 |
* if it doesn't exist create it. |
| 214 |
* |
| 215 |
* @param string|null $subdir mainwp sub diectories. |
| 216 |
* @param bool $direct_access Return true if Direct access file system. Default: false. |
| 217 |
* |
| 218 |
* @return string $dir mainwp sub-directory. |
| 219 |
*/ |
| 220 |
public static function get_mainwp_sub_dir( $subdir = null, $direct_access = false ) { |
| 221 |
$dirs = self::get_mainwp_dir( $subdir, $direct_access ); |
| 222 |
return $dirs[0]; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Method get_download_dir() |
| 227 |
* |
| 228 |
* @param mixed $what What url. |
| 229 |
* @param mixed $filename File Name. |
| 230 |
* |
| 231 |
* @return string Download URL. |
| 232 |
*/ |
| 233 |
public static function get_download_url( $what, $filename ) { |
| 234 |
$specificDir = self::get_mainwp_specific_dir( $what ); |
| 235 |
$mwpDir = self::get_mainwp_dir(); |
| 236 |
$mwpDir = $mwpDir[0]; |
| 237 |
$fullFile = $specificDir . $filename; |
| 238 |
|
| 239 |
return admin_url( '?sig=' . md5( filesize( $fullFile ) ) . '&mwpdl=' . rawurlencode( str_replace( $mwpDir, '', $fullFile ) ) ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Method get_mainwp_specific_dir() |
| 244 |
* |
| 245 |
* Get MainWP Specific directory, |
| 246 |
* if it doesn't exist create it. |
| 247 |
* |
| 248 |
* Update .htaccess. |
| 249 |
* |
| 250 |
* @param null $dir Current MainWP directory. |
| 251 |
* |
| 252 |
* @return string $newdir |
| 253 |
*/ |
| 254 |
public static function get_mainwp_specific_dir( $dir = null ) { |
| 255 |
if ( MainWP_System::instance()->is_single_user() ) { |
| 256 |
$userid = 0; |
| 257 |
} else { |
| 258 |
|
| 259 |
/** |
| 260 |
* Current user global. |
| 261 |
* |
| 262 |
* @global string |
| 263 |
*/ |
| 264 |
global $current_user; |
| 265 |
|
| 266 |
$userid = $current_user->ID; |
| 267 |
} |
| 268 |
|
| 269 |
$hasWPFileSystem = self::get_wp_file_system(); |
| 270 |
|
| 271 |
global $wp_filesystem; |
| 272 |
|
| 273 |
$dirs = self::get_mainwp_dir(); |
| 274 |
$newdir = $dirs[0] . $userid . ( null != $dir ? DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR : '' ); |
| 275 |
|
| 276 |
if ( $hasWPFileSystem && ! empty( $wp_filesystem ) ) { |
| 277 |
|
| 278 |
if ( ! $wp_filesystem->is_dir( $newdir ) ) { |
| 279 |
$wp_filesystem->mkdir( $newdir, 0777, true ); |
| 280 |
} |
| 281 |
|
| 282 |
if ( null != $dirs[0] . $userid && ! $wp_filesystem->exists( trailingslashit( $dirs[0] . $userid ) . '.htaccess' ) ) { |
| 283 |
$file_htaccess = trailingslashit( $dirs[0] . $userid ) . '.htaccess'; |
| 284 |
$wp_filesystem->put_contents( $file_htaccess, 'deny from all' ); |
| 285 |
} |
| 286 |
} else { |
| 287 |
|
| 288 |
if ( ! file_exists( $newdir ) ) { |
| 289 |
mkdir( $newdir, 0777, true ); |
| 290 |
} |
| 291 |
|
| 292 |
if ( null != $dirs[0] . $userid && ! file_exists( trailingslashit( $dirs[0] . $userid ) . '.htaccess' ) ) { |
| 293 |
$file = fopen( trailingslashit( $dirs[0] . $userid ) . '.htaccess', 'w+' ); |
| 294 |
fwrite( $file, 'deny from all' ); |
| 295 |
fclose( $file ); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return $newdir; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Method get_mainwp_specific_url() |
| 304 |
* |
| 305 |
* Get MainWP specific URL. |
| 306 |
* |
| 307 |
* @param mixed $dir MainWP Directory. |
| 308 |
* |
| 309 |
* @return string MainWP URL. |
| 310 |
*/ |
| 311 |
public static function get_mainwp_specific_url( $dir ) { |
| 312 |
if ( MainWP_System::instance()->is_single_user() ) { |
| 313 |
$userid = 0; |
| 314 |
} else { |
| 315 |
|
| 316 |
/** |
| 317 |
* Current user global. |
| 318 |
* |
| 319 |
* @global string |
| 320 |
*/ |
| 321 |
global $current_user; |
| 322 |
|
| 323 |
$userid = $current_user->ID; |
| 324 |
} |
| 325 |
$dirs = self::get_mainwp_dir(); |
| 326 |
|
| 327 |
return $dirs[1] . $userid . '/' . $dir . '/'; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Method get_wp_file_system() |
| 332 |
* |
| 333 |
* Get WP file system & define Global Variable FS_METHOD. |
| 334 |
* |
| 335 |
* @return boolean $init True. |
| 336 |
*/ |
| 337 |
public static function get_wp_file_system() { |
| 338 |
|
| 339 |
/** |
| 340 |
* WordPress files system object. |
| 341 |
* |
| 342 |
* @global object |
| 343 |
*/ |
| 344 |
global $wp_filesystem; |
| 345 |
|
| 346 |
if ( empty( $wp_filesystem ) ) { |
| 347 |
ob_start(); |
| 348 |
if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) { |
| 349 |
include_once ABSPATH . '/wp-admin/includes/screen.php'; |
| 350 |
} |
| 351 |
if ( file_exists( ABSPATH . '/wp-admin/includes/template.php' ) ) { |
| 352 |
include_once ABSPATH . '/wp-admin/includes/template.php'; |
| 353 |
} |
| 354 |
include_once ABSPATH . 'wp-admin/includes/file.php'; |
| 355 |
$creds = request_filesystem_credentials( 'test' ); |
| 356 |
ob_end_clean(); |
| 357 |
if ( empty( $creds ) ) { |
| 358 |
|
| 359 |
/** |
| 360 |
* Define WordPress File system. |
| 361 |
* |
| 362 |
* @const ( bool ) Default: true |
| 363 |
* @source https://code-reference.mainwp.com/classes/MainWP.Dashboard.MainWP_System_Utility.html |
| 364 |
*/ |
| 365 |
define( 'FS_METHOD', 'direct' ); |
| 366 |
} |
| 367 |
$init = \WP_Filesystem( $creds ); |
| 368 |
} else { |
| 369 |
$init = true; |
| 370 |
} |
| 371 |
|
| 372 |
return $init; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Method can_edit_website() |
| 377 |
* |
| 378 |
* Check if current user can edit Child Site. |
| 379 |
* |
| 380 |
* @param mixed $website Child Site. |
| 381 |
* |
| 382 |
* @return mixed true|false|userid |
| 383 |
*/ |
| 384 |
public static function can_edit_website( &$website ) { |
| 385 |
if ( null == $website ) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
if ( MainWP_System::instance()->is_single_user() ) { |
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Current user global. |
| 395 |
* |
| 396 |
* @global string |
| 397 |
*/ |
| 398 |
global $current_user; |
| 399 |
|
| 400 |
return ( $website->userid == $current_user->ID ); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Method get_current_wpid() |
| 405 |
* |
| 406 |
* Get current Child Site ID. |
| 407 |
* |
| 408 |
* @return string $current_user->current_site_id Current Child Site ID. |
| 409 |
*/ |
| 410 |
public static function get_current_wpid() { |
| 411 |
|
| 412 |
/** |
| 413 |
* Current user global. |
| 414 |
* |
| 415 |
* @global string |
| 416 |
*/ |
| 417 |
global $current_user; |
| 418 |
|
| 419 |
return $current_user->current_site_id; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Method set_current_wpid() |
| 424 |
* |
| 425 |
* Set the current Child Site ID. |
| 426 |
* |
| 427 |
* @param mixed $wpid Child Site ID. |
| 428 |
*/ |
| 429 |
public static function set_current_wpid( $wpid ) { |
| 430 |
|
| 431 |
/** |
| 432 |
* Current user global. |
| 433 |
* |
| 434 |
* @global string |
| 435 |
*/ |
| 436 |
global $current_user; |
| 437 |
|
| 438 |
$current_user->current_site_id = $wpid; |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Method get_page_id() |
| 443 |
* |
| 444 |
* Get current Page ID. |
| 445 |
* |
| 446 |
* @param null $screen Current Screen ID. |
| 447 |
* |
| 448 |
* @return string $page Current page ID. |
| 449 |
*/ |
| 450 |
public static function get_page_id( $screen = null ) { |
| 451 |
|
| 452 |
if ( empty( $screen ) ) { |
| 453 |
$screen = get_current_screen(); |
| 454 |
} elseif ( is_string( $screen ) ) { |
| 455 |
$screen = convert_to_screen( $screen ); |
| 456 |
} |
| 457 |
|
| 458 |
if ( ! isset( $screen->id ) ) { |
| 459 |
return; |
| 460 |
} |
| 461 |
|
| 462 |
$page = $screen->id; |
| 463 |
|
| 464 |
return $page; |
| 465 |
} |
| 466 |
|
| 467 |
/** |
| 468 |
* Method get_child_response() |
| 469 |
* |
| 470 |
* Get response from Child Site. |
| 471 |
* |
| 472 |
* @param mixed $data Data to process. |
| 473 |
* |
| 474 |
* @return json $data|true. |
| 475 |
*/ |
| 476 |
public static function get_child_response( $data ) { |
| 477 |
if ( is_serialized( $data ) ) { |
| 478 |
return unserialize( $data, array( 'allowed_classes' => false ) ); // phpcs:ignore -- for compatability. |
| 479 |
} else { |
| 480 |
return json_decode( $data, true ); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Method maybe_unserialyze() |
| 486 |
* |
| 487 |
* Check if $data is serialized, |
| 488 |
* if it isn't then base64_decode it. |
| 489 |
* |
| 490 |
* @param mixed $data Data to check. |
| 491 |
* |
| 492 |
* @return mixed $data. |
| 493 |
*/ |
| 494 |
public static function maybe_unserialyze( $data ) { |
| 495 |
if ( '' == $data || is_array( $data ) ) { |
| 496 |
return $data; |
| 497 |
} elseif ( is_serialized( $data ) ) { |
| 498 |
// phpcs:ignore -- for compatability. |
| 499 |
return maybe_unserialize( $data ); |
| 500 |
} else { |
| 501 |
// phpcs:ignore -- for compatability. |
| 502 |
return maybe_unserialize( base64_decode( $data ) ); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Method get_openssl_conf() |
| 508 |
* |
| 509 |
* Get dashboard openssl configuration. |
| 510 |
*/ |
| 511 |
public static function get_openssl_conf() { |
| 512 |
|
| 513 |
if ( defined( 'MAINWP_CRYPT_RSA_OPENSSL_CONFIG' ) ) { |
| 514 |
return MAINWP_CRYPT_RSA_OPENSSL_CONFIG; |
| 515 |
} |
| 516 |
|
| 517 |
$setup_conf_loc = ''; |
| 518 |
if ( MainWP_Settings::is_local_window_config() ) { |
| 519 |
$setup_conf_loc = get_option( 'mwp_setup_opensslLibLocation' ); |
| 520 |
} elseif ( get_option( 'mainwp_opensslLibLocation' ) != '' ) { |
| 521 |
$setup_conf_loc = get_option( 'mainwp_opensslLibLocation' ); |
| 522 |
} |
| 523 |
return $setup_conf_loc; |
| 524 |
} |
| 525 |
|
| 526 |
/** |
| 527 |
* Get tokens of site. |
| 528 |
* |
| 529 |
* @param object $site The website. |
| 530 |
* |
| 531 |
* @return array Array of tokens. |
| 532 |
*/ |
| 533 |
public static function get_tokens_site_values( $site ) { |
| 534 |
|
| 535 |
$tokens_values = array( |
| 536 |
'[site.name]' => $site->name, |
| 537 |
'[site.url]' => $site->url, |
| 538 |
); |
| 539 |
|
| 540 |
$site_info = json_decode( MainWP_DB::instance()->get_website_option( $site, 'site_info' ), true ); |
| 541 |
if ( is_array( $site_info ) ) { |
| 542 |
$map_site_tokens = array( |
| 543 |
'client.site.version' => 'wpversion', // Displays the WP version of the child site. |
| 544 |
'client.site.theme' => 'themeactivated', // Displays the currently active theme for the child site. |
| 545 |
'client.site.php' => 'phpversion', // Displays the PHP version of the child site. |
| 546 |
'client.site.mysql' => 'mysql_version', // Displays the MySQL version of the child site. |
| 547 |
); |
| 548 |
foreach ( $map_site_tokens as $tok => $val ) { |
| 549 |
$tokens_value[ '[' . $tok . ']' ] = ( is_array( $site_info ) && isset( $site_info[ $val ] ) ) ? $site_info[ $val ] : ''; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
return $tokens_values; |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* |
| 558 |
* Replace site tokens. |
| 559 |
* |
| 560 |
* @param string $string String data. |
| 561 |
* @param array $replace_tokens array of tokens. |
| 562 |
* |
| 563 |
* @return string content with replaced tokens. |
| 564 |
*/ |
| 565 |
public static function replace_tokens_values( $string, $replace_tokens ) { |
| 566 |
$tokens = array_keys( $replace_tokens ); |
| 567 |
$values = array_values( $replace_tokens ); |
| 568 |
return str_replace( $tokens, $values, $string ); |
| 569 |
} |
| 570 |
} |
| 571 |
|