| @@ -11,131 +11,141 @@ | ||
| 11 | 11 | |
| 12 | 12 | use phpseclib3\Crypt\RSA; |
| 13 | 13 | use phpseclib3\Crypt\PublicKeyLoader; |
| 14 | 14 | |
| 15 | +// Exit if accessed directly. | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit; | |
| 18 | +} | |
| 19 | + | |
| 15 | 20 | /** |
| 16 | 21 | * Class MainWP_Connect_Lib |
| 17 | 22 | * |
| 18 | 23 | * @package MainWP\Dashboard |
| 19 | 24 | */ |
| 20 | -class MainWP_Connect_Lib { | |
| 25 | +class MainWP_Connect_Lib { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. | |
| 21 | 26 | |
| 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; | |
| 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; | |
| 30 | 35 | |
| 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 === self::$instance ) { | |
| 41 | - self::$instance = new self(); | |
| 42 | - } | |
| 43 | - return self::$instance; | |
| 44 | - } | |
| 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 | + } | |
| 45 | 50 | |
| 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 | - } | |
| 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 | + } | |
| 56 | 61 | |
| 57 | - /** | |
| 58 | - * Class constructor. | |
| 59 | - * | |
| 60 | - * Run each time the class is called. | |
| 61 | - */ | |
| 62 | - public function __construct() { | |
| 63 | - } | |
| 62 | + /** | |
| 63 | + * Class constructor. | |
| 64 | + * | |
| 65 | + * Run each time the class is called. | |
| 66 | + */ | |
| 67 | + public function __construct() { | |
| 68 | + } | |
| 64 | 69 | |
| 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 | - } | |
| 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 | + } | |
| 87 | 92 | |
| 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 | - } | |
| 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 | + } | |
| 109 | 119 | |
| 110 | 120 | |
| 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 | - } | |
| 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 | + } | |
| 132 | 142 | |
| 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 | - } | |
| 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 | + } | |
| 141 | 151 | } |