| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Connect Lib |
| 4 |
* |
| 5 |
* MainWP Connect Lib functions. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
use phpseclib3\Crypt\RSA; |
| 13 |
use phpseclib3\Crypt\PublicKeyLoader; |
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Class MainWP_Connect_Lib |
| 22 |
* |
| 23 |
* @package MainWP\Dashboard |
| 24 |
*/ |
| 25 |
class MainWP_Connect_Lib { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 26 |
|
| 27 |
/** |
| 28 |
* Private static variable to hold the single instance of the class. |
| 29 |
* |
| 30 |
* @static |
| 31 |
* |
| 32 |
* @var mixed Default null |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Method instance() |
| 38 |
* |
| 39 |
* Create a public static instance. |
| 40 |
* |
| 41 |
* @static |
| 42 |
* @return Instance class. |
| 43 |
*/ |
| 44 |
public static function instance() { |
| 45 |
if ( null === static::$instance ) { |
| 46 |
static::$instance = new self(); |
| 47 |
} |
| 48 |
return static::$instance; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Method get_class_name() |
| 53 |
* |
| 54 |
* Get Class Name. |
| 55 |
* |
| 56 |
* @return object Class name. |
| 57 |
*/ |
| 58 |
public static function get_class_name() { |
| 59 |
return __CLASS__; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Class constructor. |
| 64 |
* |
| 65 |
* Run each time the class is called. |
| 66 |
*/ |
| 67 |
public function __construct() { |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Method create_connect_keys() |
| 72 |
* |
| 73 |
* Create connect keys. |
| 74 |
*/ |
| 75 |
public function create_connect_keys() { |
| 76 |
try { |
| 77 |
//phpcs:ignore -- note. |
| 78 |
// RSA::useInternalEngine(); // to use PHP engine. |
| 79 |
$private = RSA::createKey(); |
| 80 |
$public = $private->getPublicKey(); |
| 81 |
$private_key = $private->toString( 'PKCS1' ); |
| 82 |
$public_key = $public->toString( 'PKCS1' ); |
| 83 |
return array( |
| 84 |
'pub' => $public_key, |
| 85 |
'priv' => $private_key, |
| 86 |
); |
| 87 |
} catch ( \Exception $ex ) { |
| 88 |
// error happen. |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Method connect_sign() |
| 95 |
* |
| 96 |
* Sign to connect. |
| 97 |
* |
| 98 |
* @param mixed $data The data. |
| 99 |
* @param mixed $signature The signature. |
| 100 |
* @param mixed $privkey The privkey. |
| 101 |
* @param int $site_id site id. |
| 102 |
*/ |
| 103 |
public static function connect_sign( $data, &$signature, $privkey, $site_id ) { |
| 104 |
try { |
| 105 |
$de_privkey = MainWP_Encrypt_Data_Lib::instance()->decrypt_privkey( $privkey, $site_id ); |
| 106 |
if ( empty( $de_privkey ) ) { |
| 107 |
$de_privkey = $privkey; // compatible. |
| 108 |
} |
| 109 |
$private = PublicKeyLoader::loadPrivateKey( $de_privkey ); |
| 110 |
$signature = $private->sign( $data ); |
| 111 |
if ( ! empty( $signature ) ) { |
| 112 |
return true; |
| 113 |
} |
| 114 |
} catch ( \Exception $ex ) { |
| 115 |
// error happen. |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Method is_use_fallback_sec_lib() |
| 123 |
* |
| 124 |
* Use php connect lib or not. |
| 125 |
* |
| 126 |
* @param mixed $website The website. |
| 127 |
*/ |
| 128 |
public static function is_use_fallback_sec_lib( $website ) { |
| 129 |
$use_fallback_lib = false; |
| 130 |
if ( is_object( $website ) && property_exists( $website, 'verify_method' ) ) { |
| 131 |
$val = (int) $website->verify_method; |
| 132 |
if ( 2 === $val ) { |
| 133 |
$use_fallback_lib = true; |
| 134 |
} elseif ( 3 === $val || empty( $val ) ) { // use general settings. |
| 135 |
$use_fallback_lib = ( 2 === (int) get_option( 'mainwp_verify_connection_method' ) ) ? true : false; |
| 136 |
} |
| 137 |
} else { |
| 138 |
$use_fallback_lib = ( 2 === (int) get_option( 'mainwp_verify_connection_method' ) ) ? true : false; |
| 139 |
} |
| 140 |
return $use_fallback_lib; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Method get_connection_algo_settings_note() |
| 145 |
* |
| 146 |
* Get connection settings note. |
| 147 |
*/ |
| 148 |
public static function get_connection_algo_settings_note() { |
| 149 |
return esc_html__( 'Due to security reasons, switching back to OPENSSL_ALGO_SHA1 breaks the connection to your child site(s). It is required to deactivate & reactivate the MainWP Child plugin on child sites before you can reconnect them. Use OPENSSL_ALGO_SHA1 only if necessary.', 'mainwp' ); |
| 150 |
} |
| 151 |
} |
| 152 |
|