PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.1
6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 All 155 releases
mainwp / class / class-mainwp-api-manager-password-management.php

class-mainwp-api-manager-password-management.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.2.1, at class/class-mainwp-api-manager-password-management.php

131 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce API Password Handler
4 *
5 * Encrypts & Decrypts API Passwords.
6 *
7 * @package MainWP/MainWP_API_Passwords_Manager
8 */
9
10 namespace MainWP\Dashboard;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class MainWP_Api_Manager_Password_Management
19 *
20 * WooCommerce API Password Handler.
21 *
22 * @package MainWP/MainWP_API_Passwords_Manager
23 * @author Todd Lahman LLC
24 * @copyright Copyright (c) Todd Lahman LLC
25 * @since 1.0.0
26 */
27 class MainWP_Api_Manager_Password_Management { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
28
29 /**
30 * Set encryption type.
31 *
32 * @var string $ENCRYPT
33 */
34 private static $ENCRYPT = 'AMEncrypt';
35
36 /**
37 * Generate password. Creates a unique instance ID.
38 *
39 * @param int $length Length of ID.
40 * @param bool $special_chars Valid special characters.
41 * @param bool $extra_special_chars Extra special characters.
42 *
43 * @return string Password.
44 */
45 public static function generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
46 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
47 if ( $special_chars ) {
48 $chars .= '!@#$%^&*()';
49 }
50 if ( $extra_special_chars ) {
51 $chars .= '-_ []{}<>~`+=,.;:/?|';
52 }
53
54 $password = '';
55 for ( $i = 0; $i < $length; $i++ ) {
56 $password .= substr( $chars, wp_rand( 0, strlen( $chars ) - 1 ), 1 );
57 }
58
59 // random_password filter was previously in random_password function which was deprecated.
60 return $password;
61 }
62
63 /**
64 * Encrypt string.
65 *
66 * @param string $str String to encrypt.
67 *
68 * @return string Encrypted string.
69 */
70 public static function encrypt_string( $str ) {
71 return static::encrypt( $str, static::$ENCRYPT );
72 }
73
74 /**
75 * Decrypts string.
76 *
77 * @param string $encrypted Sting to decrypt.
78 *
79 * @return string Decrypted string.
80 */
81 public static function decrypt_string( $encrypted ) {
82 return static::decrypt( $encrypted, static::$ENCRYPT );
83 }
84
85
86 /**
87 * Encrypt.
88 *
89 * @param string $str String to encrypt.
90 * @param string $pass String.
91 *
92 * @return string Encrypted string.
93 */
94 public static function encrypt( $str, $pass ) {
95 if ( ! is_string( $str ) ) {
96 return '';
97 }
98 $pass = str_split( str_pad( '', strlen( $str ), $pass, STR_PAD_RIGHT ) );
99 $stra = str_split( $str );
100 foreach ( $stra as $k => $v ) {
101 $tmp = ord( $v ) + ord( $pass[ $k ] );
102 $stra[ $k ] = chr( 255 < $tmp ? ( $tmp - 256 ) : $tmp );
103 }
104
105 return base64_encode( join( '', $stra ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
106 }
107
108 /**
109 * Decrypt.
110 *
111 * @param string $str String to Decrypt.
112 * @param string $pass String.
113 *
114 * @return string Decrypted string.
115 */
116 public static function decrypt( $str, $pass ) {
117 if ( ! is_string( $str ) ) {
118 return '';
119 }
120 $str = base64_decode( $str ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
121 $pass = str_split( str_pad( '', strlen( $str ), $pass, STR_PAD_RIGHT ) );
122 $stra = str_split( $str );
123 foreach ( $stra as $k => $v ) {
124 $tmp = ord( $v ) - ord( $pass[ $k ] );
125 $stra[ $k ] = chr( 0 > $tmp ? ( $tmp + 256 ) : $tmp );
126 }
127
128 return join( '', $stra );
129 }
130 }
131