| 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 |
/** |
| 16 |
* Class MainWP_Connect_Lib |
| 17 |
* |
| 18 |
* @package MainWP\Dashboard |
| 19 |
*/ |
| 20 |
class MainWP_Connect_Lib { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 21 |
|
| 22 |
/** |
| 23 |
* Private static variable to hold the single instance of the class. |
| 24 |
* |
| 25 |
* @static |
| 26 |
* |
| 27 |
* @var mixed Default null |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Method instance() |
| 33 |
* |
| 34 |
* Create a public static instance. |
| 35 |
* |
| 36 |
* @static |
| 37 |
* @return Instance class. |
| 38 |
*/ |
| 39 |
public static function instance() { |
| 40 |
if ( null === static::$instance ) { |
| 41 |
static::$instance = new self(); |
| 42 |
} |
| 43 |
return static::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Method get_class_name() |
| 48 |
* |
| 49 |
* Get Class Name. |
| 50 |
* |
| 51 |
* @return object Class name. |
| 52 |
*/ |
| 53 |
public static function get_class_name() { |
| 54 |
return __CLASS__; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Class constructor. |
| 59 |
* |
| 60 |
* Run each time the class is called. |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Method create_connect_keys() |
| 67 |
* |
| 68 |
* Create connect keys. |
| 69 |
*/ |
| 70 |
public function create_connect_keys() { |
| 71 |
try { |
| 72 |
//phpcs:ignore -- note. |
| 73 |
// RSA::useInternalEngine(); // to use PHP engine. |
| 74 |
$private = RSA::createKey(); |
| 75 |
$public = $private->getPublicKey(); |
| 76 |
$private_key = $private->toString( 'PKCS1' ); |
| 77 |
$public_key = $public->toString( 'PKCS1' ); |
| 78 |
return array( |
| 79 |
'pub' => $public_key, |
| 80 |
'priv' => $private_key, |
| 81 |
); |
| 82 |
} catch ( \Exception $ex ) { |
| 83 |
// error happen. |
| 84 |
} |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Method connect_sign() |
| 90 |
* |
| 91 |
* Sign to connect. |
| 92 |
* |
| 93 |
* @param mixed $data The data. |
| 94 |
* @param mixed $signature The signature. |
| 95 |
* @param mixed $privkey The privkey. |
| 96 |
*/ |
| 97 |
public static function connect_sign( $data, &$signature, $privkey ) { |
| 98 |
try { |
| 99 |
$private = PublicKeyLoader::loadPrivateKey( $privkey ); |
| 100 |
$signature = $private->sign( $data ); |
| 101 |
if ( ! empty( $signature ) ) { |
| 102 |
return true; |
| 103 |
} |
| 104 |
} catch ( \Exception $ex ) { |
| 105 |
// error happen. |
| 106 |
} |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Method is_use_fallback_sec_lib() |
| 113 |
* |
| 114 |
* Use php connect lib or not. |
| 115 |
* |
| 116 |
* @param mixed $website The website. |
| 117 |
*/ |
| 118 |
public static function is_use_fallback_sec_lib( $website ) { |
| 119 |
$use_fallback_lib = false; |
| 120 |
if ( is_object( $website ) && property_exists( $website, 'verify_method' ) ) { |
| 121 |
$val = (int) $website->verify_method; |
| 122 |
if ( 2 === $val ) { |
| 123 |
$use_fallback_lib = true; |
| 124 |
} elseif ( 3 === $val || empty( $val ) ) { // use general settings. |
| 125 |
$use_fallback_lib = ( 2 === (int) get_option( 'mainwp_verify_connection_method' ) ) ? true : false; |
| 126 |
} |
| 127 |
} else { |
| 128 |
$use_fallback_lib = ( 2 === (int) get_option( 'mainwp_verify_connection_method' ) ) ? true : false; |
| 129 |
} |
| 130 |
return $use_fallback_lib; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Method get_connection_algo_settings_note() |
| 135 |
* |
| 136 |
* Get connection settings note. |
| 137 |
*/ |
| 138 |
public static function get_connection_algo_settings_note() { |
| 139 |
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' ); |
| 140 |
} |
| 141 |
} |
| 142 |
|